github.com/randomtask1155/cli@v6.41.1-0.20181227003417-a98eed78cbde+incompatible/integration/v6/experimental/v3_restart_app_instance_command_test.go (about)

     1  package experimental
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3/constant"
     7  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccversion"
     8  	"code.cloudfoundry.org/cli/integration/helpers"
     9  	. "github.com/onsi/ginkgo"
    10  	. "github.com/onsi/gomega"
    11  	. "github.com/onsi/gomega/gbytes"
    12  	. "github.com/onsi/gomega/gexec"
    13  	. "github.com/onsi/gomega/ghttp"
    14  )
    15  
    16  var _ = Describe("v3-restart-app-instance command", func() {
    17  	var (
    18  		orgName   string
    19  		spaceName string
    20  		appName   string
    21  	)
    22  
    23  	BeforeEach(func() {
    24  		orgName = helpers.NewOrgName()
    25  		spaceName = helpers.NewSpaceName()
    26  		appName = helpers.PrefixedRandomName("app")
    27  	})
    28  
    29  	When("--help flag is set", func() {
    30  		It("Displays command usage to output", func() {
    31  			session := helpers.CF("v3-restart-app-instance", "--help")
    32  			Eventually(session).Should(Say("NAME:"))
    33  			Eventually(session).Should(Say("v3-restart-app-instance - Terminate, then instantiate an app instance"))
    34  			Eventually(session).Should(Say("USAGE:"))
    35  			Eventually(session).Should(Say(`cf v3-restart-app-instance APP_NAME INDEX [--process PROCESS]`))
    36  			Eventually(session).Should(Say("SEE ALSO:"))
    37  			Eventually(session).Should(Say("v3-restart"))
    38  			Eventually(session).Should(Exit(0))
    39  		})
    40  	})
    41  
    42  	When("the app name is not provided", func() {
    43  		It("tells the user that the app name is required, prints help text, and exits 1", func() {
    44  			session := helpers.CF("v3-restart-app-instance")
    45  
    46  			Eventually(session.Err).Should(Say("Incorrect Usage: the required arguments `APP_NAME` and `INDEX` were not provided"))
    47  			Eventually(session).Should(Say("NAME:"))
    48  			Eventually(session).Should(Exit(1))
    49  		})
    50  	})
    51  
    52  	When("the index is not provided", func() {
    53  		It("tells the user that the index is required, prints help text, and exits 1", func() {
    54  			session := helpers.CF("v3-restart-app-instance", appName)
    55  
    56  			Eventually(session.Err).Should(Say("Incorrect Usage: the required argument `INDEX` was not provided"))
    57  			Eventually(session).Should(Say("NAME:"))
    58  			Eventually(session).Should(Exit(1))
    59  		})
    60  	})
    61  
    62  	It("displays the experimental warning", func() {
    63  		session := helpers.CF("v3-restart-app-instance", appName, "1")
    64  		Eventually(session.Err).Should(Say("This command is in EXPERIMENTAL stage and may change without notice"))
    65  		Eventually(session).Should(Exit())
    66  	})
    67  
    68  	When("the environment is not setup correctly", func() {
    69  		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-restart-app-instance", appName, "1")
    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  		When("the v3 api version is lower than the minimum version", func() {
    83  			var server *Server
    84  
    85  			BeforeEach(func() {
    86  				server = helpers.StartAndTargetServerWithAPIVersions(helpers.DefaultV2Version, ccversion.MinV3ClientVersion)
    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-restart-app-instance", appName, "1")
    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  		When("not logged in", func() {
   102  			BeforeEach(func() {
   103  				helpers.LogoutCF()
   104  			})
   105  
   106  			It("fails with not logged in message", func() {
   107  				session := helpers.CF("v3-restart-app-instance", appName, "1")
   108  				Eventually(session).Should(Say("FAILED"))
   109  				Eventually(session.Err).Should(Say("Not logged in. Use 'cf login' to log in."))
   110  				Eventually(session).Should(Exit(1))
   111  			})
   112  		})
   113  
   114  		When("there is no org set", func() {
   115  			BeforeEach(func() {
   116  				helpers.LogoutCF()
   117  				helpers.LoginCF()
   118  			})
   119  
   120  			It("fails with no targeted org error message", func() {
   121  				session := helpers.CF("v3-restart-app-instance", appName, "1")
   122  				Eventually(session).Should(Say("FAILED"))
   123  				Eventually(session.Err).Should(Say("No org targeted, use 'cf target -o ORG' to target an org."))
   124  				Eventually(session).Should(Exit(1))
   125  			})
   126  		})
   127  
   128  		When("there is no space set", func() {
   129  			BeforeEach(func() {
   130  				helpers.LogoutCF()
   131  				helpers.LoginCF()
   132  				helpers.TargetOrg(ReadOnlyOrg)
   133  			})
   134  
   135  			It("fails with no targeted space error message", func() {
   136  				session := helpers.CF("v3-restart-app-instance", appName, "1")
   137  				Eventually(session).Should(Say("FAILED"))
   138  				Eventually(session.Err).Should(Say("No space targeted, use 'cf target -s SPACE' to target a space."))
   139  				Eventually(session).Should(Exit(1))
   140  			})
   141  		})
   142  	})
   143  
   144  	When("the environment is setup correctly", func() {
   145  		var userName string
   146  
   147  		BeforeEach(func() {
   148  			helpers.SetupCF(orgName, spaceName)
   149  			userName, _ = helpers.GetCredentials()
   150  		})
   151  
   152  		AfterEach(func() {
   153  			helpers.QuickDeleteOrg(orgName)
   154  		})
   155  
   156  		When("app does not exist", func() {
   157  			It("fails with error", func() {
   158  				session := helpers.CF("v3-restart-app-instance", appName, "0", "--process", "some-process")
   159  				Eventually(session).Should(Say("Restarting instance 0 of process some-process of app %s in org %s / space %s as %s", appName, orgName, spaceName, userName))
   160  				Eventually(session.Err).Should(Say("App %s not found", appName))
   161  				Eventually(session).Should(Exit(1))
   162  			})
   163  		})
   164  
   165  		When("app exists", func() {
   166  			BeforeEach(func() {
   167  				helpers.WithProcfileApp(func(appDir string) {
   168  					Eventually(helpers.CustomCF(helpers.CFEnv{WorkingDirectory: appDir}, "v3-push", appName)).Should(Exit(0))
   169  				})
   170  			})
   171  
   172  			When("process type is not provided", func() {
   173  				It("defaults to web process", func() {
   174  					appOutputSession := helpers.CF("app", appName)
   175  					Eventually(appOutputSession).Should(Exit(0))
   176  					firstAppTable := helpers.ParseV3AppProcessTable(appOutputSession.Out.Contents())
   177  
   178  					session := helpers.CF("v3-restart-app-instance", appName, "0")
   179  					Eventually(session).Should(Say("Restarting instance 0 of process web of app %s in org %s / space %s as %s", appName, orgName, spaceName, userName))
   180  					Eventually(session).Should(Say("OK"))
   181  					Eventually(session).Should(Exit(0))
   182  
   183  					Eventually(func() string {
   184  						var restartedAppTable helpers.AppTable
   185  						Eventually(func() string {
   186  							appOutputSession := helpers.CF("app", appName)
   187  							Eventually(appOutputSession).Should(Exit(0))
   188  							restartedAppTable = helpers.ParseV3AppProcessTable(appOutputSession.Out.Contents())
   189  
   190  							if len(restartedAppTable.Processes) > 0 {
   191  								return fmt.Sprintf("%s, %s", restartedAppTable.Processes[0].Type, restartedAppTable.Processes[0].InstanceCount)
   192  							}
   193  
   194  							return ""
   195  						}).Should(Equal(`web, 1/1`))
   196  						Expect(restartedAppTable.Processes[0].Instances).ToNot(BeEmpty())
   197  						return restartedAppTable.Processes[0].Instances[0].Since
   198  					}).ShouldNot(Equal(firstAppTable.Processes[0].Instances[0].Since))
   199  				})
   200  			})
   201  
   202  			When("a process type is provided", func() {
   203  				When("the process type does not exist", func() {
   204  					It("fails with error", func() {
   205  						session := helpers.CF("v3-restart-app-instance", appName, "0", "--process", "unknown-process")
   206  						Eventually(session).Should(Say("Restarting instance 0 of process unknown-process of app %s in org %s / space %s as %s", appName, orgName, spaceName, userName))
   207  						Eventually(session.Err).Should(Say("Process unknown-process not found"))
   208  						Eventually(session).Should(Exit(1))
   209  					})
   210  				})
   211  
   212  				When("the process type exists", func() {
   213  					When("instance index exists", func() {
   214  						findConsoleProcess := func(appTable helpers.AppTable) (helpers.AppProcessTable, bool) {
   215  							for _, process := range appTable.Processes {
   216  								if process.Type == "console" {
   217  									return process, true
   218  								}
   219  							}
   220  							return helpers.AppProcessTable{}, false
   221  						}
   222  
   223  						It("defaults to requested process", func() {
   224  							By("scaling worker process to 1 instance")
   225  							session := helpers.CF("v3-scale", appName, "--process", "console", "-i", "1")
   226  							Eventually(session).Should(Exit(0))
   227  
   228  							By("waiting for worker process to come up")
   229  							var firstAppTableConsoleProcess helpers.AppProcessTable
   230  							Eventually(func() string {
   231  								appOutputSession := helpers.CF("app", appName)
   232  								Eventually(appOutputSession).Should(Exit(0))
   233  								firstAppTable := helpers.ParseV3AppProcessTable(appOutputSession.Out.Contents())
   234  
   235  								var found bool
   236  								firstAppTableConsoleProcess, found = findConsoleProcess(firstAppTable)
   237  								Expect(found).To(BeTrue())
   238  								return fmt.Sprintf("%s, %s", firstAppTableConsoleProcess.Type, firstAppTableConsoleProcess.InstanceCount)
   239  							}).Should(MatchRegexp(`console, 1/1`))
   240  
   241  							By("restarting worker process instance")
   242  							session = helpers.CF("v3-restart-app-instance", appName, "0", "--process", "console")
   243  							Eventually(session).Should(Say("Restarting instance 0 of process console of app %s in org %s / space %s as %s", appName, orgName, spaceName, userName))
   244  							Eventually(session).Should(Say("OK"))
   245  							Eventually(session).Should(Exit(0))
   246  
   247  							By("waiting for restarted process instance to come up")
   248  							Eventually(func() string {
   249  								var restartedAppTableConsoleProcess helpers.AppProcessTable
   250  
   251  								Eventually(func() string {
   252  									appOutputSession := helpers.CF("app", appName)
   253  									Eventually(appOutputSession).Should(Exit(0))
   254  
   255  									restartedAppTable := helpers.ParseV3AppProcessTable(appOutputSession.Out.Contents())
   256  									var found bool
   257  									restartedAppTableConsoleProcess, found = findConsoleProcess(restartedAppTable)
   258  									Expect(found).To(BeTrue())
   259  
   260  									return fmt.Sprintf("%s, %s", restartedAppTableConsoleProcess.Type, restartedAppTableConsoleProcess.InstanceCount)
   261  								}).Should(MatchRegexp(`console, 1/1`))
   262  
   263  								return restartedAppTableConsoleProcess.Instances[0].Since
   264  							}).ShouldNot(Equal(firstAppTableConsoleProcess.Instances[0].Since))
   265  						})
   266  					})
   267  
   268  					When("instance index does not exist", func() {
   269  						It("fails with error", func() {
   270  							session := helpers.CF("v3-restart-app-instance", appName, "42", "--process", constant.ProcessTypeWeb)
   271  							Eventually(session).Should(Say("Restarting instance 42 of process web of app %s in org %s / space %s as %s", appName, orgName, spaceName, userName))
   272  							Eventually(session.Err).Should(Say("Instance 42 of process web not found"))
   273  							Eventually(session).Should(Exit(1))
   274  						})
   275  					})
   276  				})
   277  			})
   278  		})
   279  	})
   280  })