This repository has been archived by the owner on Apr 28, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 36
/
sail_helpers_test.go
210 lines (170 loc) · 4.58 KB
/
sail_helpers_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
package main
import (
"context"
"math/rand"
"path/filepath"
"testing"
"time"
"github.com/docker/docker/api/types"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.coder.com/sail/internal/dockutil"
"go.coder.com/sail/internal/xnet"
)
func init() {
rand.Seed(time.Now().UnixNano())
}
type params struct {
rb *rollback
proj *project
bldr *hatBuilder
runner *runner
port string
}
// run sets up our params test suite. It ensures that the project image
// and hat image are applied if one is specified, and the container is
// started. Once all of the setup is done, then any of the provided test
// functions can be run.
func run(t *testing.T, name, repo, hatPath string, fns ...func(t *testing.T, p *params)) {
t.Run(name, func(t *testing.T) {
t.Parallel()
p := ¶ms{
rb: newRollback(),
}
defer p.rb.run()
conf := mustReadConfig(filepath.Join(metaRoot(), ".sail.toml"))
repo, err := parseRepo("ssh", "github.com", "", repo)
require.NoError(t, err)
p.proj = &project{
conf: conf,
repo: repo,
}
// Ensure our project repo is cloned to the local machine.
err = p.proj.ensureDir()
require.NoError(t, err)
p.rb.add(func() {
// TODO: Do we want to remove this? I accidentally deleted
// my own sail path that I was developing in...
// err := os.RemoveAll(p.proj.localDir())
// require.NoError(t, err)
})
// Use the project's custom sail image if one is built.
baseImage, isCustom, err := p.proj.buildImage()
require.NoError(t, err)
if !isCustom {
baseImage = p.proj.conf.DefaultImage
} else {
p.rb.add(func() {
requireImageRemove(t, baseImage)
})
}
image := baseImage
// Create the hat builder and apply the hat if one
// is specified.
p.bldr = &hatBuilder{
hatPath: hatPath,
baseImage: baseImage,
}
if hatPath != "" {
image, err = p.bldr.applyHat()
require.NoError(t, err)
p.rb.add(func() {
requireImageRemove(t, image)
})
}
// Construct our container runner and run
// the container.
p.port, err = xnet.FindAvailablePort()
require.NoError(t, err)
p.runner = &runner{
projectName: p.proj.repo.BaseName(),
projectLocalDir: p.proj.localDir(),
cntName: p.proj.cntName(),
hostname: p.proj.repo.BaseName(),
port: p.port,
}
err = p.runner.runContainer(image)
require.NoError(t, err)
p.rb.add(func() {
requireContainerRemove(t, p.proj.cntName())
})
// Iterate through all the provided testing functions.
for _, fn := range fns {
fn(t, p)
}
})
}
func requireProjectsNotRunning(t *testing.T, projects ...string) {
runningProjects, err := listProjects()
require.NoError(t, err)
for _, proj := range projects {
for _, runningProj := range runningProjects {
require.NotEqual(t,
proj, runningProj.name,
"Unable to run tests, %s currently running and needed for tests", proj,
)
}
}
}
func requireGetImageLabels(t *testing.T, image string) map[string]string {
return requireImageInspect(t, image).ContainerConfig.Labels
}
func requireImageInspect(t *testing.T, image string) types.ImageInspect {
cli := dockerClient()
defer cli.Close()
insp, _, err := cli.ImageInspectWithRaw(context.Background(), image)
require.NoError(t, err)
return insp
}
func requireGetContainerLabels(t *testing.T, cntName string) map[string]string {
return requireContainerInspect(t, cntName).Config.Labels
}
func requireContainerInspect(t *testing.T, cntName string) types.ContainerJSON {
cli := dockerClient()
defer cli.Close()
insp, err := cli.ContainerInspect(context.Background(), cntName)
require.NoError(t, err)
return insp
}
func assertLabel(t *testing.T, labels map[string]string, key, val string) {
assert.Contains(t, labels, key)
assert.Equal(t, val, labels[key])
}
func requireImageRemove(t *testing.T, image string) {
cli := dockerClient()
defer cli.Close()
_, err := cli.ImageRemove(
context.Background(),
image,
types.ImageRemoveOptions{
Force: false,
PruneChildren: true,
},
)
require.NoError(t, err)
}
func requireContainerRemove(t *testing.T, cntName string) {
cli := dockerClient()
defer cli.Close()
err := dockutil.StopRemove(context.Background(), cli, cntName)
require.NoError(t, err)
}
func requireUbuntuDevImage(t *testing.T) {
require.NoError(t, ensureImage("codercom/ubuntu-dev"))
}
type rollback struct {
fns []func()
}
func newRollback() *rollback {
return &rollback{
fns: make([]func(), 0),
}
}
func (r *rollback) run() {
for i := len(r.fns) - 1; i >= 0; i-- {
r.fns[i]()
}
}
func (r *rollback) add(fn func()) {
r.fns = append(r.fns, fn)
}