This repository has been archived by the owner on Aug 29, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 80
/
integration_test.go
275 lines (225 loc) · 8.69 KB
/
integration_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
package sti
import (
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"os"
"path"
"runtime"
"testing"
"time"
. "launchpad.net/gocheck"
"github.com/fsouza/go-dockerclient"
)
// Register gocheck with the 'testing' runner
func Test(t *testing.T) { TestingT(t) }
type IntegrationTestSuite struct {
dockerClient *docker.Client
}
// Register IntegrationTestSuite with the gocheck suite manager and add support for 'go test' flags,
// viz: go test -integration
var (
_ = Suite(&IntegrationTestSuite{})
integration = flag.Bool("integration", false, "Include integration tests")
)
const (
DockerSocket = "unix:///var/run/docker.sock"
TestSource = "git://github.com/pmorie/simple-html"
FakeBaseImage = "sti_test/sti-fake"
FakeUserImage = "sti_test/sti-fake-user"
FakeBrokenBaseImage = "sti_test/sti-fake-broken"
TagCleanBuild = "test/sti-fake-app"
TagCleanBuildUser = "test/sti-fake-app-user"
TagIncrementalBuild = "test/sti-incremental-app"
TagIncrementalBuildUser = "test/sti-incremental-app-user"
// Need to serve the scripts from localhost so any potential changes to the
// scripts are made available for integration testing.
//
// Port 23456 must match the port used in the fake image Dockerfiles
FakeScriptsHttpUrl = "http://localhost:23456/sti-fake/.sti/bin"
)
var (
FakeScriptsFileUrl string
)
// Suite/Test fixtures are provided by gocheck
func (s *IntegrationTestSuite) SetUpSuite(c *C) {
if !*integration {
c.Skip("-integration not provided")
}
// get the full path to this .go file so we can construct the file url
// using this file's dirname
_, filename, _, _ := runtime.Caller(0)
testImagesDir := path.Join(path.Dir(path.Dir(filename)), "tests", "images")
FakeScriptsFileUrl = "file://" + path.Join(testImagesDir, "sti-fake", ".sti", "bin")
s.dockerClient, _ = docker.NewClient(DockerSocket)
for _, image := range []string{TagCleanBuild, TagCleanBuildUser, TagIncrementalBuild, TagIncrementalBuildUser} {
s.dockerClient.RemoveImage(image)
}
go http.ListenAndServe(":23456", http.FileServer(http.Dir(testImagesDir)))
fmt.Printf("Waiting for mock HTTP server to start...")
if err := waitForHttpReady(); err != nil {
fmt.Printf("[ERROR] Unable to start mock HTTP server: %s\n", err)
}
fmt.Println("done")
}
// Wait for the mock HTTP server to become ready to serve the HTTP requests.
//
func waitForHttpReady() error {
retryCount := 50
for {
if resp, err := http.Get("http://localhost:23456/"); err != nil {
resp.Body.Close()
if retryCount -= 1; retryCount > 0 {
time.Sleep(20 * time.Millisecond)
} else {
return err
}
} else {
return nil
}
}
}
func (s *IntegrationTestSuite) SetUpTest(c *C) {
}
// TestXxxx methods are identified as test cases
// Test a clean build. The simplest case.
func (s *IntegrationTestSuite) TestCleanBuild(c *C) {
s.exerciseCleanBuild(c, TagCleanBuild, false, FakeBaseImage, "")
}
func (s *IntegrationTestSuite) TestCleanBuildUser(c *C) {
s.exerciseCleanBuild(c, TagCleanBuildUser, false, FakeUserImage, "")
}
func (s *IntegrationTestSuite) TestCleanBuildFileScriptsUrl(c *C) {
s.exerciseCleanBuild(c, TagCleanBuild, false, FakeBaseImage, FakeScriptsFileUrl)
}
func (s *IntegrationTestSuite) TestCleanBuildHttpScriptsUrl(c *C) {
s.exerciseCleanBuild(c, TagCleanBuild, false, FakeBaseImage, FakeScriptsHttpUrl)
}
// Test that a build request with a callbackUrl will invoke HTTP endpoint
func (s *IntegrationTestSuite) TestCleanBuildCallbackInvoked(c *C) {
s.exerciseCleanBuild(c, TagCleanBuild, true, FakeBaseImage, "")
}
func (s *IntegrationTestSuite) exerciseCleanBuild(c *C, tag string, verifyCallback bool, imageName string, scriptsUrl string) {
callbackUrl := ""
callbackInvoked := false
callbackHasValidJson := false
if verifyCallback {
handler := func(w http.ResponseWriter, r *http.Request) {
// we got called
callbackInvoked = true
// the header is as expected
contentType := r.Header["Content-Type"][0]
callbackHasValidJson = contentType == "application/json"
// the request body is as expected
if callbackHasValidJson {
defer r.Body.Close()
body, _ := ioutil.ReadAll(r.Body)
type CallbackMessage struct {
Payload string
Success bool
}
var callbackMessage CallbackMessage
err := json.Unmarshal(body, &callbackMessage)
callbackHasValidJson = (err == nil) && (callbackMessage.Success)
}
}
ts := httptest.NewServer(http.HandlerFunc(handler))
defer ts.Close()
callbackUrl = ts.URL
}
req := &STIRequest{
DockerSocket: DockerSocket,
Verbose: true,
BaseImage: imageName,
Source: TestSource,
Tag: tag,
Clean: true,
Writer: os.Stdout,
CallbackUrl: callbackUrl,
ScriptsUrl: scriptsUrl}
resp, err := Build(req)
c.Assert(err, IsNil, Commentf("Sti build failed"))
c.Assert(resp.Success, Equals, true, Commentf("Sti build failed"))
c.Assert(callbackInvoked, Equals, verifyCallback, Commentf("Sti build did not invoke callback"))
c.Assert(callbackHasValidJson, Equals, verifyCallback, Commentf("Sti build did not invoke callback with valid json message"))
s.checkForImage(c, tag)
containerId := s.createContainer(c, tag)
defer s.removeContainer(containerId)
s.checkBasicBuildState(c, containerId, resp.WorkingDir)
}
// Test an incremental build.
func (s *IntegrationTestSuite) TestIncrementalBuildAndRemovePreviousImage(c *C) {
s.exerciseIncrementalBuild(c, TagIncrementalBuild, true)
}
func (s *IntegrationTestSuite) TestIncrementalBuildAndKeepPreviousImage(c *C) {
s.exerciseIncrementalBuild(c, TagIncrementalBuild, true)
}
func (s *IntegrationTestSuite) TestIncrementalBuildUser(c *C) {
s.exerciseIncrementalBuild(c, TagIncrementalBuildUser, true)
}
func (s *IntegrationTestSuite) exerciseIncrementalBuild(c *C, tag string, removePreviousImage bool) {
req := &STIRequest{
DockerSocket: DockerSocket,
Verbose: true,
BaseImage: FakeBaseImage,
Source: TestSource,
Tag: tag,
Clean: true,
RemovePreviousImage: removePreviousImage,
Writer: os.Stdout}
resp, err := Build(req)
c.Assert(err, IsNil, Commentf("Sti build failed"))
c.Assert(resp.Success, Equals, true, Commentf("Sti build failed"))
previousImageId := resp.ImageID
req.Clean = false
resp, err = Build(req)
c.Assert(err, IsNil, Commentf("Sti build failed"))
c.Assert(resp.Success, Equals, true, Commentf("Sti build failed"))
s.checkForImage(c, tag)
containerId := s.createContainer(c, tag)
defer s.removeContainer(containerId)
s.checkIncrementalBuildState(c, containerId, resp.WorkingDir)
_, err = s.dockerClient.InspectImage(previousImageId)
if removePreviousImage {
c.Assert(err, NotNil, Commentf("Previous image %s not deleted", previousImageId))
} else {
c.Assert(err, IsNil, Commentf("Coudln't find previous image %s", previousImageId))
}
}
// Support methods
func (s *IntegrationTestSuite) checkForImage(c *C, tag string) {
_, err := s.dockerClient.InspectImage(tag)
c.Assert(err, IsNil, Commentf("Couldn't find built image"))
}
func (s *IntegrationTestSuite) createContainer(c *C, image string) string {
config := docker.Config{Image: image, AttachStdout: false, AttachStdin: false}
container, err := s.dockerClient.CreateContainer(docker.CreateContainerOptions{Name: "", Config: &config})
c.Assert(err, IsNil, Commentf("Couldn't create container from image %s", image))
err = s.dockerClient.StartContainer(container.ID, &docker.HostConfig{})
c.Assert(err, IsNil, Commentf("Couldn't start container: %s", container.ID))
exitCode, err := s.dockerClient.WaitContainer(container.ID)
c.Assert(exitCode, Equals, 0, Commentf("Bad exit code from container: %d", exitCode))
return container.ID
}
func (s *IntegrationTestSuite) removeContainer(cId string) {
s.dockerClient.RemoveContainer(docker.RemoveContainerOptions{cId, true, true})
}
func (s *IntegrationTestSuite) checkFileExists(c *C, cId string, filePath string) {
res := FileExistsInContainer(s.dockerClient, cId, filePath)
c.Assert(res, Equals, true, Commentf("Couldn't find file %s in container %s", filePath, cId))
}
func (s *IntegrationTestSuite) checkBasicBuildState(c *C, cId string, workingDir string) {
s.checkFileExists(c, cId, "/sti-fake/assemble-invoked")
s.checkFileExists(c, cId, "/sti-fake/run-invoked")
s.checkFileExists(c, cId, "/sti-fake/src/index.html")
_, err := os.Stat(workingDir)
c.Assert(err, NotNil) // workingDir shouldn't exist, so err should be non-nil
c.Assert(os.IsNotExist(err), Equals, true) // err should be IsNotExist
}
func (s *IntegrationTestSuite) checkIncrementalBuildState(c *C, cId string, workingDir string) {
s.checkBasicBuildState(c, cId, workingDir)
s.checkFileExists(c, cId, "/sti-fake/save-artifacts-invoked")
}