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

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