github.com/liamawhite/cli-with-i18n@v6.32.1-0.20171122084555-dede0a5c3448+incompatible/integration/experimental/v3_restart_app_instance_command_test.go (about)

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