github.com/chenbh/concourse/v6@v6.4.2/fly/integration/pause_job_test.go (about) 1 package integration_test 2 3 import ( 4 "fmt" 5 "net/http" 6 "os/exec" 7 8 "github.com/chenbh/concourse/v6/atc" 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("Fly CLI", func() { 17 Describe("Pause Job", func() { 18 var ( 19 flyCmd *exec.Cmd 20 pipelineName string 21 jobName string 22 fullJobName string 23 apiPath string 24 ) 25 26 BeforeEach(func() { 27 pipelineName = "pipeline" 28 jobName = "job-name-potato" 29 fullJobName = fmt.Sprintf("%s/%s", pipelineName, jobName) 30 apiPath = fmt.Sprintf("/api/v1/teams/main/pipelines/%s/jobs/%s/pause", pipelineName, jobName) 31 }) 32 33 Context("when user is on the same team as the given pipeline/job's team", func() { 34 BeforeEach(func() { 35 atcServer.AppendHandlers( 36 ghttp.CombineHandlers( 37 ghttp.VerifyRequest("PUT", apiPath), 38 ghttp.RespondWith(http.StatusOK, nil), 39 ), 40 ) 41 }) 42 43 It("successfully pauses the job", func() { 44 flyCmd = exec.Command(flyPath, "-t", targetName, "pause-job", "-j", fullJobName) 45 sess, err := gexec.Start(flyCmd, GinkgoWriter, GinkgoWriter) 46 Expect(err).NotTo(HaveOccurred()) 47 <-sess.Exited 48 Expect(sess.ExitCode()).To(Equal(0)) 49 Eventually(sess.Out.Contents).Should(ContainSubstring(fmt.Sprintf("paused '%s'\n", jobName))) 50 }) 51 }) 52 53 Context("user is NOT on the same team as the given pipeline/job's team", func() { 54 BeforeEach(func() { 55 atcServer.AppendHandlers( 56 ghttp.CombineHandlers( 57 ghttp.VerifyRequest("PUT", apiPath), 58 ghttp.RespondWith(http.StatusForbidden, nil), 59 ), 60 ) 61 }) 62 63 It("fails to pause the job", func() { 64 flyCmd = exec.Command(flyPath, "-t", targetName, "pause-job", "-j", fullJobName) 65 sess, err := gexec.Start(flyCmd, GinkgoWriter, GinkgoWriter) 66 Expect(err).NotTo(HaveOccurred()) 67 <-sess.Exited 68 Expect(sess.ExitCode()).To(Equal(1)) 69 Eventually(sess.Err.Contents).Should(ContainSubstring("error")) 70 }) 71 }) 72 73 Context("user is NOT currently targeted to the team the given pipeline/job belongs to", func() { 74 BeforeEach(func() { 75 apiPath = fmt.Sprintf("/api/v1/teams/other-team/pipelines/%s/jobs/%s/pause", pipelineName, jobName) 76 77 atcServer.AppendHandlers( 78 ghttp.CombineHandlers( 79 ghttp.VerifyRequest("GET", "/api/v1/teams/other-team"), 80 ghttp.RespondWithJSONEncoded(http.StatusOK, atc.Team{Name: "other-team"}), 81 ), 82 ghttp.CombineHandlers( 83 ghttp.VerifyRequest("PUT", apiPath), 84 ghttp.RespondWith(http.StatusOK, nil), 85 ), 86 ) 87 }) 88 89 It("successfully pauses the job", func() { 90 flyCmd = exec.Command(flyPath, "-t", targetName, "pause-job", "-j", fullJobName, "--team", "other-team") 91 sess, err := gexec.Start(flyCmd, GinkgoWriter, GinkgoWriter) 92 Expect(err).NotTo(HaveOccurred()) 93 <-sess.Exited 94 Expect(sess.ExitCode()).To(Equal(0)) 95 Eventually(sess).Should(gbytes.Say(fmt.Sprintf("paused '%s'\n", jobName))) 96 }) 97 }) 98 99 Context("the pipeline/job does not exist", func() { 100 BeforeEach(func() { 101 atcServer.AppendHandlers( 102 ghttp.CombineHandlers( 103 ghttp.VerifyRequest("PUT", "/api/v1/teams/main/pipelines/doesnotexist-pipeline/jobs/doesnotexist-job/pause"), 104 ghttp.RespondWith(http.StatusNotFound, nil), 105 ), 106 ) 107 }) 108 109 It("returns an error", func() { 110 flyCmd = exec.Command(flyPath, "-t", targetName, "pause-job", "-j", "doesnotexist-pipeline/doesnotexist-job") 111 112 sess, err := gexec.Start(flyCmd, GinkgoWriter, GinkgoWriter) 113 Expect(err).NotTo(HaveOccurred()) 114 <-sess.Exited 115 Expect(sess.ExitCode()).To(Equal(1)) 116 Eventually(sess.Err.Contents).Should(ContainSubstring("doesnotexist-pipeline/doesnotexist-job not found on team main")) 117 }) 118 }) 119 120 Context("when pause-job fails", func() { 121 BeforeEach(func() { 122 atcServer.AppendHandlers( 123 ghttp.CombineHandlers( 124 ghttp.VerifyRequest("PUT", apiPath), 125 ghttp.RespondWith(http.StatusInternalServerError, nil), 126 ), 127 ) 128 }) 129 130 It("exits 1 and outputs an error", func() { 131 flyCmd = exec.Command(flyPath, "-t", targetName, "pause-job", "-j", fullJobName) 132 sess, err := gexec.Start(flyCmd, GinkgoWriter, GinkgoWriter) 133 Expect(err).NotTo(HaveOccurred()) 134 Eventually(sess.Err).Should(gbytes.Say(`error`)) 135 <-sess.Exited 136 Expect(sess.ExitCode()).To(Equal(1)) 137 }) 138 }) 139 }) 140 })