github.com/pf-qiu/concourse/v6@v6.7.3-0.20201207032516-1f455d73275f/fly/integration/disable_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("disable-resource-version", func() {
    21  		var (
    22  			expectedGetStatus    int
    23  			expectedPutStatus    int
    24  			disablePath, getPath string
    25  			err                  error
    26  			teamName             = "main"
    27  			pipelineName         = "pipeline"
    28  			resourceName         = "resource"
    29  			resourceVersionID    = "42"
    30  			disableVersion       = "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: true,
    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 disable-resource-version command", func() {
    47  				flyCmd := exec.Command(flyPath, "disable-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  					disablePath, err = atc.Routes.CreatePathForRoute(atc.DisableResourceVersion, 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", disablePath, "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  
   100  						expectedVersion.Enabled = true
   101  					})
   102  
   103  					It("disables the resource version", func() {
   104  						Expect(func() {
   105  							flyCmd := exec.Command(flyPath, "-t", targetName, "disable-resource-version", "-r", pipelineResource, "-v", disableVersion)
   106  
   107  							sess, err := gexec.Start(flyCmd, GinkgoWriter, GinkgoWriter)
   108  							Expect(err).NotTo(HaveOccurred())
   109  
   110  							Eventually(sess.Out).Should(gbytes.Say(fmt.Sprintf("disabled '%s' with version {\"some\":\"value\"}\n", pipelineResource)))
   111  
   112  							<-sess.Exited
   113  							Expect(sess.ExitCode()).To(Equal(0))
   114  						}).To(Change(func() int {
   115  							return len(atcServer.ReceivedRequests())
   116  						}).By(3))
   117  					})
   118  				})
   119  
   120  				Context("when the resource does not exist", func() {
   121  					BeforeEach(func() {
   122  						expectedGetStatus = http.StatusNotFound
   123  					})
   124  
   125  					It("errors", func() {
   126  						Expect(func() {
   127  							flyCmd := exec.Command(flyPath, "-t", targetName, "disable-resource-version", "-r", pipelineResource, "-v", disableVersion)
   128  
   129  							sess, err := gexec.Start(flyCmd, GinkgoWriter, GinkgoWriter)
   130  							Expect(err).NotTo(HaveOccurred())
   131  
   132  							Eventually(sess.Err).Should(gbytes.Say(fmt.Sprintf("could not find version matching {\"some\":\"value\"}\n")))
   133  
   134  							<-sess.Exited
   135  							Expect(sess.ExitCode()).To(Equal(1))
   136  						}).To(Change(func() int {
   137  							return len(atcServer.ReceivedRequests())
   138  						}).By(2))
   139  					})
   140  				})
   141  
   142  				Context("when the resource version does not exist", func() {
   143  					BeforeEach(func() {
   144  						expectedPutStatus = http.StatusNotFound
   145  						expectedGetStatus = http.StatusOK
   146  
   147  						expectedVersion.Enabled = true
   148  					})
   149  
   150  					It("fails to disable", func() {
   151  						Expect(func() {
   152  							flyCmd := exec.Command(flyPath, "-t", targetName, "disable-resource-version", "-r", pipelineResource, "-v", disableVersion)
   153  
   154  							sess, err := gexec.Start(flyCmd, GinkgoWriter, GinkgoWriter)
   155  							Expect(err).NotTo(HaveOccurred())
   156  
   157  							Eventually(sess.Err).Should(gbytes.Say(fmt.Sprintf("could not disable '%s', make sure the resource version exists", pipelineResource)))
   158  
   159  							<-sess.Exited
   160  							Expect(sess.ExitCode()).To(Equal(1))
   161  						}).To(Change(func() int {
   162  							return len(atcServer.ReceivedRequests())
   163  						}).By(3))
   164  					})
   165  				})
   166  				Context("when the resource version is already disabled", func() {
   167  					BeforeEach(func() {
   168  						expectedGetStatus = http.StatusOK
   169  						expectedVersion.Enabled = false
   170  					})
   171  
   172  					It("returns successfully without calling api", func() {
   173  						Expect(func() {
   174  							flyCmd := exec.Command(flyPath, "-t", targetName, "disable-resource-version", "-r", pipelineResource, "-v", disableVersion)
   175  
   176  							sess, err := gexec.Start(flyCmd, GinkgoWriter, GinkgoWriter)
   177  							Expect(err).NotTo(HaveOccurred())
   178  
   179  							Eventually(sess.Out).Should(gbytes.Say(fmt.Sprintf("disabled '%s' with version {\"some\":\"value\"}\n", pipelineResource)))
   180  
   181  							<-sess.Exited
   182  							Expect(sess.ExitCode()).To(Equal(0))
   183  
   184  							for _, request := range atcServer.ReceivedRequests() {
   185  								Expect(request.RequestURI).NotTo(Equal(disablePath))
   186  							}
   187  						}).To(Change(func() int {
   188  							return len(atcServer.ReceivedRequests())
   189  						}).By(2))
   190  					})
   191  				})
   192  			})
   193  		})
   194  	})
   195  })