github.com/pf-qiu/concourse/v6@v6.7.3-0.20201207032516-1f455d73275f/fly/integration/unpin_resource_test.go (about) 1 package integration_test 2 3 import ( 4 "fmt" 5 "net/http" 6 "os/exec" 7 8 "github.com/pf-qiu/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 "github.com/tedsuo/rata" 15 ) 16 17 var _ = Describe("Fly CLI", func() { 18 Describe("unpin-resource", func() { 19 var ( 20 expectedStatus int 21 path string 22 err error 23 teamName = "main" 24 pipelineName = "pipeline" 25 resourceName = "resource" 26 pipelineRef = atc.PipelineRef{Name: pipelineName, InstanceVars: atc.InstanceVars{"branch": "master"}} 27 pipelineResource = fmt.Sprintf("%s/%s", pipelineRef.String(), resourceName) 28 expectedQueryParams = "instance_vars=%7B%22branch%22%3A%22master%22%7D" 29 ) 30 31 BeforeEach(func() { 32 path, err = atc.Routes.CreatePathForRoute(atc.UnpinResource, rata.Params{ 33 "pipeline_name": pipelineName, 34 "team_name": teamName, 35 "resource_name": resourceName, 36 }) 37 Expect(err).NotTo(HaveOccurred()) 38 }) 39 40 JustBeforeEach(func() { 41 atcServer.AppendHandlers( 42 ghttp.CombineHandlers( 43 ghttp.VerifyRequest("PUT", path, expectedQueryParams), 44 ghttp.RespondWith(expectedStatus, nil), 45 ), 46 ) 47 }) 48 49 Context("make sure the command exists", func() { 50 It("calls the unpin-resource command", func() { 51 flyCmd := exec.Command(flyPath, "unpin-resource") 52 sess, err := gexec.Start(flyCmd, GinkgoWriter, GinkgoWriter) 53 54 Expect(err).ToNot(HaveOccurred()) 55 Consistently(sess.Err).ShouldNot(gbytes.Say("error: Unknown command")) 56 57 <-sess.Exited 58 }) 59 }) 60 61 Context("when the resource is specified", func() { 62 Context("when the resource exists", func() { 63 BeforeEach(func() { 64 expectedStatus = http.StatusOK 65 }) 66 67 It("pins the resource version", func() { 68 Expect(func() { 69 flyCmd := exec.Command(flyPath, "-t", targetName, "unpin-resource", "-r", pipelineResource) 70 71 sess, err := gexec.Start(flyCmd, GinkgoWriter, GinkgoWriter) 72 Expect(err).NotTo(HaveOccurred()) 73 74 Eventually(sess.Out).Should(gbytes.Say(fmt.Sprintf("unpinned '%s'\n", pipelineResource))) 75 76 <-sess.Exited 77 Expect(sess.ExitCode()).To(Equal(0)) 78 }).To(Change(func() int { 79 return len(atcServer.ReceivedRequests()) 80 }).By(2)) 81 }) 82 }) 83 84 Context("when the resource does not exist", func() { 85 BeforeEach(func() { 86 expectedStatus = http.StatusNotFound 87 }) 88 89 It("fails to unpin", func() { 90 Expect(func() { 91 flyCmd := exec.Command(flyPath, "-t", targetName, "unpin-resource", "-r", pipelineResource) 92 93 sess, err := gexec.Start(flyCmd, GinkgoWriter, GinkgoWriter) 94 Expect(err).NotTo(HaveOccurred()) 95 96 Eventually(sess.Err).Should(gbytes.Say(fmt.Sprintf("could not find resource '%s'", pipelineResource))) 97 98 <-sess.Exited 99 Expect(sess.ExitCode()).To(Equal(1)) 100 }).To(Change(func() int { 101 return len(atcServer.ReceivedRequests()) 102 }).By(2)) 103 }) 104 }) 105 }) 106 }) 107 })