github.com/pf-qiu/concourse/v6@v6.7.3-0.20201207032516-1f455d73275f/fly/integration/resources_test.go (about)

     1  package integration_test
     2  
     3  import (
     4  	"os/exec"
     5  
     6  	"github.com/pf-qiu/concourse/v6/atc"
     7  	"github.com/pf-qiu/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("resources", func() {
    18  		var (
    19  			flyCmd *exec.Cmd
    20  		)
    21  
    22  		Context("when pipeline name is not specified", func() {
    23  			It("fails and says pipeline name is required", func() {
    24  				flyCmd := exec.Command(flyPath, "-t", targetName, "resources")
    25  
    26  				sess, err := gexec.Start(flyCmd, GinkgoWriter, GinkgoWriter)
    27  				Expect(err).NotTo(HaveOccurred())
    28  
    29  				<-sess.Exited
    30  				Expect(sess.ExitCode()).To(Equal(1))
    31  
    32  				Expect(sess.Err).To(gbytes.Say("error: the required flag `" + osFlag("p", "pipeline") + "' was not specified"))
    33  			})
    34  		})
    35  
    36  		Context("when resources are returned from the API", func() {
    37  			BeforeEach(func() {
    38  				flyCmd = exec.Command(flyPath, "-t", targetName, "resources", "--pipeline", "pipeline/branch:master")
    39  				pipelineRef := atc.PipelineRef{Name: "pipeline", InstanceVars: atc.InstanceVars{"branch": "master"}}
    40  				atcServer.AppendHandlers(
    41  					ghttp.CombineHandlers(
    42  						ghttp.VerifyRequest("GET", "/api/v1/teams/main/pipelines/pipeline/resources", "instance_vars=%7B%22branch%22%3A%22master%22%7D"),
    43  						ghttp.RespondWithJSONEncoded(200, []atc.Resource{
    44  							{
    45  								Name:                 "resource-1",
    46  								PipelineID:           1,
    47  								PipelineName:         pipelineRef.Name,
    48  								PipelineInstanceVars: pipelineRef.InstanceVars,
    49  								TeamName:             teamName,
    50  								Type:                 "time",
    51  								Build: &atc.BuildSummary{
    52  									ID:                   122,
    53  									Name:                 "122",
    54  									Status:               atc.StatusSucceeded,
    55  									TeamName:             teamName,
    56  									PipelineID:           1,
    57  									PipelineName:         pipelineRef.Name,
    58  									PipelineInstanceVars: pipelineRef.InstanceVars,
    59  								},
    60  							},
    61  							{
    62  								Name:                 "resource-2",
    63  								PipelineID:           1,
    64  								PipelineName:         pipelineRef.Name,
    65  								PipelineInstanceVars: pipelineRef.InstanceVars,
    66  								TeamName:             teamName,
    67  								Type:                 "custom",
    68  								PinnedVersion:        atc.Version{"some": "version"},
    69  							},
    70  							{
    71  								Name:                 "resource-3",
    72  								PipelineID:           1,
    73  								PipelineName:         pipelineRef.Name,
    74  								PipelineInstanceVars: pipelineRef.InstanceVars,
    75  								TeamName:             teamName,
    76  								Type:                 "mock",
    77  								Build: &atc.BuildSummary{
    78  									ID:                   123,
    79  									Name:                 "123",
    80  									Status:               atc.StatusFailed,
    81  									TeamName:             teamName,
    82  									PipelineID:           1,
    83  									PipelineName:         pipelineRef.Name,
    84  									PipelineInstanceVars: pipelineRef.InstanceVars,
    85  								},
    86  							},
    87  							{
    88  								Name:                 "resource-4",
    89  								PipelineID:           1,
    90  								PipelineName:         pipelineRef.Name,
    91  								PipelineInstanceVars: pipelineRef.InstanceVars,
    92  								TeamName:             teamName,
    93  								Type:                 "mock",
    94  								Build: &atc.BuildSummary{
    95  									ID:                   124,
    96  									Name:                 "124",
    97  									Status:               atc.StatusErrored,
    98  									TeamName:             teamName,
    99  									PipelineID:           1,
   100  									PipelineName:         pipelineRef.Name,
   101  									PipelineInstanceVars: pipelineRef.InstanceVars,
   102  								},
   103  							},
   104  						}),
   105  					),
   106  				)
   107  			})
   108  
   109  			Context("when --json is given", func() {
   110  				BeforeEach(func() {
   111  					flyCmd.Args = append(flyCmd.Args, "--json")
   112  				})
   113  
   114  				It("prints response in json as stdout", func() {
   115  					sess, err := gexec.Start(flyCmd, GinkgoWriter, GinkgoWriter)
   116  					Expect(err).NotTo(HaveOccurred())
   117  
   118  					Eventually(sess).Should(gexec.Exit(0))
   119  					Expect(sess.Out.Contents()).To(MatchJSON(`[
   120                {
   121                  "name": "resource-1",
   122                  "pipeline_id": 1,
   123                  "pipeline_name": "pipeline",
   124                  "pipeline_instance_vars": {
   125                    "branch": "master"
   126                  },
   127                  "team_name": "main",
   128                  "type": "time",
   129  								"build": {
   130  									"id": 122,
   131  									"name": "122",
   132  									"pipeline_id": 1,
   133  									"pipeline_name": "pipeline",
   134  									"pipeline_instance_vars": {
   135  										"branch": "master"
   136  									},
   137  									"team_name": "main",
   138  									"status": "succeeded"
   139  								}
   140                },
   141                {
   142                  "name": "resource-2",
   143                  "pipeline_id": 1,
   144                  "pipeline_name": "pipeline",
   145                  "pipeline_instance_vars": {
   146                    "branch": "master"
   147                  },
   148                  "team_name": "main",
   149                  "type": "custom",
   150                  "pinned_version": {"some": "version"}
   151                },
   152                {
   153                  "name": "resource-3",
   154                  "pipeline_id": 1,
   155                  "pipeline_name": "pipeline",
   156                  "pipeline_instance_vars": {
   157                    "branch": "master"
   158                  },
   159                  "team_name": "main",
   160                  "type": "mock",
   161  								"build": {
   162  									"id": 123,
   163  									"name": "123",
   164  									"pipeline_id": 1,
   165  									"pipeline_name": "pipeline",
   166  									"pipeline_instance_vars": {
   167  										"branch": "master"
   168  									},
   169  									"team_name": "main",
   170  									"status": "failed"
   171  								}
   172                },
   173                {
   174                  "name": "resource-4",
   175                  "pipeline_id": 1,
   176                  "pipeline_name": "pipeline",
   177                  "pipeline_instance_vars": {
   178                    "branch": "master"
   179                  },
   180                  "team_name": "main",
   181                  "type": "mock",
   182  								"build": {
   183  									"id": 124,
   184  									"name": "124",
   185  									"pipeline_id": 1,
   186  									"pipeline_name": "pipeline",
   187  									"pipeline_instance_vars": {
   188  										"branch": "master"
   189  									},
   190  									"team_name": "main",
   191  									"status": "errored"
   192  								}
   193                }
   194              ]`))
   195  				})
   196  			})
   197  
   198  			It("shows the pipeline's resources", func() {
   199  				sess, err := gexec.Start(flyCmd, GinkgoWriter, GinkgoWriter)
   200  				Expect(err).NotTo(HaveOccurred())
   201  				Eventually(sess).Should(gexec.Exit(0))
   202  
   203  				Expect(sess.Out).To(PrintTable(ui.Table{
   204  					Data: []ui.TableRow{
   205  						{{Contents: "resource-1"}, {Contents: "time"}, {Contents: "n/a"}, {Contents: "succeeded", Color: color.New(color.FgGreen)}},
   206  						{{Contents: "resource-2"}, {Contents: "custom"}, {Contents: "some:version", Color: color.New(color.FgCyan)}, {Contents: "n/a", Color: color.New(color.Faint)}},
   207  						{{Contents: "resource-3"}, {Contents: "mock"}, {Contents: "n/a"}, {Contents: "failed", Color: color.New(color.FgRed)}},
   208  						{{Contents: "resource-4"}, {Contents: "mock"}, {Contents: "n/a"}, {Contents: "errored", Color: color.New(color.FgRed, color.Bold)}},
   209  					},
   210  				}))
   211  			})
   212  		})
   213  
   214  		Context("when the api returns an internal server error", func() {
   215  			BeforeEach(func() {
   216  				flyCmd = exec.Command(flyPath, "-t", targetName, "resources", "-p", "pipeline")
   217  				atcServer.AppendHandlers(
   218  					ghttp.CombineHandlers(
   219  						ghttp.VerifyRequest("GET", "/api/v1/teams/main/pipelines/pipeline/resources"),
   220  						ghttp.RespondWith(500, ""),
   221  					),
   222  				)
   223  			})
   224  
   225  			It("writes an error message to stderr", func() {
   226  				sess, err := gexec.Start(flyCmd, GinkgoWriter, GinkgoWriter)
   227  				Expect(err).NotTo(HaveOccurred())
   228  
   229  				Eventually(sess).Should(gexec.Exit(1))
   230  				Eventually(sess.Err).Should(gbytes.Say("Unexpected Response"))
   231  			})
   232  		})
   233  	})
   234  })