github.com/arunkumar7540/cli@v6.45.0+incompatible/api/cloudcontroller/ccv3/deployment_test.go (about)

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