github.com/pf-qiu/concourse/v6@v6.7.3-0.20201207032516-1f455d73275f/fly/integration/abort_build_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/gomega"
     9  
    10  	"github.com/onsi/gomega/gbytes"
    11  	"github.com/onsi/gomega/gexec"
    12  	"github.com/onsi/gomega/ghttp"
    13  
    14  	"github.com/pf-qiu/concourse/v6/atc"
    15  )
    16  
    17  var _ = Describe("AbortBuild", func() {
    18  	var expectedAbortURL = "/api/v1/builds/23/abort"
    19  
    20  	var expectedBuild = atc.Build{
    21  		ID:      23,
    22  		Name:    "42",
    23  		Status:  "running",
    24  		JobName: "myjob",
    25  		APIURL:  "api/v1/builds/123",
    26  	}
    27  
    28  	Context("when the job name is not specified", func() {
    29  		Context("and the build id is specified", func() {
    30  			BeforeEach(func() {
    31  				expectedURL := "/api/v1/builds/23"
    32  
    33  				atcServer.AppendHandlers(
    34  					ghttp.CombineHandlers(
    35  						ghttp.VerifyRequest("GET", expectedURL),
    36  						ghttp.RespondWithJSONEncoded(http.StatusOK, expectedBuild),
    37  					),
    38  
    39  					ghttp.CombineHandlers(
    40  						ghttp.VerifyRequest("PUT", expectedAbortURL),
    41  						ghttp.RespondWith(http.StatusNoContent, ""),
    42  					),
    43  				)
    44  			})
    45  
    46  			It("aborts the build", func() {
    47  				Expect(func() {
    48  					flyCmd := exec.Command(flyPath, "-t", targetName, "abort-build", "-b", "23")
    49  
    50  					sess, err := gexec.Start(flyCmd, GinkgoWriter, GinkgoWriter)
    51  					Expect(err).NotTo(HaveOccurred())
    52  
    53  					Eventually(sess).Should(gexec.Exit(0))
    54  
    55  					Expect(sess.Out).To(gbytes.Say("build successfully aborted"))
    56  				}).To(Change(func() int {
    57  					return len(atcServer.ReceivedRequests())
    58  				}).By(3))
    59  			})
    60  		})
    61  
    62  		Context("and the build id does not exist", func() {
    63  			BeforeEach(func() {
    64  				expectedURL := "/api/v1/builds/42"
    65  
    66  				atcServer.AppendHandlers(
    67  					ghttp.CombineHandlers(
    68  						ghttp.VerifyRequest("GET", expectedURL),
    69  						ghttp.RespondWith(http.StatusNotFound, ""),
    70  					),
    71  				)
    72  			})
    73  
    74  			It("asks the user to specify a build id", func() {
    75  				flyCmd := exec.Command(flyPath, "-t", targetName, "abort-build", "-b", "42")
    76  
    77  				sess, err := gexec.Start(flyCmd, GinkgoWriter, GinkgoWriter)
    78  				Expect(err).NotTo(HaveOccurred())
    79  
    80  				Eventually(sess).Should(gexec.Exit(1))
    81  
    82  				Expect(sess.Err).To(gbytes.Say("error: build does not exist"))
    83  			})
    84  		})
    85  
    86  		Context("and the build id is not specified", func() {
    87  			It("asks the user to specify a build id", func() {
    88  				flyCmd := exec.Command(flyPath, "-t", targetName, "abort-build")
    89  
    90  				sess, err := gexec.Start(flyCmd, GinkgoWriter, GinkgoWriter)
    91  				Expect(err).NotTo(HaveOccurred())
    92  
    93  				Eventually(sess).Should(gexec.Exit(1))
    94  
    95  				Expect(sess.Err).To(gbytes.Say("error: the required flag `" + osFlag("b", "build") + "' was not specified"))
    96  			})
    97  		})
    98  	})
    99  
   100  	Context("when the pipeline/build exists", func() {
   101  		Context("and the build name is specified", func() {
   102  			BeforeEach(func() {
   103  				expectedURL := "/api/v1/teams/main/pipelines/my-pipeline/jobs/my-job/builds/42"
   104  
   105  				atcServer.AppendHandlers(
   106  					ghttp.CombineHandlers(
   107  						ghttp.VerifyRequest("GET", expectedURL, "instance_vars=%7B%22branch%22%3A%22master%22%7D"),
   108  						ghttp.RespondWithJSONEncoded(http.StatusOK, expectedBuild),
   109  					),
   110  
   111  					ghttp.CombineHandlers(
   112  						ghttp.VerifyRequest("PUT", expectedAbortURL),
   113  						ghttp.RespondWith(http.StatusNoContent, ""),
   114  					),
   115  				)
   116  			})
   117  
   118  			It("aborts the build", func() {
   119  				Expect(func() {
   120  					flyCmd := exec.Command(flyPath, "-t", targetName, "abort-build", "-j", "my-pipeline/branch:master/my-job", "-b", "42")
   121  
   122  					sess, err := gexec.Start(flyCmd, GinkgoWriter, GinkgoWriter)
   123  					Expect(err).NotTo(HaveOccurred())
   124  
   125  					Eventually(sess).Should(gexec.Exit(0))
   126  
   127  					Expect(sess.Out).To(gbytes.Say("build successfully aborted"))
   128  				}).To(Change(func() int {
   129  					return len(atcServer.ReceivedRequests())
   130  				}).By(3))
   131  			})
   132  		})
   133  
   134  		Context("and the build number does not exist", func() {
   135  			BeforeEach(func() {
   136  				expectedURL := "/api/v1/teams/main/pipelines/my-pipeline/jobs/my-job/builds/23"
   137  
   138  				atcServer.AppendHandlers(
   139  					ghttp.CombineHandlers(
   140  						ghttp.VerifyRequest("GET", expectedURL),
   141  						ghttp.RespondWith(http.StatusNotFound, ""),
   142  					),
   143  				)
   144  			})
   145  
   146  			It("asks the user to specify a build name", func() {
   147  				flyCmd := exec.Command(flyPath, "-t", targetName, "abort-build", "-j", "my-pipeline/my-job", "-b", "23")
   148  
   149  				sess, err := gexec.Start(flyCmd, GinkgoWriter, GinkgoWriter)
   150  				Expect(err).NotTo(HaveOccurred())
   151  
   152  				Eventually(sess).Should(gexec.Exit(1))
   153  
   154  				Expect(sess.Err).To(gbytes.Say("error: build does not exist"))
   155  			})
   156  		})
   157  
   158  		Context("and the build name is not specified", func() {
   159  			It("asks the user to specify a build name", func() {
   160  				flyCmd := exec.Command(flyPath, "-t", targetName, "abort-build", "-j", "some-pipeline-name/some-job-name")
   161  
   162  				sess, err := gexec.Start(flyCmd, GinkgoWriter, GinkgoWriter)
   163  				Expect(err).NotTo(HaveOccurred())
   164  
   165  				Eventually(sess).Should(gexec.Exit(1))
   166  
   167  				Expect(sess.Err).To(gbytes.Say("error: the required flag `" + osFlag("b", "build") + "' was not specified"))
   168  			})
   169  		})
   170  	})
   171  
   172  	Context("when the build or pipeline does not exist", func() {
   173  		BeforeEach(func() {
   174  			expectedJobBuildURL := "/api/v1/teams/main/pipelines/my-pipeline/jobs/my-job/builds/42"
   175  
   176  			atcServer.AppendHandlers(
   177  				ghttp.CombineHandlers(
   178  					ghttp.VerifyRequest("GET", expectedJobBuildURL),
   179  					ghttp.RespondWith(http.StatusNotFound, "{}"),
   180  				),
   181  			)
   182  		})
   183  
   184  		It("returns a helpful error message", func() {
   185  			Expect(func() {
   186  				flyCmd := exec.Command(flyPath, "-t", targetName, "abort-build", "-j", "my-pipeline/my-job", "-b", "42")
   187  				sess, err := gexec.Start(flyCmd, GinkgoWriter, GinkgoWriter)
   188  				Expect(err).NotTo(HaveOccurred())
   189  
   190  				Eventually(sess).Should(gexec.Exit(1))
   191  				Expect(sess.Err).To(gbytes.Say("error: build does not exist"))
   192  			}).To(Change(func() int {
   193  				return len(atcServer.ReceivedRequests())
   194  			}).By(2))
   195  		})
   196  	})
   197  })