github.com/pf-qiu/concourse/v6@v6.7.3-0.20201207032516-1f455d73275f/fly/integration/unpause_pipeline_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/pf-qiu/concourse/v6/atc"
    11  	"github.com/onsi/gomega/gbytes"
    12  	"github.com/onsi/gomega/gexec"
    13  	"github.com/onsi/gomega/ghttp"
    14  	"github.com/tedsuo/rata"
    15  )
    16  
    17  var _ = Describe("Fly CLI", func() {
    18  	Describe("unpause-pipeline", func() {
    19  		Context("when the pipeline name is specified", func() {
    20  			var (
    21  				mainPath    string
    22  				otherPath   string
    23  				queryParams string
    24  				err         error
    25  			)
    26  			BeforeEach(func() {
    27  				mainPath, err = atc.Routes.CreatePathForRoute(atc.UnpausePipeline, rata.Params{"pipeline_name": "awesome-pipeline", "team_name": "main"})
    28  				Expect(err).NotTo(HaveOccurred())
    29  
    30  				otherPath, err = atc.Routes.CreatePathForRoute(atc.UnpausePipeline, rata.Params{"pipeline_name": "awesome-pipeline", "team_name": "other-team"})
    31  				Expect(err).NotTo(HaveOccurred())
    32  
    33  				queryParams = "instance_vars=%7B%22branch%22%3A%22master%22%7D"
    34  			})
    35  
    36  			Context("when the pipeline exists", func() {
    37  
    38  				Context("user and pipeline are part of the main team", func() {
    39  					Context("user is targeting the same team the pipeline belongs to", func() {
    40  						BeforeEach(func() {
    41  							atcServer.AppendHandlers(
    42  								ghttp.CombineHandlers(
    43  									ghttp.VerifyRequest("PUT", mainPath, queryParams),
    44  									ghttp.RespondWith(http.StatusOK, nil),
    45  								),
    46  							)
    47  						})
    48  
    49  						It("unpauses the pipeline", func() {
    50  							Expect(func() {
    51  								flyCmd := exec.Command(flyPath, "-t", targetName, "unpause-pipeline", "-p", "awesome-pipeline/branch:master")
    52  
    53  								sess, err := gexec.Start(flyCmd, GinkgoWriter, GinkgoWriter)
    54  								Expect(err).NotTo(HaveOccurred())
    55  
    56  								Eventually(sess).Should(gbytes.Say(`unpaused 'awesome-pipeline/branch:master'`))
    57  
    58  								<-sess.Exited
    59  								Expect(sess.ExitCode()).To(Equal(0))
    60  							}).To(Change(func() int {
    61  								return len(atcServer.ReceivedRequests())
    62  							}).By(2))
    63  						})
    64  					})
    65  
    66  					Context("user is NOT targeting the same team the pipeline belongs to", func() {
    67  						BeforeEach(func() {
    68  							atcServer.AppendHandlers(
    69  								ghttp.CombineHandlers(
    70  									ghttp.VerifyRequest("GET", "/api/v1/teams/other-team"),
    71  									ghttp.RespondWithJSONEncoded(http.StatusOK, atc.Team{
    72  										Name: "other-team",
    73  									}),
    74  								),
    75  								ghttp.CombineHandlers(
    76  									ghttp.VerifyRequest("PUT", otherPath),
    77  									ghttp.RespondWith(http.StatusOK, nil),
    78  								),
    79  							)
    80  						})
    81  
    82  						It("unpauses the pipeline", func() {
    83  							flyCmd := exec.Command(flyPath, "-t", targetName, "unpause-pipeline", "-p", "awesome-pipeline", "--team", "other-team")
    84  
    85  							sess, err := gexec.Start(flyCmd, GinkgoWriter, GinkgoWriter)
    86  							Expect(err).NotTo(HaveOccurred())
    87  
    88  							Eventually(sess).Should(gbytes.Say(`unpaused 'awesome-pipeline'`))
    89  
    90  							<-sess.Exited
    91  							Expect(sess.ExitCode()).To(Equal(0))
    92  						})
    93  					})
    94  
    95  				})
    96  
    97  			})
    98  
    99  			Context("when the pipeline doesn't exist", func() {
   100  				BeforeEach(func() {
   101  					atcServer.AppendHandlers(
   102  						ghttp.CombineHandlers(
   103  							ghttp.VerifyRequest("PUT", mainPath),
   104  							ghttp.RespondWith(http.StatusNotFound, nil),
   105  						),
   106  					)
   107  				})
   108  
   109  				It("prints helpful message", func() {
   110  					Expect(func() {
   111  						flyCmd := exec.Command(flyPath, "-t", targetName, "unpause-pipeline", "-p", "awesome-pipeline")
   112  
   113  						sess, err := gexec.Start(flyCmd, GinkgoWriter, GinkgoWriter)
   114  						Expect(err).NotTo(HaveOccurred())
   115  
   116  						Eventually(sess.Err).Should(gbytes.Say(`pipeline 'awesome-pipeline' not found`))
   117  
   118  						<-sess.Exited
   119  						Expect(sess.ExitCode()).To(Equal(1))
   120  					}).To(Change(func() int {
   121  						return len(atcServer.ReceivedRequests())
   122  					}).By(2))
   123  				})
   124  			})
   125  		})
   126  
   127  		Context("when the pipline name or --all is not specified", func() {
   128  			It("errors", func() {
   129  				Expect(func() {
   130  					flyCmd := exec.Command(flyPath, "-t", targetName, "unpause-pipeline")
   131  
   132  					sess, err := gexec.Start(flyCmd, GinkgoWriter, GinkgoWriter)
   133  					Expect(err).NotTo(HaveOccurred())
   134  
   135  					Eventually(sess.Err).Should(gbytes.Say(`one of the flags '-p, --pipeline' or '-a, --all' is required`))
   136  
   137  					<-sess.Exited
   138  					Expect(sess.ExitCode()).To(Equal(1))
   139  				}).To(Change(func() int {
   140  					return len(atcServer.ReceivedRequests())
   141  				}).By(0))
   142  			})
   143  		})
   144  
   145  		Context("when both the pipline name and --all are specified", func() {
   146  			It("errors", func() {
   147  				Expect(func() {
   148  					flyCmd := exec.Command(flyPath, "-t", targetName, "unpause-pipeline", "-p", "awesome-pipeline", "--all")
   149  
   150  					sess, err := gexec.Start(flyCmd, GinkgoWriter, GinkgoWriter)
   151  					Expect(err).NotTo(HaveOccurred())
   152  
   153  					Eventually(sess.Err).Should(gbytes.Say(`only one of the flags '-p, --pipeline' or '-a, --all' is allowed`))
   154  
   155  					<-sess.Exited
   156  					Expect(sess.ExitCode()).To(Equal(1))
   157  				}).To(Change(func() int {
   158  					return len(atcServer.ReceivedRequests())
   159  				}).By(0))
   160  			})
   161  		})
   162  
   163  		Context("when the pipeline flag is invalid", func() {
   164  			It("fails and print invalid flag error", func() {
   165  				flyCmd := exec.Command(flyPath, "-t", targetName, "unpause-pipeline", "-p", "forbidden/pipelinename")
   166  
   167  				sess, err := gexec.Start(flyCmd, GinkgoWriter, GinkgoWriter)
   168  				Expect(err).NotTo(HaveOccurred())
   169  
   170  				<-sess.Exited
   171  				Expect(sess.ExitCode()).To(Equal(1))
   172  
   173  				Expect(sess.Err).To(gbytes.Say("error: invalid argument for flag `" + osFlag("p", "pipeline")))
   174  			})
   175  		})
   176  
   177  	})
   178  	Context("when the --all flag is passed", func() {
   179  		var (
   180  			somePath      string
   181  			someOtherPath string
   182  			err           error
   183  		)
   184  
   185  		BeforeEach(func() {
   186  			somePath, err = atc.Routes.CreatePathForRoute(atc.UnpausePipeline, rata.Params{"pipeline_name": "awesome-pipeline", "team_name": "main"})
   187  			Expect(err).NotTo(HaveOccurred())
   188  
   189  			someOtherPath, err = atc.Routes.CreatePathForRoute(atc.UnpausePipeline, rata.Params{"pipeline_name": "more-awesome-pipeline", "team_name": "main"})
   190  			Expect(err).NotTo(HaveOccurred())
   191  
   192  			atcServer.AppendHandlers(
   193  				ghttp.CombineHandlers(
   194  					ghttp.VerifyRequest("GET", "/api/v1/teams/main/pipelines"),
   195  					ghttp.RespondWithJSONEncoded(200, []atc.Pipeline{
   196  						{Name: "awesome-pipeline", Paused: false, Public: false},
   197  						{Name: "more-awesome-pipeline", Paused: true, Public: false},
   198  					}),
   199  				),
   200  				ghttp.CombineHandlers(
   201  					ghttp.VerifyRequest("PUT", somePath),
   202  					ghttp.RespondWith(http.StatusOK, nil),
   203  				),
   204  				ghttp.CombineHandlers(
   205  					ghttp.VerifyRequest("PUT", someOtherPath),
   206  					ghttp.RespondWith(http.StatusOK, nil),
   207  				),
   208  			)
   209  		})
   210  
   211  		It("unpauses every pipeline", func() {
   212  			Expect(func() {
   213  				flyCmd := exec.Command(flyPath, "-t", targetName, "unpause-pipeline", "--all")
   214  
   215  				sess, err := gexec.Start(flyCmd, GinkgoWriter, GinkgoWriter)
   216  				Expect(err).NotTo(HaveOccurred())
   217  
   218  				Eventually(sess).Should(gbytes.Say(`unpaused 'awesome-pipeline'`))
   219  				Eventually(sess).Should(gbytes.Say(`unpaused 'more-awesome-pipeline'`))
   220  
   221  				<-sess.Exited
   222  				Expect(sess.ExitCode()).To(Equal(0))
   223  			}).To(Change(func() int {
   224  				return len(atcServer.ReceivedRequests())
   225  			}).By(4))
   226  		})
   227  
   228  	})
   229  })