github.com/naphatkrit/deis@v1.12.3/client/controller/models/releases/releases_test.go (about) 1 package releases 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 releasesFixture string = ` 18 { 19 "count": 3, 20 "next": null, 21 "previous": null, 22 "results": [ 23 { 24 "app": "example-go", 25 "build": null, 26 "config": "95bd6dea-1685-4f78-a03d-fd7270b058d1", 27 "created": "2014-01-01T00:00:00UTC", 28 "owner": "test", 29 "summary": "test created initial release", 30 "updated": "2014-01-01T00:00:00UTC", 31 "uuid": "de1bf5b5-4a72-4f94-a10c-d2a3741cdf75", 32 "version": 1 33 } 34 ] 35 }` 36 37 const releaseFixture string = ` 38 { 39 "app": "example-go", 40 "build": null, 41 "config": "95bd6dea-1685-4f78-a03d-fd7270b058d1", 42 "created": "2014-01-01T00:00:00UTC", 43 "owner": "test", 44 "summary": "test created initial release", 45 "updated": "2014-01-01T00:00:00UTC", 46 "uuid": "de1bf5b5-4a72-4f94-a10c-d2a3741cdf75", 47 "version": 1 48 } 49 ` 50 51 const rollbackFixture string = ` 52 {"version": 5} 53 ` 54 const rollbackerFixture string = ` 55 {"version": 7} 56 ` 57 58 const rollbackExpected string = `{"version":2}` 59 const rollbackerExpected string = `` 60 61 type fakeHTTPServer struct{} 62 63 func (fakeHTTPServer) ServeHTTP(res http.ResponseWriter, req *http.Request) { 64 res.Header().Add("DEIS_API_VERSION", version.APIVersion) 65 66 if req.URL.Path == "/v1/apps/example-go/releases/" && req.Method == "GET" { 67 res.Write([]byte(releasesFixture)) 68 return 69 } 70 71 if req.URL.Path == "/v1/apps/example-go/releases/v1/" && req.Method == "GET" { 72 res.Write([]byte(releaseFixture)) 73 return 74 } 75 76 if req.URL.Path == "/v1/apps/example-go/releases/rollback/" && req.Method == "POST" { 77 body, err := ioutil.ReadAll(req.Body) 78 79 if err != nil { 80 fmt.Println(err) 81 res.WriteHeader(http.StatusInternalServerError) 82 res.Write(nil) 83 } 84 85 if string(body) != rollbackExpected { 86 fmt.Printf("Expected '%s', Got '%s'\n", rollbackExpected, body) 87 res.WriteHeader(http.StatusInternalServerError) 88 res.Write(nil) 89 return 90 } 91 92 res.WriteHeader(http.StatusCreated) 93 res.Write([]byte(rollbackFixture)) 94 return 95 } 96 97 if req.URL.Path == "/v1/apps/rollbacker/releases/rollback/" && req.Method == "POST" { 98 body, err := ioutil.ReadAll(req.Body) 99 100 if err != nil { 101 fmt.Println(err) 102 res.WriteHeader(http.StatusInternalServerError) 103 res.Write(nil) 104 } 105 106 if string(body) != rollbackerExpected { 107 fmt.Printf("Expected '%s', Got '%s'\n", rollbackerExpected, body) 108 res.WriteHeader(http.StatusInternalServerError) 109 res.Write(nil) 110 return 111 } 112 113 res.WriteHeader(http.StatusCreated) 114 res.Write([]byte(rollbackerFixture)) 115 return 116 } 117 118 fmt.Printf("Unrecognized URL %s\n", req.URL) 119 res.WriteHeader(http.StatusNotFound) 120 res.Write(nil) 121 } 122 123 func TestReleasesList(t *testing.T) { 124 t.Parallel() 125 126 expected := []api.Release{ 127 api.Release{ 128 App: "example-go", 129 Build: "", 130 Config: "95bd6dea-1685-4f78-a03d-fd7270b058d1", 131 Created: "2014-01-01T00:00:00UTC", 132 Owner: "test", 133 Summary: "test created initial release", 134 Updated: "2014-01-01T00:00:00UTC", 135 UUID: "de1bf5b5-4a72-4f94-a10c-d2a3741cdf75", 136 Version: 1, 137 }, 138 } 139 140 handler := fakeHTTPServer{} 141 server := httptest.NewServer(handler) 142 defer server.Close() 143 144 u, err := url.Parse(server.URL) 145 146 if err != nil { 147 t.Fatal(err) 148 } 149 150 httpClient := client.CreateHTTPClient(false) 151 152 client := client.Client{HTTPClient: httpClient, ControllerURL: *u, Token: "abc"} 153 154 actual, _, err := List(&client, "example-go", 100) 155 156 if err != nil { 157 t.Fatal(err) 158 } 159 160 if !reflect.DeepEqual(expected, actual) { 161 t.Error(fmt.Errorf("Expected %v, Got %v", expected, actual)) 162 } 163 } 164 165 func TestReleasesGet(t *testing.T) { 166 t.Parallel() 167 168 expected := api.Release{ 169 App: "example-go", 170 Build: "", 171 Config: "95bd6dea-1685-4f78-a03d-fd7270b058d1", 172 Created: "2014-01-01T00:00:00UTC", 173 Owner: "test", 174 Summary: "test created initial release", 175 Updated: "2014-01-01T00:00:00UTC", 176 UUID: "de1bf5b5-4a72-4f94-a10c-d2a3741cdf75", 177 Version: 1, 178 } 179 180 handler := fakeHTTPServer{} 181 server := httptest.NewServer(handler) 182 defer server.Close() 183 184 u, err := url.Parse(server.URL) 185 186 if err != nil { 187 t.Fatal(err) 188 } 189 190 httpClient := client.CreateHTTPClient(false) 191 192 client := client.Client{HTTPClient: httpClient, ControllerURL: *u, Token: "abc"} 193 194 actual, err := Get(&client, "example-go", 1) 195 196 if err != nil { 197 t.Fatal(err) 198 } 199 200 if !reflect.DeepEqual(expected, actual) { 201 t.Error(fmt.Errorf("Expected %v, Got %v", expected, actual)) 202 } 203 } 204 205 func TestRollback(t *testing.T) { 206 t.Parallel() 207 208 expected := 5 209 210 handler := fakeHTTPServer{} 211 server := httptest.NewServer(handler) 212 defer server.Close() 213 214 u, err := url.Parse(server.URL) 215 216 if err != nil { 217 t.Fatal(err) 218 } 219 220 httpClient := client.CreateHTTPClient(false) 221 222 client := client.Client{HTTPClient: httpClient, ControllerURL: *u, Token: "abc"} 223 224 actual, err := Rollback(&client, "example-go", 2) 225 226 if err != nil { 227 t.Fatal(err) 228 } 229 230 if expected != actual { 231 t.Error(fmt.Errorf("Expected %v, Got %v", expected, actual)) 232 } 233 } 234 235 func TestRollbacker(t *testing.T) { 236 t.Parallel() 237 238 expected := 7 239 240 handler := fakeHTTPServer{} 241 server := httptest.NewServer(handler) 242 defer server.Close() 243 244 u, err := url.Parse(server.URL) 245 246 if err != nil { 247 t.Fatal(err) 248 } 249 250 httpClient := client.CreateHTTPClient(false) 251 252 client := client.Client{HTTPClient: httpClient, ControllerURL: *u, Token: "abc"} 253 254 actual, err := Rollback(&client, "rollbacker", -1) 255 256 if err != nil { 257 t.Fatal(err) 258 } 259 260 if expected != actual { 261 t.Error(fmt.Errorf("Expected %v, Got %v", expected, actual)) 262 } 263 }