github.com/sap/cf-mta-plugin@v2.6.3+incompatible/clients/restclient/retryable_rest_client_test.go (about) 1 package restclient_test 2 3 import ( 4 "net/http" 5 "time" 6 7 "github.com/cloudfoundry-incubator/multiapps-cli-plugin/clients/baseclient" 8 "github.com/cloudfoundry-incubator/multiapps-cli-plugin/clients/restclient" 9 "github.com/cloudfoundry-incubator/multiapps-cli-plugin/testutil" 10 . "github.com/onsi/ginkgo" 11 . "github.com/onsi/gomega" 12 ) 13 14 var _ = Describe("RetryableRestClient", func() { 15 expectedRetriesCount := 4 16 17 Describe("PurgeConfiguration", func() { 18 Context("when the backend returns not 204 No Content", func() { 19 It("should return an error", func() { 20 client := newRetryableRestClient(http.StatusInternalServerError) 21 err := client.PurgeConfiguration("org", "space") 22 Expect(err).To(HaveOccurred()) 23 }) 24 }) 25 26 Context("when the backend returns 204 No Content", func() { 27 It("should not return an error", func() { 28 client := newRetryableRestClient(http.StatusNoContent) 29 err := client.PurgeConfiguration("org", "space") 30 Expect(err).ToNot(HaveOccurred()) 31 }) 32 }) 33 Context("with an 500 server error returned by the backend", func() { 34 It("should return the error, zero result and retry expectedRetriesCount times", func() { 35 client := newRetryableRestClient(500) 36 retryableRestClient := client.(restclient.RetryableRestClient) 37 err := retryableRestClient.PurgeConfiguration("org", "space") 38 testutil.ExpectError(err) 39 mockRestClient := retryableRestClient.RestClient.(*MockRestClient) 40 Expect(mockRestClient.GetRetriesCount()).To(Equal(expectedRetriesCount)) 41 }) 42 }) 43 Context("with an 502 server error returned by the backend", func() { 44 It("should return the error, zero result and retry expectedRetriesCount times", func() { 45 client := newRetryableRestClient(502) 46 retryableRestClient := client.(restclient.RetryableRestClient) 47 err := retryableRestClient.PurgeConfiguration("org", "space") 48 testutil.ExpectError(err) 49 mockRestClient := retryableRestClient.RestClient.(*MockRestClient) 50 Expect(mockRestClient.GetRetriesCount()).To(Equal(expectedRetriesCount)) 51 }) 52 }) 53 }) 54 }) 55 56 func newRetryableRestClient(statusCode int) restclient.RestClientOperations { 57 tokenFactory := baseclient.NewCustomTokenFactory("test-token") 58 roundTripper := testutil.NewCustomTransport(statusCode) 59 return NewMockRetryableRestClient("http://localhost:1000", roundTripper, tokenFactory) 60 } 61 62 // NewRetryableRestClient creates a new retryable REST client 63 func NewMockRetryableRestClient(host string, rt http.RoundTripper, tokenFactory baseclient.TokenFactory) restclient.RestClientOperations { 64 restClient := NewMockRestClient(host, rt, tokenFactory) 65 return restclient.RetryableRestClient{RestClient: restClient, MaxRetriesCount: 3, RetryInterval: time.Microsecond * 1} 66 } 67 68 type MockRestClient struct { 69 restClient restclient.RestClientOperations 70 retriesCount int 71 } 72 73 // NewMockRestClient creates a new Rest client 74 func NewMockRestClient(host string, rt http.RoundTripper, tokenFactory baseclient.TokenFactory) restclient.RestClientOperations { 75 restClient := restclient.NewRestClient(host, rt, tokenFactory) 76 return &MockRestClient{restClient, 0} 77 } 78 79 // PurgeConfiguration purges configuration 80 func (c *MockRestClient) PurgeConfiguration(org, space string) error { 81 c.retriesCount++ 82 return c.restClient.PurgeConfiguration(org, space) 83 } 84 85 // GetRetriesCount returns retries count 86 func (c *MockRestClient) GetRetriesCount() int { 87 return c.retriesCount 88 }