generated from brevdev/seed
-
Notifications
You must be signed in to change notification settings - Fork 9
/
setup.go
521 lines (442 loc) · 14.3 KB
/
setup.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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
package setup
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"testing"
"time"
breverrors "github.com/brevdev/brev-cli/pkg/errors"
"github.com/brevdev/brev-cli/pkg/files"
"github.com/brevdev/brev-cli/pkg/store"
"github.com/hashicorp/go-multierror"
"github.com/stretchr/testify/assert"
)
type WorkspaceTestClient struct {
SetupParams *store.SetupParamsV0
ContainerParams []ContainerParams
TestWorkspaces []Workspace
}
type ContainerParams struct {
Name string
Image string
Ports []string
}
type BrevBinaryPath struct {
BinaryPath string
}
func (b BrevBinaryPath) ApplyWorkspaceTestClientOption(allOptions *AllWorkspaceTestClientOption) {
allOptions.BrevBinaryPath = b.BinaryPath
}
type WorkspaceTestClientOption interface {
ApplyWorkspaceTestClientOption(allOptions *AllWorkspaceTestClientOption)
}
type AllWorkspaceTestClientOption struct {
BrevBinaryPath string
TestNamePrefix string
}
func NewWorkspaceTestClientOptions(options []WorkspaceTestClientOption) AllWorkspaceTestClientOption {
allOptions := AllWorkspaceTestClientOption{}
for _, o := range options {
o.ApplyWorkspaceTestClientOption(&allOptions)
}
return allOptions
}
type TestNamePrefix struct {
Name string
}
func (t TestNamePrefix) ApplyWorkspaceTestClientOption(allOptions *AllWorkspaceTestClientOption) {
allOptions.TestNamePrefix = t.Name
}
func NewWorkspaceTestClient(setupParams *store.SetupParamsV0, containerParams []ContainerParams, options ...WorkspaceTestClientOption) *WorkspaceTestClient {
allOptions := NewWorkspaceTestClientOptions(options)
if allOptions.TestNamePrefix == "" {
pc, _, _, ok := runtime.Caller(1)
if !ok {
panic("not ok")
}
details := runtime.FuncForPC(pc)
allOptions.TestNamePrefix = strings.Split(details.Name(), ".")[2]
}
workspaces := []Workspace{}
for _, p := range containerParams {
containerName := fmt.Sprintf("%s-%s", allOptions.TestNamePrefix, p.Name)
// [a-zA-Z0-9][a-zA-Z0-9_.-]
workspace := NewTestWorkspace(allOptions.BrevBinaryPath, containerName, p.Image, p.Ports, setupParams)
err := workspace.Done()
if err != nil {
fmt.Println(err)
}
workspace.ShowOut = true
workspaces = append(workspaces, workspace)
}
return &WorkspaceTestClient{
SetupParams: setupParams,
ContainerParams: containerParams,
TestWorkspaces: workspaces,
}
}
func (w WorkspaceTestClient) Done() error {
var allError error
for _, w := range w.TestWorkspaces {
err := w.Done()
if err != nil {
allError = multierror.Append(allError, err)
}
}
if allError != nil {
return breverrors.WrapAndTrace(allError)
}
return nil
}
type workspaceTest func(workspace Workspace, err error)
func (w WorkspaceTestClient) Test(test workspaceTest) error {
for _, w := range w.TestWorkspaces {
err := w.Setup()
test(w, err)
}
return nil
}
type Workspace interface {
Setup() error
Done() error
Reset() error
Exec(arg ...string) ([]byte, error) // always returns []byte even if error since stdout/err is still useful
Copy(src string, dest string) error
UpdateParams(*store.SetupParamsV0)
}
type ExecResult interface {
CombinedOutput() ([]byte, error)
}
type TestWorkspace struct {
SetupParams *store.SetupParamsV0
ContainerName string
Image string
Ports []string
TestBrevBinary string // path to brev binary that should be tested
ShowOut bool
}
var _ Workspace = &TestWorkspace{}
func NewTestWorkspace(testBrevBinaryPath string, containerName string, image string, ports []string, setupParams *store.SetupParamsV0) *TestWorkspace {
return &TestWorkspace{SetupParams: setupParams, ContainerName: containerName, Ports: ports, Image: image, TestBrevBinary: testBrevBinaryPath}
}
func (w *TestWorkspace) UpdateParams(params *store.SetupParamsV0) {
w.SetupParams = params
}
func sendToOut(c *exec.Cmd) {
c.Stderr = os.Stderr
c.Stdout = os.Stdout
}
func (w TestWorkspace) getWorkspaceVolumeName() string {
return fmt.Sprintf("%s-workspace", w.ContainerName)
}
func (w TestWorkspace) Setup() error {
ports := []string{}
for _, p := range w.Ports {
ports = append(ports, "-p", p)
}
dockerRunArgs := append([]string{
"-d",
"--privileged=true",
fmt.Sprintf("--name=%s", w.ContainerName),
// "-v", fmt.Sprintf("%s:/home/brev/workspace", w.getWorkspaceVolumeName()),
"-v", fmt.Sprintf("%s:/home/brev", w.getWorkspaceVolumeName()),
"--rm", "-it",
}, ports...)
dockerRunArgs = append(append([]string{"run"}, dockerRunArgs...), []string{w.Image, "bash"}...)
time.Sleep(time.Second * 2)
cmdR := exec.Command("docker", dockerRunArgs...) //nolint:gosec // for tests
if w.ShowOut {
sendToOut(cmdR)
}
err := cmdR.Run()
if err != nil {
return breverrors.WrapAndTrace(err)
}
_, err = w.Exec("mkdir", "-p", "/etc/meta")
if err != nil {
return breverrors.WrapAndTrace(err)
}
tmpSetupFile := fmt.Sprintf("/tmp/%s_setup_v0.json", w.ContainerName)
err = files.OverwriteJSON(files.AppFs, tmpSetupFile, w.SetupParams)
if err != nil {
return breverrors.WrapAndTrace(err)
}
setupPath := fmt.Sprintf("%s:/etc/meta/setup_v0.json", w.ContainerName)
err = w.Copy(tmpSetupFile, setupPath)
if err != nil {
return breverrors.WrapAndTrace(err)
}
brevBinPath := "/usr/bin/brev"
binPath := fmt.Sprintf("%s:%s", w.ContainerName, brevBinPath)
err = w.Copy(w.TestBrevBinary, binPath)
if err != nil {
return breverrors.WrapAndTrace(err)
}
res, err := w.Exec(brevBinPath, "--version")
if err != nil {
return breverrors.WrapAndTrace(err)
}
if !strings.Contains(string(res), "dev-") {
return fmt.Errorf("not dev version")
}
_, err = w.Exec(brevBinPath, "setupworkspace", "--force-enable")
if err != nil {
return breverrors.WrapAndTrace(err)
}
return nil
}
func (w TestWorkspace) Exec(arg ...string) ([]byte, error) {
arg = append([]string{"exec", w.ContainerName}, arg...)
cmdM := exec.Command("docker", arg...)
out, err := cmdM.CombinedOutput()
if w.ShowOut {
fmt.Print(string(out))
}
if err != nil {
return out, breverrors.WrapAndTrace(err)
}
return out, nil
}
func (w TestWorkspace) Copy(src string, dest string) error {
cmdC := exec.Command("docker", "cp", src, dest)
if w.ShowOut {
sendToOut(cmdC)
}
err := cmdC.Run()
if err != nil {
return breverrors.WrapAndTrace(err)
}
return nil
}
func (w TestWorkspace) Reset() error {
err := w.KillContainer()
if err != nil {
return breverrors.WrapAndTrace(err)
}
err = w.Setup()
if err != nil {
return breverrors.WrapAndTrace(err)
}
return nil
}
func (w TestWorkspace) KillContainer() error {
cmd := exec.Command("docker", "kill", w.ContainerName) //nolint:gosec // for tests
if w.ShowOut {
sendToOut(cmd)
}
err := cmd.Run()
if err != nil {
return breverrors.WrapAndTrace(err)
}
return nil
}
func (w TestWorkspace) RmContainer() error {
cmd := exec.Command("docker", "rm", w.ContainerName) //nolint:gosec // for tests
if w.ShowOut {
sendToOut(cmd)
}
err := cmd.Run()
if err != nil {
return breverrors.WrapAndTrace(err)
}
return nil
}
func (w TestWorkspace) RmVolume() error {
cmd := exec.Command("docker", "volume", "rm", w.getWorkspaceVolumeName()) //nolint:gosec // for tests
if w.ShowOut {
sendToOut(cmd)
}
err := cmd.Run()
if err != nil {
return breverrors.WrapAndTrace(err)
}
return nil
}
func (w TestWorkspace) Done() error {
err := w.KillContainer()
if err != nil {
return breverrors.WrapAndTrace(err)
}
err = w.RmContainer()
if err != nil {
fmt.Println(err)
}
err = w.RmVolume()
if err != nil {
return breverrors.WrapAndTrace(err)
}
return nil
}
func NewTestSetupParams(keyPair *store.KeyPair) *store.SetupParamsV0 {
return &store.SetupParamsV0{
WorkspaceHost: "name-rand-org.x.y",
WorkspacePort: 22778,
WorkspaceBaseRepo: "github.com:brevdev/test-repo-user-config.git",
WorkspaceProjectRepo: "github.com:brevdev/test-repo-dotbrev.git",
WorkspaceApplicationStartScripts: []string{},
WorkspaceUsername: "brevtest",
WorkspaceEmail: "test+3333@gmail.com",
WorkspacePassword: "12345",
WorkspaceKeyPair: keyPair,
ProjectSetupScript: nil,
DisableSetup: true,
}
}
func GetTestKeys(prefix string) (*store.KeyPair, error) {
kp := store.KeyPair{}
path := filepath.Join(prefix, "assets", "test_keypair.json")
err := files.ReadJSON(files.AppFs, path, &kp)
if err != nil {
return nil, breverrors.WrapAndTrace(err)
}
return &kp, nil
}
func GetUnauthedTestKeys(prefix string) (*store.KeyPair, error) {
kp := store.KeyPair{}
path := filepath.Join(prefix, "assets", "test_keypair.json")
err := files.ReadJSON(files.AppFs, path, &kp)
if err != nil {
return nil, breverrors.WrapAndTrace(err)
}
return &kp, nil
}
func AssertWorkspaceSetup(t *testing.T, w Workspace, password string, host string) {
t.Helper()
AssertUser(t, w, "root")
AssertCwd(t, w, "/home/brev/workspace")
AssertInternalCurlOuputContains(t, w, "localhost:22778", "Found. Redirecting to ./login", 100)
AssertInternalCurlOuputContains(t, w, "localhost:22779/proxy", "Bad Request", 100)
AssertFileContainsString(t, w, "/home/brev/.config/code-server/config.yaml", password)
AssertFileContainsString(t, w, "/home/brev/.config/code-server/config.yaml", host)
AssertInternalSSHServerRunning(t, w, "/home/brev/.ssh/id_rsa", "brev", "ls")
AssertDockerRunning(t, w)
}
func AssertValidBrevBaseRepoSetup(t *testing.T, w Workspace, repoPath string) {
t.Helper()
AssertPathExists(t, w, repoPath)
AssertPathExists(t, w, fmt.Sprintf("%s/.git", repoPath))
AssertPathExists(t, w, fmt.Sprintf("%s/.brev", repoPath))
AssertPathExists(t, w, fmt.Sprintf("%s/.brev/setup.sh", repoPath))
AssertPathExists(t, w, fmt.Sprintf("%s/.brev/logs", repoPath))
}
func AssertValidUserBrevSetup(t *testing.T, w Workspace, repoPath string) {
t.Helper()
AssertValidBrevBaseRepoSetup(t, w, repoPath)
// AssertPathNotExist(t, w, fmt.Sprintf("%s/.brev/ports.yaml", repoPath))
}
func AssertTestUserRepoSetupRan(t *testing.T, w Workspace, repoPath string) {
t.Helper()
AssertFileContainsString(t, w, fmt.Sprintf("%s/.brev/logs/setup.log", repoPath), "user setup script ran")
AssertFileContainsString(t, w, fmt.Sprintf("%s/.brev/logs/setup.log", repoPath), "user: brev")
AssertFileContainsString(t, w, fmt.Sprintf("%s/.brev/logs/setup.log", repoPath), fmt.Sprintf("pwd: %s", filepath.Join("/home/brev/workspace", repoPath)))
}
func AssertValidBrevProjRepo(t *testing.T, w Workspace, repoPath string) {
t.Helper()
AssertValidBrevBaseRepoSetup(t, w, repoPath)
AssertPathExists(t, w, fmt.Sprintf("%s/.brev/.gitignore", repoPath))
AssertPathExists(t, w, fmt.Sprintf("%s/.brev/ports.yaml", repoPath))
}
func AssertTestRepoSetupRan(t *testing.T, w Workspace, repoPath string) {
t.Helper()
AssertCustomTestRepoSetupRan(t, w, repoPath, "repo setup script ran", "brev", filepath.Join("/home/brev/workspace", repoPath), "setup.log")
}
func AssertCustomTestRepoSetupRan(t *testing.T, w Workspace, repoPath string, logStr string, user string, pwd string, logFile string) {
t.Helper()
AssertFileContainsString(t, w, fmt.Sprintf("%s/.brev/logs/%s", repoPath, logFile), logStr)
AssertFileContainsString(t, w, fmt.Sprintf("%s/.brev/logs/%s", repoPath, logFile), fmt.Sprintf("user: %s", user))
AssertFileContainsString(t, w, fmt.Sprintf("%s/.brev/logs/%s", repoPath, logFile), fmt.Sprintf("pwd: %s", pwd))
}
func AssertCwd(t *testing.T, w Workspace, expectedCwd string) {
t.Helper()
out, err := w.Exec("pwd")
assert.Nil(t, err)
assert.Equal(t, expectedCwd, strings.TrimSpace(string(out)))
}
func AssertUser(t *testing.T, w Workspace, expectedUser string) {
t.Helper()
out, err := w.Exec("whoami")
assert.Nil(t, err)
assert.Equal(t, expectedUser, strings.TrimSpace(string(out)))
}
func AssertInternalCurlOuputContains(t *testing.T, w Workspace, url string, contains string, retries int) {
t.Helper()
for retries > 0 {
retries--
out, err := w.Exec("curl", "-s", url)
if retries == 0 {
assert.Nil(t, err)
assert.Contains(t, string(out), contains)
}
if strings.Contains(string(out), contains) {
return
}
time.Sleep(3 * time.Second)
}
}
// func AssertCodeServerPasswordWorks(t *testing.T) {
// }
func AssertInternalSSHServerRunning(t *testing.T, w Workspace, privKeyPath string, user string, healthCheck string) {
t.Helper()
if !AssertPathExists(t, w, privKeyPath) {
return
}
_, err := w.Exec("ssh", "-i", privKeyPath, "-o", "StrictHostKeyChecking no", fmt.Sprintf("%s@localhost", user), healthCheck)
assert.Nil(t, err, "can connect with private key")
_, err = w.Exec("ssh", "-o", "StrictHostKeyChecking no", fmt.Sprintf("%s@localhost", user), healthCheck)
assert.Error(t, err, "can not connect without private key")
}
// func AssertInternalSSHServerNoPass(t *testing.T, w Workspace, user string, pass string, healthCheck string) {
// t.Helper()
// // TODO assert can't connect with password
// // TODO how to pass password in one line - use sshpass?
// }
func AssertPathExists(t *testing.T, workspace Workspace, path string) bool {
t.Helper()
_, err := workspace.Exec("ls", path)
return assert.Nil(t, err)
}
func AssertPathNotExist(t *testing.T, workspace Workspace, path string) bool {
t.Helper()
_, err := workspace.Exec("ls", path)
return assert.NotNil(t, err)
}
func AssertPathDoesNotExist(t *testing.T, workspace Workspace, path string) bool {
t.Helper()
_, err := workspace.Exec("ls", path)
return assert.Error(t, err)
}
func AssertFileContainsString(t *testing.T, w Workspace, filePath string, contains string) bool {
t.Helper()
res, err := w.Exec("grep", contains, filePath)
assert.Contains(t, string(res), contains)
return assert.Nil(t, err)
}
func AssertFileNotContainsString(t *testing.T, w Workspace, filePath string, contains string) bool {
t.Helper()
_, err := w.Exec("grep", contains, filePath)
return assert.Error(t, err)
}
func AssertDockerRunning(t *testing.T, w Workspace) bool {
t.Helper()
_, err := w.Exec("docker", "info")
assert.Nil(t, err)
out, err := w.Exec("systemctl", "is-active", "docker")
assert.Nil(t, err)
return strings.Contains(string(out), "active")
}
func AssertRepoHasNumFiles(t *testing.T, w Workspace, filePath string, num int) {
t.Helper()
out, err := w.Exec("ls", "-a", filePath)
assert.Nil(t, err)
assert.Len(t, strings.Fields(string(out)), num)
}
func UpdateFile(w Workspace, filePath string, content string) error {
_, err := w.Exec("sh", "-c", fmt.Sprintf(`echo '%s' > %s`, content, filePath))
if err != nil {
return breverrors.WrapAndTrace(err)
}
return nil
}