github.com/chenbh/concourse/v6@v6.4.2/fly/integration/enable_resource_version_test.go (about)

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