github.com/cloudfoundry-attic/cli-with-i18n@v6.32.1-0.20171002233121-7401370d3b85+incompatible/integration/isolated/terminate_task_command_test.go (about) 1 package isolated 2 3 import ( 4 "fmt" 5 6 "code.cloudfoundry.org/cli/integration/helpers" 7 . "github.com/onsi/ginkgo" 8 . "github.com/onsi/gomega" 9 . "github.com/onsi/gomega/gbytes" 10 . "github.com/onsi/gomega/gexec" 11 . "github.com/onsi/gomega/ghttp" 12 ) 13 14 var _ = Describe("terminate-task command", func() { 15 Context("when the environment is not setup correctly", func() { 16 Context("when no API endpoint is set", func() { 17 BeforeEach(func() { 18 helpers.UnsetAPI() 19 }) 20 21 It("fails with no API endpoint set message", func() { 22 session := helpers.CF("terminate-task", "app-name", "3") 23 Eventually(session.Out).Should(Say("FAILED")) 24 Eventually(session.Err).Should(Say("No API endpoint set. Use 'cf login' or 'cf api' to target an endpoint.")) 25 Eventually(session).Should(Exit(1)) 26 }) 27 }) 28 29 Context("when the v3 api does not exist", func() { 30 var server *Server 31 32 BeforeEach(func() { 33 server = helpers.StartAndTargetServerWithoutV3API() 34 }) 35 36 AfterEach(func() { 37 server.Close() 38 }) 39 40 It("fails with error message that the minimum version is not met", func() { 41 session := helpers.CF("terminate-task", "app-name", "3") 42 Eventually(session).Should(Say("FAILED")) 43 Eventually(session.Err).Should(Say("This command requires CF API version 3\\.0\\.0 or higher\\.")) 44 Eventually(session).Should(Exit(1)) 45 }) 46 }) 47 48 Context("when not logged in", func() { 49 BeforeEach(func() { 50 helpers.LogoutCF() 51 }) 52 53 It("fails with not logged in message", func() { 54 session := helpers.CF("terminate-task", "app-name", "3") 55 Eventually(session.Out).Should(Say("FAILED")) 56 Eventually(session.Err).Should(Say("Not logged in. Use 'cf login' to log in.")) 57 Eventually(session).Should(Exit(1)) 58 }) 59 }) 60 61 Context("when there no org set", func() { 62 BeforeEach(func() { 63 helpers.LogoutCF() 64 helpers.LoginCF() 65 }) 66 67 It("fails with no targeted org error message", func() { 68 session := helpers.CF("terminate-task", "app-name", "3") 69 Eventually(session.Out).Should(Say("FAILED")) 70 Eventually(session.Err).Should(Say("No org targeted, use 'cf target -o ORG' to target an org.")) 71 Eventually(session).Should(Exit(1)) 72 }) 73 }) 74 75 Context("when there no space set", func() { 76 BeforeEach(func() { 77 helpers.LogoutCF() 78 helpers.LoginCF() 79 helpers.TargetOrg(ReadOnlyOrg) 80 }) 81 82 It("fails with no space targeted error message", func() { 83 session := helpers.CF("terminate-task", "app-name", "3") 84 Eventually(session.Out).Should(Say("FAILED")) 85 Eventually(session.Err).Should(Say("No space targeted, use 'cf target -s SPACE' to target a space")) 86 Eventually(session).Should(Exit(1)) 87 }) 88 }) 89 }) 90 91 Context("when the environment is setup correctly", func() { 92 var ( 93 orgName string 94 spaceName string 95 appName string 96 ) 97 98 BeforeEach(func() { 99 orgName = helpers.NewOrgName() 100 spaceName = helpers.NewSpaceName() 101 appName = helpers.PrefixedRandomName("APP") 102 103 setupCF(orgName, spaceName) 104 }) 105 106 AfterEach(func() { 107 helpers.QuickDeleteOrg(orgName) 108 }) 109 110 Context("when the application does not exist", func() { 111 It("fails to terminate task and outputs an error message", func() { 112 session := helpers.CF("terminate-task", appName, "1") 113 Eventually(session.Err).Should(Say(fmt.Sprintf("App %s not found", appName))) 114 Eventually(session.Out).Should(Say("FAILED")) 115 Eventually(session).Should(Exit(1)) 116 }) 117 }) 118 119 Context("when the application exists", func() { 120 BeforeEach(func() { 121 helpers.WithHelloWorldApp(func(appDir string) { 122 Eventually(helpers.CF("push", appName, "-p", appDir, "-b", "staticfile_buildpack")).Should(Exit(0)) 123 }) 124 }) 125 126 Context("when the wrong data type is provided to terminate-task", func() { 127 It("outputs an error message to the user, provides help text, and exits 1", func() { 128 session := helpers.CF("terminate-task", appName, "not-an-integer") 129 Eventually(session.Err).Should(Say("Incorrect usage: Value for TASK_ID must be integer")) 130 Eventually(session.Out).Should(Say("FAILED")) 131 Eventually(session.Out).Should(Say("terminate-task APP_NAME TASK_ID")) // help 132 Eventually(session).Should(Exit(1)) 133 }) 134 }) 135 136 Context("when the task is in the RUNNING state", func() { 137 BeforeEach(func() { 138 helpers.WithHelloWorldApp(func(appDir string) { 139 Eventually(helpers.CF("run-task", appName, "sleep 1000")).Should(Exit(0)) 140 }) 141 }) 142 143 It("terminates the task", func() { 144 tasksSession := helpers.CF("tasks", appName) 145 Eventually(tasksSession).Should(Exit(0)) 146 Expect(tasksSession.Out).To(Say("1\\s+[a-zA-Z-0-9]+\\s+RUNNING")) 147 148 session := helpers.CF("terminate-task", appName, "1") 149 userName, _ := helpers.GetCredentials() 150 Eventually(session.Out).Should(Say( 151 fmt.Sprintf("Terminating task 1 of app %s in org %s / space %s as %s..", appName, orgName, spaceName, userName))) 152 Eventually(session.Out).Should(Say("OK")) 153 Eventually(session).Should(Exit(0)) 154 }) 155 }) 156 157 Context("when the task is in the SUCCEEDED state", func() { 158 BeforeEach(func() { 159 helpers.WithHelloWorldApp(func(appDir string) { 160 Eventually(helpers.CF("run-task", appName, "echo test")).Should(Exit(0)) 161 }) 162 }) 163 164 It("fails to terminate the task and prints an error", func() { 165 Eventually(func() *Buffer { 166 taskSession := helpers.CF("tasks", appName) 167 Eventually(taskSession).Should(Exit(0)) 168 return taskSession.Out 169 }).Should(Say("1\\s+[a-zA-Z-0-9]+\\s+SUCCEEDED")) 170 171 session := helpers.CF("terminate-task", appName, "1") 172 Eventually(session.Err).Should(Say("Task state is SUCCEEDED and therefore cannot be canceled")) 173 Eventually(session.Out).Should(Say("FAILED")) 174 Eventually(session).Should(Exit(1)) 175 }) 176 }) 177 178 Context("when the task is in the FAILED state", func() { 179 BeforeEach(func() { 180 helpers.WithHelloWorldApp(func(appDir string) { 181 Eventually(helpers.CF("run-task", appName, "false")).Should(Exit(0)) 182 }) 183 }) 184 185 It("fails to terminate the task and prints an error", func() { 186 Eventually(func() *Buffer { 187 taskSession := helpers.CF("tasks", appName) 188 Eventually(taskSession).Should(Exit(0)) 189 return taskSession.Out 190 }).Should(Say("1\\s+[a-zA-Z-0-9]+\\s+FAILED")) 191 192 session := helpers.CF("terminate-task", appName, "1") 193 Eventually(session.Err).Should(Say("Task state is FAILED and therefore cannot be canceled")) 194 Eventually(session.Out).Should(Say("FAILED")) 195 Eventually(session).Should(Exit(1)) 196 }) 197 }) 198 199 Context("when the task ID does not exist", func() { 200 It("fails to terminate the task and prints an error", func() { 201 session := helpers.CF("terminate-task", appName, "1") 202 Eventually(session.Err).Should(Say("Task sequence ID 1 not found.")) 203 Eventually(session.Out).Should(Say("FAILED")) 204 Eventually(session).Should(Exit(1)) 205 }) 206 }) 207 }) 208 }) 209 })