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