github.com/chenbh/concourse/v6@v6.4.2/fly/integration/destroy_pipeline_test.go (about)

     1  package integration_test
     2  
     3  import (
     4  	"fmt"
     5  	"github.com/chenbh/concourse/v6/atc"
     6  	"io"
     7  	"net/http"
     8  	"os/exec"
     9  
    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 specifying a pipeline name with a '/' character", func() {
    55  			It("fails and says '/' characters are not allowed", 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: pipeline name cannot contain '/'"))
    65  			})
    66  		})
    67  
    68  		Context("when a pipeline name is specified", func() {
    69  			BeforeEach(func() {
    70  				args = append(args, "-p", "some-pipeline")
    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  			It("warns that it's about to do bad things", func() {
    84  				Eventually(sess).Should(gbytes.Say("!!! this will remove all data for pipeline `some-pipeline`"))
    85  			})
    86  
    87  			It("bails out if the user says no", func() {
    88  				no()
    89  				Eventually(sess).Should(gbytes.Say(`bailing out`))
    90  				Eventually(sess).Should(gexec.Exit(0))
    91  			})
    92  
    93  			Context("when the pipeline exists", func() {
    94  				BeforeEach(func() {
    95  					atcServer.AppendHandlers(
    96  						ghttp.CombineHandlers(
    97  							ghttp.VerifyRequest("DELETE", "/api/v1/teams/main/pipelines/some-pipeline"),
    98  							ghttp.RespondWith(204, ""),
    99  						),
   100  					)
   101  				})
   102  
   103  				It("succeeds if the user says yes", func() {
   104  					yes()
   105  					Eventually(sess).Should(gbytes.Say("`some-pipeline` deleted"))
   106  					Eventually(sess).Should(gexec.Exit(0))
   107  				})
   108  
   109  				Context("when run noninteractively", func() {
   110  					BeforeEach(func() {
   111  						args = append(args, "-n")
   112  					})
   113  
   114  					It("destroys the pipeline without confirming", func() {
   115  						Eventually(sess).Should(gbytes.Say("`some-pipeline` deleted"))
   116  						Eventually(sess).Should(gexec.Exit(0))
   117  					})
   118  				})
   119  			})
   120  
   121  			Context("and the pipeline does not exist", func() {
   122  				BeforeEach(func() {
   123  					atcServer.AppendHandlers(
   124  						ghttp.CombineHandlers(
   125  							ghttp.VerifyRequest("DELETE", "/api/v1/teams/main/pipelines/some-pipeline"),
   126  							ghttp.RespondWith(404, ""),
   127  						),
   128  					)
   129  				})
   130  
   131  				It("writes that it did not exist and exits successfully", func() {
   132  					yes()
   133  					Eventually(sess).Should(gbytes.Say("`some-pipeline` does not exist"))
   134  					Eventually(sess).Should(gexec.Exit(0))
   135  				})
   136  			})
   137  
   138  			Context("and the api returns an unexpected status code", func() {
   139  				BeforeEach(func() {
   140  					atcServer.AppendHandlers(
   141  						ghttp.CombineHandlers(
   142  							ghttp.VerifyRequest("DELETE", "/api/v1/teams/main/pipelines/some-pipeline"),
   143  							ghttp.RespondWith(402, ""),
   144  						),
   145  					)
   146  				})
   147  
   148  				It("writes an error message to stderr", func() {
   149  					yes()
   150  					Eventually(sess.Err).Should(gbytes.Say("Unexpected Response"))
   151  					Eventually(sess).Should(gexec.Exit(1))
   152  				})
   153  			})
   154  
   155  			Context("with a team specified", func() {
   156  				BeforeEach(func() {
   157  					args = append(args, "--team", "team-two")
   158  
   159  					atcServer.AppendHandlers(
   160  						ghttp.CombineHandlers(
   161  							ghttp.VerifyRequest("GET", "/api/v1/teams/team-two"),
   162  							ghttp.RespondWithJSONEncoded(http.StatusOK, atc.Team{
   163  								Name: "team-two",
   164  							}),
   165  						),
   166  					)
   167  				})
   168  
   169  				It("warns that it's about to do bad things", func() {
   170  					Eventually(sess).Should(gbytes.Say("!!! this will remove all data for pipeline `some-pipeline`"))
   171  				})
   172  
   173  				It("bails out if the user says no", func() {
   174  					no()
   175  					Eventually(sess).Should(gbytes.Say(`bailing out`))
   176  					Eventually(sess).Should(gexec.Exit(0))
   177  				})
   178  
   179  				Context("when the pipeline exists", func() {
   180  					BeforeEach(func() {
   181  						atcServer.AppendHandlers(
   182  							ghttp.CombineHandlers(
   183  								ghttp.VerifyRequest("DELETE", "/api/v1/teams/team-two/pipelines/some-pipeline"),
   184  								ghttp.RespondWith(204, ""),
   185  							),
   186  						)
   187  					})
   188  
   189  					It("succeeds if the user says yes", func() {
   190  						yes()
   191  						Eventually(sess).Should(gbytes.Say("`some-pipeline` deleted"))
   192  						Eventually(sess).Should(gexec.Exit(0))
   193  					})
   194  				})
   195  
   196  				Context("and the pipeline does not exist", func() {
   197  					BeforeEach(func() {
   198  						atcServer.AppendHandlers(
   199  							ghttp.CombineHandlers(
   200  								ghttp.VerifyRequest("DELETE", "/api/v1/teams/team-two/pipelines/some-pipeline"),
   201  								ghttp.RespondWith(404, ""),
   202  							),
   203  						)
   204  					})
   205  
   206  					It("writes that it did not exist and exits successfully", func() {
   207  						yes()
   208  						Eventually(sess).Should(gbytes.Say("`some-pipeline` does not exist"))
   209  						Eventually(sess).Should(gexec.Exit(0))
   210  					})
   211  				})
   212  			})
   213  		})
   214  	})
   215  })