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

     1  package integration_test
     2  
     3  import (
     4  	"fmt"
     5  	"os/exec"
     6  
     7  	"github.com/chenbh/concourse/v6/atc"
     8  	"github.com/chenbh/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("resources", func() {
    19  		var (
    20  			flyCmd *exec.Cmd
    21  		)
    22  
    23  		Context("when pipeline name is not specified", func() {
    24  			It("fails and says pipeline name is required", func() {
    25  				flyCmd := exec.Command(flyPath, "-t", targetName, "resources")
    26  
    27  				sess, err := gexec.Start(flyCmd, GinkgoWriter, GinkgoWriter)
    28  				Expect(err).NotTo(HaveOccurred())
    29  
    30  				<-sess.Exited
    31  				Expect(sess.ExitCode()).To(Equal(1))
    32  
    33  				Expect(sess.Err).To(gbytes.Say("error: the required flag `" + osFlag("p", "pipeline") + "' was not specified"))
    34  			})
    35  		})
    36  
    37  		Context("when resources are returned from the API", func() {
    38  			createResource := func(num int, pinnedVersion atc.Version, resourceType string) atc.Resource {
    39  				return atc.Resource{
    40  					Name:          fmt.Sprintf("resource-%d", num),
    41  					PinnedVersion: pinnedVersion,
    42  					Type:          resourceType,
    43  				}
    44  			}
    45  
    46  			BeforeEach(func() {
    47  				pipelineName := "pipeline"
    48  				flyCmd = exec.Command(flyPath, "-t", targetName, "resources", "--pipeline", pipelineName)
    49  				atcServer.AppendHandlers(
    50  					ghttp.CombineHandlers(
    51  						ghttp.VerifyRequest("GET", "/api/v1/teams/main/pipelines/pipeline/resources"),
    52  						ghttp.RespondWithJSONEncoded(200, []atc.Resource{
    53  							createResource(1, nil, "time"),
    54  							createResource(2, atc.Version{"some": "version"}, "custom"),
    55  						}),
    56  					),
    57  				)
    58  			})
    59  
    60  			Context("when --json is given", func() {
    61  				BeforeEach(func() {
    62  					flyCmd.Args = append(flyCmd.Args, "--json")
    63  				})
    64  
    65  				It("prints response in json as stdout", func() {
    66  					sess, err := gexec.Start(flyCmd, GinkgoWriter, GinkgoWriter)
    67  					Expect(err).NotTo(HaveOccurred())
    68  
    69  					Eventually(sess).Should(gexec.Exit(0))
    70  					Expect(sess.Out.Contents()).To(MatchJSON(`[
    71                {
    72                  "name": "resource-1",
    73                  "pipeline_name": "",
    74                  "team_name": "",
    75                  "type": "time"
    76                },
    77                {
    78                  "name": "resource-2",
    79                  "pipeline_name": "",
    80                  "team_name": "",
    81                  "type": "custom",
    82  								"pinned_version": {"some": "version"}
    83                }
    84              ]`))
    85  				})
    86  			})
    87  
    88  			It("shows the pipeline's resources", func() {
    89  				sess, err := gexec.Start(flyCmd, GinkgoWriter, GinkgoWriter)
    90  				Expect(err).NotTo(HaveOccurred())
    91  				Eventually(sess).Should(gexec.Exit(0))
    92  
    93  				Expect(sess.Out).To(PrintTable(ui.Table{
    94  					Data: []ui.TableRow{
    95  						{{Contents: "resource-1"}, {Contents: "time"}, {Contents: "n/a"}},
    96  						{{Contents: "resource-2"}, {Contents: "custom"}, {Contents: "some:version", Color: color.New(color.FgCyan)}},
    97  					},
    98  				}))
    99  			})
   100  		})
   101  
   102  		Context("when the api returns an internal server error", func() {
   103  			BeforeEach(func() {
   104  				flyCmd = exec.Command(flyPath, "-t", targetName, "resources", "-p", "pipeline")
   105  				atcServer.AppendHandlers(
   106  					ghttp.CombineHandlers(
   107  						ghttp.VerifyRequest("GET", "/api/v1/teams/main/pipelines/pipeline/resources"),
   108  						ghttp.RespondWith(500, ""),
   109  					),
   110  				)
   111  			})
   112  
   113  			It("writes an error message to stderr", func() {
   114  				sess, err := gexec.Start(flyCmd, GinkgoWriter, GinkgoWriter)
   115  				Expect(err).NotTo(HaveOccurred())
   116  
   117  				Eventually(sess).Should(gexec.Exit(1))
   118  				Eventually(sess.Err).Should(gbytes.Say("Unexpected Response"))
   119  			})
   120  		})
   121  	})
   122  })