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

     1  package integration_test
     2  
     3  import (
     4  	"fmt"
     5  	"net/http"
     6  	"os/exec"
     7  
     8  	"github.com/pf-qiu/concourse/v6/atc"
     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("Pause Job", func() {
    18  		var (
    19  			flyCmd       *exec.Cmd
    20  			pipelineName string
    21  			jobName      string
    22  			fullJobName  string
    23  			apiPath      string
    24  			queryParams  string
    25  			pipelineRef  atc.PipelineRef
    26  		)
    27  
    28  		BeforeEach(func() {
    29  			pipelineName = "pipeline"
    30  			jobName = "job-name-potato"
    31  			pipelineRef = atc.PipelineRef{Name: pipelineName, InstanceVars: atc.InstanceVars{"branch": "master"}}
    32  			fullJobName = fmt.Sprintf("%s/%s", pipelineRef.String(), jobName)
    33  			apiPath = fmt.Sprintf("/api/v1/teams/main/pipelines/%s/jobs/%s/pause", pipelineName, jobName)
    34  			queryParams = "instance_vars=%7B%22branch%22%3A%22master%22%7D"
    35  		})
    36  
    37  		Context("when user is on the same team as the given pipeline/job's team", func() {
    38  			BeforeEach(func() {
    39  				atcServer.AppendHandlers(
    40  					ghttp.CombineHandlers(
    41  						ghttp.VerifyRequest("PUT", apiPath, queryParams),
    42  						ghttp.RespondWith(http.StatusOK, nil),
    43  					),
    44  				)
    45  			})
    46  
    47  			It("successfully pauses the job", func() {
    48  				flyCmd = exec.Command(flyPath, "-t", targetName, "pause-job", "-j", fullJobName)
    49  				sess, err := gexec.Start(flyCmd, GinkgoWriter, GinkgoWriter)
    50  				Expect(err).NotTo(HaveOccurred())
    51  				<-sess.Exited
    52  				Expect(sess.ExitCode()).To(Equal(0))
    53  				Eventually(sess.Out.Contents).Should(ContainSubstring(fmt.Sprintf("paused '%s'\n", jobName)))
    54  			})
    55  		})
    56  
    57  		Context("user is NOT on the same team as the given pipeline/job's team", func() {
    58  			BeforeEach(func() {
    59  				atcServer.AppendHandlers(
    60  					ghttp.CombineHandlers(
    61  						ghttp.VerifyRequest("PUT", apiPath, queryParams),
    62  						ghttp.RespondWith(http.StatusForbidden, nil),
    63  					),
    64  				)
    65  			})
    66  
    67  			It("fails to pause the job", func() {
    68  				flyCmd = exec.Command(flyPath, "-t", targetName, "pause-job", "-j", fullJobName)
    69  				sess, err := gexec.Start(flyCmd, GinkgoWriter, GinkgoWriter)
    70  				Expect(err).NotTo(HaveOccurred())
    71  				<-sess.Exited
    72  				Expect(sess.ExitCode()).To(Equal(1))
    73  				Eventually(sess.Err.Contents).Should(ContainSubstring("error"))
    74  			})
    75  		})
    76  
    77  		Context("user is NOT currently targeted to the team the given pipeline/job belongs to", func() {
    78  			BeforeEach(func() {
    79  				apiPath = fmt.Sprintf("/api/v1/teams/other-team/pipelines/%s/jobs/%s/pause", pipelineName, jobName)
    80  
    81  				atcServer.AppendHandlers(
    82  					ghttp.CombineHandlers(
    83  						ghttp.VerifyRequest("GET", "/api/v1/teams/other-team"),
    84  						ghttp.RespondWithJSONEncoded(http.StatusOK, atc.Team{Name: "other-team"}),
    85  					),
    86  					ghttp.CombineHandlers(
    87  						ghttp.VerifyRequest("PUT", apiPath, queryParams),
    88  						ghttp.RespondWith(http.StatusOK, nil),
    89  					),
    90  				)
    91  			})
    92  
    93  			It("successfully pauses the job", func() {
    94  				flyCmd = exec.Command(flyPath, "-t", targetName, "pause-job", "-j", fullJobName, "--team", "other-team")
    95  				sess, err := gexec.Start(flyCmd, GinkgoWriter, GinkgoWriter)
    96  				Expect(err).NotTo(HaveOccurred())
    97  				<-sess.Exited
    98  				Expect(sess.ExitCode()).To(Equal(0))
    99  				Eventually(sess).Should(gbytes.Say(fmt.Sprintf("paused '%s'\n", jobName)))
   100  			})
   101  		})
   102  
   103  		Context("the pipeline/job does not exist", func() {
   104  			BeforeEach(func() {
   105  				atcServer.AppendHandlers(
   106  					ghttp.CombineHandlers(
   107  						ghttp.VerifyRequest("PUT", "/api/v1/teams/main/pipelines/doesnotexist-pipeline/jobs/doesnotexist-job/pause"),
   108  						ghttp.RespondWith(http.StatusNotFound, nil),
   109  					),
   110  				)
   111  			})
   112  
   113  			It("returns an error", func() {
   114  				flyCmd = exec.Command(flyPath, "-t", targetName, "pause-job", "-j", "doesnotexist-pipeline/doesnotexist-job")
   115  
   116  				sess, err := gexec.Start(flyCmd, GinkgoWriter, GinkgoWriter)
   117  				Expect(err).NotTo(HaveOccurred())
   118  				<-sess.Exited
   119  				Expect(sess.ExitCode()).To(Equal(1))
   120  				Eventually(sess.Err.Contents).Should(ContainSubstring("doesnotexist-pipeline/doesnotexist-job not found on team main"))
   121  			})
   122  		})
   123  
   124  		Context("when pause-job fails", func() {
   125  			BeforeEach(func() {
   126  				atcServer.AppendHandlers(
   127  					ghttp.CombineHandlers(
   128  						ghttp.VerifyRequest("PUT", apiPath, queryParams),
   129  						ghttp.RespondWith(http.StatusInternalServerError, nil),
   130  					),
   131  				)
   132  			})
   133  
   134  			It("exits 1 and outputs an error", func() {
   135  				flyCmd = exec.Command(flyPath, "-t", targetName, "pause-job", "-j", fullJobName)
   136  				sess, err := gexec.Start(flyCmd, GinkgoWriter, GinkgoWriter)
   137  				Expect(err).NotTo(HaveOccurred())
   138  				Eventually(sess.Err).Should(gbytes.Say(`error`))
   139  				<-sess.Exited
   140  				Expect(sess.ExitCode()).To(Equal(1))
   141  			})
   142  		})
   143  	})
   144  })