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

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