github.com/loggregator/cli@v6.33.1-0.20180224010324-82334f081791+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/types"
     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("GetApplicationEnvironmentVariables", func() {
    22  		var (
    23  			fetchedEnvVars EnvironmentVariableGroups
    24  			warnings       Warnings
    25  			err            error
    26  		)
    27  
    28  		JustBeforeEach(func() {
    29  			fetchedEnvVars, warnings, err = client.GetApplicationEnvironmentVariables("some-app-guid")
    30  		})
    31  
    32  		Context("when the request errors", func() {
    33  			BeforeEach(func() {
    34  				server.AppendHandlers(
    35  					CombineHandlers(
    36  						VerifyRequest(http.MethodGet, "/v3/apps/some-app-guid/env"),
    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(err).To(MatchError(ccerror.V3UnexpectedResponseError{ResponseCode: http.StatusTeapot}))
    44  				Expect(warnings).To(ConsistOf("this is a warning"))
    45  			})
    46  		})
    47  
    48  		Context("when the request succeeds", func() {
    49  			BeforeEach(func() {
    50  				responseBody := `{
    51    "staging_env_json": {
    52      "staging-name": "staging-value"
    53    },
    54    "running_env_json": {
    55      "running-name": "running-value"
    56    },
    57    "environment_variables": {
    58      "user-name": "user-value"
    59    },
    60    "system_env_json": {
    61      "VCAP_SERVICES": {
    62        "mysql": [
    63          {
    64            "name": "db-for-my-app"
    65          }
    66        ]
    67      }
    68    },
    69    "application_env_json": {
    70      "VCAP_APPLICATION": {
    71        "application_name": "my_app"
    72      }
    73    }
    74  }`
    75  				server.AppendHandlers(
    76  					CombineHandlers(
    77  						VerifyRequest(http.MethodGet, "/v3/apps/some-app-guid/env"),
    78  						RespondWith(http.StatusOK, responseBody, http.Header{"X-Cf-Warnings": {"this is a warning"}}),
    79  					),
    80  				)
    81  			})
    82  
    83  			It("returns env variable groups", func() {
    84  				Expect(err).ToNot(HaveOccurred())
    85  				Expect(warnings).To(ConsistOf("this is a warning"))
    86  				Expect(fetchedEnvVars).To(Equal(EnvironmentVariableGroups{
    87  					SystemProvided: map[string]interface{}{
    88  						"VCAP_SERVICES": map[string]interface{}{
    89  							"mysql": []interface{}{map[string]interface{}{"name": "db-for-my-app"}},
    90  						},
    91  					},
    92  					ApplicationProvided: map[string]interface{}{
    93  						"VCAP_APPLICATION": map[string]interface{}{
    94  							"application_name": "my_app",
    95  						},
    96  					},
    97  					UserProvided: map[string]interface{}{
    98  						"user-name": "user-value",
    99  					},
   100  					RunningGroup: map[string]interface{}{
   101  						"running-name": "running-value",
   102  					},
   103  					StagingGroup: map[string]interface{}{
   104  						"staging-name": "staging-value",
   105  					},
   106  				}))
   107  			})
   108  		})
   109  	})
   110  
   111  	Describe("PatchApplicationUserProvidedEnvironmentVariables", func() {
   112  		var (
   113  			envVars        EnvironmentVariables
   114  			patchedEnvVars EnvironmentVariables
   115  
   116  			warnings Warnings
   117  			err      error
   118  		)
   119  
   120  		JustBeforeEach(func() {
   121  			patchedEnvVars, warnings, err = client.PatchApplicationUserProvidedEnvironmentVariables("some-app-guid", envVars)
   122  		})
   123  
   124  		Context("when the request errors", func() {
   125  			BeforeEach(func() {
   126  				envVars = EnvironmentVariables{Variables: map[string]types.FilteredString{"my-var": {Value: "my-val", IsSet: true}}}
   127  				expectedBody := map[string]interface{}{
   128  					"var": map[string]types.FilteredString{
   129  						"my-var": {Value: "my-val", IsSet: true},
   130  					},
   131  				}
   132  				server.AppendHandlers(
   133  					CombineHandlers(
   134  						VerifyRequest(http.MethodPatch, "/v3/apps/some-app-guid/environment_variables"),
   135  						VerifyJSONRepresenting(expectedBody),
   136  						RespondWith(http.StatusTeapot, "{}", http.Header{"X-Cf-Warnings": {"this is a warning"}}),
   137  					),
   138  				)
   139  			})
   140  
   141  			It("returns the error and warnings", func() {
   142  				Expect(err).To(MatchError(ccerror.V3UnexpectedResponseError{ResponseCode: http.StatusTeapot}))
   143  				Expect(warnings).To(ConsistOf("this is a warning"))
   144  			})
   145  		})
   146  
   147  		Context("when the request succeeds", func() {
   148  			Context("when env variable is being set", func() {
   149  				BeforeEach(func() {
   150  					envVars = EnvironmentVariables{Variables: map[string]types.FilteredString{"my-var": {Value: "my-val", IsSet: true}}}
   151  					expectedBody := map[string]interface{}{
   152  						"var": map[string]types.FilteredString{
   153  							"my-var": {Value: "my-val", IsSet: true},
   154  						},
   155  					}
   156  
   157  					responseBody := `{
   158  	"var": {
   159  		"DEBUG": "false",
   160  		"my-var": "my-val"
   161  	}
   162  }`
   163  
   164  					server.AppendHandlers(
   165  						CombineHandlers(
   166  							VerifyRequest(http.MethodPatch, "/v3/apps/some-app-guid/environment_variables"),
   167  							VerifyJSONRepresenting(expectedBody),
   168  							RespondWith(http.StatusOK, responseBody, http.Header{"X-Cf-Warnings": {"this is a warning"}}),
   169  						),
   170  					)
   171  				})
   172  
   173  				It("returns the error and warnings", func() {
   174  					Expect(err).ToNot(HaveOccurred())
   175  					Expect(warnings).To(ConsistOf("this is a warning"))
   176  					Expect(patchedEnvVars).To(Equal(EnvironmentVariables{Variables: map[string]types.FilteredString{
   177  						"DEBUG":  {Value: "false", IsSet: true},
   178  						"my-var": {Value: "my-val", IsSet: true},
   179  					}}))
   180  				})
   181  			})
   182  
   183  			Context("when env variable is being unset", func() {
   184  				BeforeEach(func() {
   185  					envVars = EnvironmentVariables{Variables: map[string]types.FilteredString{"my-var": {Value: "", IsSet: false}}}
   186  					expectedBody := map[string]interface{}{
   187  						"var": map[string]types.FilteredString{
   188  							"my-var": {Value: "", IsSet: false},
   189  						},
   190  					}
   191  
   192  					responseBody := `{
   193  	"var": {
   194  		"DEBUG": "false"
   195  	}
   196  }`
   197  
   198  					server.AppendHandlers(
   199  						CombineHandlers(
   200  							VerifyRequest(http.MethodPatch, "/v3/apps/some-app-guid/environment_variables"),
   201  							VerifyJSONRepresenting(expectedBody),
   202  							RespondWith(http.StatusOK, responseBody, http.Header{"X-Cf-Warnings": {"this is a warning"}}),
   203  						),
   204  					)
   205  				})
   206  
   207  				It("returns the error and warnings", func() {
   208  					Expect(err).ToNot(HaveOccurred())
   209  					Expect(warnings).To(ConsistOf("this is a warning"))
   210  					Expect(patchedEnvVars).To(Equal(EnvironmentVariables{Variables: map[string]types.FilteredString{
   211  						"DEBUG": {Value: "false", IsSet: true},
   212  					}}))
   213  				})
   214  			})
   215  		})
   216  	})
   217  })