github.com/m3db/m3@v1.5.0/src/aggregator/tools/deploy/client_test.go (about) 1 // Copyright (c) 2017 Uber Technologies, Inc. 2 // 3 // Permission is hereby granted, free of charge, to any person obtaining a copy 4 // of this software and associated documentation files (the "Software"), to deal 5 // in the Software without restriction, including without limitation the rights 6 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 // copies of the Software, and to permit persons to whom the Software is 8 // furnished to do so, subject to the following conditions: 9 // 10 // The above copyright notice and this permission notice shall be included in 11 // all copies or substantial portions of the Software. 12 // 13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 // THE SOFTWARE. 20 21 package deploy 22 23 import ( 24 "bytes" 25 "encoding/json" 26 "errors" 27 "io/ioutil" 28 "net/http" 29 "testing" 30 31 "github.com/m3db/m3/src/aggregator/aggregator" 32 httpserver "github.com/m3db/m3/src/aggregator/server/http" 33 34 "github.com/stretchr/testify/require" 35 ) 36 37 var ( 38 testInstanceID = "testInstanceID" 39 ) 40 41 func TestIsHealthyRequestError(t *testing.T) { 42 errRequest := errors.New("request error") 43 c := NewAggregatorClient(nil).(*client) 44 c.doRequestFn = func(*http.Request) (*http.Response, error) { return nil, errRequest } 45 require.Equal(t, errRequest, c.IsHealthy(testInstanceID)) 46 } 47 48 func TestIsHealthyResponseStatusNotOK(t *testing.T) { 49 c := NewAggregatorClient(nil).(*client) 50 response := generateTestResponse(t, http.StatusServiceUnavailable, nil) 51 c.doRequestFn = func(*http.Request) (*http.Response, error) { return response, nil } 52 require.Error(t, c.IsHealthy(testInstanceID)) 53 } 54 55 func TestIsHealthyResponseUnmarshalError(t *testing.T) { 56 c := NewAggregatorClient(nil).(*client) 57 response := generateTestResponse(t, http.StatusOK, 0) 58 c.doRequestFn = func(*http.Request) (*http.Response, error) { return response, nil } 59 require.Error(t, c.IsHealthy(testInstanceID)) 60 } 61 62 func TestIsHealthyResponseWithErrorMessage(t *testing.T) { 63 c := NewAggregatorClient(nil).(*client) 64 payload := httpserver.NewResponse() 65 payload.Error = "some error occurred" 66 response := generateTestResponse(t, http.StatusOK, payload) 67 c.doRequestFn = func(*http.Request) (*http.Response, error) { return response, nil } 68 require.Error(t, c.IsHealthy(testInstanceID)) 69 } 70 71 func TestIsHealthySuccess(t *testing.T) { 72 c := NewAggregatorClient(nil).(*client) 73 payload := httpserver.NewResponse() 74 response := generateTestResponse(t, http.StatusOK, payload) 75 c.doRequestFn = func(*http.Request) (*http.Response, error) { return response, nil } 76 require.NoError(t, c.IsHealthy(testInstanceID)) 77 } 78 79 func TestStatusRequestError(t *testing.T) { 80 errRequest := errors.New("request error") 81 c := NewAggregatorClient(nil).(*client) 82 c.doRequestFn = func(*http.Request) (*http.Response, error) { return nil, errRequest } 83 _, err := c.Status(testInstanceID) 84 require.Equal(t, errRequest, err) 85 } 86 87 func TestStatusResponseStatusNotOK(t *testing.T) { 88 c := NewAggregatorClient(nil).(*client) 89 response := generateTestResponse(t, http.StatusServiceUnavailable, nil) 90 c.doRequestFn = func(*http.Request) (*http.Response, error) { return response, nil } 91 _, err := c.Status(testInstanceID) 92 require.Error(t, err) 93 } 94 95 func TestStatusResponseUnmarshalError(t *testing.T) { 96 c := NewAggregatorClient(nil).(*client) 97 response := generateTestResponse(t, http.StatusOK, 0) 98 c.doRequestFn = func(*http.Request) (*http.Response, error) { return response, nil } 99 _, err := c.Status(testInstanceID) 100 require.Error(t, err) 101 } 102 103 func TestStatusResponseWithErrorMessage(t *testing.T) { 104 c := NewAggregatorClient(nil).(*client) 105 payload := httpserver.NewStatusResponse() 106 payload.Error = "some error occurred" 107 response := generateTestResponse(t, http.StatusOK, payload) 108 c.doRequestFn = func(*http.Request) (*http.Response, error) { return response, nil } 109 _, err := c.Status(testInstanceID) 110 require.Error(t, err) 111 } 112 113 func TestStatusSuccess(t *testing.T) { 114 c := NewAggregatorClient(nil).(*client) 115 expected := aggregator.RuntimeStatus{ 116 FlushStatus: aggregator.FlushStatus{ 117 ElectionState: aggregator.LeaderState, 118 CanLead: true, 119 }, 120 } 121 payload := httpserver.NewStatusResponse() 122 payload.Status = expected 123 response := generateTestResponse(t, http.StatusOK, payload) 124 c.doRequestFn = func(*http.Request) (*http.Response, error) { return response, nil } 125 status, err := c.Status(testInstanceID) 126 require.NoError(t, err) 127 require.Equal(t, expected, status) 128 } 129 130 func TestResignRequestError(t *testing.T) { 131 errRequest := errors.New("request error") 132 c := NewAggregatorClient(nil).(*client) 133 c.doRequestFn = func(*http.Request) (*http.Response, error) { return nil, errRequest } 134 require.Equal(t, errRequest, c.Resign(testInstanceID)) 135 } 136 137 func TestResignResponseStatusNotOK(t *testing.T) { 138 c := NewAggregatorClient(nil).(*client) 139 response := generateTestResponse(t, http.StatusServiceUnavailable, nil) 140 c.doRequestFn = func(*http.Request) (*http.Response, error) { return response, nil } 141 require.Error(t, c.Resign(testInstanceID)) 142 } 143 144 func TestResignResponseUnmarshalError(t *testing.T) { 145 c := NewAggregatorClient(nil).(*client) 146 response := generateTestResponse(t, http.StatusOK, 0) 147 c.doRequestFn = func(*http.Request) (*http.Response, error) { return response, nil } 148 require.Error(t, c.Resign(testInstanceID)) 149 } 150 151 func TestResignResponseWithErrorMessage(t *testing.T) { 152 c := NewAggregatorClient(nil).(*client) 153 payload := httpserver.NewResponse() 154 payload.Error = "some error occurred" 155 response := generateTestResponse(t, http.StatusOK, payload) 156 c.doRequestFn = func(*http.Request) (*http.Response, error) { return response, nil } 157 require.Error(t, c.Resign(testInstanceID)) 158 } 159 160 func TestResignSuccess(t *testing.T) { 161 c := NewAggregatorClient(nil).(*client) 162 payload := httpserver.NewResponse() 163 response := generateTestResponse(t, http.StatusOK, payload) 164 c.doRequestFn = func(*http.Request) (*http.Response, error) { return response, nil } 165 require.NoError(t, c.Resign(testInstanceID)) 166 } 167 168 func generateTestResponse(t *testing.T, statusCode int, payload interface{}) *http.Response { 169 response := &http.Response{} 170 response.StatusCode = statusCode 171 if payload == nil { 172 response.Body = ioutil.NopCloser(bytes.NewBuffer(nil)) 173 return response 174 } 175 b, err := json.Marshal(payload) 176 require.NoError(t, err) 177 r := ioutil.NopCloser(bytes.NewBuffer(b)) 178 response.Body = r 179 return response 180 }