github.com/jenspinney/cli@v6.42.1-0.20190207184520-7450c600020e+incompatible/integration/shared/experimental/v3_apply_manifest_command_test.go (about)

     1  package experimental
     2  
     3  import (
     4  	"io/ioutil"
     5  	"os"
     6  	"path/filepath"
     7  	"regexp"
     8  
     9  	"code.cloudfoundry.org/cli/integration/helpers"
    10  	. "github.com/onsi/ginkgo"
    11  	. "github.com/onsi/gomega"
    12  	. "github.com/onsi/gomega/gbytes"
    13  	. "github.com/onsi/gomega/gexec"
    14  )
    15  
    16  var _ = Describe("v3-apply-manifest command", func() {
    17  	var (
    18  		orgName      string
    19  		spaceName    string
    20  		appName      string
    21  		manifestPath string
    22  		appDir       string
    23  	)
    24  
    25  	BeforeEach(func() {
    26  		orgName = helpers.NewOrgName()
    27  		spaceName = helpers.NewSpaceName()
    28  		appName = helpers.PrefixedRandomName("app")
    29  		appDir, _ = ioutil.TempDir("", "simple-app")
    30  		manifestPath = filepath.Join(appDir, "manifest.yml")
    31  		// Ensure the file exists at the minimum
    32  		helpers.WriteManifest(manifestPath, map[string]interface{}{})
    33  	})
    34  
    35  	AfterEach(func() {
    36  		Expect(os.RemoveAll(appDir)).ToNot(HaveOccurred())
    37  	})
    38  
    39  	Describe("help", func() {
    40  		When("--help flag is set", func() {
    41  			It("displays command usage to output", func() {
    42  				session := helpers.CF("v3-apply-manifest", "--help")
    43  
    44  				Eventually(session).Should(Say("NAME:"))
    45  				Eventually(session).Should(Say("v3-apply-manifest - Applies manifest properties to an application"))
    46  				Eventually(session).Should(Say("USAGE:"))
    47  				Eventually(session).Should(Say("cf v3-apply-manifest -f APP_MANIFEST_PATH"))
    48  
    49  				Eventually(session).Should(Exit(0))
    50  			})
    51  		})
    52  	})
    53  
    54  	When("the -f flag is not given an arg", func() {
    55  		It("tells the user that the flag requires an arg, prints help text, and exits 1", func() {
    56  			session := helpers.CF("v3-apply-manifest", "-f")
    57  
    58  			Eventually(session.Err).Should(Say("Incorrect Usage: expected argument for flag `-f'"))
    59  			Eventually(session).Should(Say("NAME:"))
    60  			Eventually(session).Should(Exit(1))
    61  		})
    62  	})
    63  
    64  	When("the -f flag path does not exist", func() {
    65  		It("tells the user that the provided path doesn't exist, prints help text, and exits 1", func() {
    66  			session := helpers.CF("v3-apply-manifest", "-f", "path/that/does/not/exist")
    67  
    68  			Eventually(session.Err).Should(Say("Incorrect Usage: The specified path 'path/that/does/not/exist' does not exist."))
    69  			Eventually(session).Should(Say("NAME:"))
    70  			Eventually(session).Should(Exit(1))
    71  		})
    72  	})
    73  
    74  	When("the environment is not setup correctly", func() {
    75  		It("fails with the appropriate errors", func() {
    76  			helpers.CheckEnvironmentTargetedCorrectly(true, true, ReadOnlyOrg, "v3-apply-manifest", "-f", manifestPath)
    77  		})
    78  	})
    79  
    80  	When("the environment is set up correctly", func() {
    81  		BeforeEach(func() {
    82  			helpers.SetupCF(orgName, spaceName)
    83  		})
    84  
    85  		AfterEach(func() {
    86  			helpers.QuickDeleteOrg(orgName)
    87  		})
    88  
    89  		When("the app exists", func() {
    90  			BeforeEach(func() {
    91  				helpers.WithHelloWorldApp(func(appDir string) {
    92  					Eventually(helpers.CustomCF(helpers.CFEnv{WorkingDirectory: appDir}, "v3-push", appName)).Should(Exit(0))
    93  				})
    94  			})
    95  
    96  			When("the app name in the manifest is missing", func() {
    97  				BeforeEach(func() {
    98  					helpers.WriteManifest(manifestPath, map[string]interface{}{
    99  						"applications": []map[string]interface{}{
   100  							{
   101  								"instances": 3,
   102  							},
   103  						},
   104  					})
   105  				})
   106  
   107  				It("reports an error", func() {
   108  					session := helpers.CF("v3-apply-manifest", "-f", manifestPath)
   109  					Eventually(session.Err).Should(Say("Found an application with no name specified"))
   110  					Eventually(session).Should(Say("FAILED"))
   111  					Eventually(session).Should(Exit(1))
   112  				})
   113  			})
   114  
   115  			When("the app name in the manifest doesn't exist", func() {
   116  				var invalidAppName string
   117  				BeforeEach(func() {
   118  					invalidAppName = "no-such-app"
   119  					helpers.WriteManifest(manifestPath, map[string]interface{}{
   120  						"applications": []map[string]interface{}{
   121  							{
   122  								"name":      invalidAppName,
   123  								"instances": 3,
   124  							},
   125  						},
   126  					})
   127  				})
   128  
   129  				It("reports an error", func() {
   130  					session := helpers.CF("v3-apply-manifest", "-f", manifestPath)
   131  					Eventually(session.Err).Should(Say("App %s not found", invalidAppName))
   132  					Eventually(session).Should(Say("FAILED"))
   133  
   134  					Eventually(session).Should(Exit(1))
   135  				})
   136  			})
   137  
   138  			When("the app name in the manifest does exist", func() {
   139  				When("the instances value is negative", func() {
   140  					BeforeEach(func() {
   141  						helpers.WriteManifest(manifestPath, map[string]interface{}{
   142  							"applications": []map[string]interface{}{
   143  								{
   144  									"name":      appName,
   145  									"instances": -1,
   146  								},
   147  							},
   148  						})
   149  					})
   150  
   151  					It("reports an error", func() {
   152  						session := helpers.CF("v3-apply-manifest", "-f", manifestPath)
   153  						Eventually(session.Err).Should(Say("Instances must be greater than or equal to 0"))
   154  						Eventually(session).Should(Say("FAILED"))
   155  
   156  						Eventually(session).Should(Exit(1))
   157  					})
   158  				})
   159  
   160  				When("the instances value is more than the space quota limit", func() {
   161  					BeforeEach(func() {
   162  						Eventually(helpers.CF("create-space-quota", "some-space-quota-name", "-a", "4")).Should(Exit(0))
   163  						Eventually(helpers.CF("set-space-quota", spaceName, "some-space-quota-name")).Should(Exit(0))
   164  						helpers.WriteManifest(manifestPath, map[string]interface{}{
   165  							"applications": []map[string]interface{}{
   166  								{
   167  									"name":      appName,
   168  									"instances": 5,
   169  								},
   170  							},
   171  						})
   172  					})
   173  
   174  					It("reports an error", func() {
   175  						session := helpers.CF("v3-apply-manifest", "-f", manifestPath)
   176  						Eventually(session.Err).Should(Say("memory space_quota_exceeded, app_instance_limit space_app_instance_limit_exceeded"))
   177  						Eventually(session).Should(Say("FAILED"))
   178  
   179  						Eventually(session).Should(Exit(1))
   180  					})
   181  				})
   182  
   183  				When("instances are specified correctly", func() {
   184  					BeforeEach(func() {
   185  						helpers.WriteManifest(manifestPath, map[string]interface{}{
   186  							"applications": []map[string]interface{}{
   187  								{
   188  									"name":      appName,
   189  									"instances": 3,
   190  								},
   191  							},
   192  						})
   193  					})
   194  
   195  					It("displays the experimental warning", func() {
   196  						session := helpers.CF("v3-apply-manifest", "-f", manifestPath)
   197  						Eventually(session.Err).Should(Say("This command is in EXPERIMENTAL stage and may change without notice"))
   198  						Eventually(session).Should(Exit())
   199  					})
   200  
   201  					It("rescales the app", func() {
   202  						session := helpers.CF("app", appName)
   203  						userName, _ := helpers.GetCredentials()
   204  						Eventually(session).Should(Say(`instances:\s+%s`, "1/1"))
   205  						Eventually(session).Should(Exit())
   206  
   207  						session = helpers.CF("v3-apply-manifest", "-f", manifestPath)
   208  						Eventually(session).Should(Say("Applying manifest %s in org %s / space %s as %s...", regexp.QuoteMeta(manifestPath), orgName, spaceName, userName))
   209  						Eventually(session).Should(Exit())
   210  
   211  						session = helpers.CF("app", appName)
   212  						Eventually(session).Should(Say(`instances:\s+%s`, `\d/3`))
   213  						Eventually(session).Should(Exit())
   214  					})
   215  				})
   216  			})
   217  
   218  		})
   219  	})
   220  })