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

     1  package integration_test
     2  
     3  import (
     4  	"os/exec"
     5  
     6  	"github.com/chenbh/concourse/v6/fly/rc"
     7  	"github.com/chenbh/concourse/v6/fly/ui"
     8  	"github.com/fatih/color"
     9  	"github.com/onsi/gomega/gbytes"
    10  	"github.com/onsi/gomega/gexec"
    11  
    12  	. "github.com/onsi/ginkgo"
    13  	. "github.com/onsi/gomega"
    14  )
    15  
    16  var _ = Describe("Fly CLI", func() {
    17  	var (
    18  		flyCmd  *exec.Cmd
    19  		targets rc.Targets
    20  	)
    21  
    22  	JustBeforeEach(func() {
    23  		createFlyRc(targets)
    24  
    25  		flyCmd = exec.Command(flyPath, "targets")
    26  	})
    27  
    28  	BeforeEach(func() {
    29  		targets = rc.Targets{
    30  			"another-test": {
    31  				API:      "https://example.com/another-test",
    32  				TeamName: "test",
    33  				Token:    &rc.TargetToken{Type: "Bearer", Value: validAccessToken(date(2020, 1, 1))},
    34  			},
    35  			"no-token": {
    36  				API:      "https://example.com/no-token",
    37  				TeamName: "main",
    38  				Token:    nil,
    39  			},
    40  			"omt": {
    41  				API:      "https://example.com/omt",
    42  				TeamName: "main",
    43  				Token:    &rc.TargetToken{Type: "Bearer", Value: validAccessToken(date(2020, 1, 2))},
    44  			},
    45  			"test": {
    46  				API:      "https://example.com/test",
    47  				TeamName: "test",
    48  				Token:    &rc.TargetToken{Type: "Bearer", Value: validAccessToken(date(2020, 1, 3))},
    49  			},
    50  		}
    51  	})
    52  
    53  	Describe("targets", func() {
    54  		Context("when there are targets in the .flyrc", func() {
    55  			It("displays all the targets with their token expiration", func() {
    56  				sess, err := gexec.Start(flyCmd, GinkgoWriter, GinkgoWriter)
    57  				Expect(err).NotTo(HaveOccurred())
    58  
    59  				Eventually(sess).Should(gexec.Exit(0))
    60  
    61  				Expect(sess.Out).To(PrintTable(ui.Table{
    62  					Headers: ui.TableRow{
    63  						{Contents: "name", Color: color.New(color.Bold)},
    64  						{Contents: "url", Color: color.New(color.Bold)},
    65  						{Contents: "team", Color: color.New(color.Bold)},
    66  						{Contents: "expiry", Color: color.New(color.Bold)},
    67  					},
    68  					Data: []ui.TableRow{
    69  						{{Contents: "another-test"}, {Contents: "https://example.com/another-test"}, {Contents: "test"}, {Contents: "Wed, 01 Jan 2020 00:00:00 UTC"}},
    70  						{{Contents: "no-token"}, {Contents: "https://example.com/no-token"}, {Contents: "main"}, {Contents: "n/a"}},
    71  						{{Contents: "omt"}, {Contents: "https://example.com/omt"}, {Contents: "main"}, {Contents: "Thu, 02 Jan 2020 00:00:00 UTC"}},
    72  						{{Contents: "test"}, {Contents: "https://example.com/test"}, {Contents: "test"}, {Contents: "Fri, 03 Jan 2020 00:00:00 UTC"}},
    73  					},
    74  				}))
    75  			})
    76  		})
    77  
    78  		Context("when the .flyrc contains a target with an invalid token", func() {
    79  			BeforeEach(func() {
    80  				targets = rc.Targets{
    81  					"test": {
    82  						API:   "https://example.com/test",
    83  						Token: &rc.TargetToken{Type: "Bearer", Value: "banana"},
    84  					},
    85  				}
    86  			})
    87  
    88  			It("indicates the token is invalid", func() {
    89  				sess, err := gexec.Start(flyCmd, GinkgoWriter, GinkgoWriter)
    90  				Expect(err).NotTo(HaveOccurred())
    91  
    92  				Eventually(sess).Should(gexec.Exit(0))
    93  
    94  				Expect(sess).Should(gbytes.Say("n/a: invalid token"))
    95  			})
    96  		})
    97  
    98  		Context("when no targets are available", func() {
    99  			BeforeEach(func() {
   100  				createFlyRc(nil)
   101  			})
   102  
   103  			It("prints an empty table", func() {
   104  				sess, err := gexec.Start(flyCmd, GinkgoWriter, GinkgoWriter)
   105  				Expect(err).NotTo(HaveOccurred())
   106  
   107  				Eventually(sess).Should(gexec.Exit(0))
   108  				Expect(sess.Out).To(PrintTable(ui.Table{
   109  					Headers: ui.TableRow{
   110  						{Contents: "name", Color: color.New(color.Bold)},
   111  						{Contents: "url", Color: color.New(color.Bold)},
   112  						{Contents: "team", Color: color.New(color.Bold)},
   113  						{Contents: "expiry", Color: color.New(color.Bold)},
   114  					},
   115  					Data: []ui.TableRow{}}))
   116  			})
   117  		})
   118  	})
   119  })