github.com/pf-qiu/concourse/v6@v6.7.3-0.20201207032516-1f455d73275f/fly/integration/pause_pipeline_test.go (about) 1 package integration_test 2 3 import ( 4 "net/http" 5 "os/exec" 6 7 . "github.com/onsi/ginkgo" 8 . "github.com/onsi/gomega" 9 10 "github.com/pf-qiu/concourse/v6/atc" 11 "github.com/onsi/gomega/gbytes" 12 "github.com/onsi/gomega/gexec" 13 "github.com/onsi/gomega/ghttp" 14 "github.com/tedsuo/rata" 15 ) 16 17 var _ = Describe("Fly CLI", func() { 18 Describe("pause-pipeline", func() { 19 Context("when the pipeline name is specified", func() { 20 var ( 21 path string 22 queryParams string 23 err error 24 ) 25 BeforeEach(func() { 26 path, err = atc.Routes.CreatePathForRoute(atc.PausePipeline, rata.Params{"pipeline_name": "awesome-pipeline", "team_name": teamName}) 27 Expect(err).NotTo(HaveOccurred()) 28 29 queryParams = "instance_vars=%7B%22branch%22%3A%22master%22%7D" 30 }) 31 32 Context("when the pipeline exists", func() { 33 BeforeEach(func() { 34 atcServer.AppendHandlers( 35 ghttp.CombineHandlers( 36 ghttp.VerifyRequest("PUT", path, queryParams), 37 ghttp.RespondWith(http.StatusOK, nil), 38 ), 39 ) 40 }) 41 42 It("pauses the pipeline", func() { 43 Expect(func() { 44 flyCmd := exec.Command(flyPath, "-t", targetName, "pause-pipeline", "-p", "awesome-pipeline/branch:master") 45 46 sess, err := gexec.Start(flyCmd, GinkgoWriter, GinkgoWriter) 47 Expect(err).NotTo(HaveOccurred()) 48 49 Eventually(sess).Should(gbytes.Say(`paused 'awesome-pipeline/branch:master'`)) 50 51 <-sess.Exited 52 Expect(sess.ExitCode()).To(Equal(0)) 53 }).To(Change(func() int { 54 return len(atcServer.ReceivedRequests()) 55 }).By(2)) 56 }) 57 }) 58 59 Context("when the pipeline doesn't exist", func() { 60 BeforeEach(func() { 61 atcServer.AppendHandlers( 62 ghttp.CombineHandlers( 63 ghttp.VerifyRequest("PUT", path), 64 ghttp.RespondWith(http.StatusNotFound, nil), 65 ), 66 ) 67 }) 68 69 It("prints helpful message", func() { 70 Expect(func() { 71 flyCmd := exec.Command(flyPath, "-t", targetName, "pause-pipeline", "-p", "awesome-pipeline") 72 73 sess, err := gexec.Start(flyCmd, GinkgoWriter, GinkgoWriter) 74 Expect(err).NotTo(HaveOccurred()) 75 76 Eventually(sess.Err).Should(gbytes.Say(`pipeline 'awesome-pipeline' not found`)) 77 78 <-sess.Exited 79 Expect(sess.ExitCode()).To(Equal(1)) 80 }).To(Change(func() int { 81 return len(atcServer.ReceivedRequests()) 82 }).By(2)) 83 }) 84 }) 85 }) 86 87 Context("when both the team and pipeline name are specified", func() { 88 teamName := "other-team" 89 var ( 90 path string 91 err error 92 ) 93 BeforeEach(func() { 94 path, err = atc.Routes.CreatePathForRoute(atc.PausePipeline, rata.Params{"pipeline_name": "awesome-pipeline", "team_name": teamName}) 95 Expect(err).NotTo(HaveOccurred()) 96 }) 97 98 Context("when the pipeline exists", func() { 99 BeforeEach(func() { 100 atcServer.AppendHandlers( 101 ghttp.CombineHandlers( 102 ghttp.VerifyRequest("GET", "/api/v1/teams/"+teamName), 103 ghttp.RespondWithJSONEncoded(http.StatusOK, atc.Team{ 104 Name: teamName, 105 }), 106 ), 107 ghttp.CombineHandlers( 108 ghttp.VerifyRequest("PUT", path), 109 ghttp.RespondWith(http.StatusOK, nil), 110 ), 111 ) 112 }) 113 114 It("pauses the pipeline", func() { 115 Expect(func() { 116 flyCmd := exec.Command(flyPath, "-t", targetName, "pause-pipeline", "--team", teamName, "-p", "awesome-pipeline") 117 118 sess, err := gexec.Start(flyCmd, GinkgoWriter, GinkgoWriter) 119 Expect(err).NotTo(HaveOccurred()) 120 121 Eventually(sess).Should(gbytes.Say(`paused 'awesome-pipeline'`)) 122 123 <-sess.Exited 124 Expect(sess.ExitCode()).To(Equal(0)) 125 }).To(Change(func() int { 126 return len(atcServer.ReceivedRequests()) 127 }).By(3)) 128 }) 129 }) 130 131 Context("when the pipeline doesn't exist", func() { 132 BeforeEach(func() { 133 atcServer.AppendHandlers( 134 ghttp.CombineHandlers( 135 ghttp.VerifyRequest("GET", "/api/v1/teams/"+teamName), 136 ghttp.RespondWithJSONEncoded(http.StatusOK, atc.Team{ 137 Name: teamName, 138 }), 139 ), 140 ghttp.CombineHandlers( 141 ghttp.VerifyRequest("PUT", path), 142 ghttp.RespondWith(http.StatusNotFound, nil), 143 ), 144 ) 145 }) 146 147 It("prints helpful message", func() { 148 Expect(func() { 149 flyCmd := exec.Command(flyPath, "-t", targetName, "pause-pipeline", "--team", teamName, "-p", "awesome-pipeline") 150 151 sess, err := gexec.Start(flyCmd, GinkgoWriter, GinkgoWriter) 152 Expect(err).NotTo(HaveOccurred()) 153 154 Eventually(sess.Err).Should(gbytes.Say(`pipeline 'awesome-pipeline' not found`)) 155 156 <-sess.Exited 157 Expect(sess.ExitCode()).To(Equal(1)) 158 }).To(Change(func() int { 159 return len(atcServer.ReceivedRequests()) 160 }).By(3)) 161 }) 162 }) 163 }) 164 165 Context("when the pipline name or --all is not specified", func() { 166 It("errors", func() { 167 Expect(func() { 168 flyCmd := exec.Command(flyPath, "-t", targetName, "pause-pipeline") 169 170 sess, err := gexec.Start(flyCmd, GinkgoWriter, GinkgoWriter) 171 Expect(err).NotTo(HaveOccurred()) 172 173 Eventually(sess.Err).Should(gbytes.Say(`one of the flags '-p, --pipeline' or '-a, --all' is required`)) 174 175 <-sess.Exited 176 Expect(sess.ExitCode()).To(Equal(1)) 177 }).To(Change(func() int { 178 return len(atcServer.ReceivedRequests()) 179 }).By(0)) 180 }) 181 }) 182 183 Context("when both the pipline name and --all are specified", func() { 184 It("errors", func() { 185 Expect(func() { 186 flyCmd := exec.Command(flyPath, "-t", targetName, "pause-pipeline", "-p", "awesome-pipeline", "--all") 187 188 sess, err := gexec.Start(flyCmd, GinkgoWriter, GinkgoWriter) 189 Expect(err).NotTo(HaveOccurred()) 190 191 Eventually(sess.Err).Should(gbytes.Say(`only one of the flags '-p, --pipeline' or '-a, --all' is allowed`)) 192 193 <-sess.Exited 194 Expect(sess.ExitCode()).To(Equal(1)) 195 }).To(Change(func() int { 196 return len(atcServer.ReceivedRequests()) 197 }).By(0)) 198 }) 199 }) 200 201 Context("when the pipeline flag is invalid", func() { 202 It("fails and print invalid flag error", func() { 203 flyCmd := exec.Command(flyPath, "-t", targetName, "pause-pipeline", "-p", "forbidden/pipelinename") 204 205 sess, err := gexec.Start(flyCmd, GinkgoWriter, GinkgoWriter) 206 Expect(err).NotTo(HaveOccurred()) 207 208 <-sess.Exited 209 Expect(sess.ExitCode()).To(Equal(1)) 210 211 Expect(sess.Err).To(gbytes.Say("error: invalid argument for flag `" + osFlag("p", "pipeline"))) 212 }) 213 }) 214 215 Context("when the --all flag is passed", func() { 216 var ( 217 somePath string 218 someOtherPath string 219 err error 220 ) 221 222 BeforeEach(func() { 223 somePath, err = atc.Routes.CreatePathForRoute(atc.PausePipeline, rata.Params{"pipeline_name": "awesome-pipeline", "team_name": teamName}) 224 Expect(err).NotTo(HaveOccurred()) 225 226 someOtherPath, err = atc.Routes.CreatePathForRoute(atc.PausePipeline, rata.Params{"pipeline_name": "more-awesome-pipeline", "team_name": teamName}) 227 Expect(err).NotTo(HaveOccurred()) 228 229 atcServer.AppendHandlers( 230 ghttp.CombineHandlers( 231 ghttp.VerifyRequest("GET", "/api/v1/teams/main/pipelines"), 232 ghttp.RespondWithJSONEncoded(200, []atc.Pipeline{ 233 {Name: "awesome-pipeline", Paused: false, Public: false}, 234 {Name: "more-awesome-pipeline", Paused: true, Public: false}, 235 }), 236 ), 237 ghttp.CombineHandlers( 238 ghttp.VerifyRequest("PUT", somePath), 239 ghttp.RespondWith(http.StatusOK, nil), 240 ), 241 ghttp.CombineHandlers( 242 ghttp.VerifyRequest("PUT", someOtherPath), 243 ghttp.RespondWith(http.StatusOK, nil), 244 ), 245 ) 246 }) 247 248 It("pauses every pipeline", func() { 249 Expect(func() { 250 flyCmd := exec.Command(flyPath, "-t", targetName, "pause-pipeline", "--all") 251 252 sess, err := gexec.Start(flyCmd, GinkgoWriter, GinkgoWriter) 253 Expect(err).NotTo(HaveOccurred()) 254 255 Eventually(sess).Should(gbytes.Say(`paused 'awesome-pipeline'`)) 256 Eventually(sess).Should(gbytes.Say(`paused 'more-awesome-pipeline'`)) 257 258 <-sess.Exited 259 Expect(sess.ExitCode()).To(Equal(0)) 260 }).To(Change(func() int { 261 return len(atcServer.ReceivedRequests()) 262 }).By(4)) 263 }) 264 }) 265 }) 266 })