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

     1  package integration_test
     2  
     3  import (
     4  	"fmt"
     5  	"net/http"
     6  	"os/exec"
     7  
     8  	. "github.com/onsi/ginkgo"
     9  	. "github.com/onsi/gomega"
    10  
    11  	"github.com/onsi/gomega/gbytes"
    12  	"github.com/onsi/gomega/gexec"
    13  	"github.com/onsi/gomega/ghttp"
    14  )
    15  
    16  var _ = Describe("RenamePipeline", func() {
    17  	var (
    18  		expectedURL         string
    19  		expectedQueryParams string
    20  		expectedStatusCode  int
    21  		newName             string
    22  	)
    23  
    24  	BeforeEach(func() {
    25  		expectedURL = "/api/v1/teams/main/pipelines/some-pipeline/rename"
    26  		expectedStatusCode = http.StatusNoContent
    27  		newName = "brandnew"
    28  	})
    29  
    30  	JustBeforeEach(func() {
    31  		atcServer.AppendHandlers(
    32  			ghttp.CombineHandlers(
    33  				ghttp.VerifyRequest("PUT", expectedURL, expectedQueryParams),
    34  				ghttp.VerifyJSON(fmt.Sprintf(`{"name":%q}`, newName)),
    35  				ghttp.RespondWith(expectedStatusCode, ""),
    36  			),
    37  		)
    38  	})
    39  
    40  	Context("when not specifying a pipeline name", func() {
    41  		It("fails and says you should provide a pipeline name", func() {
    42  			flyCmd := exec.Command(flyPath, "-t", targetName, "rename-pipeline", "-n", "some-new-name")
    43  
    44  			sess, err := gexec.Start(flyCmd, GinkgoWriter, GinkgoWriter)
    45  			Expect(err).NotTo(HaveOccurred())
    46  
    47  			Eventually(sess).Should(gexec.Exit(1))
    48  
    49  			Expect(sess.Err).To(gbytes.Say("error: the required flag `" + osFlag("o", "old-name") + "' was not specified"))
    50  		})
    51  	})
    52  
    53  	Context("when specifying a new pipeline name with a '/' character in it", func() {
    54  		It("fails and says '/' characters are not allowed", func() {
    55  			flyCmd := exec.Command(flyPath, "-t", targetName, "rename-pipeline", "-o", "some-pipeline", "-n", "forbidden/pipelinename")
    56  
    57  			sess, err := gexec.Start(flyCmd, GinkgoWriter, GinkgoWriter)
    58  			Expect(err).NotTo(HaveOccurred())
    59  
    60  			<-sess.Exited
    61  			Expect(sess.ExitCode()).To(Equal(1))
    62  
    63  			Expect(sess.Err).To(gbytes.Say("error: pipeline name cannot contain '/'"))
    64  		})
    65  	})
    66  
    67  	Context("when the pipeline flag is invalid", func() {
    68  		It("fails and print invalid flag error", func() {
    69  			flyCmd := exec.Command(flyPath, "-t", targetName, "rename-pipeline", "-o", "forbidden/pipelinename", "-n", "some-new-name")
    70  
    71  			sess, err := gexec.Start(flyCmd, GinkgoWriter, GinkgoWriter)
    72  			Expect(err).NotTo(HaveOccurred())
    73  
    74  			<-sess.Exited
    75  			Expect(sess.ExitCode()).To(Equal(1))
    76  
    77  			Expect(sess.Err).To(gbytes.Say("error: invalid argument for flag `" + osFlag("o", "old-name")))
    78  		})
    79  	})
    80  
    81  	Context("when not specifying a new name", func() {
    82  		It("fails and says you should provide a new name for the pipeline", func() {
    83  			flyCmd := exec.Command(flyPath, "-t", targetName, "rename-pipeline", "-o", "some-pipeline")
    84  
    85  			sess, err := gexec.Start(flyCmd, GinkgoWriter, GinkgoWriter)
    86  			Expect(err).NotTo(HaveOccurred())
    87  
    88  			Eventually(sess).Should(gexec.Exit(1))
    89  
    90  			Expect(sess.Err).To(gbytes.Say("error: the required flag `" + osFlag("n", "new-name") + "' was not specified"))
    91  		})
    92  	})
    93  
    94  	Context("when all the inputs are provided", func() {
    95  
    96  		BeforeEach(func() {
    97  			expectedQueryParams = "instance_vars=%7B%22branch%22%3A%22master%22%7D"
    98  		})
    99  
   100  		It("successfully renames the pipeline to the provided name", func() {
   101  			flyCmd := exec.Command(flyPath, "-t", targetName, "rename-pipeline", "-o", "some-pipeline/branch:master", "-n", newName)
   102  
   103  			sess, err := gexec.Start(flyCmd, GinkgoWriter, GinkgoWriter)
   104  			Expect(err).NotTo(HaveOccurred())
   105  
   106  			Eventually(sess).Should(gexec.Exit(0))
   107  			Expect(atcServer.ReceivedRequests()).To(HaveLen(5))
   108  			Expect(sess.Out).To(gbytes.Say(fmt.Sprintf("pipeline successfully renamed to %s", newName)))
   109  		})
   110  
   111  		Context("when the pipeline is not found", func() {
   112  			BeforeEach(func() {
   113  				expectedStatusCode = http.StatusNotFound
   114  			})
   115  
   116  			It("returns an error", func() {
   117  				flyCmd := exec.Command(flyPath, "-t", targetName, "rename-pipeline", "-o", "some-pipeline/branch:master", "-n", newName)
   118  
   119  				sess, err := gexec.Start(flyCmd, GinkgoWriter, GinkgoWriter)
   120  				Expect(err).NotTo(HaveOccurred())
   121  
   122  				Eventually(sess).Should(gexec.Exit(1))
   123  				Expect(atcServer.ReceivedRequests()).To(HaveLen(5))
   124  				Expect(sess.Err).To(gbytes.Say("pipeline 'some-pipeline/branch:master' not found"))
   125  			})
   126  		})
   127  
   128  		Context("when an error occurs", func() {
   129  			BeforeEach(func() {
   130  				expectedStatusCode = http.StatusTeapot
   131  			})
   132  
   133  			It("returns an error", func() {
   134  				flyCmd := exec.Command(flyPath, "-t", targetName, "rename-pipeline", "-o", "some-pipeline/branch:master", "-n", newName)
   135  
   136  				sess, err := gexec.Start(flyCmd, GinkgoWriter, GinkgoWriter)
   137  				Expect(err).NotTo(HaveOccurred())
   138  
   139  				Eventually(sess).Should(gexec.Exit(1))
   140  				Expect(atcServer.ReceivedRequests()).To(HaveLen(5))
   141  				Expect(sess.Err).To(gbytes.Say("418 I'm a teapot"))
   142  			})
   143  		})
   144  	})
   145  })