/
main.go
122 lines (104 loc) · 2.82 KB
/
main.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
package build
import (
"context"
"fmt"
"io/ioutil"
"os"
"testing"
log "github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/kelda/blimp/ci/util"
)
type Test struct{}
func (test Test) GetName() string {
return "Build"
}
var buildTestFiles = map[string]string{
"docker-compose.yml": `version: '3'
services:
red:
build:
context: .
dockerfile: ./Dockerfile.red
command: tail -f /dev/null
blue:
build:
context: .
dockerfile: ./Dockerfile.blue
command: tail -f /dev/null
green:
build:
context: .
dockerfile: ./Dockerfile.green
command: tail -f /dev/null
`,
"Dockerfile.red": `FROM ubuntu:18.04
COPY file /file
`,
"Dockerfile.green": `FROM ubuntu:18.04
COPY file /file
`,
"Dockerfile.blue": `FROM ubuntu:18.04
COPY file /file
`,
"file": "1",
}
func (test Test) Run(ctx context.Context, t *testing.T) {
dir, err := util.MakeTestDirectory(buildTestFiles)
require.NoError(t, err)
defer os.RemoveAll(dir)
os.Chdir(dir)
// Start blimp.
log.Info("Booting the initial images")
upCtx, cancelUp := context.WithCancel(ctx)
waitErr, err := util.Up(upCtx)
require.NoError(t, err, "start blimp up")
checkFiles(ctx, t, map[string]string{
"red": "1",
"green": "1",
"blue": "1",
}, "Pre-rebuild")
// Update the file, and rebuild green.
log.Info("Booting the rebuilt version of green")
require.NoError(t, ioutil.WriteFile("file", []byte("2"), 0644))
out, err := util.Run(ctx, "build", "green")
require.NoError(t, err, string(out))
// Restart blimp.
cancelUp()
require.NoError(t, <-waitErr, "run blimp up")
upCtx, cancelUp = context.WithCancel(ctx)
waitErr, err = util.Up(upCtx)
require.NoError(t, err, "start blimp up")
checkFiles(ctx, t, map[string]string{
"red": "1",
"green": "2",
"blue": "1",
}, "Rebuild green")
// Update the file again, and rebuild red and blue in parallel.
log.Info("Booting the rebuilt versions of red and blue")
require.NoError(t, ioutil.WriteFile("file", []byte("3"), 0644))
out, err = util.Run(ctx, "build", "red", "blue")
require.NoError(t, err, string(out))
// Restart blimp.
cancelUp()
require.NoError(t, <-waitErr, "run blimp up")
upCtx, cancelUp = context.WithCancel(ctx)
waitErr, err = util.Up(upCtx)
require.NoError(t, err, "start blimp up")
checkFiles(ctx, t, map[string]string{
"red": "3",
"green": "2",
"blue": "3",
}, "Rebuild red and blue")
// Wait for blimp to shutdown.
cancelUp()
require.NoError(t, <-waitErr, "run blimp up")
}
func checkFiles(ctx context.Context, t *testing.T, serviceToContents map[string]string, msg string) {
for svc, exp := range serviceToContents {
actual, err := util.Run(ctx, "exec", svc, "cat", "/file")
require.NoError(t, err)
assert.Equal(t, exp, string(actual), fmt.Sprintf("%s: %s", svc, msg))
}
}