github.com/wanddynosios/cli/v8@v8.7.9-0.20240221182337-1a92e3a7017f/api/cloudcontroller/ccv3/stack_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"
     9  	"code.cloudfoundry.org/cli/resources"
    10  	. "github.com/onsi/ginkgo"
    11  	. "github.com/onsi/gomega"
    12  	. "github.com/onsi/gomega/ghttp"
    13  )
    14  
    15  var _ = Describe("Stacks", func() {
    16  	var client *Client
    17  
    18  	BeforeEach(func() {
    19  		client, _ = NewTestClient()
    20  	})
    21  
    22  	Describe("GetStacks", func() {
    23  		var (
    24  			query Query
    25  
    26  			stacks     []resources.Stack
    27  			warnings   Warnings
    28  			executeErr error
    29  		)
    30  
    31  		JustBeforeEach(func() {
    32  			stacks, warnings, executeErr = client.GetStacks(query)
    33  		})
    34  
    35  		When("stacks exist", func() {
    36  			BeforeEach(func() {
    37  				response1 := fmt.Sprintf(`{
    38  	"pagination": {
    39  		"next": {
    40  			"href": "%s/v3/stacks?names=some-stack-name&page=2&per_page=2"
    41  		}
    42  	},
    43    "resources": [
    44      {
    45        	"name": "stack-name-1",
    46        	"guid": "stack-guid-1",
    47        	"description": "stack desc 1"
    48      },
    49      {
    50        	"name": "stack-name-2",
    51        	"guid": "stack-guid-2",
    52  		"description": "stack desc 2"
    53      }
    54    ]
    55  }`, server.URL())
    56  				response2 := `{
    57  	"pagination": {
    58  		"next": null
    59  	},
    60  	"resources": [
    61  	  {
    62  		"name": "stack-name-3",
    63  		  "guid": "stack-guid-3",
    64  		"description": "stack desc 3"
    65  		}
    66  	]
    67  }`
    68  
    69  				server.AppendHandlers(
    70  					CombineHandlers(
    71  						VerifyRequest(http.MethodGet, "/v3/stacks", "names=some-stack-name"),
    72  						RespondWith(http.StatusOK, response1, http.Header{"X-Cf-Warnings": {"this is a warning"}}),
    73  					),
    74  				)
    75  				server.AppendHandlers(
    76  					CombineHandlers(
    77  						VerifyRequest(http.MethodGet, "/v3/stacks", "names=some-stack-name&page=2&per_page=2"),
    78  						RespondWith(http.StatusOK, response2, http.Header{"X-Cf-Warnings": {"this is another warning"}}),
    79  					),
    80  				)
    81  
    82  				query = Query{
    83  					Key:    NameFilter,
    84  					Values: []string{"some-stack-name"},
    85  				}
    86  			})
    87  
    88  			It("returns the queried stacks and all warnings", func() {
    89  				Expect(executeErr).NotTo(HaveOccurred())
    90  
    91  				Expect(stacks).To(ConsistOf(
    92  					resources.Stack{Name: "stack-name-1", GUID: "stack-guid-1", Description: "stack desc 1"},
    93  					resources.Stack{Name: "stack-name-2", GUID: "stack-guid-2", Description: "stack desc 2"},
    94  					resources.Stack{Name: "stack-name-3", GUID: "stack-guid-3", Description: "stack desc 3"},
    95  				))
    96  				Expect(warnings).To(ConsistOf("this is a warning", "this is another warning"))
    97  			})
    98  		})
    99  
   100  		When("the cloud controller returns errors and warnings", func() {
   101  			BeforeEach(func() {
   102  				response := `{
   103    "errors": [
   104      {
   105        "code": 10008,
   106        "detail": "The request is semantically invalid: command presence",
   107        "title": "CF-UnprocessableEntity"
   108      },
   109      {
   110        "code": 10010,
   111        "detail": "stack not found",
   112        "title": "CF-stackNotFound"
   113      }
   114    ]
   115  }`
   116  				server.AppendHandlers(
   117  					CombineHandlers(
   118  						VerifyRequest(http.MethodGet, "/v3/stacks"),
   119  						RespondWith(http.StatusTeapot, response, http.Header{"X-Cf-Warnings": {"this is a warning"}}),
   120  					),
   121  				)
   122  			})
   123  
   124  			It("returns the error and all warnings", func() {
   125  				Expect(executeErr).To(MatchError(ccerror.MultiError{
   126  					ResponseCode: http.StatusTeapot,
   127  					Errors: []ccerror.V3Error{
   128  						{
   129  							Code:   10008,
   130  							Detail: "The request is semantically invalid: command presence",
   131  							Title:  "CF-UnprocessableEntity",
   132  						},
   133  						{
   134  							Code:   10010,
   135  							Detail: "stack not found",
   136  							Title:  "CF-stackNotFound",
   137  						},
   138  					},
   139  				}))
   140  				Expect(warnings).To(ConsistOf("this is a warning"))
   141  			})
   142  		})
   143  	})
   144  })