github.com/chenbh/concourse/v6@v6.4.2/fly/integration/pin_resource_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("pin-resource", func() {
    20  		var (
    21  			expectedGetStatus             int
    22  			expectedPutStatus             int
    23  			expectedPutCommentStatus      int
    24  			pinPath, getPath, commentPath string
    25  			err                           error
    26  			teamName                      = "main"
    27  			pipelineName                  = "pipeline"
    28  			resourceName                  = "resource"
    29  			resourceVersionID             = "42"
    30  			pinVersion                    = "some:value"
    31  			pipelineResource              = fmt.Sprintf("%s/%s", pipelineName, resourceName)
    32  			expectedPinVersion            = atc.ResourceVersion{
    33  				ID:      42,
    34  				Version: atc.Version{"some": "value"},
    35  			}
    36  		)
    37  
    38  		Context("make sure the command exists", func() {
    39  			It("calls the pin-resource command", func() {
    40  				flyCmd := exec.Command(flyPath, "pin-resource")
    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  					pinPath, err = atc.Routes.CreatePathForRoute(atc.PinResourceVersion, 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{expectedPinVersion}),
    75  						),
    76  						ghttp.CombineHandlers(
    77  							ghttp.VerifyRequest("PUT", pinPath),
    78  							ghttp.RespondWith(expectedPutStatus, nil),
    79  						),
    80  					)
    81  				})
    82  				Context("when the resource and version exists", func() {
    83  					BeforeEach(func() {
    84  						expectedGetStatus = http.StatusOK
    85  						expectedPutStatus = http.StatusOK
    86  					})
    87  
    88  					It("pins the resource version", func() {
    89  						Expect(func() {
    90  							flyCmd := exec.Command(flyPath, "-t", targetName, "pin-resource", "-r", pipelineResource, "-v", pinVersion)
    91  
    92  							sess, err := gexec.Start(flyCmd, GinkgoWriter, GinkgoWriter)
    93  							Expect(err).NotTo(HaveOccurred())
    94  
    95  							Eventually(sess.Out).Should(gbytes.Say(fmt.Sprintf("pinned '%s' with version {\"some\":\"value\"}\n", pipelineResource)))
    96  
    97  							<-sess.Exited
    98  							Expect(sess.ExitCode()).To(Equal(0))
    99  						}).To(Change(func() int {
   100  							return len(atcServer.ReceivedRequests())
   101  						}).By(3))
   102  					})
   103  
   104  				})
   105  
   106  				Context("when the versions does not exist", func() {
   107  					BeforeEach(func() {
   108  						expectedGetStatus = http.StatusNotFound
   109  					})
   110  
   111  					It("errors", func() {
   112  						Expect(func() {
   113  							flyCmd := exec.Command(flyPath, "-t", targetName, "pin-resource", "-r", pipelineResource, "-v", pinVersion)
   114  
   115  							sess, err := gexec.Start(flyCmd, GinkgoWriter, GinkgoWriter)
   116  							Expect(err).NotTo(HaveOccurred())
   117  
   118  							Eventually(sess.Err).Should(gbytes.Say(fmt.Sprintf("could not find version matching {\"some\":\"value\"}\n")))
   119  
   120  							<-sess.Exited
   121  							Expect(sess.ExitCode()).To(Equal(1))
   122  						}).To(Change(func() int {
   123  							return len(atcServer.ReceivedRequests())
   124  						}).By(2))
   125  					})
   126  				})
   127  
   128  				Context("when the resource does not exist", func() {
   129  					BeforeEach(func() {
   130  						expectedPutStatus = http.StatusNotFound
   131  						expectedGetStatus = http.StatusOK
   132  					})
   133  
   134  					It("fails to pin", func() {
   135  						Expect(func() {
   136  							flyCmd := exec.Command(flyPath, "-t", targetName, "pin-resource", "-r", pipelineResource, "-v", pinVersion)
   137  
   138  							sess, err := gexec.Start(flyCmd, GinkgoWriter, GinkgoWriter)
   139  							Expect(err).NotTo(HaveOccurred())
   140  
   141  							Eventually(sess.Err).Should(gbytes.Say(fmt.Sprintf("could not pin '%s', make sure the resource exists", pipelineResource)))
   142  
   143  							<-sess.Exited
   144  							Expect(sess.ExitCode()).To(Equal(1))
   145  						}).To(Change(func() int {
   146  							return len(atcServer.ReceivedRequests())
   147  						}).By(3))
   148  					})
   149  				})
   150  			})
   151  
   152  			Context("when pin comment is provided", func() {
   153  				BeforeEach(func() {
   154  					commentPath, err = atc.Routes.CreatePathForRoute(atc.SetPinCommentOnResource, rata.Params{
   155  						"pipeline_name": pipelineName,
   156  						"team_name":     teamName,
   157  						"resource_name": resourceName,
   158  					})
   159  					Expect(err).NotTo(HaveOccurred())
   160  				})
   161  
   162  				JustBeforeEach(func() {
   163  					atcServer.AppendHandlers(
   164  						ghttp.CombineHandlers(
   165  							ghttp.VerifyRequest("PUT", commentPath),
   166  							ghttp.VerifyJSONRepresenting(atc.SetPinCommentRequestBody{PinComment: "some pin message"}),
   167  							ghttp.RespondWith(expectedPutCommentStatus, nil),
   168  						),
   169  					)
   170  				})
   171  
   172  				Context("when resource is pinned", func() {
   173  					BeforeEach(func() {
   174  						expectedPutCommentStatus = http.StatusOK
   175  					})
   176  
   177  					It("save the comment to pin resource", func() {
   178  						Expect(func() {
   179  							flyCmd := exec.Command(flyPath, "-t", targetName, "pin-resource", "-r", pipelineResource, "-c", "some pin message")
   180  
   181  							sess, err := gexec.Start(flyCmd, GinkgoWriter, GinkgoWriter)
   182  							Expect(err).NotTo(HaveOccurred())
   183  
   184  							Eventually(sess.Out).Should(gbytes.Say(fmt.Sprintf("pin comment 'some pin message' is saved\n")))
   185  							<-sess.Exited
   186  							Expect(sess.ExitCode()).To(Equal(0))
   187  						}).To(Change(func() int {
   188  							return len(atcServer.ReceivedRequests())
   189  						}).By(2))
   190  					})
   191  				})
   192  
   193  				Context("when resource is not pinned", func() {
   194  					BeforeEach(func() {
   195  						expectedPutCommentStatus = http.StatusNotFound
   196  					})
   197  
   198  					It("shows error", func() {
   199  						Expect(func() {
   200  							flyCmd := exec.Command(flyPath, "-t", targetName, "pin-resource", "-r", pipelineResource, "-c", "some pin message")
   201  
   202  							sess, err := gexec.Start(flyCmd, GinkgoWriter, GinkgoWriter)
   203  							Expect(err).NotTo(HaveOccurred())
   204  
   205  							Eventually(sess.Err).Should(gbytes.Say(fmt.Sprintf("could not save comment, make sure '%s' is pinned\n", pipelineResource)))
   206  							<-sess.Exited
   207  							Expect(sess.ExitCode()).To(Equal(1))
   208  						}).To(Change(func() int {
   209  							return len(atcServer.ReceivedRequests())
   210  						}).By(2))
   211  					})
   212  				})
   213  			})
   214  		})
   215  	})
   216  })