github.com/chenbh/concourse/v6@v6.4.2/fly/integration/clear_task_cache_test.go (about) 1 package integration_test 2 3 import ( 4 "fmt" 5 "io" 6 "net/http" 7 "os/exec" 8 9 "github.com/chenbh/concourse/v6/atc" 10 . "github.com/onsi/ginkgo" 11 . "github.com/onsi/gomega" 12 "github.com/onsi/gomega/gbytes" 13 "github.com/onsi/gomega/gexec" 14 "github.com/onsi/gomega/ghttp" 15 ) 16 17 var _ = Describe("Fly CLI", func() { 18 Describe("clear-task-cache", func() { 19 var ( 20 stdin io.Writer 21 args []string 22 sess *gexec.Session 23 ) 24 25 BeforeEach(func() { 26 stdin = nil 27 args = []string{} 28 }) 29 30 JustBeforeEach(func() { 31 var err error 32 33 flyCmd := exec.Command(flyPath, append([]string{"-t", targetName, "clear-task-cache"}, args...)...) 34 stdin, err = flyCmd.StdinPipe() 35 Expect(err).NotTo(HaveOccurred()) 36 37 sess, err = gexec.Start(flyCmd, GinkgoWriter, GinkgoWriter) 38 Expect(err).NotTo(HaveOccurred()) 39 }) 40 41 Context("when a job is not specified", func() { 42 It("asks the user to specify a job", func() { 43 flyCmd := exec.Command(flyPath, "-t", targetName, "clear-task-cache", "-s", "some-task-step") 44 45 sess, err := gexec.Start(flyCmd, GinkgoWriter, GinkgoWriter) 46 Expect(err).NotTo(HaveOccurred()) 47 48 Eventually(sess).Should(gexec.Exit(1)) 49 50 Expect(sess.Err).To(gbytes.Say("error: the required flag `" + osFlag("j", "job") + "' was not specified")) 51 }) 52 }) 53 54 Context("when a step is not specified", func() { 55 It("asks the user to specify a step", func() { 56 flyCmd := exec.Command(flyPath, "-t", targetName, "clear-task-cache", "-j", "some-pipeline/some-job") 57 58 sess, err := gexec.Start(flyCmd, GinkgoWriter, GinkgoWriter) 59 Expect(err).NotTo(HaveOccurred()) 60 61 Eventually(sess).Should(gexec.Exit(1)) 62 63 Expect(sess.Err).To(gbytes.Say("error: the required flag `" + osFlag("s", "step") + "' was not specified")) 64 }) 65 }) 66 67 Context("when specifying a job without a pipeline name", func() { 68 It("asks the user to specify a pipeline name", func() { 69 flyCmd := exec.Command(flyPath, "-t", targetName, "clear-task-cache", "-s", "some-task-step", "-j", "myjob") 70 71 sess, err := gexec.Start(flyCmd, GinkgoWriter, GinkgoWriter) 72 Expect(err).NotTo(HaveOccurred()) 73 74 <-sess.Exited 75 Expect(sess.ExitCode()).To(Equal(1)) 76 77 Expect(sess.Err).To(gbytes.Say("error: invalid argument for flag `" + osFlag("j", "job"))) 78 Expect(sess.Err).To(gbytes.Say(`argument format should be <pipeline>/<job>`)) 79 }) 80 }) 81 82 Context("when a job and step name are specified", func() { 83 BeforeEach(func() { 84 args = append(args, "-j", "some-pipeline/some-job", "-s", "some-step-name") 85 }) 86 87 yes := func() { 88 Eventually(sess).Should(gbytes.Say(`are you sure\? \[yN\]: `)) 89 fmt.Fprintf(stdin, "y\n") 90 } 91 92 no := func() { 93 Eventually(sess).Should(gbytes.Say(`are you sure\? \[yN\]: `)) 94 fmt.Fprintf(stdin, "n\n") 95 } 96 97 It("warns that it's about to do bad things", func() { 98 Eventually(sess).Should(gbytes.Say("!!! this will remove the task cache\\(s\\) for `some-pipeline/some-job`, task step `some-step-name`")) 99 }) 100 101 It("bails out if the user says no", func() { 102 no() 103 Eventually(sess).Should(gbytes.Say(`bailing out`)) 104 Eventually(sess).Should(gexec.Exit(0)) 105 }) 106 107 Context("when the task step exists", func() { 108 BeforeEach(func() { 109 atcServer.AppendHandlers( 110 ghttp.CombineHandlers( 111 ghttp.VerifyRequest("DELETE", "/api/v1/teams/main/pipelines/some-pipeline/jobs/some-job/tasks/some-step-name/cache"), 112 ghttp.RespondWithJSONEncoded(http.StatusOK, atc.ClearTaskCacheResponse{CachesRemoved: 1}), 113 ), 114 ) 115 }) 116 117 It("succeeds if the user says yes", func() { 118 yes() 119 Eventually(sess).Should(gbytes.Say("1 caches removed")) 120 Eventually(sess).Should(gexec.Exit(0)) 121 }) 122 123 Context("when run noninteractively", func() { 124 BeforeEach(func() { 125 args = append(args, "-n") 126 }) 127 128 It("destroys the task step cache without confirming", func() { 129 Eventually(sess).Should(gbytes.Say("1 caches removed")) 130 Eventually(sess).Should(gexec.Exit(0)) 131 }) 132 }) 133 134 Context("and a cache path is specified", func() { 135 BeforeEach(func() { 136 args = append(args, "-c", "path/to/cache") 137 }) 138 139 It("succeeds if the user says yes", func() { 140 yes() 141 Eventually(sess).Should(gbytes.Say("1 caches removed")) 142 Eventually(sess).Should(gexec.Exit(0)) 143 }) 144 145 }) 146 }) 147 148 Context("and the task step does not exist", func() { 149 BeforeEach(func() { 150 atcServer.AppendHandlers( 151 ghttp.CombineHandlers( 152 ghttp.VerifyRequest("DELETE", "/api/v1/teams/main/pipelines/some-pipeline/jobs/some-job/tasks/some-step-name/cache"), 153 ghttp.RespondWithJSONEncoded(http.StatusOK, atc.ClearTaskCacheResponse{CachesRemoved: 0}), 154 ), 155 ) 156 }) 157 158 It("writes that it did not exist and exits successfully", func() { 159 yes() 160 Eventually(sess).Should(gbytes.Say("0 caches removed")) 161 Eventually(sess).Should(gexec.Exit(0)) 162 }) 163 }) 164 165 Context("and the api returns an unexpected status code", func() { 166 BeforeEach(func() { 167 atcServer.AppendHandlers( 168 ghttp.CombineHandlers( 169 ghttp.VerifyRequest("DELETE", "/api/v1/teams/main/pipelines/some-pipeline/jobs/some-job/tasks/some-step-name/cache"), 170 ghttp.RespondWith(402, ""), 171 ), 172 ) 173 }) 174 175 It("writes an error message to stderr", func() { 176 yes() 177 Eventually(sess.Err).Should(gbytes.Say("Unexpected Response")) 178 Eventually(sess).Should(gexec.Exit(1)) 179 }) 180 }) 181 }) 182 }) 183 })