github.com/chenbh/concourse/v6@v6.4.2/fly/integration/disable_resource_version_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/ghttp" 13 "github.com/tedsuo/rata" 14 15 "github.com/onsi/gomega/gexec" 16 ) 17 18 var _ = Describe("Fly CLI", func() { 19 Describe("disable-resource-version", func() { 20 var ( 21 expectedGetStatus int 22 expectedPutStatus int 23 disablePath, getPath string 24 err error 25 teamName = "main" 26 pipelineName = "pipeline" 27 resourceName = "resource" 28 resourceVersionID = "42" 29 disableVersion = "some:value" 30 pipelineResource = fmt.Sprintf("%s/%s", pipelineName, resourceName) 31 expectedVersion = atc.ResourceVersion{ 32 ID: 42, 33 Version: atc.Version{"some": "value"}, 34 Enabled: true, 35 } 36 ) 37 38 Context("make sure the command exists", func() { 39 It("calls the disable-resource-version command", func() { 40 flyCmd := exec.Command(flyPath, "disable-resource-version") 41 sess, err := gexec.Start(flyCmd, GinkgoWriter, GinkgoWriter) 42 43 Expect(err).ToNot(HaveOccurred()) 44 Consistently(sess.Err).ShouldNot(gbytes.Say("error: Unknown command")) 45 46 <-sess.Exited 47 }) 48 }) 49 50 Context("when the resource is specified", func() { 51 Context("when the resource version json string is specified", func() { 52 BeforeEach(func() { 53 getPath, err = atc.Routes.CreatePathForRoute(atc.ListResourceVersions, rata.Params{ 54 "pipeline_name": pipelineName, 55 "team_name": teamName, 56 "resource_name": resourceName, 57 }) 58 Expect(err).NotTo(HaveOccurred()) 59 60 disablePath, err = atc.Routes.CreatePathForRoute(atc.DisableResourceVersion, rata.Params{ 61 "pipeline_name": pipelineName, 62 "team_name": teamName, 63 "resource_name": resourceName, 64 "resource_config_version_id": resourceVersionID, 65 }) 66 Expect(err).NotTo(HaveOccurred()) 67 68 }) 69 70 JustBeforeEach(func() { 71 atcServer.AppendHandlers( 72 ghttp.CombineHandlers( 73 ghttp.VerifyRequest("GET", getPath, "filter=some:value"), 74 ghttp.RespondWithJSONEncoded(expectedGetStatus, []atc.ResourceVersion{expectedVersion}), 75 ), 76 ghttp.CombineHandlers( 77 ghttp.VerifyRequest("PUT", disablePath), 78 ghttp.RespondWith(expectedPutStatus, nil), 79 ), 80 ) 81 }) 82 83 Context("when the resource and version exists", func() { 84 BeforeEach(func() { 85 expectedGetStatus = http.StatusOK 86 expectedPutStatus = http.StatusOK 87 88 expectedVersion.Enabled = true 89 }) 90 91 It("disables the resource version", func() { 92 Expect(func() { 93 flyCmd := exec.Command(flyPath, "-t", targetName, "disable-resource-version", "-r", pipelineResource, "-v", disableVersion) 94 95 sess, err := gexec.Start(flyCmd, GinkgoWriter, GinkgoWriter) 96 Expect(err).NotTo(HaveOccurred()) 97 98 Eventually(sess.Out).Should(gbytes.Say(fmt.Sprintf("disabled '%s' with version {\"some\":\"value\"}\n", pipelineResource))) 99 100 <-sess.Exited 101 Expect(sess.ExitCode()).To(Equal(0)) 102 }).To(Change(func() int { 103 return len(atcServer.ReceivedRequests()) 104 }).By(3)) 105 }) 106 }) 107 108 Context("when the resource does not exist", func() { 109 BeforeEach(func() { 110 expectedGetStatus = http.StatusNotFound 111 }) 112 113 It("errors", func() { 114 Expect(func() { 115 flyCmd := exec.Command(flyPath, "-t", targetName, "disable-resource-version", "-r", pipelineResource, "-v", disableVersion) 116 117 sess, err := gexec.Start(flyCmd, GinkgoWriter, GinkgoWriter) 118 Expect(err).NotTo(HaveOccurred()) 119 120 Eventually(sess.Err).Should(gbytes.Say(fmt.Sprintf("could not find version matching {\"some\":\"value\"}\n"))) 121 122 <-sess.Exited 123 Expect(sess.ExitCode()).To(Equal(1)) 124 }).To(Change(func() int { 125 return len(atcServer.ReceivedRequests()) 126 }).By(2)) 127 }) 128 }) 129 130 Context("when the resource version does not exist", func() { 131 BeforeEach(func() { 132 expectedPutStatus = http.StatusNotFound 133 expectedGetStatus = http.StatusOK 134 135 expectedVersion.Enabled = true 136 }) 137 138 It("fails to disable", func() { 139 Expect(func() { 140 flyCmd := exec.Command(flyPath, "-t", targetName, "disable-resource-version", "-r", pipelineResource, "-v", disableVersion) 141 142 sess, err := gexec.Start(flyCmd, GinkgoWriter, GinkgoWriter) 143 Expect(err).NotTo(HaveOccurred()) 144 145 Eventually(sess.Err).Should(gbytes.Say(fmt.Sprintf("could not disable '%s', make sure the resource version exists", pipelineResource))) 146 147 <-sess.Exited 148 Expect(sess.ExitCode()).To(Equal(1)) 149 }).To(Change(func() int { 150 return len(atcServer.ReceivedRequests()) 151 }).By(3)) 152 }) 153 }) 154 Context("when the resource version is already disabled", func() { 155 BeforeEach(func() { 156 expectedGetStatus = http.StatusOK 157 expectedVersion.Enabled = false 158 }) 159 160 It("returns successfully without calling api", func() { 161 Expect(func() { 162 flyCmd := exec.Command(flyPath, "-t", targetName, "disable-resource-version", "-r", pipelineResource, "-v", disableVersion) 163 164 sess, err := gexec.Start(flyCmd, GinkgoWriter, GinkgoWriter) 165 Expect(err).NotTo(HaveOccurred()) 166 167 Eventually(sess.Out).Should(gbytes.Say(fmt.Sprintf("disabled '%s' with version {\"some\":\"value\"}\n", pipelineResource))) 168 169 <-sess.Exited 170 Expect(sess.ExitCode()).To(Equal(0)) 171 172 for _, request := range atcServer.ReceivedRequests() { 173 Expect(request.RequestURI).NotTo(Equal(disablePath)) 174 } 175 }).To(Change(func() int { 176 return len(atcServer.ReceivedRequests()) 177 }).By(2)) 178 }) 179 }) 180 }) 181 }) 182 }) 183 })