github.com/mook-as/cf-cli@v7.0.0-beta.28.0.20200120190804-b91c115fae48+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/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 When("no API endpoint is set", func() { 68 BeforeEach(func() { 69 helpers.UnsetAPI() 70 }) 71 72 It("fails with no API endpoint set message", func() { 73 session := helpers.CF("v3-restart-app-instance", appName, "1") 74 Eventually(session).Should(Say("FAILED")) 75 Eventually(session.Err).Should(Say("No API endpoint set. Use 'cf login' or 'cf api' to target an endpoint.")) 76 Eventually(session).Should(Exit(1)) 77 }) 78 }) 79 80 When("not logged in", func() { 81 BeforeEach(func() { 82 helpers.LogoutCF() 83 }) 84 85 It("fails with not logged in message", func() { 86 session := helpers.CF("v3-restart-app-instance", appName, "1") 87 Eventually(session).Should(Say("FAILED")) 88 Eventually(session.Err).Should(Say("Not logged in. Use 'cf login' or 'cf login --sso' to log in.")) 89 Eventually(session).Should(Exit(1)) 90 }) 91 }) 92 93 When("there is no org set", func() { 94 BeforeEach(func() { 95 helpers.LogoutCF() 96 helpers.LoginCF() 97 }) 98 99 It("fails with no targeted org error message", func() { 100 session := helpers.CF("v3-restart-app-instance", appName, "1") 101 Eventually(session).Should(Say("FAILED")) 102 Eventually(session.Err).Should(Say("No org targeted, use 'cf target -o ORG' to target an org.")) 103 Eventually(session).Should(Exit(1)) 104 }) 105 }) 106 107 When("there is no space set", func() { 108 BeforeEach(func() { 109 helpers.LogoutCF() 110 helpers.LoginCF() 111 helpers.TargetOrg(ReadOnlyOrg) 112 }) 113 114 It("fails with no targeted space error message", func() { 115 session := helpers.CF("v3-restart-app-instance", appName, "1") 116 Eventually(session).Should(Say("FAILED")) 117 Eventually(session.Err).Should(Say("No space targeted, use 'cf target -s SPACE' to target a space.")) 118 Eventually(session).Should(Exit(1)) 119 }) 120 }) 121 }) 122 123 When("the environment is setup correctly", func() { 124 var userName string 125 126 BeforeEach(func() { 127 helpers.SetupCF(orgName, spaceName) 128 userName, _ = helpers.GetCredentials() 129 }) 130 131 AfterEach(func() { 132 helpers.QuickDeleteOrg(orgName) 133 }) 134 135 When("app does not exist", func() { 136 It("fails with error", func() { 137 session := helpers.CF("v3-restart-app-instance", appName, "0", "--process", "some-process") 138 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)) 139 Eventually(session.Err).Should(Say("App '%s' not found", appName)) 140 Eventually(session).Should(Exit(1)) 141 }) 142 }) 143 144 When("app exists", func() { 145 BeforeEach(func() { 146 helpers.WithProcfileApp(func(appDir string) { 147 Eventually(helpers.CustomCF(helpers.CFEnv{WorkingDirectory: appDir}, "v3-push", appName)).Should(Exit(0)) 148 }) 149 }) 150 151 When("process type is not provided", func() { 152 It("defaults to web process", func() { 153 appOutputSession := helpers.CF("app", appName) 154 Eventually(appOutputSession).Should(Exit(0)) 155 firstAppTable := helpers.ParseV3AppProcessTable(appOutputSession.Out.Contents()) 156 157 session := helpers.CF("v3-restart-app-instance", appName, "0") 158 Eventually(session).Should(Say("Restarting instance 0 of process web of app %s in org %s / space %s as %s", appName, orgName, spaceName, userName)) 159 Eventually(session).Should(Say("OK")) 160 Eventually(session).Should(Exit(0)) 161 162 Eventually(func() string { 163 var restartedAppTable helpers.AppTable 164 Eventually(func() string { 165 appOutputSession := helpers.CF("app", appName) 166 Eventually(appOutputSession).Should(Exit(0)) 167 restartedAppTable = helpers.ParseV3AppProcessTable(appOutputSession.Out.Contents()) 168 169 if len(restartedAppTable.Processes) > 0 { 170 return fmt.Sprintf("%s, %s", restartedAppTable.Processes[0].Type, restartedAppTable.Processes[0].InstanceCount) 171 } 172 173 return "" 174 }).Should(Equal(`web, 1/1`)) 175 Expect(restartedAppTable.Processes[0].Instances).ToNot(BeEmpty()) 176 return restartedAppTable.Processes[0].Instances[0].Since 177 }).ShouldNot(Equal(firstAppTable.Processes[0].Instances[0].Since)) 178 }) 179 }) 180 181 When("a process type is provided", func() { 182 When("the process type does not exist", func() { 183 It("fails with error", func() { 184 session := helpers.CF("v3-restart-app-instance", appName, "0", "--process", "unknown-process") 185 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)) 186 Eventually(session.Err).Should(Say("Process unknown-process not found")) 187 Eventually(session).Should(Exit(1)) 188 }) 189 }) 190 191 When("the process type exists", func() { 192 When("instance index exists", func() { 193 findConsoleProcess := func(appTable helpers.AppTable) (helpers.AppProcessTable, bool) { 194 for _, process := range appTable.Processes { 195 if process.Type == "console" { 196 return process, true 197 } 198 } 199 return helpers.AppProcessTable{}, false 200 } 201 202 It("defaults to requested process", func() { 203 By("scaling worker process to 1 instance") 204 session := helpers.CF("v3-scale", appName, "--process", "console", "-i", "1") 205 Eventually(session).Should(Exit(0)) 206 207 By("waiting for worker process to come up") 208 var firstAppTableConsoleProcess helpers.AppProcessTable 209 Eventually(func() string { 210 appOutputSession := helpers.CF("app", appName) 211 Eventually(appOutputSession).Should(Exit(0)) 212 firstAppTable := helpers.ParseV3AppProcessTable(appOutputSession.Out.Contents()) 213 214 var found bool 215 firstAppTableConsoleProcess, found = findConsoleProcess(firstAppTable) 216 Expect(found).To(BeTrue()) 217 return fmt.Sprintf("%s, %s", firstAppTableConsoleProcess.Type, firstAppTableConsoleProcess.InstanceCount) 218 }).Should(MatchRegexp(`console, 1/1`)) 219 220 By("restarting worker process instance") 221 session = helpers.CF("v3-restart-app-instance", appName, "0", "--process", "console") 222 Eventually(session).Should(Say("Restarting instance 0 of process console of app %s in org %s / space %s as %s", appName, orgName, spaceName, userName)) 223 Eventually(session).Should(Say("OK")) 224 Eventually(session).Should(Exit(0)) 225 226 By("waiting for restarted process instance to come up") 227 Eventually(func() string { 228 var restartedAppTableConsoleProcess helpers.AppProcessTable 229 230 Eventually(func() string { 231 appOutputSession := helpers.CF("app", appName) 232 Eventually(appOutputSession).Should(Exit(0)) 233 234 restartedAppTable := helpers.ParseV3AppProcessTable(appOutputSession.Out.Contents()) 235 var found bool 236 restartedAppTableConsoleProcess, found = findConsoleProcess(restartedAppTable) 237 Expect(found).To(BeTrue()) 238 239 return fmt.Sprintf("%s, %s", restartedAppTableConsoleProcess.Type, restartedAppTableConsoleProcess.InstanceCount) 240 }).Should(MatchRegexp(`console, 1/1`)) 241 242 return restartedAppTableConsoleProcess.Instances[0].Since 243 }).ShouldNot(Equal(firstAppTableConsoleProcess.Instances[0].Since)) 244 }) 245 }) 246 247 When("instance index does not exist", func() { 248 It("fails with error", func() { 249 session := helpers.CF("v3-restart-app-instance", appName, "42", "--process", constant.ProcessTypeWeb) 250 Eventually(session).Should(Say("Restarting instance 42 of process web of app %s in org %s / space %s as %s", appName, orgName, spaceName, userName)) 251 Eventually(session.Err).Should(Say("Instance 42 of process web not found")) 252 Eventually(session).Should(Exit(1)) 253 }) 254 }) 255 }) 256 }) 257 }) 258 }) 259 })