github.com/pf-qiu/concourse/v6@v6.7.3-0.20201207032516-1f455d73275f/fly/integration/unpause_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("Unpause 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/unpause", pipelineName, jobName)
    34  			queryParams = "instance_vars=%7B%22branch%22%3A%22master%22%7D"
    35  
    36  			flyCmd = exec.Command(flyPath, "-t", targetName, "unpause-job", "-j", fullJobName)
    37  		})
    38  
    39  		Context("when the job flag is provided", func() {
    40  			Context("when user and pipeline belong to the same team", func() {
    41  				Context("user is targeting the same team that the pipeline belongs to", func() {
    42  					BeforeEach(func() {
    43  						atcServer.AppendHandlers(
    44  							ghttp.CombineHandlers(
    45  								ghttp.VerifyRequest("PUT", apiPath, queryParams),
    46  								ghttp.RespondWith(http.StatusOK, nil),
    47  							),
    48  						)
    49  					})
    50  
    51  					It("successfully unpauses the job", func() {
    52  						sess, err := gexec.Start(flyCmd, GinkgoWriter, GinkgoWriter)
    53  						Expect(err).NotTo(HaveOccurred())
    54  
    55  						<-sess.Exited
    56  						Expect(sess.ExitCode()).To(Equal(0))
    57  						Eventually(sess).Should(gbytes.Say(fmt.Sprintf("unpaused '%s'\n", jobName)))
    58  					})
    59  				})
    60  
    61  				Context("user is NOT targeting the same team that the pipeline belongs to", func() {
    62  					BeforeEach(func() {
    63  						apiPath = fmt.Sprintf("/api/v1/teams/other-team/pipelines/%s/jobs/%s/unpause", pipelineName, jobName)
    64  
    65  						atcServer.AppendHandlers(
    66  							ghttp.CombineHandlers(
    67  								ghttp.VerifyRequest("GET", "/api/v1/teams/other-team"),
    68  								ghttp.RespondWithJSONEncoded(http.StatusOK, atc.Team{
    69  									Name: "other-team",
    70  								}),
    71  							),
    72  							ghttp.CombineHandlers(
    73  								ghttp.VerifyRequest("PUT", apiPath, queryParams),
    74  								ghttp.RespondWith(http.StatusOK, nil),
    75  							),
    76  						)
    77  					})
    78  
    79  					It("successfully unpauses the job", func() {
    80  						flyCmd = exec.Command(flyPath, "-t", targetName, "unpause-job", "-j", fullJobName, "--team", "other-team")
    81  						sess, err := gexec.Start(flyCmd, GinkgoWriter, GinkgoWriter)
    82  						Expect(err).NotTo(HaveOccurred())
    83  						<-sess.Exited
    84  						Expect(sess.ExitCode()).To(Equal(0))
    85  						Eventually(sess).Should(gbytes.Say(fmt.Sprintf("unpaused '%s'\n", jobName)))
    86  					})
    87  				})
    88  			})
    89  
    90  			Context("when unpause-job fails", func() {
    91  				BeforeEach(func() {
    92  					apiPath := fmt.Sprintf("/api/v1/teams/main/pipelines/%s/jobs/%s/unpause", pipelineName, jobName)
    93  					atcServer.AppendHandlers(
    94  						ghttp.CombineHandlers(
    95  							ghttp.VerifyRequest("PUT", apiPath, queryParams),
    96  							ghttp.RespondWith(http.StatusInternalServerError, nil),
    97  						),
    98  					)
    99  				})
   100  
   101  				It("exits 1 and outputs an error", func() {
   102  					sess, err := gexec.Start(flyCmd, GinkgoWriter, GinkgoWriter)
   103  					Expect(err).NotTo(HaveOccurred())
   104  
   105  					Eventually(sess.Err).Should(gbytes.Say(`error`))
   106  
   107  					<-sess.Exited
   108  					Expect(sess.ExitCode()).To(Equal(1))
   109  				})
   110  			})
   111  		})
   112  
   113  		Context("when the job flag is not provided", func() {
   114  			BeforeEach(func() {
   115  				flyCmd = exec.Command(flyPath, "-t", targetName, "unpause-job")
   116  			})
   117  
   118  			It("exits 1 and outputs an error", func() {
   119  				sess, err := gexec.Start(flyCmd, GinkgoWriter, GinkgoWriter)
   120  				Expect(err).NotTo(HaveOccurred())
   121  
   122  				Eventually(sess.Err).Should(gbytes.Say(`error`))
   123  
   124  				<-sess.Exited
   125  				Expect(sess.ExitCode()).To(Equal(1))
   126  			})
   127  		})
   128  	})
   129  })