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