github.com/loggregator/cli@v6.33.1-0.20180224010324-82334f081791+incompatible/integration/experimental/v3_apply_manifest_command_test.go (about)

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