github.com/chenbh/concourse/v6@v6.4.2/fly/integration/containers_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("containers", func() {
    18  		var (
    19  			flyCmd *exec.Cmd
    20  		)
    21  
    22  		BeforeEach(func() {
    23  			flyCmd = exec.Command(flyPath, "-t", targetName, "containers")
    24  		})
    25  
    26  		Context("when containers are returned from the API", func() {
    27  			BeforeEach(func() {
    28  				atcServer.AppendHandlers(
    29  					ghttp.CombineHandlers(
    30  						ghttp.VerifyRequest("GET", "/api/v1/teams/main/containers"),
    31  						ghttp.RespondWithJSONEncoded(200, []atc.Container{
    32  							{
    33  								ID:           "handle-1",
    34  								WorkerName:   "worker-name-1",
    35  								PipelineName: "pipeline-name",
    36  								Type:         "check",
    37  								ResourceName: "git-repo",
    38  							},
    39  							{
    40  								ID:           "early-handle",
    41  								WorkerName:   "worker-name-1",
    42  								PipelineName: "pipeline-name",
    43  								JobName:      "job-name-1",
    44  								BuildName:    "3",
    45  								BuildID:      123,
    46  								Type:         "get",
    47  								StepName:     "git-repo",
    48  								Attempt:      "1.5",
    49  							},
    50  							{
    51  								ID:           "other-handle",
    52  								WorkerName:   "worker-name-2",
    53  								PipelineName: "pipeline-name",
    54  								JobName:      "job-name-2",
    55  								BuildName:    "2",
    56  								BuildID:      122,
    57  								Type:         "task",
    58  								StepName:     "unit-tests",
    59  							},
    60  							{
    61  								ID:         "post-handle",
    62  								WorkerName: "worker-name-3",
    63  								BuildID:    142,
    64  								Type:       "task",
    65  								StepName:   "one-off",
    66  							},
    67  						}),
    68  					),
    69  				)
    70  			})
    71  
    72  			Context("when --json is given", func() {
    73  				BeforeEach(func() {
    74  					flyCmd.Args = append(flyCmd.Args, "--json")
    75  				})
    76  
    77  				It("prints response in json as stdout", func() {
    78  					sess, err := gexec.Start(flyCmd, GinkgoWriter, GinkgoWriter)
    79  					Expect(err).NotTo(HaveOccurred())
    80  
    81  					Eventually(sess).Should(gexec.Exit(0))
    82  					Expect(sess.Out.Contents()).To(MatchJSON(`[
    83                {
    84                  "id": "handle-1",
    85                  "worker_name": "worker-name-1",
    86                  "type": "check",
    87                  "pipeline_name": "pipeline-name",
    88                  "resource_name": "git-repo"
    89                },
    90                {
    91                  "id": "early-handle",
    92                  "worker_name": "worker-name-1",
    93                  "type": "get",
    94                  "step_name": "git-repo",
    95                  "attempt": "1.5",
    96                  "build_id": 123,
    97                  "pipeline_name": "pipeline-name",
    98                  "job_name": "job-name-1",
    99                  "build_name": "3"
   100                },
   101                {
   102                  "id": "other-handle",
   103                  "worker_name": "worker-name-2",
   104                  "type": "task",
   105                  "step_name": "unit-tests",
   106                  "build_id": 122,
   107                  "pipeline_name": "pipeline-name",
   108                  "job_name": "job-name-2",
   109                  "build_name": "2"
   110                },
   111                {
   112                  "id": "post-handle",
   113                  "worker_name": "worker-name-3",
   114                  "type": "task",
   115                  "step_name": "one-off",
   116                  "build_id": 142
   117                }
   118              ]`))
   119  				})
   120  			})
   121  
   122  			It("lists them to the user, ordered by name", func() {
   123  				sess, err := gexec.Start(flyCmd, GinkgoWriter, GinkgoWriter)
   124  				Expect(err).NotTo(HaveOccurred())
   125  
   126  				Eventually(sess).Should(gexec.Exit(0))
   127  				Expect(sess.Out).To(PrintTable(ui.Table{
   128  					Headers: ui.TableRow{
   129  						{Contents: "handle", Color: color.New(color.Bold)},
   130  						{Contents: "worker", Color: color.New(color.Bold)},
   131  						{Contents: "pipeline", Color: color.New(color.Bold)},
   132  						{Contents: "job", Color: color.New(color.Bold)},
   133  						{Contents: "build #", Color: color.New(color.Bold)},
   134  						{Contents: "build id", Color: color.New(color.Bold)},
   135  						{Contents: "type", Color: color.New(color.Bold)},
   136  						{Contents: "name", Color: color.New(color.Bold)},
   137  						{Contents: "attempt", Color: color.New(color.Bold)},
   138  					},
   139  					Data: []ui.TableRow{
   140  						{{Contents: "early-handle"}, {Contents: "worker-name-1"}, {Contents: "pipeline-name"}, {Contents: "job-name-1"}, {Contents: "3"}, {Contents: "123"}, {Contents: "get"}, {Contents: "git-repo"}, {Contents: "1.5"}},
   141  						{{Contents: "handle-1"}, {Contents: "worker-name-1"}, {Contents: "pipeline-name"}, {Contents: "none", Color: color.New(color.Faint)}, {Contents: "none", Color: color.New(color.Faint)}, {Contents: "none", Color: color.New(color.Faint)}, {Contents: "check"}, {Contents: "git-repo"}, {Contents: "n/a", Color: color.New(color.Faint)}},
   142  						{{Contents: "other-handle"}, {Contents: "worker-name-2"}, {Contents: "pipeline-name"}, {Contents: "job-name-2"}, {Contents: "2"}, {Contents: "122"}, {Contents: "task"}, {Contents: "unit-tests"}, {Contents: "n/a", Color: color.New(color.Faint)}},
   143  						{{Contents: "post-handle"}, {Contents: "worker-name-3"}, {Contents: "none", Color: color.New(color.Faint)}, {Contents: "none", Color: color.New(color.Faint)}, {Contents: "none", Color: color.New(color.Faint)}, {Contents: "142"}, {Contents: "task"}, {Contents: "one-off"}, {Contents: "n/a", Color: color.New(color.Faint)}},
   144  					},
   145  				}))
   146  			})
   147  		})
   148  
   149  		Context("and the api returns an internal server error", func() {
   150  			BeforeEach(func() {
   151  				atcServer.AppendHandlers(
   152  					ghttp.CombineHandlers(
   153  						ghttp.VerifyRequest("GET", "/api/v1/teams/main/containers"),
   154  						ghttp.RespondWith(500, ""),
   155  					),
   156  				)
   157  			})
   158  
   159  			It("writes an error message to stderr", func() {
   160  				sess, err := gexec.Start(flyCmd, GinkgoWriter, GinkgoWriter)
   161  				Expect(err).NotTo(HaveOccurred())
   162  
   163  				Eventually(sess).Should(gexec.Exit(1))
   164  				Eventually(sess.Err).Should(gbytes.Say("Unexpected Response"))
   165  			})
   166  		})
   167  	})
   168  })