github.com/sleungcy-sap/cli@v7.1.0+incompatible/api/cloudcontroller/ccv3/environment_variables_test.go (about) 1 package ccv3_test 2 3 import ( 4 "net/http" 5 6 "code.cloudfoundry.org/cli/api/cloudcontroller/ccerror" 7 . "code.cloudfoundry.org/cli/api/cloudcontroller/ccv3" 8 "code.cloudfoundry.org/cli/api/cloudcontroller/ccv3/constant" 9 . "github.com/onsi/ginkgo" 10 . "github.com/onsi/gomega" 11 . "github.com/onsi/gomega/ghttp" 12 ) 13 14 var _ = Describe("EnvironmentVariables", func() { 15 var client *Client 16 17 BeforeEach(func() { 18 client, _ = NewTestClient() 19 }) 20 21 Describe("GetEnvironmentVariableGroup", func() { 22 var ( 23 envVars EnvironmentVariables 24 warnings Warnings 25 executeErr error 26 ) 27 28 JustBeforeEach(func() { 29 envVars, warnings, executeErr = client.GetEnvironmentVariableGroup(constant.StagingEnvironmentVariableGroup) 30 }) 31 32 When("the request errors", func() { 33 BeforeEach(func() { 34 server.AppendHandlers( 35 CombineHandlers( 36 VerifyRequest(http.MethodGet, "/v3/environment_variable_groups/staging"), 37 RespondWith(http.StatusTeapot, "{}", http.Header{"X-Cf-Warnings": {"this is a warning"}}), 38 ), 39 ) 40 }) 41 42 It("returns the error and warnings", func() { 43 Expect(executeErr).To(MatchError(ccerror.V3UnexpectedResponseError{ResponseCode: http.StatusTeapot})) 44 Expect(warnings).To(ConsistOf("this is a warning")) 45 }) 46 }) 47 48 When("the request succeeds", func() { 49 BeforeEach(func() { 50 responseBody := `{ 51 "var": { 52 "DEBUG": "false", 53 "my-var": "my-val", 54 "number": 6, 55 "json": {"is": "fun"} 56 } 57 }` 58 59 server.AppendHandlers( 60 CombineHandlers( 61 VerifyRequest(http.MethodGet, "/v3/environment_variable_groups/staging"), 62 RespondWith(http.StatusOK, responseBody, http.Header{"X-Cf-Warnings": {"this is a warning"}}), 63 ), 64 ) 65 }) 66 67 It("returns the error and warnings", func() { 68 Expect(executeErr).ToNot(HaveOccurred()) 69 Expect(warnings).To(ConsistOf("this is a warning")) 70 Expect(envVars).To(Equal(EnvironmentVariables{ 71 "DEBUG": {Value: "false", IsSet: true}, 72 "my-var": {Value: "my-val", IsSet: true}, 73 "number": {Value: "6", IsSet: true}, 74 "json": {Value: "{\"is\":\"fun\"}", IsSet: true}, 75 })) 76 }) 77 }) 78 }) 79 80 Describe("UpdateApplicationEnvironmentVariables", func() { 81 var ( 82 envVars EnvironmentVariables 83 patchedEnvVars EnvironmentVariables 84 85 warnings Warnings 86 executeErr error 87 ) 88 89 JustBeforeEach(func() { 90 patchedEnvVars, warnings, executeErr = client.UpdateApplicationEnvironmentVariables("some-app-guid", envVars) 91 }) 92 93 When("the request errors", func() { 94 BeforeEach(func() { 95 envVars = EnvironmentVariables{"my-var": {Value: "my-val", IsSet: true}} 96 97 expectedBody := map[string]interface{}{ 98 "var": map[string]string{ 99 "my-var": "my-val", 100 }, 101 } 102 103 server.AppendHandlers( 104 CombineHandlers( 105 VerifyRequest(http.MethodPatch, "/v3/apps/some-app-guid/environment_variables"), 106 VerifyJSONRepresenting(expectedBody), 107 RespondWith(http.StatusTeapot, "{}", http.Header{"X-Cf-Warnings": {"this is a warning"}}), 108 ), 109 ) 110 }) 111 112 It("returns the error and warnings", func() { 113 Expect(executeErr).To(MatchError(ccerror.V3UnexpectedResponseError{ResponseCode: http.StatusTeapot})) 114 Expect(warnings).To(ConsistOf("this is a warning")) 115 }) 116 }) 117 118 When("the request succeeds", func() { 119 When("env variable is being set", func() { 120 BeforeEach(func() { 121 envVars = EnvironmentVariables{ 122 "my-var": {Value: "my-val", IsSet: true}, 123 "delete-me": {}, 124 } 125 126 expectedBody := map[string]interface{}{ 127 "var": map[string]interface{}{ 128 "my-var": "my-val", 129 "delete-me": nil, 130 }, 131 } 132 133 responseBody := `{ 134 "var": { 135 "DEBUG": "false", 136 "my-var": "my-val" 137 } 138 }` 139 140 server.AppendHandlers( 141 CombineHandlers( 142 VerifyRequest(http.MethodPatch, "/v3/apps/some-app-guid/environment_variables"), 143 VerifyJSONRepresenting(expectedBody), 144 RespondWith(http.StatusOK, responseBody, http.Header{"X-Cf-Warnings": {"this is a warning"}}), 145 ), 146 ) 147 }) 148 149 It("returns the error and warnings", func() { 150 Expect(executeErr).ToNot(HaveOccurred()) 151 Expect(warnings).To(ConsistOf("this is a warning")) 152 Expect(patchedEnvVars).To(Equal(EnvironmentVariables{ 153 "DEBUG": {Value: "false", IsSet: true}, 154 "my-var": {Value: "my-val", IsSet: true}, 155 })) 156 }) 157 }) 158 159 When("env variable is being unset", func() { 160 BeforeEach(func() { 161 envVars = EnvironmentVariables{ 162 "my-var": {Value: "", IsSet: false}, 163 } 164 165 expectedBody := map[string]interface{}{ 166 "var": map[string]interface{}{ 167 "my-var": nil, 168 }, 169 } 170 171 responseBody := `{ 172 "var": { 173 "DEBUG": "false" 174 } 175 }` 176 177 server.AppendHandlers( 178 CombineHandlers( 179 VerifyRequest(http.MethodPatch, "/v3/apps/some-app-guid/environment_variables"), 180 VerifyJSONRepresenting(expectedBody), 181 RespondWith(http.StatusOK, responseBody, http.Header{"X-Cf-Warnings": {"this is a warning"}}), 182 ), 183 ) 184 }) 185 186 It("returns the patchedEnvVars and warnings", func() { 187 Expect(executeErr).ToNot(HaveOccurred()) 188 Expect(warnings).To(ConsistOf("this is a warning")) 189 Expect(patchedEnvVars).To(Equal(EnvironmentVariables{ 190 "DEBUG": {Value: "false", IsSet: true}, 191 })) 192 }) 193 }) 194 }) 195 }) 196 197 Describe("UpdateEnvironmentVariableGroup", func() { 198 var ( 199 warnings Warnings 200 executeErr error 201 envVars EnvironmentVariables 202 patchedEnvVars EnvironmentVariables 203 ) 204 205 JustBeforeEach(func() { 206 patchedEnvVars, warnings, executeErr = client.UpdateEnvironmentVariableGroup(constant.StagingEnvironmentVariableGroup, envVars) 207 }) 208 209 When("the request errors", func() { 210 BeforeEach(func() { 211 envVars = EnvironmentVariables{"my-var": {Value: "my-val", IsSet: true}} 212 213 expectedBody := map[string]interface{}{ 214 "var": map[string]string{ 215 "my-var": "my-val", 216 }, 217 } 218 219 server.AppendHandlers( 220 CombineHandlers( 221 VerifyRequest(http.MethodPatch, "/v3/environment_variable_groups/staging"), 222 VerifyJSONRepresenting(expectedBody), 223 RespondWith(http.StatusTeapot, "{}", http.Header{"X-Cf-Warnings": {"this is a warning"}}), 224 ), 225 ) 226 }) 227 228 It("returns the error and warnings", func() { 229 Expect(executeErr).To(MatchError(ccerror.V3UnexpectedResponseError{ResponseCode: http.StatusTeapot})) 230 Expect(warnings).To(ConsistOf("this is a warning")) 231 }) 232 }) 233 234 When("the request succeeds", func() { 235 When("env variable is being set", func() { 236 BeforeEach(func() { 237 envVars = EnvironmentVariables{ 238 "my-var": {Value: "my-val", IsSet: true}, 239 "delete-me": {}, 240 } 241 242 expectedBody := map[string]interface{}{ 243 "var": map[string]interface{}{ 244 "my-var": "my-val", 245 "delete-me": nil, 246 }, 247 } 248 249 responseBody := `{ 250 "var": { 251 "my-var": "my-val" 252 } 253 }` 254 255 server.AppendHandlers( 256 CombineHandlers( 257 VerifyRequest(http.MethodPatch, "/v3/environment_variable_groups/staging"), 258 VerifyJSONRepresenting(expectedBody), 259 RespondWith(http.StatusOK, responseBody, http.Header{"X-Cf-Warnings": {"this is a warning"}}), 260 ), 261 ) 262 }) 263 264 It("returns the patchedEnvVars and warnings", func() { 265 Expect(executeErr).ToNot(HaveOccurred()) 266 Expect(warnings).To(ConsistOf("this is a warning")) 267 Expect(patchedEnvVars).To(Equal(EnvironmentVariables{ 268 "my-var": {Value: "my-val", IsSet: true}, 269 })) 270 }) 271 }) 272 }) 273 }) 274 })