github.com/naphatkrit/deis@v1.12.3/client/controller/models/config/config_test.go (about) 1 package config 2 3 import ( 4 "fmt" 5 "io/ioutil" 6 "net/http" 7 "net/http/httptest" 8 "net/url" 9 "reflect" 10 "testing" 11 12 "github.com/deis/deis/client/controller/api" 13 "github.com/deis/deis/client/controller/client" 14 "github.com/deis/deis/version" 15 ) 16 17 const configFixture string = ` 18 { 19 "owner": "test", 20 "app": "example-go", 21 "values": { 22 "TEST": "testing", 23 "FOO": "bar" 24 }, 25 "memory": { 26 "web": "1G" 27 }, 28 "cpu": { 29 "web": "1000" 30 }, 31 "tags": { 32 "test": "tests" 33 }, 34 "created": "2014-01-01T00:00:00UTC", 35 "updated": "2014-01-01T00:00:00UTC", 36 "uuid": "de1bf5b5-4a72-4f94-a10c-d2a3741cdf75" 37 } 38 ` 39 40 const configUnsetFixture string = ` 41 { 42 "owner": "test", 43 "app": "unset-test", 44 "values": {}, 45 "memory": {}, 46 "cpu": {}, 47 "tags": {}, 48 "created": "2014-01-01T00:00:00UTC", 49 "updated": "2014-01-01T00:00:00UTC", 50 "uuid": "de1bf5b5-4a72-4f94-a10c-d2a3741cdf75" 51 } 52 ` 53 54 const configSetExpected string = `{"values":{"FOO":"bar","TEST":"testing"},"memory":{"web":"1G"},"cpu":{"web":"1000"},"tags":{"test":"tests"}}` 55 const configUnsetExpected string = `{"values":{"FOO":null,"TEST":null},"memory":{"web":null},"cpu":{"web":null},"tags":{"test":null}}` 56 57 type fakeHTTPServer struct{} 58 59 func (f *fakeHTTPServer) ServeHTTP(res http.ResponseWriter, req *http.Request) { 60 res.Header().Add("DEIS_API_VERSION", version.APIVersion) 61 62 if req.URL.Path == "/v1/apps/example-go/config/" && req.Method == "POST" { 63 body, err := ioutil.ReadAll(req.Body) 64 65 if err != nil { 66 fmt.Println(err) 67 res.WriteHeader(http.StatusInternalServerError) 68 res.Write(nil) 69 } 70 71 if string(body) != configSetExpected { 72 fmt.Printf("Expected '%s', Got '%s'\n", configSetExpected, body) 73 res.WriteHeader(http.StatusInternalServerError) 74 res.Write(nil) 75 return 76 } 77 78 res.WriteHeader(http.StatusCreated) 79 res.Write([]byte(configFixture)) 80 return 81 } 82 83 if req.URL.Path == "/v1/apps/unset-test/config/" && req.Method == "POST" { 84 body, err := ioutil.ReadAll(req.Body) 85 86 if err != nil { 87 fmt.Println(err) 88 res.WriteHeader(http.StatusInternalServerError) 89 res.Write(nil) 90 } 91 92 if string(body) != configUnsetExpected { 93 fmt.Printf("Expected '%s', Got '%s'\n", configUnsetExpected, body) 94 res.WriteHeader(http.StatusInternalServerError) 95 res.Write(nil) 96 return 97 } 98 99 res.WriteHeader(http.StatusCreated) 100 res.Write([]byte(configUnsetFixture)) 101 return 102 } 103 104 if req.URL.Path == "/v1/apps/example-go/config/" && req.Method == "GET" { 105 res.Write([]byte(configFixture)) 106 return 107 } 108 109 fmt.Printf("Unrecognized URL %s\n", req.URL) 110 res.WriteHeader(http.StatusNotFound) 111 res.Write(nil) 112 } 113 114 func TestConfigSet(t *testing.T) { 115 t.Parallel() 116 117 handler := fakeHTTPServer{} 118 server := httptest.NewServer(&handler) 119 defer server.Close() 120 121 u, err := url.Parse(server.URL) 122 123 if err != nil { 124 t.Fatal(err) 125 } 126 127 httpClient := client.CreateHTTPClient(false) 128 129 client := client.Client{HTTPClient: httpClient, ControllerURL: *u, Token: "abc"} 130 131 expected := api.Config{ 132 Owner: "test", 133 App: "example-go", 134 Values: map[string]interface{}{ 135 "TEST": "testing", 136 "FOO": "bar", 137 }, 138 Memory: map[string]interface{}{ 139 "web": "1G", 140 }, 141 CPU: map[string]interface{}{ 142 "web": "1000", 143 }, 144 Tags: map[string]interface{}{ 145 "test": "tests", 146 }, 147 Created: "2014-01-01T00:00:00UTC", 148 Updated: "2014-01-01T00:00:00UTC", 149 UUID: "de1bf5b5-4a72-4f94-a10c-d2a3741cdf75", 150 } 151 152 configVars := api.Config{ 153 Values: map[string]interface{}{ 154 "TEST": "testing", 155 "FOO": "bar", 156 }, 157 Memory: map[string]interface{}{ 158 "web": "1G", 159 }, 160 CPU: map[string]interface{}{ 161 "web": "1000", 162 }, 163 Tags: map[string]interface{}{ 164 "test": "tests", 165 }, 166 } 167 168 actual, err := Set(&client, "example-go", configVars) 169 170 if err != nil { 171 t.Error(err) 172 } 173 174 if !reflect.DeepEqual(expected, actual) { 175 t.Errorf("Expected %v, Got %v", expected, actual) 176 } 177 } 178 179 func TestConfigUnset(t *testing.T) { 180 t.Parallel() 181 182 handler := fakeHTTPServer{} 183 server := httptest.NewServer(&handler) 184 defer server.Close() 185 186 u, err := url.Parse(server.URL) 187 188 if err != nil { 189 t.Fatal(err) 190 } 191 192 httpClient := client.CreateHTTPClient(false) 193 194 client := client.Client{HTTPClient: httpClient, ControllerURL: *u, Token: "abc"} 195 196 expected := api.Config{ 197 Owner: "test", 198 App: "unset-test", 199 Values: map[string]interface{}{}, 200 Memory: map[string]interface{}{}, 201 CPU: map[string]interface{}{}, 202 Tags: map[string]interface{}{}, 203 Created: "2014-01-01T00:00:00UTC", 204 Updated: "2014-01-01T00:00:00UTC", 205 UUID: "de1bf5b5-4a72-4f94-a10c-d2a3741cdf75", 206 } 207 208 configVars := api.Config{ 209 Values: map[string]interface{}{ 210 "TEST": nil, 211 "FOO": nil, 212 }, 213 Memory: map[string]interface{}{ 214 "web": nil, 215 }, 216 CPU: map[string]interface{}{ 217 "web": nil, 218 }, 219 Tags: map[string]interface{}{ 220 "test": nil, 221 }, 222 } 223 224 actual, err := Set(&client, "unset-test", configVars) 225 226 if err != nil { 227 t.Error(err) 228 } 229 230 if !reflect.DeepEqual(expected, actual) { 231 t.Errorf("Expected %v, Got %v", expected, actual) 232 } 233 } 234 235 func TestConfigList(t *testing.T) { 236 t.Parallel() 237 238 handler := fakeHTTPServer{} 239 server := httptest.NewServer(&handler) 240 defer server.Close() 241 242 u, err := url.Parse(server.URL) 243 244 if err != nil { 245 t.Fatal(err) 246 } 247 248 httpClient := client.CreateHTTPClient(false) 249 250 client := client.Client{HTTPClient: httpClient, ControllerURL: *u, Token: "abc"} 251 252 expected := api.Config{ 253 Owner: "test", 254 App: "example-go", 255 Values: map[string]interface{}{ 256 "TEST": "testing", 257 "FOO": "bar", 258 }, 259 Memory: map[string]interface{}{ 260 "web": "1G", 261 }, 262 CPU: map[string]interface{}{ 263 "web": "1000", 264 }, 265 Tags: map[string]interface{}{ 266 "test": "tests", 267 }, 268 Created: "2014-01-01T00:00:00UTC", 269 Updated: "2014-01-01T00:00:00UTC", 270 UUID: "de1bf5b5-4a72-4f94-a10c-d2a3741cdf75", 271 } 272 273 actual, err := List(&client, "example-go") 274 275 if err != nil { 276 t.Error(err) 277 } 278 279 if !reflect.DeepEqual(expected, actual) { 280 t.Errorf("Expected %v, Got %v", expected, actual) 281 } 282 }