github.com/pf-qiu/concourse/v6@v6.7.3-0.20201207032516-1f455d73275f/fly/integration/pin_resource_test.go (about) 1 package integration_test 2 3 import ( 4 "fmt" 5 "net/http" 6 "os/exec" 7 "strings" 8 9 "github.com/pf-qiu/concourse/v6/atc" 10 . "github.com/onsi/ginkgo" 11 . "github.com/onsi/gomega" 12 "github.com/onsi/gomega/gbytes" 13 "github.com/onsi/gomega/ghttp" 14 "github.com/tedsuo/rata" 15 16 "github.com/onsi/gomega/gexec" 17 ) 18 19 var _ = Describe("Fly CLI", func() { 20 Describe("pin-resource", func() { 21 var ( 22 listVersionsStatus int 23 pinVersionStatus int 24 saveCommentStatus int 25 pinVersionPath, listVersionsPath, commentPath string 26 err error 27 teamName = "main" 28 pipelineName = "pipeline" 29 resourceName = "resource" 30 resourceVersionID = "42" 31 pinVersion = "some:value" 32 pipelineRef = atc.PipelineRef{Name: pipelineName, InstanceVars: atc.InstanceVars{"branch": "master"}} 33 pipelineResource = fmt.Sprintf("%s/%s", pipelineRef.String(), resourceName) 34 versionToPin = atc.ResourceVersion{ 35 ID: 42, 36 Version: atc.Version{"some": "value"}, 37 } 38 expectedQueryParams []string 39 ) 40 41 BeforeEach(func() { 42 listVersionsPath, err = atc.Routes.CreatePathForRoute(atc.ListResourceVersions, rata.Params{ 43 "pipeline_name": pipelineName, 44 "team_name": teamName, 45 "resource_name": resourceName, 46 }) 47 Expect(err).NotTo(HaveOccurred()) 48 pinVersionPath, err = atc.Routes.CreatePathForRoute(atc.PinResourceVersion, rata.Params{ 49 "pipeline_name": pipelineName, 50 "team_name": teamName, 51 "resource_name": resourceName, 52 "resource_config_version_id": resourceVersionID, 53 }) 54 Expect(err).NotTo(HaveOccurred()) 55 commentPath, err = atc.Routes.CreatePathForRoute(atc.SetPinCommentOnResource, rata.Params{ 56 "pipeline_name": pipelineName, 57 "team_name": teamName, 58 "resource_name": resourceName, 59 }) 60 Expect(err).NotTo(HaveOccurred()) 61 expectedQueryParams = []string{ 62 "instance_vars=%7B%22branch%22%3A%22master%22%7D", 63 "filter=some:value", 64 } 65 }) 66 67 It("is a subcommand", func() { 68 flyCmd := exec.Command(flyPath, "pin-resource") 69 sess, err := gexec.Start(flyCmd, GinkgoWriter, GinkgoWriter) 70 71 Expect(err).ToNot(HaveOccurred()) 72 Consistently(sess.Err).ShouldNot(gbytes.Say("error: Unknown command")) 73 74 <-sess.Exited 75 }) 76 77 It("asks the user to specify a version when no version or comment are specified", func() { 78 flyCmd := exec.Command(flyPath, "-t", targetName, "pin-resource", "-r", pipelineResource) 79 sess, err := gexec.Start(flyCmd, GinkgoWriter, GinkgoWriter) 80 Expect(err).NotTo(HaveOccurred()) 81 82 Eventually(sess).Should(gexec.Exit(1)) 83 Expect(sess.Err).To(gbytes.Say("error: the required flag `" + osFlag("v", "version") + "' was not specified")) 84 }) 85 86 87 Context("when a version is specified", func() { 88 JustBeforeEach(func() { 89 atcServer.AppendHandlers( 90 ghttp.CombineHandlers( 91 ghttp.VerifyRequest("GET", listVersionsPath, strings.Join(expectedQueryParams, "&")), 92 ghttp.RespondWithJSONEncoded(listVersionsStatus, []atc.ResourceVersion{versionToPin}), 93 ), 94 ghttp.CombineHandlers( 95 ghttp.VerifyRequest("PUT", pinVersionPath, "instance_vars=%7B%22branch%22%3A%22master%22%7D"), 96 ghttp.RespondWith(pinVersionStatus, nil), 97 ), 98 ) 99 }) 100 101 Context("when the resource and versions exist and pinning succeeds", func() { 102 BeforeEach(func() { 103 listVersionsStatus = http.StatusOK 104 pinVersionStatus = http.StatusOK 105 }) 106 107 It("pins the resource version", func() { 108 flyCmd := exec.Command(flyPath, "-t", targetName, "pin-resource", "-r", pipelineResource, "-v", pinVersion) 109 110 sess, err := gexec.Start(flyCmd, GinkgoWriter, GinkgoWriter) 111 Expect(err).NotTo(HaveOccurred()) 112 113 Eventually(sess.Out).Should(gbytes.Say(fmt.Sprintf("pinned '%s' with version {\"some\":\"value\"}\n", pipelineResource))) 114 115 <-sess.Exited 116 Expect(sess.ExitCode()).To(Equal(0)) 117 }) 118 119 }) 120 121 Context("when the resource or version cannot be found", func() { 122 BeforeEach(func() { 123 listVersionsStatus = http.StatusNotFound 124 }) 125 126 It("errors", func() { 127 flyCmd := exec.Command(flyPath, "-t", targetName, "pin-resource", "-r", pipelineResource, "-v", pinVersion) 128 129 sess, err := gexec.Start(flyCmd, GinkgoWriter, GinkgoWriter) 130 Expect(err).NotTo(HaveOccurred()) 131 132 Eventually(sess.Err).Should(gbytes.Say(fmt.Sprintf("could not find version matching {\"some\":\"value\"}\n"))) 133 134 <-sess.Exited 135 Expect(sess.ExitCode()).To(Equal(1)) 136 }) 137 }) 138 139 Context("when the resource disappears before pinning", func() { 140 BeforeEach(func() { 141 listVersionsStatus = http.StatusOK 142 pinVersionStatus = http.StatusNotFound 143 }) 144 145 It("fails to pin", func() { 146 flyCmd := exec.Command(flyPath, "-t", targetName, "pin-resource", "-r", pipelineResource, "-v", pinVersion) 147 148 sess, err := gexec.Start(flyCmd, GinkgoWriter, GinkgoWriter) 149 Expect(err).NotTo(HaveOccurred()) 150 151 Eventually(sess.Err).Should(gbytes.Say(fmt.Sprintf("could not pin '%s', make sure the resource exists", pipelineResource))) 152 153 <-sess.Exited 154 Expect(sess.ExitCode()).To(Equal(1)) 155 }) 156 }) 157 }) 158 159 Context("when version and comment are provided", func() { 160 var sess *gexec.Session 161 JustBeforeEach(func() { 162 atcServer.AppendHandlers( 163 ghttp.CombineHandlers( 164 ghttp.VerifyRequest("GET", listVersionsPath, strings.Join(expectedQueryParams, "&")), 165 ghttp.RespondWithJSONEncoded(listVersionsStatus, []atc.ResourceVersion{versionToPin}), 166 ), 167 ghttp.CombineHandlers( 168 ghttp.VerifyRequest("PUT", pinVersionPath, "instance_vars=%7B%22branch%22%3A%22master%22%7D"), 169 ghttp.RespondWith(pinVersionStatus, nil), 170 ), 171 ghttp.CombineHandlers( 172 ghttp.VerifyRequest("PUT", commentPath), 173 ghttp.VerifyJSONRepresenting(atc.SetPinCommentRequestBody{PinComment: "some pin message"}), 174 ghttp.RespondWith(saveCommentStatus, nil), 175 ), 176 ) 177 178 var err error 179 flyCmd := exec.Command(flyPath, "-t", targetName, "pin-resource", "-r", pipelineResource, "-c", "some pin message", "-v", pinVersion) 180 sess, err = gexec.Start(flyCmd, GinkgoWriter, GinkgoWriter) 181 Expect(err).NotTo(HaveOccurred()) 182 }) 183 184 Context("when the resource and versions exist, pinning succeeds and saving the comment succeeds", func() { 185 BeforeEach(func() { 186 listVersionsStatus = http.StatusOK 187 pinVersionStatus = http.StatusOK 188 saveCommentStatus = http.StatusOK 189 }) 190 191 It("saves the pin comment", func() { 192 Eventually(sess.Out).Should(gbytes.Say(fmt.Sprintf("pin comment 'some pin message' is saved\n"))) 193 <-sess.Exited 194 Expect(sess.ExitCode()).To(Equal(0)) 195 }) 196 }) 197 198 Context("when the resource or versions cannot be found", func() { 199 BeforeEach(func() { 200 listVersionsStatus = http.StatusNotFound 201 pinVersionStatus = http.StatusOK 202 saveCommentStatus = http.StatusOK 203 }) 204 205 It("errors", func() { 206 Eventually(sess.Err).Should(gbytes.Say(fmt.Sprintf("could not find version matching"))) 207 <-sess.Exited 208 Expect(sess.ExitCode()).To(Equal(1)) 209 }) 210 }) 211 212 Context("when the resource disappears before pinning", func() { 213 BeforeEach(func() { 214 listVersionsStatus = http.StatusOK 215 pinVersionStatus = http.StatusNotFound 216 saveCommentStatus = http.StatusOK 217 }) 218 219 It("errors", func() { 220 Eventually(sess.Err).Should(gbytes.Say(fmt.Sprintf("could not pin '%s', make sure the resource exists", pipelineResource))) 221 <-sess.Exited 222 Expect(sess.ExitCode()).To(Equal(1)) 223 }) 224 }) 225 }) 226 227 Context("when comment is provided without version and saving the comment fails", func() { 228 BeforeEach(func() { 229 saveCommentStatus = http.StatusNotFound 230 }) 231 232 JustBeforeEach(func() { 233 atcServer.AppendHandlers( 234 ghttp.CombineHandlers( 235 ghttp.VerifyRequest("PUT", commentPath), 236 ghttp.VerifyJSONRepresenting(atc.SetPinCommentRequestBody{PinComment: "some pin message"}), 237 ghttp.RespondWith(saveCommentStatus, nil), 238 ), 239 ) 240 }) 241 242 It("errors", func() { 243 flyCmd := exec.Command(flyPath, "-t", targetName, "pin-resource", "-r", pipelineResource, "-c", "some pin message") 244 245 sess, err := gexec.Start(flyCmd, GinkgoWriter, GinkgoWriter) 246 Expect(err).NotTo(HaveOccurred()) 247 248 Eventually(sess.Err).Should(gbytes.Say(fmt.Sprintf("could not save comment, make sure '%s' is pinned", pipelineResource))) 249 <-sess.Exited 250 Expect(sess.ExitCode()).To(Equal(1)) 251 }) 252 }) 253 }) 254 })