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

     1  package integration_test
     2  
     3  import (
     4  	"fmt"
     5  	"net/http"
     6  	"os/exec"
     7  	"strings"
     8  
     9  	"github.com/pf-qiu/concourse/v6/atc"
    10  	. "github.com/onsi/ginkgo"
    11  	. "github.com/onsi/gomega"
    12  	"github.com/onsi/gomega/gbytes"
    13  	"github.com/onsi/gomega/ghttp"
    14  	"github.com/tedsuo/rata"
    15  
    16  	"github.com/onsi/gomega/gexec"
    17  )
    18  
    19  var _ = Describe("Fly CLI", func() {
    20  	Describe("enable-resource-version", func() {
    21  		var (
    22  			expectedGetStatus   int
    23  			expectedPutStatus   int
    24  			enablePath, getPath string
    25  			err                 error
    26  			teamName            = "main"
    27  			pipelineName        = "pipeline"
    28  			resourceName        = "resource"
    29  			resourceVersionID   = "42"
    30  			enableVersion       = "some:value"
    31  			pipelineRef         = atc.PipelineRef{Name: pipelineName, InstanceVars: atc.InstanceVars{"branch": "master"}}
    32  			pipelineResource    = fmt.Sprintf("%s/%s", pipelineRef.String(), resourceName)
    33  			expectedVersion     = atc.ResourceVersion{
    34  				ID:      42,
    35  				Version: atc.Version{"some": "value"},
    36  				Enabled: false,
    37  			}
    38  			expectedQueryParams []string
    39  		)
    40  
    41  		BeforeEach(func() {
    42  			expectedQueryParams = []string{}
    43  		})
    44  
    45  		Context("make sure the command exists", func() {
    46  			It("calls the enable-resource-version command", func() {
    47  				flyCmd := exec.Command(flyPath, "enable-resource-version")
    48  				sess, err := gexec.Start(flyCmd, GinkgoWriter, GinkgoWriter)
    49  
    50  				Expect(err).ToNot(HaveOccurred())
    51  				Consistently(sess.Err).ShouldNot(gbytes.Say("error: Unknown command"))
    52  
    53  				<-sess.Exited
    54  			})
    55  		})
    56  
    57  		Context("when the resource is specified", func() {
    58  			BeforeEach(func() {
    59  				expectedQueryParams = append(expectedQueryParams, "instance_vars=%7B%22branch%22%3A%22master%22%7D")
    60  			})
    61  
    62  			Context("when the resource version json string is specified", func() {
    63  				BeforeEach(func() {
    64  					getPath, err = atc.Routes.CreatePathForRoute(atc.ListResourceVersions, rata.Params{
    65  						"pipeline_name": pipelineName,
    66  						"team_name":     teamName,
    67  						"resource_name": resourceName,
    68  					})
    69  					Expect(err).NotTo(HaveOccurred())
    70  
    71  					enablePath, err = atc.Routes.CreatePathForRoute(atc.EnableResourceVersion, rata.Params{
    72  						"pipeline_name":              pipelineName,
    73  						"team_name":                  teamName,
    74  						"resource_name":              resourceName,
    75  						"resource_config_version_id": resourceVersionID,
    76  					})
    77  					Expect(err).NotTo(HaveOccurred())
    78  
    79  					expectedQueryParams = append(expectedQueryParams, "filter=some:value")
    80  				})
    81  
    82  				JustBeforeEach(func() {
    83  					atcServer.AppendHandlers(
    84  						ghttp.CombineHandlers(
    85  							ghttp.VerifyRequest("GET", getPath, strings.Join(expectedQueryParams, "&")),
    86  							ghttp.RespondWithJSONEncoded(expectedGetStatus, []atc.ResourceVersion{expectedVersion}),
    87  						),
    88  						ghttp.CombineHandlers(
    89  							ghttp.VerifyRequest("PUT", enablePath, "instance_vars=%7B%22branch%22%3A%22master%22%7D"),
    90  							ghttp.RespondWith(expectedPutStatus, nil),
    91  						),
    92  					)
    93  				})
    94  
    95  				Context("when the resource and version exists", func() {
    96  					BeforeEach(func() {
    97  						expectedGetStatus = http.StatusOK
    98  						expectedPutStatus = http.StatusOK
    99  						expectedVersion.Enabled = false
   100  					})
   101  
   102  					It("enables the resource version", func() {
   103  						Expect(func() {
   104  							flyCmd := exec.Command(flyPath, "-t", targetName, "enable-resource-version", "-r", pipelineResource, "-v", enableVersion)
   105  
   106  							sess, err := gexec.Start(flyCmd, GinkgoWriter, GinkgoWriter)
   107  							Expect(err).NotTo(HaveOccurred())
   108  
   109  							Eventually(sess.Out).Should(gbytes.Say(fmt.Sprintf("enabled '%s' with version {\"some\":\"value\"}\n", pipelineResource)))
   110  
   111  							<-sess.Exited
   112  							Expect(sess.ExitCode()).To(Equal(0))
   113  						}).To(Change(func() int {
   114  							return len(atcServer.ReceivedRequests())
   115  						}).By(3))
   116  					})
   117  				})
   118  
   119  				Context("when the resource does not exist", func() {
   120  					BeforeEach(func() {
   121  						expectedGetStatus = http.StatusNotFound
   122  					})
   123  
   124  					It("errors", func() {
   125  						Expect(func() {
   126  							flyCmd := exec.Command(flyPath, "-t", targetName, "enable-resource-version", "-r", pipelineResource, "-v", enableVersion)
   127  
   128  							sess, err := gexec.Start(flyCmd, GinkgoWriter, GinkgoWriter)
   129  							Expect(err).NotTo(HaveOccurred())
   130  
   131  							Eventually(sess.Err).Should(gbytes.Say(fmt.Sprintf("could not find version matching {\"some\":\"value\"}\n")))
   132  
   133  							<-sess.Exited
   134  							Expect(sess.ExitCode()).To(Equal(1))
   135  						}).To(Change(func() int {
   136  							return len(atcServer.ReceivedRequests())
   137  						}).By(2))
   138  					})
   139  				})
   140  
   141  				Context("when the resource version does not exist", func() {
   142  					BeforeEach(func() {
   143  						expectedPutStatus = http.StatusNotFound
   144  						expectedGetStatus = http.StatusOK
   145  						expectedVersion.Enabled = false
   146  					})
   147  
   148  					It("fails to enable", func() {
   149  						Expect(func() {
   150  							flyCmd := exec.Command(flyPath, "-t", targetName, "enable-resource-version", "-r", pipelineResource, "-v", enableVersion)
   151  
   152  							sess, err := gexec.Start(flyCmd, GinkgoWriter, GinkgoWriter)
   153  							Expect(err).NotTo(HaveOccurred())
   154  
   155  							Eventually(sess.Err).Should(gbytes.Say(fmt.Sprintf("could not enable '%s', make sure the resource version exists", pipelineResource)))
   156  
   157  							<-sess.Exited
   158  							Expect(sess.ExitCode()).To(Equal(1))
   159  						}).To(Change(func() int {
   160  							return len(atcServer.ReceivedRequests())
   161  						}).By(3))
   162  					})
   163  				})
   164  
   165  				Context("when the resource version is already enabled", func() {
   166  					BeforeEach(func() {
   167  						expectedGetStatus = http.StatusOK
   168  						expectedVersion.Enabled = true
   169  					})
   170  
   171  					It("returns successfully without calling api", func() {
   172  						Expect(func() {
   173  							flyCmd := exec.Command(flyPath, "-t", targetName, "enable-resource-version", "-r", pipelineResource, "-v", enableVersion)
   174  
   175  							sess, err := gexec.Start(flyCmd, GinkgoWriter, GinkgoWriter)
   176  							Expect(err).NotTo(HaveOccurred())
   177  
   178  							Eventually(sess.Out).Should(gbytes.Say(fmt.Sprintf("enabled '%s' with version {\"some\":\"value\"}\n", pipelineResource)))
   179  
   180  							<-sess.Exited
   181  							Expect(sess.ExitCode()).To(Equal(0))
   182  
   183  							for _, request := range atcServer.ReceivedRequests() {
   184  								Expect(request.RequestURI).NotTo(Equal(enablePath))
   185  							}
   186  						}).To(Change(func() int {
   187  							return len(atcServer.ReceivedRequests())
   188  						}).By(2))
   189  					})
   190  				})
   191  			})
   192  		})
   193  	})
   194  })