github.com/pf-qiu/concourse/v6@v6.7.3-0.20201207032516-1f455d73275f/fly/integration/ordering_pipelines_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("ordering-pipeline", func() {
    19  		Context("when pipeline names are specified", func() {
    20  			var (
    21  				path string
    22  				err  error
    23  			)
    24  			BeforeEach(func() {
    25  				path, err = atc.Routes.CreatePathForRoute(atc.OrderPipelines, rata.Params{"team_name": "main"})
    26  				Expect(err).NotTo(HaveOccurred())
    27  			})
    28  
    29  			Context("when the pipeline exists", func() {
    30  				var requestBody atc.OrderPipelinesRequest
    31  
    32  				BeforeEach(func() {
    33  					requestBody = atc.OrderPipelinesRequest{
    34  						{Name: "awesome-pipeline"},
    35  						{Name: "awesome-pipeline-2"},
    36  					}
    37  				})
    38  
    39  				JustBeforeEach(func() {
    40  					atcServer.AppendHandlers(
    41  						ghttp.CombineHandlers(
    42  							ghttp.VerifyJSONRepresenting(requestBody),
    43  							ghttp.VerifyRequest("PUT", path),
    44  							ghttp.RespondWith(http.StatusOK, nil),
    45  						),
    46  					)
    47  				})
    48  
    49  				Context("with instanced pipelines", func() {
    50  
    51  					BeforeEach(func() {
    52  						requestBody = atc.OrderPipelinesRequest{
    53  							{Name: "awesome-pipeline", InstanceVars: atc.InstanceVars{"branch": "master"}},
    54  							{Name: "awesome-pipeline", InstanceVars: atc.InstanceVars{"branch": "feature/bar"}},
    55  						}
    56  					})
    57  
    58  					It("orders the pipeline instances", func() {
    59  						Expect(func() {
    60  							flyCmd := exec.Command(flyPath, "-t", targetName, "order-pipelines", "-p", "awesome-pipeline/branch:master", "-p", "awesome-pipeline/branch:feature/bar")
    61  
    62  							sess, err := gexec.Start(flyCmd, GinkgoWriter, GinkgoWriter)
    63  							Expect(err).NotTo(HaveOccurred())
    64  
    65  							<-sess.Exited
    66  							Expect(sess.ExitCode()).To(Equal(0))
    67  							Eventually(sess).Should(gbytes.Say(`ordered pipelines`))
    68  							Eventually(sess).Should(gbytes.Say(`  - awesome-pipeline/branch:master`))
    69  							Eventually(sess).Should(gbytes.Say(`  - awesome-pipeline/branch:feature/bar`))
    70  
    71  						}).To(Change(func() int {
    72  							return len(atcServer.ReceivedRequests())
    73  						}).By(2))
    74  					})
    75  				})
    76  
    77  				It("orders the pipeline", func() {
    78  					Expect(func() {
    79  						flyCmd := exec.Command(flyPath, "-t", targetName, "order-pipelines", "-p", "awesome-pipeline", "-p", "awesome-pipeline-2")
    80  
    81  						sess, err := gexec.Start(flyCmd, GinkgoWriter, GinkgoWriter)
    82  						Expect(err).NotTo(HaveOccurred())
    83  
    84  						<-sess.Exited
    85  						Expect(sess.ExitCode()).To(Equal(0))
    86  						Eventually(sess).Should(gbytes.Say(`ordered pipelines`))
    87  						Eventually(sess).Should(gbytes.Say(`  - awesome-pipeline`))
    88  						Eventually(sess).Should(gbytes.Say(`  - awesome-pipeline-2`))
    89  
    90  					}).To(Change(func() int {
    91  						return len(atcServer.ReceivedRequests())
    92  					}).By(2))
    93  				})
    94  
    95  				It("orders the pipeline with alias", func() {
    96  					Expect(func() {
    97  						flyCmd := exec.Command(flyPath, "-t", targetName, "op", "-p", "awesome-pipeline", "-p", "awesome-pipeline-2")
    98  
    99  						sess, err := gexec.Start(flyCmd, GinkgoWriter, GinkgoWriter)
   100  						Expect(err).NotTo(HaveOccurred())
   101  
   102  						<-sess.Exited
   103  						Expect(sess.ExitCode()).To(Equal(0))
   104  						Eventually(sess).Should(gbytes.Say(`ordered pipelines`))
   105  						Eventually(sess).Should(gbytes.Say(`  - awesome-pipeline`))
   106  						Eventually(sess).Should(gbytes.Say(`  - awesome-pipeline-2`))
   107  
   108  					}).To(Change(func() int {
   109  						return len(atcServer.ReceivedRequests())
   110  					}).By(2))
   111  				})
   112  			})
   113  
   114  			Context("when the alphabetical option is passed", func() {
   115  				BeforeEach(func() {
   116  					atcServer.AppendHandlers(
   117  						ghttp.CombineHandlers(
   118  							ghttp.VerifyRequest("GET", "/api/v1/teams/main/pipelines"),
   119  							ghttp.RespondWithJSONEncoded(200, []atc.Pipeline{
   120  								{Name: "beautiful-pipeline", Paused: false, Public: false},
   121  								{Name: "awesome-pipeline", Paused: true, Public: false},
   122  								{Name: "delightful-pipeline", Paused: false, Public: true},
   123  								{Name: "charming-pipeline", Paused: false, Public: true},
   124  							}),
   125  						),
   126  						ghttp.CombineHandlers(
   127  							ghttp.VerifyRequest("PUT", path),
   128  							ghttp.RespondWith(http.StatusOK, nil),
   129  						),
   130  					)
   131  				})
   132  
   133  				It("orders all the pipelines in alphabetical order", func() {
   134  					Expect(func() {
   135  						flyCmd := exec.Command(flyPath, "-t", targetName, "order-pipelines", "--alphabetical")
   136  
   137  						sess, err := gexec.Start(flyCmd, GinkgoWriter, GinkgoWriter)
   138  						Expect(err).NotTo(HaveOccurred())
   139  
   140  						<-sess.Exited
   141  						Expect(sess.ExitCode()).To(Equal(0))
   142  						Eventually(sess).Should(gbytes.Say(`ordered pipelines`))
   143  						Eventually(sess).Should(gbytes.Say(`  - awesome-pipeline`))
   144  						Eventually(sess).Should(gbytes.Say(`  - beautiful-pipeline`))
   145  						Eventually(sess).Should(gbytes.Say(`  - charming-pipeline`))
   146  						Eventually(sess).Should(gbytes.Say(`  - delightful-pipeline`))
   147  
   148  					}).To(Change(func() int {
   149  						return len(atcServer.ReceivedRequests())
   150  					}).By(3))
   151  				})
   152  			})
   153  
   154  			Context("when the pipeline doesn't exist", func() {
   155  				BeforeEach(func() {
   156  					atcServer.AppendHandlers(
   157  						ghttp.CombineHandlers(
   158  							ghttp.VerifyRequest("PUT", path),
   159  							ghttp.RespondWith(http.StatusInternalServerError, nil),
   160  						),
   161  					)
   162  				})
   163  
   164  				It("prints error message", func() {
   165  					Expect(func() {
   166  						flyCmd := exec.Command(flyPath, "-t", targetName, "order-pipelines", "-p", "awesome-pipeline")
   167  
   168  						sess, err := gexec.Start(flyCmd, GinkgoWriter, GinkgoWriter)
   169  						Expect(err).NotTo(HaveOccurred())
   170  
   171  						<-sess.Exited
   172  						Expect(sess.ExitCode()).To(Equal(1))
   173  						Eventually(sess.Err).Should(gbytes.Say(`failed to order pipelines`))
   174  
   175  					}).To(Change(func() int {
   176  						return len(atcServer.ReceivedRequests())
   177  					}).By(2))
   178  				})
   179  			})
   180  		})
   181  
   182  		Context("when the pipline name is not specified", func() {
   183  			It("errors", func() {
   184  				Expect(func() {
   185  					flyCmd := exec.Command(flyPath, "-t", targetName, "order-pipelines")
   186  
   187  					sess, err := gexec.Start(flyCmd, GinkgoWriter, GinkgoWriter)
   188  					Expect(err).NotTo(HaveOccurred())
   189  
   190  					<-sess.Exited
   191  					Expect(sess.ExitCode()).To(Equal(1))
   192  					Expect(sess.Err).Should(gbytes.Say("error: either --pipeline or --alphabetical are required"))
   193  				}).To(Change(func() int {
   194  					return len(atcServer.ReceivedRequests())
   195  				}).By(0))
   196  			})
   197  		})
   198  
   199  		Context("when the pipeline flag is invalid", func() {
   200  			It("fails and print invalid flag error", func() {
   201  				flyCmd := exec.Command(flyPath, "-t", targetName, "order-pipelines", "-p", "forbidden/pipelinename")
   202  
   203  				sess, err := gexec.Start(flyCmd, GinkgoWriter, GinkgoWriter)
   204  				Expect(err).NotTo(HaveOccurred())
   205  
   206  				<-sess.Exited
   207  				Expect(sess.ExitCode()).To(Equal(1))
   208  
   209  				Expect(sess.Err).To(gbytes.Say("error: invalid argument for flag `" + osFlag("p", "pipeline")))
   210  			})
   211  		})
   212  
   213  	})
   214  })