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