github.com/chenbh/concourse/v6@v6.4.2/fly/integration/resource_versions_test.go (about)

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