github.com/willmadison/cli@v6.40.1-0.20181018160101-29d5937903ff+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/api/cloudcontroller/ccversion"
    10  	"code.cloudfoundry.org/cli/integration/helpers"
    11  	. "github.com/onsi/ginkgo"
    12  	. "github.com/onsi/gomega"
    13  	. "github.com/onsi/gomega/gbytes"
    14  	. "github.com/onsi/gomega/gexec"
    15  	. "github.com/onsi/gomega/ghttp"
    16  )
    17  
    18  var _ = Describe("v3-apply-manifest command", func() {
    19  	var (
    20  		orgName      string
    21  		spaceName    string
    22  		appName      string
    23  		manifestPath string
    24  		appDir       string
    25  	)
    26  
    27  	BeforeEach(func() {
    28  		orgName = helpers.NewOrgName()
    29  		spaceName = helpers.NewSpaceName()
    30  		appName = helpers.PrefixedRandomName("app")
    31  		appDir, _ = ioutil.TempDir("", "simple-app")
    32  		manifestPath = filepath.Join(appDir, "manifest.yml")
    33  		// Ensure the file exists at the minimum
    34  		helpers.WriteManifest(manifestPath, map[string]interface{}{})
    35  	})
    36  
    37  	AfterEach(func() {
    38  		Expect(os.RemoveAll(appDir)).ToNot(HaveOccurred())
    39  	})
    40  
    41  	Describe("help", func() {
    42  		When("--help flag is set", func() {
    43  			It("displays command usage to output", func() {
    44  				session := helpers.CF("v3-apply-manifest", "--help")
    45  
    46  				Eventually(session).Should(Say("NAME:"))
    47  				Eventually(session).Should(Say("v3-apply-manifest - Applies manifest properties to an application"))
    48  				Eventually(session).Should(Say("USAGE:"))
    49  				Eventually(session).Should(Say("cf v3-apply-manifest -f APP_MANIFEST_PATH"))
    50  
    51  				Eventually(session).Should(Exit(0))
    52  			})
    53  		})
    54  	})
    55  
    56  	When("the -f flag is not given an arg", func() {
    57  		It("tells the user that the flag requires an arg, prints help text, and exits 1", func() {
    58  			session := helpers.CF("v3-apply-manifest", "-f")
    59  
    60  			Eventually(session.Err).Should(Say("Incorrect Usage: expected argument for flag `-f'"))
    61  			Eventually(session).Should(Say("NAME:"))
    62  			Eventually(session).Should(Exit(1))
    63  		})
    64  	})
    65  
    66  	When("the -f flag path does not exist", func() {
    67  		It("tells the user that the provided path doesn't exist, prints help text, and exits 1", func() {
    68  			session := helpers.CF("v3-apply-manifest", "-f", "path/that/does/not/exist")
    69  
    70  			Eventually(session.Err).Should(Say("Incorrect Usage: The specified path 'path/that/does/not/exist' does not exist."))
    71  			Eventually(session).Should(Say("NAME:"))
    72  			Eventually(session).Should(Exit(1))
    73  		})
    74  	})
    75  
    76  	When("the environment is not setup correctly", func() {
    77  		When("no API endpoint is set", func() {
    78  			BeforeEach(func() {
    79  				helpers.UnsetAPI()
    80  			})
    81  
    82  			It("fails with no API endpoint set message", func() {
    83  				session := helpers.CF("v3-apply-manifest", "-f", manifestPath)
    84  				Eventually(session).Should(Say("FAILED"))
    85  				Eventually(session.Err).Should(Say("No API endpoint set\\. Use 'cf login' or 'cf api' to target an endpoint\\."))
    86  				Eventually(session).Should(Exit(1))
    87  			})
    88  		})
    89  
    90  		When("the v3 api version is lower than the minimum version", func() {
    91  			var server *Server
    92  
    93  			BeforeEach(func() {
    94  				server = helpers.StartAndTargetServerWithAPIVersions(helpers.DefaultV2Version, ccversion.MinV3ClientVersion)
    95  			})
    96  
    97  			AfterEach(func() {
    98  				server.Close()
    99  			})
   100  
   101  			It("fails with error message that the minimum version is not met", func() {
   102  				session := helpers.CF("v3-apply-manifest", "-f", manifestPath)
   103  				Eventually(session).Should(Say("FAILED"))
   104  				Eventually(session.Err).Should(Say("This command requires CF API version 3\\.27\\.0 or higher\\."))
   105  				Eventually(session).Should(Exit(1))
   106  			})
   107  		})
   108  
   109  		When("not logged in", func() {
   110  			BeforeEach(func() {
   111  				helpers.LogoutCF()
   112  			})
   113  
   114  			It("fails with not logged in message", func() {
   115  				session := helpers.CF("v3-apply-manifest", "-f", manifestPath)
   116  				Eventually(session.Err).Should(Say("Not logged in\\. Use 'cf login' to log in\\."))
   117  				Eventually(session).Should(Say("FAILED"))
   118  				Eventually(session).Should(Exit(1))
   119  			})
   120  		})
   121  
   122  		When("there is no org set", func() {
   123  			BeforeEach(func() {
   124  				helpers.LogoutCF()
   125  				helpers.LoginCF()
   126  			})
   127  
   128  			It("fails with no org targeted error message", func() {
   129  				session := helpers.CF("v3-apply-manifest", "-f", manifestPath)
   130  				Eventually(session).Should(Say("FAILED"))
   131  				Eventually(session.Err).Should(Say("No org targeted, use 'cf target -o ORG' to target an org\\."))
   132  				Eventually(session).Should(Exit(1))
   133  			})
   134  		})
   135  
   136  		When("there is no space set", func() {
   137  			BeforeEach(func() {
   138  				helpers.LogoutCF()
   139  				helpers.LoginCF()
   140  				helpers.TargetOrg(ReadOnlyOrg)
   141  			})
   142  
   143  			It("fails with no space targeted error message", func() {
   144  				session := helpers.CF("v3-apply-manifest", "-f", manifestPath)
   145  				Eventually(session).Should(Say("FAILED"))
   146  				Eventually(session.Err).Should(Say("No space targeted, use 'cf target -s SPACE' to target a space\\."))
   147  				Eventually(session).Should(Exit(1))
   148  			})
   149  		})
   150  	})
   151  
   152  	When("the environment is set up correctly", func() {
   153  		BeforeEach(func() {
   154  			helpers.SetupCF(orgName, spaceName)
   155  		})
   156  
   157  		AfterEach(func() {
   158  			helpers.QuickDeleteOrg(orgName)
   159  		})
   160  
   161  		When("the app exists", func() {
   162  			BeforeEach(func() {
   163  				helpers.WithHelloWorldApp(func(appDir string) {
   164  					Eventually(helpers.CustomCF(helpers.CFEnv{WorkingDirectory: appDir}, "v3-push", appName)).Should(Exit(0))
   165  				})
   166  			})
   167  
   168  			When("the app name in the manifest is missing", func() {
   169  				BeforeEach(func() {
   170  					helpers.WriteManifest(manifestPath, map[string]interface{}{
   171  						"applications": []map[string]interface{}{
   172  							{
   173  								"instances": 3,
   174  							},
   175  						},
   176  					})
   177  				})
   178  
   179  				It("reports an error", func() {
   180  					session := helpers.CF("v3-apply-manifest", "-f", manifestPath)
   181  					Eventually(session.Err).Should(Say("Found an application with no name specified"))
   182  					Eventually(session).Should(Say("FAILED"))
   183  					Eventually(session).Should(Exit(1))
   184  				})
   185  			})
   186  
   187  			When("the app name in the manifest doesn't exist", func() {
   188  				var invalidAppName string
   189  				BeforeEach(func() {
   190  					invalidAppName = "no-such-app"
   191  					helpers.WriteManifest(manifestPath, map[string]interface{}{
   192  						"applications": []map[string]interface{}{
   193  							{
   194  								"name":      invalidAppName,
   195  								"instances": 3,
   196  							},
   197  						},
   198  					})
   199  				})
   200  
   201  				It("reports an error", func() {
   202  					session := helpers.CF("v3-apply-manifest", "-f", manifestPath)
   203  					Eventually(session.Err).Should(Say("App %s not found", invalidAppName))
   204  					Eventually(session).Should(Say("FAILED"))
   205  
   206  					Eventually(session).Should(Exit(1))
   207  				})
   208  			})
   209  
   210  			When("the app name in the manifest does exist", func() {
   211  				When("the instances value is negative", func() {
   212  					BeforeEach(func() {
   213  						helpers.WriteManifest(manifestPath, map[string]interface{}{
   214  							"applications": []map[string]interface{}{
   215  								{
   216  									"name":      appName,
   217  									"instances": -1,
   218  								},
   219  							},
   220  						})
   221  					})
   222  
   223  					It("reports an error", func() {
   224  						session := helpers.CF("v3-apply-manifest", "-f", manifestPath)
   225  						Eventually(session.Err).Should(Say("Instances must be greater than or equal to 0"))
   226  						Eventually(session).Should(Say("FAILED"))
   227  
   228  						Eventually(session).Should(Exit(1))
   229  					})
   230  				})
   231  
   232  				When("the instances value is more than the space quota limit", func() {
   233  					BeforeEach(func() {
   234  						Eventually(helpers.CF("create-space-quota", "some-space-quota-name", "-a", "4")).Should(Exit(0))
   235  						Eventually(helpers.CF("set-space-quota", spaceName, "some-space-quota-name")).Should(Exit(0))
   236  						helpers.WriteManifest(manifestPath, map[string]interface{}{
   237  							"applications": []map[string]interface{}{
   238  								{
   239  									"name":      appName,
   240  									"instances": 5,
   241  								},
   242  							},
   243  						})
   244  					})
   245  
   246  					It("reports an error", func() {
   247  						session := helpers.CF("v3-apply-manifest", "-f", manifestPath)
   248  						Eventually(session.Err).Should(Say("memory space_quota_exceeded, app_instance_limit space_app_instance_limit_exceeded"))
   249  						Eventually(session).Should(Say("FAILED"))
   250  
   251  						Eventually(session).Should(Exit(1))
   252  					})
   253  				})
   254  
   255  				When("instances are specified correctly", func() {
   256  					BeforeEach(func() {
   257  						helpers.WriteManifest(manifestPath, map[string]interface{}{
   258  							"applications": []map[string]interface{}{
   259  								{
   260  									"name":      appName,
   261  									"instances": 3,
   262  								},
   263  							},
   264  						})
   265  					})
   266  
   267  					It("displays the experimental warning", func() {
   268  						session := helpers.CF("v3-apply-manifest", "-f", manifestPath)
   269  						Eventually(session.Err).Should(Say("This command is in EXPERIMENTAL stage and may change without notice"))
   270  						Eventually(session).Should(Exit())
   271  					})
   272  
   273  					It("rescales the app", func() {
   274  						session := helpers.CF("app", appName)
   275  						userName, _ := helpers.GetCredentials()
   276  						Eventually(session).Should(Say("instances:\\s+%s", "1/1"))
   277  						Eventually(session).Should(Exit())
   278  
   279  						session = helpers.CF("v3-apply-manifest", "-f", manifestPath)
   280  						Eventually(session).Should(Say("Applying manifest %s in org %s / space %s as %s...", regexp.QuoteMeta(manifestPath), orgName, spaceName, userName))
   281  						Eventually(session).Should(Exit())
   282  
   283  						session = helpers.CF("app", appName)
   284  						Eventually(session).Should(Say("instances:\\s+%s", "\\d/3"))
   285  						Eventually(session).Should(Exit())
   286  					})
   287  				})
   288  			})
   289  
   290  		})
   291  	})
   292  })