github.com/pf-qiu/concourse/v6@v6.7.3-0.20201207032516-1f455d73275f/fly/integration/archive_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/fly/ui"
    10  	"github.com/fatih/color"
    11  
    12  	. "github.com/onsi/ginkgo"
    13  	. "github.com/onsi/gomega"
    14  
    15  	"github.com/pf-qiu/concourse/v6/atc"
    16  	"github.com/onsi/gomega/gbytes"
    17  	"github.com/onsi/gomega/gexec"
    18  	"github.com/onsi/gomega/ghttp"
    19  	"github.com/tedsuo/rata"
    20  )
    21  
    22  var _ = Describe("Fly CLI", func() {
    23  	yes := func(stdin io.Writer) {
    24  		fmt.Fprintf(stdin, "y\n")
    25  	}
    26  
    27  	no := func(stdin io.Writer) {
    28  		fmt.Fprintf(stdin, "n\n")
    29  	}
    30  
    31  	Describe("archive-pipeline", func() {
    32  		Context("when the pipeline name is specified", func() {
    33  			var (
    34  				path        string
    35  				queryParams string
    36  				err         error
    37  			)
    38  
    39  			BeforeEach(func() {
    40  				path, err = atc.Routes.CreatePathForRoute(atc.ArchivePipeline, rata.Params{"pipeline_name": "awesome-pipeline", "team_name": "main"})
    41  				Expect(err).NotTo(HaveOccurred())
    42  
    43  				queryParams = "instance_vars=%7B%22branch%22%3A%22master%22%7D"
    44  			})
    45  
    46  			Context("when the pipeline exists", func() {
    47  				BeforeEach(func() {
    48  					atcServer.AppendHandlers(
    49  						ghttp.CombineHandlers(
    50  							ghttp.VerifyRequest("PUT", path, queryParams),
    51  							ghttp.RespondWith(http.StatusOK, nil),
    52  						),
    53  					)
    54  				})
    55  
    56  				Context("when the user confirms", func() {
    57  					It("archives the pipeline", func() {
    58  						Expect(func() {
    59  							flyCmd := exec.Command(flyPath, "-t", targetName, "archive-pipeline", "-p", "awesome-pipeline/branch:master")
    60  							stdin, err := flyCmd.StdinPipe()
    61  							Expect(err).NotTo(HaveOccurred())
    62  
    63  							sess, err := gexec.Start(flyCmd, GinkgoWriter, GinkgoWriter)
    64  							Expect(err).NotTo(HaveOccurred())
    65  
    66  							Eventually(sess).Should(gbytes.Say("!!! archiving the pipeline will remove its configuration. Build history will be retained.\n\n"))
    67  							Eventually(sess).Should(gbytes.Say("archive pipeline 'awesome-pipeline/branch:master'?"))
    68  							yes(stdin)
    69  
    70  							Eventually(sess).Should(gbytes.Say(`archived 'awesome-pipeline/branch:master'`))
    71  
    72  							<-sess.Exited
    73  							Expect(sess.ExitCode()).To(Equal(0))
    74  						}).To(Change(func() int {
    75  							return len(atcServer.ReceivedRequests())
    76  						}).By(2))
    77  					})
    78  				})
    79  
    80  				Context("when the user declines", func() {
    81  					It("does not archive the pipelines", func() {
    82  						Expect(func() {
    83  							flyCmd := exec.Command(flyPath, "-t", targetName, "archive-pipeline", "-p", "awesome-pipeline/branch:master")
    84  							stdin, err := flyCmd.StdinPipe()
    85  							Expect(err).NotTo(HaveOccurred())
    86  
    87  							sess, err := gexec.Start(flyCmd, GinkgoWriter, GinkgoWriter)
    88  							Expect(err).NotTo(HaveOccurred())
    89  
    90  							Eventually(sess).Should(gbytes.Say("archive pipeline 'awesome-pipeline/branch:master'?"))
    91  							no(stdin)
    92  
    93  							Eventually(sess).Should(gbytes.Say(`bailing out`))
    94  
    95  							<-sess.Exited
    96  							Expect(sess.ExitCode()).To(Equal(0))
    97  						}).To(Change(func() int {
    98  							return len(atcServer.ReceivedRequests())
    99  						}).By(1))
   100  					})
   101  				})
   102  
   103  				Context("when running in non-interactive mode", func() {
   104  					It("does not prompt the user", func() {
   105  						Expect(func() {
   106  							flyCmd := exec.Command(flyPath, "-t", targetName, "archive-pipeline", "-n", "-p", "awesome-pipeline/branch:master")
   107  
   108  							sess, err := gexec.Start(flyCmd, GinkgoWriter, GinkgoWriter)
   109  							Expect(err).NotTo(HaveOccurred())
   110  
   111  							Eventually(sess).Should(gbytes.Say(`archived 'awesome-pipeline/branch:master'`))
   112  
   113  							<-sess.Exited
   114  							Expect(sess.ExitCode()).To(Equal(0))
   115  						}).To(Change(func() int {
   116  							return len(atcServer.ReceivedRequests())
   117  						}).By(2))
   118  					})
   119  				})
   120  			})
   121  
   122  			Context("when the pipeline doesn't exist", func() {
   123  				BeforeEach(func() {
   124  					atcServer.AppendHandlers(
   125  						ghttp.CombineHandlers(
   126  							ghttp.VerifyRequest("PUT", path),
   127  							ghttp.RespondWith(http.StatusNotFound, nil),
   128  						),
   129  					)
   130  				})
   131  
   132  				It("prints helpful message", func() {
   133  					Expect(func() {
   134  						flyCmd := exec.Command(flyPath, "-t", targetName, "archive-pipeline", "-n", "-p", "awesome-pipeline")
   135  
   136  						sess, err := gexec.Start(flyCmd, GinkgoWriter, GinkgoWriter)
   137  						Expect(err).NotTo(HaveOccurred())
   138  
   139  						Eventually(sess.Err).Should(gbytes.Say(`pipeline 'awesome-pipeline' not found`))
   140  
   141  						<-sess.Exited
   142  						Expect(sess.ExitCode()).To(Equal(1))
   143  					}).To(Change(func() int {
   144  						return len(atcServer.ReceivedRequests())
   145  					}).By(2))
   146  				})
   147  			})
   148  		})
   149  
   150  		Context("when the pipeline name or --all is not specified", func() {
   151  			It("errors", func() {
   152  				Expect(func() {
   153  					flyCmd := exec.Command(flyPath, "-t", targetName, "archive-pipeline")
   154  
   155  					sess, err := gexec.Start(flyCmd, GinkgoWriter, GinkgoWriter)
   156  					Expect(err).NotTo(HaveOccurred())
   157  
   158  					Eventually(sess.Err).Should(gbytes.Say(`one of the flags '-p, --pipeline' or '-a, --all' is required`))
   159  
   160  					<-sess.Exited
   161  					Expect(sess.ExitCode()).To(Equal(1))
   162  				}).To(Change(func() int {
   163  					return len(atcServer.ReceivedRequests())
   164  				}).By(0))
   165  			})
   166  		})
   167  
   168  		Context("when both the pipeline name and --all are specified", func() {
   169  			It("errors", func() {
   170  				Expect(func() {
   171  					flyCmd := exec.Command(flyPath, "-t", targetName, "archive-pipeline", "-p", "awesome-pipeline", "--all")
   172  
   173  					sess, err := gexec.Start(flyCmd, GinkgoWriter, GinkgoWriter)
   174  					Expect(err).NotTo(HaveOccurred())
   175  
   176  					Eventually(sess.Err).Should(gbytes.Say(`only one of the flags '-p, --pipeline' or '-a, --all' is allowed`))
   177  
   178  					<-sess.Exited
   179  					Expect(sess.ExitCode()).To(Equal(1))
   180  				}).To(Change(func() int {
   181  					return len(atcServer.ReceivedRequests())
   182  				}).By(0))
   183  			})
   184  		})
   185  
   186  		Context("when the --all flag is passed, and there are unarchived pipelines", func() {
   187  			var (
   188  				somePath      string
   189  				someOtherPath string
   190  				err           error
   191  			)
   192  
   193  			BeforeEach(func() {
   194  				somePath, err = atc.Routes.CreatePathForRoute(atc.ArchivePipeline, rata.Params{"pipeline_name": "awesome-pipeline", "team_name": "main"})
   195  				Expect(err).NotTo(HaveOccurred())
   196  
   197  				someOtherPath, err = atc.Routes.CreatePathForRoute(atc.ArchivePipeline, rata.Params{"pipeline_name": "more-awesome-pipeline", "team_name": "main"})
   198  				Expect(err).NotTo(HaveOccurred())
   199  
   200  				atcServer.AppendHandlers(
   201  					ghttp.CombineHandlers(
   202  						ghttp.VerifyRequest("GET", "/api/v1/teams/main/pipelines"),
   203  						ghttp.RespondWithJSONEncoded(200, []atc.Pipeline{
   204  							{Name: "awesome-pipeline", Archived: false, Public: false},
   205  							{Name: "more-awesome-pipeline", Archived: false, Public: false},
   206  							{Name: "already-archived", Archived: true, Public: false},
   207  						}),
   208  					),
   209  					ghttp.CombineHandlers(
   210  						ghttp.VerifyRequest("PUT", somePath),
   211  						ghttp.RespondWith(http.StatusOK, nil),
   212  					),
   213  					ghttp.CombineHandlers(
   214  						ghttp.VerifyRequest("PUT", someOtherPath),
   215  						ghttp.RespondWith(http.StatusOK, nil),
   216  					),
   217  				)
   218  			})
   219  
   220  			Context("when the user confirms", func() {
   221  				It("archives every currently unarchived pipeline", func() {
   222  					Expect(func() {
   223  						flyCmd := exec.Command(flyPath, "-t", targetName, "archive-pipeline", "--all")
   224  						stdin, err := flyCmd.StdinPipe()
   225  						Expect(err).NotTo(HaveOccurred())
   226  
   227  						sess, err := gexec.Start(flyCmd, GinkgoWriter, GinkgoWriter)
   228  						Expect(err).NotTo(HaveOccurred())
   229  
   230  						Eventually(sess.Out).Should(PrintTable(ui.Table{
   231  							Headers: ui.TableRow{{Contents: "pipelines", Color: color.New(color.Bold)}},
   232  							Data: []ui.TableRow{
   233  								{{Contents: "awesome-pipeline"}},
   234  								{{Contents: "more-awesome-pipeline"}},
   235  							},
   236  						}))
   237  
   238  						Eventually(sess).Should(gbytes.Say("archive 2 pipelines?"))
   239  						yes(stdin)
   240  
   241  						Eventually(sess).Should(gbytes.Say(`archived 'awesome-pipeline'`))
   242  						Eventually(sess).Should(gbytes.Say(`archived 'more-awesome-pipeline'`))
   243  						Consistently(sess).ShouldNot(gbytes.Say(`archived 'already-archived'`))
   244  
   245  						<-sess.Exited
   246  						Expect(sess.ExitCode()).To(Equal(0))
   247  					}).To(Change(func() int {
   248  						return len(atcServer.ReceivedRequests())
   249  					}).By(4))
   250  				})
   251  			})
   252  
   253  			Context("when the user denies", func() {
   254  				It("does not archive the pipelines", func() {
   255  					Expect(func() {
   256  						flyCmd := exec.Command(flyPath, "-t", targetName, "archive-pipeline", "--all")
   257  						stdin, err := flyCmd.StdinPipe()
   258  						Expect(err).NotTo(HaveOccurred())
   259  
   260  						sess, err := gexec.Start(flyCmd, GinkgoWriter, GinkgoWriter)
   261  						Expect(err).NotTo(HaveOccurred())
   262  
   263  						Eventually(sess).Should(gbytes.Say("archive 2 pipelines?"))
   264  						no(stdin)
   265  
   266  						Eventually(sess).Should(gbytes.Say(`bailing out`))
   267  
   268  						<-sess.Exited
   269  						Expect(sess.ExitCode()).To(Equal(0))
   270  					}).To(Change(func() int {
   271  						return len(atcServer.ReceivedRequests())
   272  					}).By(2))
   273  				})
   274  			})
   275  
   276  			Context("when running in non-interactive mode", func() {
   277  				It("does not prompt the user", func() {
   278  					Expect(func() {
   279  						flyCmd := exec.Command(flyPath, "-t", targetName, "archive-pipeline", "-n", "--all")
   280  
   281  						sess, err := gexec.Start(flyCmd, GinkgoWriter, GinkgoWriter)
   282  						Expect(err).NotTo(HaveOccurred())
   283  
   284  						Eventually(sess).Should(gbytes.Say(`archived 'awesome-pipeline'`))
   285  						Eventually(sess).Should(gbytes.Say(`archived 'more-awesome-pipeline'`))
   286  
   287  						<-sess.Exited
   288  						Expect(sess.ExitCode()).To(Equal(0))
   289  					}).To(Change(func() int {
   290  						return len(atcServer.ReceivedRequests())
   291  					}).By(4))
   292  				})
   293  			})
   294  		})
   295  
   296  		Context("when the --all flag is passed, but there are no unarchived pipelines", func() {
   297  			BeforeEach(func() {
   298  				atcServer.AppendHandlers(
   299  					ghttp.CombineHandlers(
   300  						ghttp.VerifyRequest("GET", "/api/v1/teams/main/pipelines"),
   301  						ghttp.RespondWithJSONEncoded(200, []atc.Pipeline{
   302  							{Name: "already-archived", Archived: true, Public: false},
   303  						}),
   304  					),
   305  				)
   306  			})
   307  
   308  			It("prints a message and exits", func() {
   309  				flyCmd := exec.Command(flyPath, "-t", targetName, "archive-pipeline", "--all")
   310  
   311  				sess, err := gexec.Start(flyCmd, GinkgoWriter, GinkgoWriter)
   312  				Expect(err).NotTo(HaveOccurred())
   313  
   314  				Eventually(sess).Should(gbytes.Say("there are no unarchived pipelines"))
   315  				Eventually(sess).Should(gbytes.Say("bailing out"))
   316  
   317  				<-sess.Exited
   318  				Expect(sess.ExitCode()).To(Equal(0))
   319  			})
   320  		})
   321  
   322  		Context("when the pipeline flag is invalid", func() {
   323  			It("fails and print invalid flag error", func() {
   324  				flyCmd := exec.Command(flyPath, "-t", targetName, "archive-pipeline", "-p", "forbidden/pipelinename")
   325  
   326  				sess, err := gexec.Start(flyCmd, GinkgoWriter, GinkgoWriter)
   327  				Expect(err).NotTo(HaveOccurred())
   328  
   329  				<-sess.Exited
   330  				Expect(sess.ExitCode()).To(Equal(1))
   331  
   332  				Expect(sess.Err).To(gbytes.Say("error: invalid argument for flag `" + osFlag("p", "pipeline")))
   333  			})
   334  		})
   335  	})
   336  })