github.com/cloudfoundry-attic/cli-with-i18n@v6.32.1-0.20171002233121-7401370d3b85+incompatible/integration/isolated/scale_command_test.go (about)

     1  package isolated
     2  
     3  import (
     4  	"code.cloudfoundry.org/cli/integration/helpers"
     5  	. "github.com/onsi/ginkgo"
     6  	. "github.com/onsi/gomega"
     7  	. "github.com/onsi/gomega/gbytes"
     8  	. "github.com/onsi/gomega/gexec"
     9  )
    10  
    11  var _ = XDescribe("scale command", func() {
    12  	var (
    13  		orgName   string
    14  		spaceName string
    15  		appName   string
    16  	)
    17  
    18  	BeforeEach(func() {
    19  		orgName = helpers.NewOrgName()
    20  		spaceName = helpers.NewSpaceName()
    21  		appName = helpers.NewAppName()
    22  	})
    23  
    24  	Describe("help", func() {
    25  		Context("when --help flag is set", func() {
    26  			It("Displays command usage to output", func() {
    27  				session := helpers.CF("scale", "--help")
    28  
    29  				Eventually(session).Should(Say("scale - Change or view the instance count, disk space limit, and memory limit for an app"))
    30  				Eventually(session).Should(Say("USAGE:"))
    31  				Eventually(session).Should(Say("cf scale APP_NAME [-i INSTANCES] [-k DISK] [-m MEMORY] [-f]"))
    32  				Eventually(session).Should(Say("OPTIONS:"))
    33  				Eventually(session).Should(Say("-f\\s+Force restart of app without prompt"))
    34  				Eventually(session).Should(Say("-i\\s+Number of instances"))
    35  				Eventually(session).Should(Say("-k\\s+Disk limit (e.g. 256M, 1024M, 1G)"))
    36  				Eventually(session).Should(Say("-m\\s+Memory limit (e.g. 256M, 1024M, 1G)"))
    37  				Eventually(session).Should(Say("SEE ALSO:"))
    38  				Eventually(session).Should(Say("push"))
    39  				Eventually(session).Should(Exit(0))
    40  			})
    41  		})
    42  	})
    43  
    44  	Context("when the environment is not setup correctly", func() {
    45  		Context("when no API endpoint is set", func() {
    46  			BeforeEach(func() {
    47  				helpers.UnsetAPI()
    48  			})
    49  
    50  			It("fails with no API endpoint set message", func() {
    51  				session := helpers.CF("scale", appName)
    52  				Eventually(session).Should(Say("FAILED"))
    53  				Eventually(session.Err).Should(Say("No API endpoint set\\. Use 'cf login' or 'cf api' to target an endpoint\\."))
    54  				Eventually(session).Should(Exit(1))
    55  			})
    56  		})
    57  
    58  		Context("when not logged in", func() {
    59  			BeforeEach(func() {
    60  				helpers.LogoutCF()
    61  			})
    62  
    63  			It("fails with not logged in message", func() {
    64  				session := helpers.CF("scale", appName)
    65  				Eventually(session).Should(Say("FAILED"))
    66  				Eventually(session.Err).Should(Say("Not logged in\\. Use 'cf login' to log in\\."))
    67  				Eventually(session).Should(Exit(1))
    68  			})
    69  		})
    70  
    71  		Context("when there is no org set", func() {
    72  			BeforeEach(func() {
    73  				helpers.LogoutCF()
    74  				helpers.LoginCF()
    75  			})
    76  
    77  			It("fails with no org targeted error message", func() {
    78  				session := helpers.CF("scale", appName)
    79  				Eventually(session.Out).Should(Say("FAILED"))
    80  				Eventually(session.Err).Should(Say("No org targeted, use 'cf target -o ORG' to target an org\\."))
    81  				Eventually(session).Should(Exit(1))
    82  			})
    83  		})
    84  
    85  		Context("when there is no space set", func() {
    86  			BeforeEach(func() {
    87  				helpers.LogoutCF()
    88  				helpers.LoginCF()
    89  				helpers.TargetOrg(ReadOnlyOrg)
    90  			})
    91  
    92  			It("fails with no space targeted error message", func() {
    93  				session := helpers.CF("scale", appName)
    94  				Eventually(session.Out).Should(Say("FAILED"))
    95  				Eventually(session.Err).Should(Say("No space targeted, use 'cf target -s SPACE' to target a space\\."))
    96  				Eventually(session).Should(Exit(1))
    97  			})
    98  		})
    99  	})
   100  
   101  	Context("when the environment is set up correctly", func() {
   102  		var userName string
   103  
   104  		BeforeEach(func() {
   105  			setupCF(orgName, spaceName)
   106  			userName, _ = helpers.GetCredentials()
   107  		})
   108  
   109  		AfterEach(func() {
   110  			helpers.QuickDeleteOrg(orgName)
   111  		})
   112  
   113  		Context("when the app name is not provided", func() {
   114  			It("tells the user that the app name is required, prints help text, and exits 1", func() {
   115  				session := helpers.CF("scale")
   116  
   117  				Eventually(session.Err).Should(Say("Incorrect Usage: the required argument `APP_NAME` was not provided"))
   118  				Eventually(session.Out).Should(Say("NAME:"))
   119  				Eventually(session).Should(Exit(1))
   120  			})
   121  		})
   122  
   123  		Context("when the app does not exist", func() {
   124  			It("tells the user that the app is not found and exits 1", func() {
   125  				session := helpers.CF("scale", appName)
   126  
   127  				Eventually(session.Out).Should(Say("FAILED"))
   128  				Eventually(session.Err).Should(Say("App %s not found", appName))
   129  				Eventually(session).Should(Exit(1))
   130  			})
   131  		})
   132  
   133  		Context("when the app does exist", func() {
   134  			var (
   135  				domainName string
   136  			)
   137  
   138  			BeforeEach(func() {
   139  				helpers.WithHelloWorldApp(func(appDir string) {
   140  					Eventually(helpers.CustomCF(helpers.CFEnv{WorkingDirectory: appDir}, "v2-push", appName)).Should(Exit(0))
   141  				})
   142  
   143  				domainName = defaultSharedDomain()
   144  			})
   145  
   146  			AfterEach(func() {
   147  				Eventually(helpers.CF("delete", appName, "-f", "-r")).Should(Exit(0))
   148  			})
   149  
   150  			Context("when scaling number of instances", func() {
   151  				Context("when the wrong data type is provided to -i", func() {
   152  					It("outputs an error message to the user, provides help text, and exits 1", func() {
   153  						session := helpers.CF("scale", appName, "-i", "not-an-integer")
   154  						Eventually(session.Err).Should(Say("Incorrect Usage: invalid argument for flag `-i' \\(expected int\\)"))
   155  						Eventually(session.Out).Should(Say("cf scale APP_NAME")) // help
   156  						Eventually(session).Should(Exit(1))
   157  					})
   158  				})
   159  
   160  				Context("when correct data is provided to -i", func() {
   161  					It("scales application to specified number of instances", func() {
   162  						session := helpers.CF("scale", appName, "-i", "2")
   163  						Eventually(session.Out).Should(Say("Scaling app %s in org %s / space %s as %s...", appName, orgName, spaceName, userName)) // help
   164  						Eventually(session).Should(Say("OK"))
   165  						Eventually(session).Should(Exit(0))
   166  					})
   167  				})
   168  			})
   169  
   170  			Context("when scaling memory", func() {
   171  				Context("when the wrong data type is provided to -m", func() {
   172  					It("outputs an error message to the user, provides help text, and exits 1", func() {
   173  						session := helpers.CF("scale", appName, "-m", "not-a-memory")
   174  						Eventually(session.Err).Should(Say("Incorrect Usage: invalid argument for flag `-m`"))
   175  						Eventually(session.Out).Should(Say("cf scale APP_NAME")) // help
   176  						Eventually(session).Should(Exit(1))
   177  					})
   178  				})
   179  
   180  				Context("when correct data is provided to -m", func() {
   181  					Context("when -f flag is not provided", func() {
   182  						var buffer *Buffer
   183  
   184  						BeforeEach(func() {
   185  							buffer = NewBuffer()
   186  						})
   187  
   188  						Context("when user enters y", func() {
   189  							It("scales application to specified memory with restart", func() {
   190  								buffer.Write([]byte("y\n"))
   191  								session := helpers.CFWithStdin(buffer, "scale", appName, "-m", "256M")
   192  								Eventually(session.Out).Should(Say("This will cause the app to restart. Are you sure you want to scale %s\\? \\[yN]]", appName))
   193  								Eventually(session.Out).Should(Say("Scaling app %s in org %s / space %s as %s...", appName, orgName, spaceName, userName))
   194  								Eventually(session.Out).Should(Say("Stopping app %s in org %s / space %s as %s...", appName, orgName, spaceName, userName))
   195  								Eventually(session.Out).Should(Say("Starting app %s in org %s / space %s as %s...", appName, orgName, spaceName, userName))
   196  								Eventually(session.Out).Should(Say("Waiting for app to start\\.\\.\\."))
   197  
   198  								Eventually(session.Out).Should(Say("name:\\s+%s", appName))
   199  								Eventually(session.Out).Should(Say("requested state:\\s+started"))
   200  								Eventually(session.Out).Should(Say("instances:\\s+1/1"))
   201  								Eventually(session.Out).Should(Say("usage:\\s+256M x 1 instances"))
   202  								Eventually(session.Out).Should(Say("routes:\\s+%s.%s", appName, domainName))
   203  								Eventually(session.Out).Should(Say("last uploaded:"))
   204  								Eventually(session.Out).Should(Say("stack:\\s+cflinuxfs2"))
   205  								Eventually(session.Out).Should(Say("buildpack:\\s+staticfile_buildpack"))
   206  								Eventually(session.Out).Should(Say("start command:"))
   207  
   208  								Eventually(session.Out).Should(Say("state\\s+since\\s+cpu\\s+memory\\s+disk\\s+details"))
   209  								Eventually(session.Out).Should(Say("#0\\s+(running|starting)\\s+.*\\d+\\.\\d+%.*of 256M.*of 1G"))
   210  								Eventually(session.Out).Should(Exit(0))
   211  							})
   212  						})
   213  
   214  						Context("when user enters n", func() {
   215  							It("does not scale the app", func() {
   216  								buffer.Write([]byte("n\n"))
   217  								session := helpers.CFWithStdin(buffer, "scale", appName, "-m", "256M")
   218  								Eventually(session.Out).Should(Say("This will cause the app to restart. Are you sure you want to scale %s\\? \\[yN]]", appName))
   219  								Eventually(session.Out).Should(Say("Scale cancelled"))
   220  								Eventually(session).Should(Exit(0))
   221  								session = helpers.CF("scale", appName)
   222  								Eventually(session.Out).Should(Say("memoty:\\s+128M"))
   223  								Eventually(session).Should(Exit(0))
   224  							})
   225  						})
   226  					})
   227  
   228  					Context("when -f flag provided", func() {
   229  						It("scales without prompt", func() {
   230  							session := helpers.CF("scale", appName, "-m", "256M", "-f")
   231  							Eventually(session.Out).Should(Say("Scaling app %s in org %s / space %s as %s...", appName, orgName, spaceName, userName))
   232  							Eventually(session).Should(Exit(0))
   233  						})
   234  					})
   235  				})
   236  			})
   237  
   238  			Context("when scaling disk", func() {
   239  				Context("when the wrong data type is provided to -k", func() {
   240  					It("outputs an error message to the user, provides help text, and exits 1", func() {
   241  						session := helpers.CF("scale", appName, "-k", "not-a-disk")
   242  						Eventually(session.Err).Should(Say("Incorrect Usage: invalid argument for flag `-k`"))
   243  						Eventually(session.Out).Should(Say("cf scale APP_NAME")) // help
   244  						Eventually(session).Should(Exit(1))
   245  					})
   246  				})
   247  
   248  				It("scales application to specified disk with restart", func() {
   249  					session := helpers.CF("scale", appName, "-k", "512M", "-f")
   250  					Eventually(session.Out).Should(Say("Scaling app %s in org %s / space %s as %s...", appName, orgName, spaceName, userName))
   251  					Eventually(session.Out).Should(Say("Stopping app %s in org %s / space %s as %s...", appName, orgName, spaceName, userName))
   252  					Eventually(session.Out).Should(Say("Starting app %s in org %s / space %s as %s...", appName, orgName, spaceName, userName))
   253  					Eventually(session.Out).Should(Say("Waiting for app to start\\.\\.\\."))
   254  
   255  					Eventually(session.Out).Should(Say("name:\\s+%s", appName))
   256  					Eventually(session.Out).Should(Say("requested state:\\s+started"))
   257  					Eventually(session.Out).Should(Say("instances:\\s+1/1"))
   258  					Eventually(session.Out).Should(Say("usage:\\s+128M x 1 instances"))
   259  					Eventually(session.Out).Should(Say("routes:\\s+%s.%s", appName, domainName))
   260  					Eventually(session.Out).Should(Say("last uploaded:"))
   261  					Eventually(session.Out).Should(Say("stack:\\s+cflinuxfs2"))
   262  					Eventually(session.Out).Should(Say("buildpack:\\s+staticfile_buildpack"))
   263  					Eventually(session.Out).Should(Say("start command:"))
   264  
   265  					Eventually(session.Out).Should(Say("state\\s+since\\s+cpu\\s+memory\\s+disk\\s+details"))
   266  					Eventually(session.Out).Should(Say("#0\\s+(running|starting)\\s+.*\\d+\\.\\d+%.*of 128M.*of 512M"))
   267  					Eventually(session.Out).Should(Exit(0))
   268  				})
   269  			})
   270  
   271  			Context("when scaling all of them", func() {
   272  				It("scales application to specified number of instances, memory and disk with restart", func() {
   273  					session := helpers.CF("scale", appName, "-i", "2", "-m", "256M", "-k", "512M", "-f")
   274  					Eventually(session.Out).Should(Say("Scaling app %s in org %s / space %s as %s...", appName, orgName, spaceName, userName))
   275  					Eventually(session.Out).Should(Say("Stopping app %s in org %s / space %s as %s...", appName, orgName, spaceName, userName))
   276  					Eventually(session.Out).Should(Say("Starting app %s in org %s / space %s as %s...", appName, orgName, spaceName, userName))
   277  					Eventually(session.Out).Should(Say("Waiting for app to start\\.\\.\\."))
   278  
   279  					Eventually(session.Out).Should(Say("name:\\s+%s", appName))
   280  					Eventually(session.Out).Should(Say("requested state:\\s+started"))
   281  					Eventually(session.Out).Should(Say("instances:\\s+2/2"))
   282  					Eventually(session.Out).Should(Say("usage:\\s+256M x 2 instances"))
   283  					Eventually(session.Out).Should(Say("routes:\\s+%s.%s", appName, domainName))
   284  					Eventually(session.Out).Should(Say("last uploaded:"))
   285  					Eventually(session.Out).Should(Say("stack:\\s+cflinuxfs2"))
   286  					Eventually(session.Out).Should(Say("buildpack:\\s+staticfile_buildpack"))
   287  					Eventually(session.Out).Should(Say("start command:"))
   288  
   289  					Eventually(session.Out).Should(Say("state\\s+since\\s+cpu\\s+memory\\s+disk\\s+details"))
   290  					Eventually(session.Out).Should(Say("#0\\s+(running|starting)\\s+.*\\d+\\.\\d+%.*of 256M.*of 512M"))
   291  					Eventually(session.Out).Should(Say("#1\\s+(running|starting)\\s+.*\\d+\\.\\d+%.*of 256M.*of 512M"))
   292  					Eventually(session.Out).Should(Exit(0))
   293  				})
   294  			})
   295  
   296  			Context("when scaling argument is not provided", func() {
   297  				It("outputs current scaling information", func() {
   298  					session := helpers.CF("scale", appName)
   299  					Eventually(session).Should(Say("Showing current scale of app %s in org %s / space %s as %s...", appName, orgName, spaceName, userName))
   300  
   301  					Eventually(session).Should(Say("memory: 128M"))
   302  					Eventually(session).Should(Say("disk: 1G"))
   303  					Eventually(session).Should(Say("instances: 1"))
   304  
   305  					Eventually(session.Out).Should(Exit(0))
   306  				})
   307  			})
   308  		})
   309  	})
   310  })