github.com/jenspinney/cli@v6.42.1-0.20190207184520-7450c600020e+incompatible/integration/v7/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/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  )
    13  
    14  var _ = Describe("v3-restart-app-instance command", func() {
    15  	var (
    16  		orgName   string
    17  		spaceName string
    18  		appName   string
    19  	)
    20  
    21  	BeforeEach(func() {
    22  		orgName = helpers.NewOrgName()
    23  		spaceName = helpers.NewSpaceName()
    24  		appName = helpers.PrefixedRandomName("app")
    25  	})
    26  
    27  	When("--help flag is set", func() {
    28  		It("Displays command usage to output", func() {
    29  			session := helpers.CF("v3-restart-app-instance", "--help")
    30  			Eventually(session).Should(Say("NAME:"))
    31  			Eventually(session).Should(Say("v3-restart-app-instance - Terminate, then instantiate an app instance"))
    32  			Eventually(session).Should(Say("USAGE:"))
    33  			Eventually(session).Should(Say(`cf v3-restart-app-instance APP_NAME INDEX [--process PROCESS]`))
    34  			Eventually(session).Should(Say("SEE ALSO:"))
    35  			Eventually(session).Should(Say("v3-restart"))
    36  			Eventually(session).Should(Exit(0))
    37  		})
    38  	})
    39  
    40  	When("the app name is not provided", func() {
    41  		It("tells the user that the app name is required, prints help text, and exits 1", func() {
    42  			session := helpers.CF("v3-restart-app-instance")
    43  
    44  			Eventually(session.Err).Should(Say("Incorrect Usage: the required arguments `APP_NAME` and `INDEX` were not provided"))
    45  			Eventually(session).Should(Say("NAME:"))
    46  			Eventually(session).Should(Exit(1))
    47  		})
    48  	})
    49  
    50  	When("the index is not provided", func() {
    51  		It("tells the user that the index is required, prints help text, and exits 1", func() {
    52  			session := helpers.CF("v3-restart-app-instance", appName)
    53  
    54  			Eventually(session.Err).Should(Say("Incorrect Usage: the required argument `INDEX` was not provided"))
    55  			Eventually(session).Should(Say("NAME:"))
    56  			Eventually(session).Should(Exit(1))
    57  		})
    58  	})
    59  
    60  	It("displays the experimental warning", func() {
    61  		session := helpers.CF("v3-restart-app-instance", appName, "1")
    62  		Eventually(session.Err).Should(Say("This command is in EXPERIMENTAL stage and may change without notice"))
    63  		Eventually(session).Should(Exit())
    64  	})
    65  
    66  	When("the environment is not setup correctly", func() {
    67  		It("fails with the appropriate errors", func() {
    68  			helpers.CheckEnvironmentTargetedCorrectly(true, true, ReadOnlyOrg, "v3-restart-app-instance", appName, "1")
    69  		})
    70  	})
    71  
    72  	When("the environment is setup correctly", func() {
    73  		var userName string
    74  
    75  		BeforeEach(func() {
    76  			helpers.SetupCF(orgName, spaceName)
    77  			userName, _ = helpers.GetCredentials()
    78  		})
    79  
    80  		AfterEach(func() {
    81  			helpers.QuickDeleteOrg(orgName)
    82  		})
    83  
    84  		When("app does not exist", func() {
    85  			It("fails with error", func() {
    86  				session := helpers.CF("v3-restart-app-instance", appName, "0", "--process", "some-process")
    87  				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))
    88  				Eventually(session.Err).Should(Say("App %s not found", appName))
    89  				Eventually(session).Should(Exit(1))
    90  			})
    91  		})
    92  
    93  		When("app exists", func() {
    94  			BeforeEach(func() {
    95  				helpers.WithProcfileApp(func(appDir string) {
    96  					Eventually(helpers.CustomCF(helpers.CFEnv{WorkingDirectory: appDir}, "push", appName)).Should(Exit(0))
    97  				})
    98  			})
    99  
   100  			When("process type is not provided", func() {
   101  				It("defaults to web process", func() {
   102  					appOutputSession := helpers.CF("app", appName)
   103  					Eventually(appOutputSession).Should(Exit(0))
   104  					firstAppTable := helpers.ParseV3AppProcessTable(appOutputSession.Out.Contents())
   105  
   106  					session := helpers.CF("v3-restart-app-instance", appName, "0")
   107  					Eventually(session).Should(Say("Restarting instance 0 of process web of app %s in org %s / space %s as %s", appName, orgName, spaceName, userName))
   108  					Eventually(session).Should(Say("OK"))
   109  					Eventually(session).Should(Exit(0))
   110  
   111  					Eventually(func() string {
   112  						var restartedAppTable helpers.AppTable
   113  						Eventually(func() string {
   114  							appOutputSession := helpers.CF("app", appName)
   115  							Eventually(appOutputSession).Should(Exit(0))
   116  							restartedAppTable = helpers.ParseV3AppProcessTable(appOutputSession.Out.Contents())
   117  
   118  							if len(restartedAppTable.Processes) > 0 {
   119  								return fmt.Sprintf("%s, %s", restartedAppTable.Processes[0].Type, restartedAppTable.Processes[0].InstanceCount)
   120  							}
   121  
   122  							return ""
   123  						}).Should(Equal(`web, 1/1`))
   124  						Expect(restartedAppTable.Processes[0].Instances).ToNot(BeEmpty())
   125  						return restartedAppTable.Processes[0].Instances[0].Since
   126  					}).ShouldNot(Equal(firstAppTable.Processes[0].Instances[0].Since))
   127  				})
   128  			})
   129  
   130  			When("a process type is provided", func() {
   131  				When("the process type does not exist", func() {
   132  					It("fails with error", func() {
   133  						session := helpers.CF("v3-restart-app-instance", appName, "0", "--process", "unknown-process")
   134  						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))
   135  						Eventually(session.Err).Should(Say("Process unknown-process not found"))
   136  						Eventually(session).Should(Exit(1))
   137  					})
   138  				})
   139  
   140  				When("the process type exists", func() {
   141  					When("instance index exists", func() {
   142  						findConsoleProcess := func(appTable helpers.AppTable) (helpers.AppProcessTable, bool) {
   143  							for _, process := range appTable.Processes {
   144  								if process.Type == "console" {
   145  									return process, true
   146  								}
   147  							}
   148  							return helpers.AppProcessTable{}, false
   149  						}
   150  
   151  						It("defaults to requested process", func() {
   152  							By("scaling worker process to 1 instance")
   153  							session := helpers.CF("scale", appName, "--process", "console", "-i", "1")
   154  							Eventually(session).Should(Exit(0))
   155  
   156  							By("waiting for worker process to come up")
   157  							var firstAppTableConsoleProcess helpers.AppProcessTable
   158  							Eventually(func() string {
   159  								appOutputSession := helpers.CF("app", appName)
   160  								Eventually(appOutputSession).Should(Exit(0))
   161  								firstAppTable := helpers.ParseV3AppProcessTable(appOutputSession.Out.Contents())
   162  
   163  								var found bool
   164  								firstAppTableConsoleProcess, found = findConsoleProcess(firstAppTable)
   165  								Expect(found).To(BeTrue())
   166  								return fmt.Sprintf("%s, %s", firstAppTableConsoleProcess.Type, firstAppTableConsoleProcess.InstanceCount)
   167  							}).Should(MatchRegexp(`console, 1/1`))
   168  
   169  							By("restarting worker process instance")
   170  							session = helpers.CF("v3-restart-app-instance", appName, "0", "--process", "console")
   171  							Eventually(session).Should(Say("Restarting instance 0 of process console of app %s in org %s / space %s as %s", appName, orgName, spaceName, userName))
   172  							Eventually(session).Should(Say("OK"))
   173  							Eventually(session).Should(Exit(0))
   174  
   175  							By("waiting for restarted process instance to come up")
   176  							Eventually(func() string {
   177  								var restartedAppTableConsoleProcess helpers.AppProcessTable
   178  
   179  								Eventually(func() string {
   180  									appOutputSession := helpers.CF("app", appName)
   181  									Eventually(appOutputSession).Should(Exit(0))
   182  
   183  									restartedAppTable := helpers.ParseV3AppProcessTable(appOutputSession.Out.Contents())
   184  									var found bool
   185  									restartedAppTableConsoleProcess, found = findConsoleProcess(restartedAppTable)
   186  									Expect(found).To(BeTrue())
   187  
   188  									return fmt.Sprintf("%s, %s", restartedAppTableConsoleProcess.Type, restartedAppTableConsoleProcess.InstanceCount)
   189  								}).Should(MatchRegexp(`console, 1/1`))
   190  
   191  								return restartedAppTableConsoleProcess.Instances[0].Since
   192  							}).ShouldNot(Equal(firstAppTableConsoleProcess.Instances[0].Since))
   193  						})
   194  					})
   195  
   196  					When("instance index does not exist", func() {
   197  						It("fails with error", func() {
   198  							session := helpers.CF("v3-restart-app-instance", appName, "42", "--process", constant.ProcessTypeWeb)
   199  							Eventually(session).Should(Say("Restarting instance 42 of process web of app %s in org %s / space %s as %s", appName, orgName, spaceName, userName))
   200  							Eventually(session.Err).Should(Say("Instance 42 of process web not found"))
   201  							Eventually(session).Should(Exit(1))
   202  						})
   203  					})
   204  				})
   205  			})
   206  		})
   207  	})
   208  })