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

     1  package integration_test
     2  
     3  import (
     4  	"net/http"
     5  	"os"
     6  	"os/exec"
     7  
     8  	. "github.com/onsi/ginkgo"
     9  	. "github.com/onsi/gomega"
    10  
    11  	"github.com/pf-qiu/concourse/v6/atc"
    12  	"github.com/onsi/gomega/gbytes"
    13  	"github.com/onsi/gomega/gexec"
    14  	"github.com/onsi/gomega/ghttp"
    15  	"github.com/tedsuo/rata"
    16  )
    17  
    18  var _ = Describe("Fly CLI", func() {
    19  	Describe("trigger-job", func() {
    20  		var (
    21  			mainPath        string
    22  			otherPath       string
    23  			otherRandomPath string
    24  			queryParams     string
    25  			err             error
    26  		)
    27  
    28  		BeforeEach(func() {
    29  			mainPath, err = atc.Routes.CreatePathForRoute(atc.CreateJobBuild, rata.Params{"pipeline_name": "awesome-pipeline", "job_name": "awesome-job", "team_name": "main"})
    30  			Expect(err).NotTo(HaveOccurred())
    31  
    32  			otherPath, err = atc.Routes.CreatePathForRoute(atc.CreateJobBuild, rata.Params{"pipeline_name": "awesome-pipeline", "job_name": "awesome-job", "team_name": "other-team"})
    33  			Expect(err).NotTo(HaveOccurred())
    34  
    35  			otherRandomPath, err = atc.Routes.CreatePathForRoute(atc.CreateJobBuild, rata.Params{"pipeline_name": "awesome-pipeline", "job_name": "awesome-job", "team_name": "random-team"})
    36  			Expect(err).NotTo(HaveOccurred())
    37  
    38  			queryParams = "instance_vars=%7B%22branch%22%3A%22master%22%7D"
    39  		})
    40  
    41  		Context("when the pipeline and job name are specified", func() {
    42  			Context("when the pipeline and job exists", func() {
    43  				Context("user and pipeline are part of the main team", func() {
    44  					Context("user is targeting the same team that the pipeline belongs to", func() {
    45  
    46  						BeforeEach(func() {
    47  							atcServer.AppendHandlers(
    48  								ghttp.CombineHandlers(
    49  									ghttp.VerifyRequest("POST", mainPath, queryParams),
    50  									ghttp.RespondWithJSONEncoded(http.StatusOK, atc.Build{ID: 57, Name: "42"}),
    51  								),
    52  							)
    53  						})
    54  
    55  						It("starts the build", func() {
    56  							flyCmd := exec.Command(flyPath, "-t", targetName, "trigger-job", "-j", "awesome-pipeline/branch:master/awesome-job")
    57  
    58  							sess, err := gexec.Start(flyCmd, GinkgoWriter, GinkgoWriter)
    59  							Expect(err).NotTo(HaveOccurred())
    60  
    61  							Eventually(sess).Should(gbytes.Say(`started awesome-pipeline/branch:master/awesome-job #42`))
    62  
    63  							<-sess.Exited
    64  							Expect(sess.ExitCode()).To(Equal(0))
    65  						})
    66  					})
    67  
    68  					Context("user is NOT targeting the same team that the pipeline belongs to", func() {
    69  
    70  						BeforeEach(func() {
    71  							atcServer.AppendHandlers(
    72  								ghttp.CombineHandlers(
    73  									ghttp.VerifyRequest("GET", "/api/v1/teams/other-team"),
    74  									ghttp.RespondWithJSONEncoded(http.StatusOK, atc.Team{
    75  										Name: "other-team",
    76  									}),
    77  								),
    78  								ghttp.CombineHandlers(
    79  									ghttp.VerifyRequest("POST", otherPath),
    80  									ghttp.RespondWithJSONEncoded(http.StatusOK, atc.Build{ID: 57, Name: "42"}),
    81  								),
    82  							)
    83  						})
    84  
    85  						It("starts the build", func() {
    86  							flyCmd := exec.Command(flyPath, "-t", targetName, "trigger-job", "-j", "awesome-pipeline/awesome-job", "--team", "other-team")
    87  
    88  							sess, err := gexec.Start(flyCmd, GinkgoWriter, GinkgoWriter)
    89  							Expect(err).NotTo(HaveOccurred())
    90  
    91  							Eventually(sess).Should(gbytes.Say(`started awesome-pipeline/awesome-job #42`))
    92  
    93  							<-sess.Exited
    94  							Expect(sess.ExitCode()).To(Equal(0))
    95  						})
    96  
    97  					})
    98  				})
    99  
   100  				Context("when -w option is provided", func() {
   101  					var streaming chan struct{}
   102  					var events chan atc.Event
   103  
   104  					BeforeEach(func() {
   105  						streaming = make(chan struct{})
   106  						events = make(chan atc.Event)
   107  						atcServer.AppendHandlers(
   108  							ghttp.CombineHandlers(
   109  								ghttp.VerifyRequest("POST", mainPath),
   110  								ghttp.RespondWithJSONEncoded(http.StatusOK, atc.Build{ID: 57, Name: "42"}),
   111  							),
   112  							BuildEventsHandler(57, streaming, events),
   113  						)
   114  					})
   115  
   116  					It("watches the build", func() {
   117  						flyCmd := exec.Command(flyPath, "-t", targetName, "trigger-job", "-j", "awesome-pipeline/awesome-job", "-w")
   118  
   119  						sess, err := gexec.Start(flyCmd, GinkgoWriter, GinkgoWriter)
   120  						Expect(err).NotTo(HaveOccurred())
   121  						Eventually(sess).Should(gbytes.Say(`started awesome-pipeline/awesome-job #42`))
   122  
   123  						AssertEvents(sess, streaming, events)
   124  					})
   125  				})
   126  			})
   127  
   128  			Context("when the pipeline/job doesn't exist", func() {
   129  				BeforeEach(func() {
   130  					atcServer.AppendHandlers(
   131  						ghttp.CombineHandlers(
   132  							ghttp.VerifyRequest("GET", "/api/v1/teams/random-team"),
   133  							ghttp.RespondWithJSONEncoded(http.StatusOK, atc.Team{
   134  								Name: "random-team",
   135  								ID:   0,
   136  								Auth: atc.TeamAuth{},
   137  							}),
   138  						),
   139  						ghttp.CombineHandlers(
   140  							ghttp.VerifyRequest("POST", otherRandomPath),
   141  							ghttp.RespondWith(http.StatusNotFound, nil),
   142  						),
   143  					)
   144  				})
   145  
   146  				It("prints an error message", func() {
   147  					flyCmd := exec.Command(flyPath, "-t", targetName, "trigger-job", "-j", "awesome-pipeline/awesome-job", "--team", "random-team")
   148  
   149  					sess, err := gexec.Start(flyCmd, GinkgoWriter, GinkgoWriter)
   150  					Expect(err).NotTo(HaveOccurred())
   151  
   152  					Eventually(sess.Err).Should(gbytes.Say(`error: resource not found`))
   153  
   154  					<-sess.Exited
   155  					Expect(sess.ExitCode()).To(Equal(1))
   156  				})
   157  			})
   158  		})
   159  
   160  		Context("when the pipeline/job name is not specified", func() {
   161  			It("errors", func() {
   162  				flyCmd := exec.Command(flyPath, "-t", targetName, "trigger-job")
   163  
   164  				sess, err := gexec.Start(flyCmd, GinkgoWriter, GinkgoWriter)
   165  				Expect(err).NotTo(HaveOccurred())
   166  
   167  				<-sess.Exited
   168  				Expect(sess.ExitCode()).To(Equal(1))
   169  			})
   170  		})
   171  
   172  		Context("completion", func() {
   173  			BeforeEach(func() {
   174  				os.Setenv("GO_FLAGS_COMPLETION", "1")
   175  			})
   176  
   177  			AfterEach(func() {
   178  				os.Unsetenv("GO_FLAGS_COMPLETION")
   179  			})
   180  
   181  			It("returns all matching pipelines", func() {
   182  				atcServer.AppendHandlers(
   183  					ghttp.CombineHandlers(
   184  						ghttp.VerifyRequest("GET", "/api/v1/teams/main/pipelines"),
   185  						ghttp.RespondWithJSONEncoded(200, []atc.Pipeline{
   186  							{Name: "some-pipeline-1", Paused: false, Public: false},
   187  							{Name: "some-pipeline-2", Paused: false, Public: false},
   188  							{Name: "another-pipeline", Paused: false, Public: false},
   189  						}),
   190  					),
   191  				)
   192  
   193  				flyCmd := exec.Command(flyPath, "-t", targetName, "trigger-job", "-j", "some-")
   194  				sess, err := gexec.Start(flyCmd, GinkgoWriter, GinkgoWriter)
   195  				Expect(err).NotTo(HaveOccurred())
   196  				Eventually(sess).Should(gexec.Exit(0))
   197  				Eventually(sess.Out).Should(gbytes.Say("some-pipeline-1/"))
   198  				Eventually(sess.Out).Should(gbytes.Say("some-pipeline-2/"))
   199  				Eventually(sess.Out).ShouldNot(gbytes.Say("another-pipeline/"))
   200  			})
   201  
   202  			It("returns all matching pipeline instances", func() {
   203  				atcServer.AppendHandlers(
   204  					ghttp.CombineHandlers(
   205  						ghttp.VerifyRequest("GET", "/api/v1/teams/main/pipelines"),
   206  						ghttp.RespondWithJSONEncoded(200, []atc.Pipeline{
   207  							{Name: "some-pipeline", InstanceVars: atc.InstanceVars{"branch": "master"}, Paused: false, Public: false},
   208  							{Name: "some-pipeline", InstanceVars: atc.InstanceVars{"branch": "feature"}, Paused: false, Public: false},
   209  						}),
   210  					),
   211  				)
   212  
   213  				flyCmd := exec.Command(flyPath, "-t", targetName, "trigger-job", "-j", "some-pipeline/branch:f")
   214  				sess, err := gexec.Start(flyCmd, GinkgoWriter, GinkgoWriter)
   215  				Expect(err).NotTo(HaveOccurred())
   216  				Eventually(sess).Should(gexec.Exit(0))
   217  				Eventually(sess.Out).Should(gbytes.Say("some-pipeline/branch:feature"))
   218  				Eventually(sess.Out).ShouldNot(gbytes.Say("some-pipeline/branch:master"))
   219  			})
   220  
   221  			It("returns all matching jobs", func() {
   222  				atcServer.AppendHandlers(
   223  					ghttp.CombineHandlers(
   224  						ghttp.VerifyRequest("GET", "/api/v1/teams/main/pipelines"),
   225  						ghttp.RespondWithJSONEncoded(200, []atc.Pipeline{
   226  							{Name: "some-pipeline-1", Paused: false, Public: false},
   227  							{Name: "some-pipeline-2", Paused: false, Public: false},
   228  							{Name: "another-pipeline", Paused: false, Public: false},
   229  						}),
   230  					),
   231  				)
   232  				atcServer.AppendHandlers(
   233  					ghttp.CombineHandlers(
   234  						ghttp.VerifyRequest("GET", "/api/v1/teams/main/pipelines/some-pipeline/jobs"),
   235  						ghttp.RespondWithJSONEncoded(200, []atc.Job{
   236  							{Name: "some-job-1"},
   237  							{Name: "some-job-2"},
   238  							{Name: "another-job"},
   239  						}),
   240  					),
   241  				)
   242  
   243  				flyCmd := exec.Command(flyPath, "-t", targetName, "trigger-job", "-j", "some-pipeline/some-")
   244  				sess, err := gexec.Start(flyCmd, GinkgoWriter, GinkgoWriter)
   245  				Expect(err).NotTo(HaveOccurred())
   246  				Eventually(sess).Should(gexec.Exit(0))
   247  				Eventually(sess.Out).Should(gbytes.Say("some-pipeline/some-job-1"))
   248  				Eventually(sess.Out).Should(gbytes.Say("some-pipeline/some-job-2"))
   249  				Eventually(sess.Out).ShouldNot(gbytes.Say("some-pipeline/another-job"))
   250  			})
   251  
   252  			It("returns all matching pipeline instance jobs", func() {
   253  				atcServer.AppendHandlers(
   254  					ghttp.CombineHandlers(
   255  						ghttp.VerifyRequest("GET", "/api/v1/teams/main/pipelines/some-pipeline/jobs", queryParams),
   256  						ghttp.RespondWithJSONEncoded(200, []atc.Job{
   257  							{Name: "some-job-1"},
   258  							{Name: "some-job-2"},
   259  							{Name: "another-job"},
   260  						}),
   261  					),
   262  				)
   263  
   264  				flyCmd := exec.Command(flyPath, "-t", targetName, "trigger-job", "-j", "some-pipeline/branch:master/some-")
   265  				sess, err := gexec.Start(flyCmd, GinkgoWriter, GinkgoWriter)
   266  				Expect(err).NotTo(HaveOccurred())
   267  				Eventually(sess).Should(gexec.Exit(0))
   268  				Eventually(sess.Out).Should(gbytes.Say("some-pipeline/branch:master/some-job-1"))
   269  				Eventually(sess.Out).Should(gbytes.Say("some-pipeline/branch:master/some-job-2"))
   270  				Eventually(sess.Out).ShouldNot(gbytes.Say("some-pipeline/branch:master/another-job"))
   271  			})
   272  		})
   273  	})
   274  })