github.com/wanddynosios/cli/v8@v8.7.9-0.20240221182337-1a92e3a7017f/api/cloudcontroller/ccv3/metadata_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 9 . "code.cloudfoundry.org/cli/api/cloudcontroller/ccv3" 10 . "code.cloudfoundry.org/cli/resources" 11 "code.cloudfoundry.org/cli/types" 12 . "github.com/onsi/ginkgo" 13 . "github.com/onsi/gomega" 14 . "github.com/onsi/gomega/ghttp" 15 ) 16 17 const errorResponse = `{ 18 "errors": [ 19 { 20 "code": 10008, 21 "detail": "Metadata label key error: 'invalid*key' contains invalid characters", 22 "title": "CF-UnprocessableEntity" 23 } 24 ] 25 }` 26 27 var _ = Describe("Metadata", func() { 28 var client *Client 29 30 BeforeEach(func() { 31 client, _ = NewTestClient() 32 }) 33 34 Describe("UpdateResourceMetadata", func() { 35 var ( 36 metadataToUpdate Metadata 37 resourceGUID string 38 jobURL JobURL 39 warnings Warnings 40 executeErr error 41 ) 42 testForResourceType := func(resourceType string, resourceTypeNameForURI string) { 43 if resourceTypeNameForURI == "" { 44 resourceTypeNameForURI = resourceType 45 } 46 47 When(fmt.Sprintf("updating metadata on %s", resourceType), func() { 48 49 JustBeforeEach(func() { 50 resourceGUID = "some-guid" 51 jobURL, warnings, executeErr = client.UpdateResourceMetadata(resourceType, resourceGUID, metadataToUpdate) 52 }) 53 54 When(fmt.Sprintf("the %s is updated successfully", resourceType), func() { 55 BeforeEach(func() { 56 expectedBody := map[string]interface{}{ 57 "metadata": map[string]interface{}{ 58 "labels": map[string]string{ 59 "k1": "v1", 60 "k2": "v2", 61 }, 62 }, 63 } 64 65 server.AppendHandlers( 66 CombineHandlers( 67 VerifyRequest(http.MethodPatch, fmt.Sprintf("/v3/%ss/some-guid", resourceTypeNameForURI)), 68 VerifyJSONRepresenting(expectedBody), 69 RespondWith(http.StatusOK, nil, http.Header{ 70 "X-Cf-Warnings": {"this is a warning"}, 71 "Location": {"fake-job-url"}, 72 }), 73 ), 74 ) 75 76 metadataToUpdate = Metadata{ 77 Labels: map[string]types.NullString{ 78 "k1": types.NewNullString("v1"), 79 "k2": types.NewNullString("v2"), 80 }, 81 } 82 }) 83 84 It("should include the labels in the JSON", func() { 85 Expect(executeErr).ToNot(HaveOccurred()) 86 Expect(warnings).To(ConsistOf(Warnings{"this is a warning"})) 87 Expect(server.ReceivedRequests()).To(HaveLen(1)) 88 Expect(jobURL).To(Equal(JobURL("fake-job-url"))) 89 }) 90 }) 91 92 When("Cloud Controller returns errors and warnings", func() { 93 BeforeEach(func() { 94 expectedBody := map[string]interface{}{ 95 "metadata": map[string]interface{}{ 96 "labels": map[string]string{ 97 "invalid*key": "v1", 98 "k2": "v2", 99 }, 100 }, 101 } 102 103 server.AppendHandlers( 104 CombineHandlers( 105 VerifyRequest(http.MethodPatch, fmt.Sprintf("/v3/%ss/some-guid", resourceTypeNameForURI)), 106 VerifyJSONRepresenting(expectedBody), 107 RespondWith(http.StatusUnprocessableEntity, errorResponse, http.Header{"X-Cf-Warnings": {"this is another warning"}}), 108 ), 109 ) 110 111 metadataToUpdate = Metadata{ 112 Labels: map[string]types.NullString{ 113 "invalid*key": types.NewNullString("v1"), 114 "k2": types.NewNullString("v2"), 115 }, 116 } 117 }) 118 119 It("returns the error and all warnings", func() { 120 Expect(executeErr).To(MatchError(ccerror.UnprocessableEntityError{ 121 Message: "Metadata label key error: 'invalid*key' contains invalid characters", 122 })) 123 Expect(warnings).To(ConsistOf(Warnings{"this is another warning"})) 124 }) 125 }) 126 }) 127 } 128 129 testForResourceType("app", "") 130 testForResourceType("domain", "") 131 testForResourceType("buildpack", "") 132 testForResourceType("org", "organization") 133 testForResourceType("route", "") 134 testForResourceType("service-broker", "service_broker") 135 testForResourceType("service-instance", "service_instance") 136 testForResourceType("service-offering", "service_offering") 137 testForResourceType("service-plan", "service_plan") 138 testForResourceType("space", "") 139 testForResourceType("stack", "") 140 141 When("updating metadata on an unsupported resource", func() { 142 It("returns an error", func() { 143 _, _, err := client.UpdateResourceMetadata("anything", "fake-guid", Metadata{}) 144 Expect(err).To(MatchError("unknown resource type (anything) requested")) 145 }) 146 }) 147 }) 148 })