|
| 1 | +package builds |
| 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-go/controller/api" |
| 13 | + "github.com/deis/deis/client-go/controller/client" |
| 14 | + "github.com/deis/deis/version" |
| 15 | +) |
| 16 | + |
| 17 | +const buildsFixture string = ` |
| 18 | +{ |
| 19 | + "count": 1, |
| 20 | + "next": null, |
| 21 | + "previous": null, |
| 22 | + "results": [ |
| 23 | + { |
| 24 | + "app": "example-go", |
| 25 | + "created": "2014-01-01T00:00:00UTC", |
| 26 | + "dockerfile": "FROM deis/slugrunner RUN mkdir -p /app WORKDIR /app ENTRYPOINT [\"/runner/init\"] ADD slug.tgz /app ENV GIT_SHA 060da68f654e75fac06dbedd1995d5f8ad9084db", |
| 27 | + "image": "example-go", |
| 28 | + "owner": "test", |
| 29 | + "procfile": { |
| 30 | + "web": "example-go" |
| 31 | + }, |
| 32 | + "sha": "060da68f", |
| 33 | + "updated": "2014-01-01T00:00:00UTC", |
| 34 | + "uuid": "de1bf5b5-4a72-4f94-a10c-d2a3741cdf75" |
| 35 | + } |
| 36 | + ] |
| 37 | +}` |
| 38 | + |
| 39 | +const buildFixture string = ` |
| 40 | +{ |
| 41 | + "app": "example-go", |
| 42 | + "created": "2014-01-01T00:00:00UTC", |
| 43 | + "dockerfile": "", |
| 44 | + "image": "deis/example-go:latest", |
| 45 | + "owner": "test", |
| 46 | + "procfile": { |
| 47 | + "web": "example-go" |
| 48 | + }, |
| 49 | + "sha": "", |
| 50 | + "updated": "2014-01-01T00:00:00UTC", |
| 51 | + "uuid": "de1bf5b5-4a72-4f94-a10c-d2a3741cdf75" |
| 52 | +}` |
| 53 | + |
| 54 | +const buildExpected string = `{"image":"deis/example-go","procfile":{"web":"example-go"}}` |
| 55 | + |
| 56 | +type fakeHTTPServer struct{} |
| 57 | + |
| 58 | +func (fakeHTTPServer) ServeHTTP(res http.ResponseWriter, req *http.Request) { |
| 59 | + res.Header().Add("DEIS_API_VERSION", version.APIVersion) |
| 60 | + |
| 61 | + if req.URL.Path == "/v1/apps/example-go/builds/" && req.Method == "GET" { |
| 62 | + res.Write([]byte(buildsFixture)) |
| 63 | + return |
| 64 | + } |
| 65 | + |
| 66 | + if req.URL.Path == "/v1/apps/example-go/builds/" && req.Method == "POST" { |
| 67 | + body, err := ioutil.ReadAll(req.Body) |
| 68 | + |
| 69 | + if err != nil { |
| 70 | + fmt.Println(err) |
| 71 | + res.WriteHeader(http.StatusInternalServerError) |
| 72 | + res.Write(nil) |
| 73 | + } |
| 74 | + |
| 75 | + if string(body) != buildExpected { |
| 76 | + fmt.Printf("Expected '%s', Got '%s'\n", buildExpected, body) |
| 77 | + res.WriteHeader(http.StatusInternalServerError) |
| 78 | + res.Write(nil) |
| 79 | + return |
| 80 | + } |
| 81 | + |
| 82 | + res.WriteHeader(http.StatusCreated) |
| 83 | + res.Write([]byte(buildFixture)) |
| 84 | + return |
| 85 | + } |
| 86 | + |
| 87 | + fmt.Printf("Unrecognized URL %s\n", req.URL) |
| 88 | + res.WriteHeader(http.StatusNotFound) |
| 89 | + res.Write(nil) |
| 90 | +} |
| 91 | + |
| 92 | +func TestBuildsList(t *testing.T) { |
| 93 | + t.Parallel() |
| 94 | + |
| 95 | + expected := []api.Build{ |
| 96 | + api.Build{ |
| 97 | + App: "example-go", |
| 98 | + Created: "2014-01-01T00:00:00UTC", |
| 99 | + Dockerfile: "FROM deis/slugrunner RUN mkdir -p /app WORKDIR /app ENTRYPOINT [\"/runner/init\"] ADD slug.tgz /app ENV GIT_SHA 060da68f654e75fac06dbedd1995d5f8ad9084db", |
| 100 | + Image: "example-go", |
| 101 | + Owner: "test", |
| 102 | + Procfile: map[string]string{ |
| 103 | + "web": "example-go", |
| 104 | + }, |
| 105 | + Sha: "060da68f", |
| 106 | + Updated: "2014-01-01T00:00:00UTC", |
| 107 | + UUID: "de1bf5b5-4a72-4f94-a10c-d2a3741cdf75", |
| 108 | + }, |
| 109 | + } |
| 110 | + |
| 111 | + handler := fakeHTTPServer{} |
| 112 | + server := httptest.NewServer(handler) |
| 113 | + defer server.Close() |
| 114 | + |
| 115 | + u, err := url.Parse(server.URL) |
| 116 | + |
| 117 | + if err != nil { |
| 118 | + t.Fatal(err) |
| 119 | + } |
| 120 | + |
| 121 | + httpClient := client.CreateHTTPClient(false) |
| 122 | + |
| 123 | + client := client.Client{HTTPClient: httpClient, ControllerURL: *u, Token: "abc"} |
| 124 | + |
| 125 | + actual, err := List(&client, "example-go") |
| 126 | + |
| 127 | + if err != nil { |
| 128 | + t.Fatal(err) |
| 129 | + } |
| 130 | + |
| 131 | + if !reflect.DeepEqual(expected, actual) { |
| 132 | + t.Error(fmt.Errorf("Expected %v, Got %v", expected, actual)) |
| 133 | + } |
| 134 | +} |
| 135 | + |
| 136 | +func TestBuildCreate(t *testing.T) { |
| 137 | + t.Parallel() |
| 138 | + |
| 139 | + expected := api.Build{ |
| 140 | + App: "example-go", |
| 141 | + Created: "2014-01-01T00:00:00UTC", |
| 142 | + Image: "deis/example-go:latest", |
| 143 | + Owner: "test", |
| 144 | + Procfile: map[string]string{ |
| 145 | + "web": "example-go", |
| 146 | + }, |
| 147 | + Updated: "2014-01-01T00:00:00UTC", |
| 148 | + UUID: "de1bf5b5-4a72-4f94-a10c-d2a3741cdf75", |
| 149 | + } |
| 150 | + |
| 151 | + handler := fakeHTTPServer{} |
| 152 | + server := httptest.NewServer(handler) |
| 153 | + defer server.Close() |
| 154 | + |
| 155 | + u, err := url.Parse(server.URL) |
| 156 | + |
| 157 | + if err != nil { |
| 158 | + t.Fatal(err) |
| 159 | + } |
| 160 | + |
| 161 | + httpClient := client.CreateHTTPClient(false) |
| 162 | + |
| 163 | + client := client.Client{HTTPClient: httpClient, ControllerURL: *u, Token: "abc"} |
| 164 | + |
| 165 | + procfile := map[string]string{ |
| 166 | + "web": "example-go", |
| 167 | + } |
| 168 | + |
| 169 | + actual, err := New(&client, "example-go", "deis/example-go", procfile) |
| 170 | + |
| 171 | + if err != nil { |
| 172 | + t.Fatal(err) |
| 173 | + } |
| 174 | + |
| 175 | + if !reflect.DeepEqual(expected, actual) { |
| 176 | + t.Error(fmt.Errorf("Expected %v, Got %v", expected, actual)) |
| 177 | + } |
| 178 | +} |
0 commit comments