github.com/sleungcy-sap/cli@v7.1.0+incompatible/integration/v7/isolated/restart_app_instance_command_test.go (about)

     1  package isolated
     2  
     3  import (
     4  	"fmt"
     5  
     6  	. "code.cloudfoundry.org/cli/cf/util/testhelpers/matchers"
     7  
     8  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3/constant"
     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("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("appears in cf help -a", func() {
    31  			session := helpers.CF("help", "-a")
    32  			Eventually(session).Should(Exit(0))
    33  			Expect(session).To(HaveCommandInCategoryWithDescription("restart-app-instance", "APPS", "Terminate, then instantiate an app instance"))
    34  		})
    35  
    36  		It("Displays command usage to output", func() {
    37  			session := helpers.CF("restart-app-instance", "--help")
    38  			Eventually(session).Should(Say("NAME:"))
    39  			Eventually(session).Should(Say("restart-app-instance - Terminate, then instantiate an app instance"))
    40  			Eventually(session).Should(Say("USAGE:"))
    41  			Eventually(session).Should(Say(`cf restart-app-instance APP_NAME INDEX [--process PROCESS]`))
    42  			Eventually(session).Should(Say(`OPTIONS:`))
    43  			Eventually(session).Should(Say(`--process\s+Process to restart \(Default: web\)`))
    44  			Eventually(session).Should(Say(`\n`))
    45  			Eventually(session).Should(Say("SEE ALSO:"))
    46  			Eventually(session).Should(Say("restart"))
    47  			Eventually(session).Should(Exit(0))
    48  		})
    49  	})
    50  
    51  	When("the app name is not provided", func() {
    52  		It("tells the user that the app name is required, prints help text, and exits 1", func() {
    53  			session := helpers.CF("restart-app-instance")
    54  
    55  			Eventually(session.Err).Should(Say("Incorrect Usage: the required arguments `APP_NAME` and `INDEX` were not provided"))
    56  			Eventually(session).Should(Say("NAME:"))
    57  			Eventually(session).Should(Exit(1))
    58  		})
    59  	})
    60  
    61  	When("the index is not provided", func() {
    62  		It("tells the user that the index is required, prints help text, and exits 1", func() {
    63  			session := helpers.CF("restart-app-instance", appName)
    64  
    65  			Eventually(session.Err).Should(Say("Incorrect Usage: the required argument `INDEX` was not provided"))
    66  			Eventually(session).Should(Say("NAME:"))
    67  			Eventually(session).Should(Exit(1))
    68  		})
    69  	})
    70  
    71  	When("the environment is not setup correctly", func() {
    72  		When("no API endpoint is set", func() {
    73  			BeforeEach(func() {
    74  				helpers.UnsetAPI()
    75  			})
    76  
    77  			It("fails with no API endpoint set message", func() {
    78  				session := helpers.CF("restart-app-instance", appName, "1")
    79  				Eventually(session).Should(Say("FAILED"))
    80  				Eventually(session.Err).Should(Say("No API endpoint set. Use 'cf login' or 'cf api' to target an endpoint."))
    81  				Eventually(session).Should(Exit(1))
    82  			})
    83  		})
    84  
    85  		When("not logged in", func() {
    86  			BeforeEach(func() {
    87  				helpers.LogoutCF()
    88  			})
    89  
    90  			It("fails with not logged in message", func() {
    91  				session := helpers.CF("restart-app-instance", appName, "1")
    92  				Eventually(session).Should(Say("FAILED"))
    93  				Eventually(session.Err).Should(Say("Not logged in. Use 'cf login' or 'cf login --sso' to log in."))
    94  				Eventually(session).Should(Exit(1))
    95  			})
    96  		})
    97  
    98  		When("there is no org set", func() {
    99  			BeforeEach(func() {
   100  				helpers.LogoutCF()
   101  				helpers.LoginCF()
   102  			})
   103  
   104  			It("fails with no targeted org error message", func() {
   105  				session := helpers.CF("restart-app-instance", appName, "1")
   106  				Eventually(session).Should(Say("FAILED"))
   107  				Eventually(session.Err).Should(Say("No org targeted, use 'cf target -o ORG' to target an org."))
   108  				Eventually(session).Should(Exit(1))
   109  			})
   110  		})
   111  
   112  		When("there is no space set", func() {
   113  			BeforeEach(func() {
   114  				helpers.LogoutCF()
   115  				helpers.LoginCF()
   116  				helpers.TargetOrg(ReadOnlyOrg)
   117  			})
   118  
   119  			It("fails with no targeted space error message", func() {
   120  				session := helpers.CF("restart-app-instance", appName, "1")
   121  				Eventually(session).Should(Say("FAILED"))
   122  				Eventually(session.Err).Should(Say("No space targeted, use 'cf target -s SPACE' to target a space."))
   123  				Eventually(session).Should(Exit(1))
   124  			})
   125  		})
   126  	})
   127  
   128  	When("the environment is setup correctly", func() {
   129  		var userName string
   130  
   131  		BeforeEach(func() {
   132  			helpers.SetupCF(orgName, spaceName)
   133  			userName, _ = helpers.GetCredentials()
   134  		})
   135  
   136  		AfterEach(func() {
   137  			helpers.QuickDeleteOrg(orgName)
   138  		})
   139  
   140  		When("app does not exist", func() {
   141  			It("fails with error", func() {
   142  				session := helpers.CF("restart-app-instance", appName, "0", "--process", "some-process")
   143  				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))
   144  				Eventually(session.Err).Should(Say("App '%s' not found", appName))
   145  				Eventually(session).Should(Exit(1))
   146  			})
   147  		})
   148  
   149  		When("app exists", func() {
   150  			BeforeEach(func() {
   151  				helpers.WithProcfileApp(func(appDir string) {
   152  					Eventually(helpers.CustomCF(helpers.CFEnv{WorkingDirectory: appDir}, "v3-push", appName)).Should(Exit(0))
   153  				})
   154  			})
   155  
   156  			When("process type is not provided", func() {
   157  				It("defaults to web process", func() {
   158  					appOutputSession := helpers.CF("app", appName)
   159  					Eventually(appOutputSession).Should(Exit(0))
   160  					firstAppTable := helpers.ParseV3AppProcessTable(appOutputSession.Out.Contents())
   161  
   162  					session := helpers.CF("restart-app-instance", appName, "0")
   163  					Eventually(session).Should(Say("Restarting instance 0 of process web of app %s in org %s / space %s as %s", appName, orgName, spaceName, userName))
   164  					Eventually(session).Should(Say("OK"))
   165  					Eventually(session).Should(Exit(0))
   166  
   167  					Eventually(func() string {
   168  						var restartedAppTable helpers.AppTable
   169  						Eventually(func() string {
   170  							appOutputSession := helpers.CF("app", appName)
   171  							Eventually(appOutputSession).Should(Exit(0))
   172  							restartedAppTable = helpers.ParseV3AppProcessTable(appOutputSession.Out.Contents())
   173  
   174  							if len(restartedAppTable.Processes) > 0 {
   175  								return fmt.Sprintf("%s, %s", restartedAppTable.Processes[0].Type, restartedAppTable.Processes[0].InstanceCount)
   176  							}
   177  
   178  							return ""
   179  						}).Should(Equal(`web, 1/1`))
   180  						Expect(restartedAppTable.Processes[0].Instances).ToNot(BeEmpty())
   181  						return restartedAppTable.Processes[0].Instances[0].Since
   182  					}).ShouldNot(Equal(firstAppTable.Processes[0].Instances[0].Since))
   183  				})
   184  			})
   185  
   186  			When("a process type is provided", func() {
   187  				When("the process type does not exist", func() {
   188  					It("fails with error", func() {
   189  						session := helpers.CF("restart-app-instance", appName, "0", "--process", "unknown-process")
   190  						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))
   191  						Eventually(session.Err).Should(Say("Process unknown-process not found"))
   192  						Eventually(session).Should(Exit(1))
   193  					})
   194  				})
   195  
   196  				When("the process type exists", func() {
   197  					When("instance index exists", func() {
   198  						findConsoleProcess := func(appTable helpers.AppTable) (helpers.AppProcessTable, bool) {
   199  							for _, process := range appTable.Processes {
   200  								if process.Type == "console" {
   201  									return process, true
   202  								}
   203  							}
   204  							return helpers.AppProcessTable{}, false
   205  						}
   206  
   207  						It("defaults to requested process", func() {
   208  							By("scaling worker process to 1 instance")
   209  							session := helpers.CF("scale", appName, "--process", "console", "-i", "1")
   210  							Eventually(session).Should(Exit(0))
   211  
   212  							By("waiting for worker process to come up")
   213  							var firstAppTableConsoleProcess helpers.AppProcessTable
   214  							Eventually(func() string {
   215  								appOutputSession := helpers.CF("app", appName)
   216  								Eventually(appOutputSession).Should(Exit(0))
   217  								firstAppTable := helpers.ParseV3AppProcessTable(appOutputSession.Out.Contents())
   218  
   219  								var found bool
   220  								firstAppTableConsoleProcess, found = findConsoleProcess(firstAppTable)
   221  								Expect(found).To(BeTrue())
   222  								return fmt.Sprintf("%s, %s", firstAppTableConsoleProcess.Type, firstAppTableConsoleProcess.InstanceCount)
   223  							}).Should(MatchRegexp(`console, 1/1`))
   224  
   225  							By("restarting worker process instance")
   226  							session = helpers.CF("restart-app-instance", appName, "0", "--process", "console")
   227  							Eventually(session).Should(Say("Restarting instance 0 of process console of app %s in org %s / space %s as %s", appName, orgName, spaceName, userName))
   228  							Eventually(session).Should(Say("OK"))
   229  							Eventually(session).Should(Exit(0))
   230  
   231  							By("waiting for restarted process instance to come up")
   232  							Eventually(func() string {
   233  								var restartedAppTableConsoleProcess helpers.AppProcessTable
   234  
   235  								Eventually(func() string {
   236  									appOutputSession := helpers.CF("app", appName)
   237  									Eventually(appOutputSession).Should(Exit(0))
   238  
   239  									restartedAppTable := helpers.ParseV3AppProcessTable(appOutputSession.Out.Contents())
   240  									var found bool
   241  									restartedAppTableConsoleProcess, found = findConsoleProcess(restartedAppTable)
   242  									Expect(found).To(BeTrue())
   243  
   244  									return fmt.Sprintf("%s, %s", restartedAppTableConsoleProcess.Type, restartedAppTableConsoleProcess.InstanceCount)
   245  								}).Should(MatchRegexp(`console, 1/1`))
   246  
   247  								return restartedAppTableConsoleProcess.Instances[0].Since
   248  							}).ShouldNot(Equal(firstAppTableConsoleProcess.Instances[0].Since))
   249  						})
   250  					})
   251  
   252  					When("instance index does not exist", func() {
   253  						It("fails with error", func() {
   254  							session := helpers.CF("restart-app-instance", appName, "42", "--process", constant.ProcessTypeWeb)
   255  							Eventually(session).Should(Say("Restarting instance 42 of process web of app %s in org %s / space %s as %s", appName, orgName, spaceName, userName))
   256  							Eventually(session.Err).Should(Say("Instance 42 of process web not found"))
   257  							Eventually(session).Should(Exit(1))
   258  						})
   259  					})
   260  				})
   261  			})
   262  		})
   263  	})
   264  })