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

     1  package integration_test
     2  
     3  import (
     4  	"fmt"
     5  	"io"
     6  	"net/http"
     7  	"os/exec"
     8  
     9  	"github.com/pf-qiu/concourse/v6/atc"
    10  	. "github.com/onsi/ginkgo"
    11  	. "github.com/onsi/gomega"
    12  	"github.com/onsi/gomega/gbytes"
    13  	"github.com/onsi/gomega/gexec"
    14  	"github.com/onsi/gomega/ghttp"
    15  )
    16  
    17  var _ = Describe("Fly CLI", func() {
    18  	Describe("destroy-pipeline", func() {
    19  		var (
    20  			stdin io.Writer
    21  			args  []string
    22  			sess  *gexec.Session
    23  		)
    24  
    25  		BeforeEach(func() {
    26  			stdin = nil
    27  			args = []string{}
    28  		})
    29  
    30  		JustBeforeEach(func() {
    31  			var err error
    32  
    33  			flyCmd := exec.Command(flyPath, append([]string{"-t", targetName, "destroy-pipeline"}, args...)...)
    34  			stdin, err = flyCmd.StdinPipe()
    35  			Expect(err).NotTo(HaveOccurred())
    36  
    37  			sess, err = gexec.Start(flyCmd, GinkgoWriter, GinkgoWriter)
    38  			Expect(err).NotTo(HaveOccurred())
    39  		})
    40  
    41  		Context("when a pipeline name is not specified", func() {
    42  			It("asks the user to specify a pipeline name", func() {
    43  				flyCmd := exec.Command(flyPath, "-t", targetName, "destroy-pipeline")
    44  
    45  				sess, err := gexec.Start(flyCmd, GinkgoWriter, GinkgoWriter)
    46  				Expect(err).NotTo(HaveOccurred())
    47  
    48  				Eventually(sess).Should(gexec.Exit(1))
    49  
    50  				Expect(sess.Err).To(gbytes.Say("error: the required flag `" + osFlag("p", "pipeline") + "' was not specified"))
    51  			})
    52  		})
    53  
    54  		Context("when the pipeline flag is invalid", func() {
    55  			It("fails and print invalid flag error", func() {
    56  				flyCmd := exec.Command(flyPath, "-t", targetName, "destroy-pipeline", "-p", "forbidden/pipelinename")
    57  
    58  				sess, err := gexec.Start(flyCmd, GinkgoWriter, GinkgoWriter)
    59  				Expect(err).NotTo(HaveOccurred())
    60  
    61  				<-sess.Exited
    62  				Expect(sess.ExitCode()).To(Equal(1))
    63  
    64  				Expect(sess.Err).To(gbytes.Say("error: invalid argument for flag `" + osFlag("p", "pipeline")))
    65  			})
    66  		})
    67  
    68  		Context("when a pipeline name is specified", func() {
    69  			BeforeEach(func() {
    70  				args = append(args, "-p", "some-pipeline/branch:master")
    71  			})
    72  
    73  			yes := func() {
    74  				Eventually(sess).Should(gbytes.Say(`are you sure\? \[yN\]: `))
    75  				fmt.Fprintf(stdin, "y\n")
    76  			}
    77  
    78  			no := func() {
    79  				Eventually(sess).Should(gbytes.Say(`are you sure\? \[yN\]: `))
    80  				fmt.Fprintf(stdin, "n\n")
    81  			}
    82  
    83  			queryParams := "instance_vars=%7B%22branch%22%3A%22master%22%7D"
    84  
    85  			It("warns that it's about to do bad things", func() {
    86  				Eventually(sess).Should(gbytes.Say("!!! this will remove all data for pipeline `some-pipeline/branch:master`"))
    87  			})
    88  
    89  			It("bails out if the user says no", func() {
    90  				no()
    91  				Eventually(sess).Should(gbytes.Say(`bailing out`))
    92  				Eventually(sess).Should(gexec.Exit(0))
    93  			})
    94  
    95  			Context("when the pipeline exists", func() {
    96  				BeforeEach(func() {
    97  					atcServer.AppendHandlers(
    98  						ghttp.CombineHandlers(
    99  							ghttp.VerifyRequest("DELETE", "/api/v1/teams/main/pipelines/some-pipeline", queryParams),
   100  							ghttp.RespondWith(204, ""),
   101  						),
   102  					)
   103  				})
   104  
   105  				It("succeeds if the user says yes", func() {
   106  					yes()
   107  					Eventually(sess).Should(gbytes.Say("`some-pipeline/branch:master` deleted"))
   108  					Eventually(sess).Should(gexec.Exit(0))
   109  				})
   110  
   111  				Context("when run noninteractively", func() {
   112  					BeforeEach(func() {
   113  						args = append(args, "-n")
   114  					})
   115  
   116  					It("destroys the pipeline without confirming", func() {
   117  						Eventually(sess).Should(gbytes.Say("`some-pipeline/branch:master` deleted"))
   118  						Eventually(sess).Should(gexec.Exit(0))
   119  					})
   120  				})
   121  			})
   122  
   123  			Context("and the pipeline does not exist", func() {
   124  				BeforeEach(func() {
   125  					atcServer.AppendHandlers(
   126  						ghttp.CombineHandlers(
   127  							ghttp.VerifyRequest("DELETE", "/api/v1/teams/main/pipelines/some-pipeline", queryParams),
   128  							ghttp.RespondWith(404, ""),
   129  						),
   130  					)
   131  				})
   132  
   133  				It("writes that it did not exist and exits successfully", func() {
   134  					yes()
   135  					Eventually(sess).Should(gbytes.Say("`some-pipeline/branch:master` does not exist"))
   136  					Eventually(sess).Should(gexec.Exit(0))
   137  				})
   138  			})
   139  
   140  			Context("and the api returns an unexpected status code", func() {
   141  				BeforeEach(func() {
   142  					atcServer.AppendHandlers(
   143  						ghttp.CombineHandlers(
   144  							ghttp.VerifyRequest("DELETE", "/api/v1/teams/main/pipelines/some-pipeline", queryParams),
   145  							ghttp.RespondWith(402, ""),
   146  						),
   147  					)
   148  				})
   149  
   150  				It("writes an error message to stderr", func() {
   151  					yes()
   152  					Eventually(sess.Err).Should(gbytes.Say("Unexpected Response"))
   153  					Eventually(sess).Should(gexec.Exit(1))
   154  				})
   155  			})
   156  
   157  			Context("with a team specified", func() {
   158  				BeforeEach(func() {
   159  					args = append(args, "--team", "team-two")
   160  
   161  					atcServer.AppendHandlers(
   162  						ghttp.CombineHandlers(
   163  							ghttp.VerifyRequest("GET", "/api/v1/teams/team-two"),
   164  							ghttp.RespondWithJSONEncoded(http.StatusOK, atc.Team{
   165  								Name: "team-two",
   166  							}),
   167  						),
   168  					)
   169  				})
   170  
   171  				It("warns that it's about to do bad things", func() {
   172  					Eventually(sess).Should(gbytes.Say("!!! this will remove all data for pipeline `some-pipeline/branch:master`"))
   173  				})
   174  
   175  				It("bails out if the user says no", func() {
   176  					no()
   177  					Eventually(sess).Should(gbytes.Say(`bailing out`))
   178  					Eventually(sess).Should(gexec.Exit(0))
   179  				})
   180  
   181  				Context("when the pipeline exists", func() {
   182  					BeforeEach(func() {
   183  						atcServer.AppendHandlers(
   184  							ghttp.CombineHandlers(
   185  								ghttp.VerifyRequest("DELETE", "/api/v1/teams/team-two/pipelines/some-pipeline", queryParams),
   186  								ghttp.RespondWith(204, ""),
   187  							),
   188  						)
   189  					})
   190  
   191  					It("succeeds if the user says yes", func() {
   192  						yes()
   193  						Eventually(sess).Should(gbytes.Say("`some-pipeline/branch:master` deleted"))
   194  						Eventually(sess).Should(gexec.Exit(0))
   195  					})
   196  				})
   197  
   198  				Context("and the pipeline does not exist", func() {
   199  					BeforeEach(func() {
   200  						atcServer.AppendHandlers(
   201  							ghttp.CombineHandlers(
   202  								ghttp.VerifyRequest("DELETE", "/api/v1/teams/team-two/pipelines/some-pipeline", queryParams),
   203  								ghttp.RespondWith(404, ""),
   204  							),
   205  						)
   206  					})
   207  
   208  					It("writes that it did not exist and exits successfully", func() {
   209  						yes()
   210  						Eventually(sess).Should(gbytes.Say("`some-pipeline/branch:master` does not exist"))
   211  						Eventually(sess).Should(gexec.Exit(0))
   212  					})
   213  				})
   214  			})
   215  		})
   216  	})
   217  })