github.com/pf-qiu/concourse/v6@v6.7.3-0.20201207032516-1f455d73275f/fly/integration/resource_versions_test.go (about) 1 package integration_test 2 3 import ( 4 "os/exec" 5 "strings" 6 7 "github.com/pf-qiu/concourse/v6/atc" 8 "github.com/pf-qiu/concourse/v6/fly/ui" 9 "github.com/fatih/color" 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("resource-versions", func() { 19 var ( 20 flyCmd *exec.Cmd 21 queryParams = []string{"instance_vars=%7B%22branch%22%3A%22master%22%7D", "limit=50"} 22 ) 23 24 BeforeEach(func() { 25 flyCmd = exec.Command(flyPath, "-t", targetName, "resource-versions", "-r", "pipeline/branch:master/foo") 26 }) 27 28 Context("when pipelines are returned from the API", func() { 29 Context("when no --all flag is given", func() { 30 BeforeEach(func() { 31 atcServer.AppendHandlers( 32 ghttp.CombineHandlers( 33 ghttp.VerifyRequest("GET", "/api/v1/teams/main/pipelines/pipeline/resources/foo/versions", strings.Join(queryParams, "&")), 34 ghttp.RespondWithJSONEncoded(200, []atc.ResourceVersion{ 35 {ID: 3, Version: atc.Version{"version": "3", "another": "field"}, Enabled: true}, 36 {ID: 2, Version: atc.Version{"version": "2", "another": "field"}, Enabled: false}, 37 {ID: 1, Version: atc.Version{"version": "1", "another": "field"}, Enabled: true}, 38 }), 39 ), 40 ) 41 }) 42 43 Context("when --json is given", func() { 44 BeforeEach(func() { 45 flyCmd.Args = append(flyCmd.Args, "--json") 46 }) 47 48 It("prints response in json as stdout", func() { 49 sess, err := gexec.Start(flyCmd, GinkgoWriter, GinkgoWriter) 50 Expect(err).NotTo(HaveOccurred()) 51 52 Eventually(sess).Should(gexec.Exit(0)) 53 Expect(sess.Out.Contents()).To(MatchJSON(`[ 54 { 55 "id": 3, 56 "version": {"version":"3","another":"field"}, 57 "enabled": true 58 }, 59 { 60 "id": 2, 61 "version": {"version":"2","another":"field"}, 62 "enabled": false 63 }, 64 { 65 "id": 1, 66 "version": {"version":"1","another":"field"}, 67 "enabled": true 68 } 69 ]`)) 70 }) 71 }) 72 73 It("lists the resource versions", func() { 74 sess, err := gexec.Start(flyCmd, GinkgoWriter, GinkgoWriter) 75 Expect(err).NotTo(HaveOccurred()) 76 Eventually(sess).Should(gexec.Exit(0)) 77 78 Expect(sess.Out).To(PrintTable(ui.Table{ 79 Headers: ui.TableRow{ 80 {Contents: "id", Color: color.New(color.Bold)}, 81 {Contents: "version", Color: color.New(color.Bold)}, 82 {Contents: "enabled", Color: color.New(color.Bold)}, 83 }, 84 Data: []ui.TableRow{ 85 {{Contents: "3"}, {Contents: "another:field,version:3"}, {Contents: "yes"}}, 86 {{Contents: "2"}, {Contents: "another:field,version:2"}, {Contents: "no"}}, 87 {{Contents: "1"}, {Contents: "another:field,version:1"}, {Contents: "yes"}}, 88 }, 89 })) 90 }) 91 }) 92 }) 93 94 Context("and the api returns an internal server error", func() { 95 BeforeEach(func() { 96 atcServer.AppendHandlers( 97 ghttp.CombineHandlers( 98 ghttp.VerifyRequest("GET", "/api/v1/teams/main/pipelines/pipeline/resources/foo/versions", strings.Join(queryParams, "&")), 99 ghttp.RespondWith(500, ""), 100 ), 101 ) 102 }) 103 104 It("writes an error message to stderr", func() { 105 sess, err := gexec.Start(flyCmd, GinkgoWriter, GinkgoWriter) 106 Expect(err).NotTo(HaveOccurred()) 107 108 Eventually(sess).Should(gexec.Exit(1)) 109 Eventually(sess.Err).Should(gbytes.Say("Unexpected Response")) 110 }) 111 }) 112 }) 113 })