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

     1  package integration_test
     2  
     3  import (
     4  	"net/http"
     5  	"os/exec"
     6  
     7  	. "github.com/onsi/ginkgo"
     8  	. "github.com/onsi/ginkgo/extensions/table"
     9  	. "github.com/onsi/gomega"
    10  	"github.com/onsi/gomega/gbytes"
    11  	"github.com/onsi/gomega/gexec"
    12  	"github.com/onsi/gomega/ghttp"
    13  )
    14  
    15  var _ = Describe("Fly CLI", func() {
    16  	Describe("auth failures", func() {
    17  		var (
    18  			flyCmd *exec.Cmd
    19  		)
    20  
    21  		BeforeEach(func() {
    22  			flyCmd = exec.Command(flyPath, "-t", targetName, "containers")
    23  		})
    24  
    25  		Context("when a 401 response is received", func() {
    26  			BeforeEach(func() {
    27  				atcServer.AppendHandlers(
    28  					ghttp.CombineHandlers(
    29  						ghttp.VerifyRequest("GET", "/api/v1/teams/main/containers"),
    30  						ghttp.RespondWith(401, ""),
    31  					),
    32  				)
    33  			})
    34  
    35  			It("instructs the user to log in", func() {
    36  				sess, err := gexec.Start(flyCmd, GinkgoWriter, GinkgoWriter)
    37  				Expect(err).ToNot(HaveOccurred())
    38  
    39  				<-sess.Exited
    40  				Expect(sess.ExitCode()).To(Equal(1))
    41  
    42  				Expect(sess.Err).To(gbytes.Say("not authorized\\. run the following to log in:\n\n    "))
    43  				Expect(sess.Err).To(gbytes.Say(`fly.* -t ` + targetName + ` login`))
    44  			})
    45  		})
    46  	})
    47  
    48  	Describe("missing target", func() {
    49  		var (
    50  			flyCmd *exec.Cmd
    51  		)
    52  
    53  		BeforeEach(func() {
    54  			flyCmd = exec.Command(flyPath, "containers")
    55  		})
    56  
    57  		It("instructs the user to specify a target", func() {
    58  			sess, err := gexec.Start(flyCmd, GinkgoWriter, GinkgoWriter)
    59  			Expect(err).ToNot(HaveOccurred())
    60  
    61  			<-sess.Exited
    62  			Expect(sess.ExitCode()).To(Equal(1))
    63  
    64  			Expect(sess.Err).To(gbytes.Say("no target specified\\. specify the target with -t or log in like so:"))
    65  			Expect(sess.Err).To(gbytes.Say(`fly.* -t \(alias\) login -c \(concourse url\)`))
    66  		})
    67  	})
    68  
    69  	Describe("network errors", func() {
    70  		var (
    71  			flyCmd *exec.Cmd
    72  		)
    73  
    74  		BeforeEach(func() {
    75  			atcServer.Close()
    76  
    77  			flyCmd = exec.Command(flyPath, "-t", targetName, "containers")
    78  		})
    79  
    80  		It("tells the user a network error occurred, and that their target may be wrong, and makes fun of them", func() {
    81  			sess, err := gexec.Start(flyCmd, GinkgoWriter, GinkgoWriter)
    82  			Expect(err).ToNot(HaveOccurred())
    83  
    84  			<-sess.Exited
    85  			Expect(sess.ExitCode()).To(Equal(1))
    86  
    87  			Expect(sess.Err).To(gbytes.Say("could not reach the Concourse server called " + targetName))
    88  			Expect(sess.Err).To(gbytes.Say("lol"))
    89  		})
    90  	})
    91  
    92  	Context("when --team is set", func() {
    93  		DescribeTable("and the team does not exist",
    94  			func(flyCmd *exec.Cmd) {
    95  				atcServer.AppendHandlers(
    96  					ghttp.CombineHandlers(
    97  						ghttp.VerifyRequest("GET", "/api/v1/teams/doesnotexist"),
    98  						ghttp.RespondWith(http.StatusNotFound, nil),
    99  					),
   100  				)
   101  				flyCmd.Path = flyPath // idk why but the .Path is not getting set when we run exec.Command even though flyPath is available...
   102  				sess, err := gexec.Start(flyCmd, GinkgoWriter, GinkgoWriter)
   103  				Expect(err).NotTo(HaveOccurred())
   104  
   105  				Eventually(sess.Err.Contents).Should(ContainSubstring(`error: team 'doesnotexist' does not exist`))
   106  			},
   107  			Entry("trigger-job command returns an error",
   108  				exec.Command(flyPath, "-t", targetName, "trigger-job", "-j", "pipeline/job", "--team", "doesnotexist")),
   109  			Entry("hide-pipeline command returns an error",
   110  				exec.Command(flyPath, "-t", targetName, "hide-pipeline", "-p", "pipeline", "--team", "doesnotexist")),
   111  			Entry("hijack command returns an error",
   112  				exec.Command(flyPath, "-t", targetName, "hijack", "--handle", "container-id", "--team", "doesnotexist")),
   113  			Entry("jobs command returns an error",
   114  				exec.Command(flyPath, "-t", targetName, "jobs", "-p", "pipeline", "--team", "doesnotexist")),
   115  			Entry("pause-job command returns an error",
   116  				exec.Command(flyPath, "-t", targetName, "pause-job", "-j", "pipeline/job", "--team", "doesnotexist")),
   117  			Entry("pause-pipeline command returns an error",
   118  				exec.Command(flyPath, "-t", targetName, "pause-pipeline", "-p", "pipeline", "--team", "doesnotexist")),
   119  			Entry("unpause-job command returns an error",
   120  				exec.Command(flyPath, "-t", targetName, "unpause-job", "-j", "pipeline/job", "--team", "doesnotexist")),
   121  			Entry("unpause-pipeline command returns an error",
   122  				exec.Command(flyPath, "-t", targetName, "unpause-pipeline", "-p", "pipeline", "--team", "doesnotexist")),
   123  			Entry("set-pipeline command returns an error",
   124  				exec.Command(flyPath, "-t", targetName, "set-pipeline", "-p", "pipeline", "-c", "fixtures/testConfig.yml", "--team", "doesnotexist")),
   125  			Entry("destroy-pipeline command returns an error",
   126  				exec.Command(flyPath, "-t", targetName, "destroy-pipeline", "-p", "pipeline", "--team", "doesnotexist")),
   127  		)
   128  
   129  		DescribeTable("and you are NOT authorized to view the team",
   130  			func(flyCmd *exec.Cmd) {
   131  				atcServer.AppendHandlers(
   132  					ghttp.CombineHandlers(
   133  						ghttp.VerifyRequest("GET", "/api/v1/teams/other-team"),
   134  						ghttp.RespondWith(http.StatusForbidden, nil),
   135  					),
   136  				)
   137  				flyCmd.Path = flyPath // idk why but the .Path is not getting set when we run exec.Command even though flyPath is available...
   138  				sess, err := gexec.Start(flyCmd, GinkgoWriter, GinkgoWriter)
   139  				Expect(err).NotTo(HaveOccurred())
   140  
   141  				Eventually(sess.Err.Contents).Should(ContainSubstring(`error: you do not have a role on team 'other-team'`))
   142  			},
   143  			Entry("trigger-job command returns an error",
   144  				exec.Command(flyPath, "-t", targetName, "trigger-job", "-j", "pipeline/job", "--team", "other-team")),
   145  			Entry("hide-pipeline command returns an error",
   146  				exec.Command(flyPath, "-t", targetName, "hide-pipeline", "-p", "pipeline", "--team", "other-team")),
   147  			Entry("hijack command returns an error",
   148  				exec.Command(flyPath, "-t", targetName, "hijack", "--handle", "container-id", "--team", "other-team")),
   149  			Entry("jobs command returns an error",
   150  				exec.Command(flyPath, "-t", targetName, "jobs", "-p", "pipeline", "--team", "other-team")),
   151  			Entry("pause-job command returns an error",
   152  				exec.Command(flyPath, "-t", targetName, "pause-job", "-j", "pipeline/job", "--team", "other-team")),
   153  			Entry("pause-pipeline command returns an error",
   154  				exec.Command(flyPath, "-t", targetName, "pause-pipeline", "-p", "pipeline", "--team", "other-team")),
   155  			Entry("unpause-job command returns an error",
   156  				exec.Command(flyPath, "-t", targetName, "unpause-job", "-j", "pipeline/job", "--team", "other-team")),
   157  			Entry("unpause-pipeline command returns an error",
   158  				exec.Command(flyPath, "-t", targetName, "unpause-pipeline", "-p", "pipeline", "--team", "other-team")),
   159  			Entry("set-pipeline command returns an error",
   160  				exec.Command(flyPath, "-t", targetName, "set-pipeline", "-p", "pipeline", "-c", "fixtures/testConfig.yml", "--team", "other-team")),
   161  			Entry("destroy-pipeline command returns an error",
   162  				exec.Command(flyPath, "-t", targetName, "destroy-pipeline", "-p", "pipeline", "--team", "other-team")),
   163  		)
   164  	})
   165  })