github.com/cloudfoundry-community/cloudfoundry-cli@v6.44.1-0.20240130060226-cda5ed8e89a5+incompatible/api/cloudcontroller/ccv3/deployment_test.go (about)

     1  package ccv3_test
     2  
     3  import (
     4  	"code.cloudfoundry.org/cli/resources"
     5  	"fmt"
     6  	"net/http"
     7  
     8  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccerror"
     9  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3/constant"
    10  
    11  	. "code.cloudfoundry.org/cli/api/cloudcontroller/ccv3"
    12  	. "github.com/onsi/ginkgo"
    13  	. "github.com/onsi/gomega"
    14  	. "github.com/onsi/gomega/ghttp"
    15  )
    16  
    17  var _ = Describe("Task", func() {
    18  	var client *Client
    19  
    20  	BeforeEach(func() {
    21  		client, _ = NewTestClient()
    22  	})
    23  
    24  	Describe("GetDeployments", func() {
    25  		var (
    26  			deployments []resources.Deployment
    27  			warnings    Warnings
    28  			executeErr  error
    29  		)
    30  
    31  		JustBeforeEach(func() {
    32  			deployments, warnings, executeErr = client.GetDeployments(Query{Key: AppGUIDFilter, Values: []string{"some-app-guid"}}, Query{Key: OrderBy, Values: []string{"-created_at"}}, Query{Key: PerPage, Values: []string{"1"}})
    33  		})
    34  
    35  		var response string
    36  		var response2 string
    37  		BeforeEach(func() {
    38  			response = fmt.Sprintf(`{
    39  	"pagination": {
    40  		"next": {
    41  			"href": "%s/v3/deployments?app_guids=some-app-guid&order_by=-created_at&page=2&per_page=1"
    42  		}
    43  	},
    44  	"resources": [
    45  		{
    46        		"guid": "newest-deployment-guid",
    47        		"created_at": "2018-05-25T22:42:10Z"
    48     	}
    49  	]
    50  }`, server.URL())
    51  			response2 = `{
    52    	"pagination": {
    53  		"next": null
    54  	},
    55  	"resources": [
    56  		{
    57        		"guid": "oldest-deployment-guid",
    58        		"created_at": "2018-04-25T22:42:10Z"
    59     	}
    60  	]
    61  }`
    62  		})
    63  
    64  		Context("when the deployment exists", func() {
    65  			BeforeEach(func() {
    66  				server.AppendHandlers(
    67  					CombineHandlers(
    68  						VerifyRequest(http.MethodGet, "/v3/deployments", "app_guids=some-app-guid&order_by=-created_at&per_page=1"),
    69  						RespondWith(http.StatusAccepted, response),
    70  					),
    71  				)
    72  
    73  				server.AppendHandlers(
    74  					CombineHandlers(
    75  						VerifyRequest(http.MethodGet, "/v3/deployments", "app_guids=some-app-guid&order_by=-created_at&page=2&per_page=1"),
    76  						RespondWith(http.StatusAccepted, response2, http.Header{"X-Cf-Warnings": {"warning"}}),
    77  					),
    78  				)
    79  
    80  			})
    81  
    82  			It("returns the deployment guid of the most recent deployment", func() {
    83  				Expect(executeErr).ToNot(HaveOccurred())
    84  				Expect(warnings).To(ConsistOf("warning"))
    85  				Expect(deployments).To(ConsistOf(
    86  					resources.Deployment{GUID: "newest-deployment-guid", CreatedAt: "2018-05-25T22:42:10Z"},
    87  					resources.Deployment{GUID: "oldest-deployment-guid", CreatedAt: "2018-04-25T22:42:10Z"},
    88  				))
    89  			})
    90  		})
    91  	})
    92  
    93  	Describe("CancelDeployment", func() {
    94  		var (
    95  			warnings   Warnings
    96  			executeErr error
    97  		)
    98  
    99  		JustBeforeEach(func() {
   100  			warnings, executeErr = client.CancelDeployment("some-deployment-guid")
   101  		})
   102  
   103  		Context("when the deployment exists", func() {
   104  			Context("when cancelling the deployment succeeds", func() {
   105  				BeforeEach(func() {
   106  					server.AppendHandlers(
   107  						CombineHandlers(
   108  							VerifyRequest(http.MethodPost, "/v3/deployments/some-deployment-guid/actions/cancel"),
   109  							RespondWith(http.StatusAccepted, "", http.Header{"X-Cf-Warnings": {"warning"}}),
   110  						),
   111  					)
   112  				})
   113  
   114  				It("cancels the deployment with no errors and returns all warnings", func() {
   115  					Expect(executeErr).ToNot(HaveOccurred())
   116  					Expect(warnings).To(ConsistOf("warning"))
   117  				})
   118  			})
   119  		})
   120  	})
   121  
   122  	Describe("CreateApplicationDeployment", func() {
   123  		var (
   124  			deploymentGUID string
   125  			warnings       Warnings
   126  			executeErr     error
   127  			dropletGUID    string
   128  		)
   129  
   130  		JustBeforeEach(func() {
   131  			deploymentGUID, warnings, executeErr = client.CreateApplicationDeployment("some-app-guid", dropletGUID)
   132  		})
   133  
   134  		Context("when the application exists", func() {
   135  			var response string
   136  			BeforeEach(func() {
   137  				dropletGUID = "some-droplet-guid"
   138  				response = `{
   139    "guid": "some-deployment-guid",
   140    "created_at": "2018-04-25T22:42:10Z",
   141    "relationships": {
   142      "app": {
   143        "data": {
   144          "guid": "some-app-guid"
   145        }
   146      }
   147    }
   148  }`
   149  			})
   150  
   151  			Context("when creating the deployment succeeds", func() {
   152  				BeforeEach(func() {
   153  					server.AppendHandlers(
   154  						CombineHandlers(
   155  							VerifyRequest(http.MethodPost, "/v3/deployments"),
   156  							VerifyJSON(`{"droplet":{ "guid":"some-droplet-guid" }, "relationships":{"app":{"data":{"guid":"some-app-guid"}}}}`),
   157  							RespondWith(http.StatusAccepted, response, http.Header{"X-Cf-Warnings": {"warning"}}),
   158  						),
   159  					)
   160  				})
   161  
   162  				It("creates the deployment with no errors and returns all warnings", func() {
   163  					Expect(deploymentGUID).To(Equal("some-deployment-guid"))
   164  					Expect(executeErr).ToNot(HaveOccurred())
   165  					Expect(warnings).To(ConsistOf("warning"))
   166  				})
   167  
   168  			})
   169  
   170  			Context("when no droplet guid is provided", func() {
   171  				BeforeEach(func() {
   172  					dropletGUID = ""
   173  					server.AppendHandlers(
   174  						CombineHandlers(
   175  							VerifyRequest(http.MethodPost, "/v3/deployments"),
   176  							VerifyJSON(`{"relationships":{"app":{"data":{"guid":"some-app-guid"}}}}`),
   177  							RespondWith(http.StatusAccepted, response, http.Header{"X-Cf-Warnings": {"warning"}}),
   178  						),
   179  					)
   180  				})
   181  
   182  				It("omits the droplet object in the JSON", func() {
   183  					Expect(executeErr).ToNot(HaveOccurred())
   184  					Expect(warnings).To(ConsistOf("warning"))
   185  				})
   186  			})
   187  		})
   188  	})
   189  
   190  	Describe("GetDeployment", func() {
   191  		var response string
   192  		Context("When the deployments exists", func() {
   193  			BeforeEach(func() {
   194  				response = `{
   195  				    "guid": "some-deployment-guid",
   196  					"state": "DEPLOYING",
   197  					"droplet": {
   198   					  "guid": "some-droplet-guid"
   199  					},
   200   					"previous_droplet": {
   201   					  "guid": "some-other-droplet-guid"
   202   					},
   203   					"created_at": "some-time",
   204   					"updated_at": "some-later-time",
   205   					"relationships": {
   206   					  "app": {
   207   					    "data": {
   208   					      "guid": "some-app-guid"
   209   					    }
   210   					  }
   211   					}
   212  				}`
   213  				server.AppendHandlers(
   214  					CombineHandlers(
   215  						VerifyRequest(http.MethodGet, "/v3/deployments/some-deployment-guid"),
   216  						RespondWith(http.StatusAccepted, response, http.Header{"X-Cf-Warnings": {"warning"}}),
   217  					),
   218  				)
   219  			})
   220  			It("Successfully returns a deployment object", func() {
   221  				deployment, warnings, err := client.GetDeployment("some-deployment-guid")
   222  				Expect(err).ToNot(HaveOccurred())
   223  				Expect(warnings).To(ConsistOf("warning"))
   224  				Expect(deployment).To(Not(BeNil()))
   225  				Expect(deployment.GUID).To(Equal("some-deployment-guid"))
   226  				Expect(deployment.State).To(Equal(constant.DeploymentDeploying))
   227  			})
   228  		})
   229  
   230  		Context("when the deployment doesn't exist", func() {
   231  			BeforeEach(func() {
   232  				response := `{
   233  					"errors": [
   234  						{
   235  							"code": 10010,
   236  							"detail": "Deployment not found",
   237  							"title": "CF-ResourceNotFound"
   238  						}
   239  					]
   240  				}`
   241  				server.AppendHandlers(
   242  					CombineHandlers(
   243  						VerifyRequest(http.MethodGet, "/v3/deployments/not-a-deployment"),
   244  						RespondWith(http.StatusNotFound, response, http.Header{"X-Cf-Warnings": {"warning-deployment"}}),
   245  					),
   246  				)
   247  			})
   248  
   249  			It("returns the error", func() {
   250  				_, warnings, err := client.GetDeployment("not-a-deployment")
   251  				Expect(err).To(MatchError(ccerror.DeploymentNotFoundError{}))
   252  				Expect(warnings).To(ConsistOf("warning-deployment"))
   253  			})
   254  		})
   255  	})
   256  })