This repository was archived by the owner on Jun 18, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuilder_test.go
More file actions
156 lines (138 loc) 路 3.63 KB
/
Copy pathbuilder_test.go
File metadata and controls
156 lines (138 loc) 路 3.63 KB
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
package build
import (
"context"
"io"
"math/rand"
"os"
"path"
"strconv"
"strings"
"testing"
"time"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/filters"
docker "github.com/docker/docker/client"
"github.com/stretchr/testify/assert"
)
func TestNewBuilder(t *testing.T) {
b := NewBuilder(BuilderConfig{})
assert.NotNil(t, b)
}
const (
DockerComposeVersion = "docker/compose:1.22.0"
HerokuishVersion = "gliderlabs/herokuish:v0.4.3"
)
// killTestContainers is a helper for tests - it implements project.ContainerStopper
func killTestContainers(cli *docker.Client, w io.Writer) error {
ctx := context.Background()
containers, err := cli.ContainerList(ctx, types.ContainerListOptions{})
if err != nil {
return err
}
// Take down all containers except the testvps
for _, container := range containers {
if container.Names[0] != "/testvps" {
err := cli.ContainerKill(ctx, container.ID, "SIGKILL")
if err != nil {
return err
}
}
}
return err
}
func TestBuilder_Build(t *testing.T) {
if testing.Short() {
t.Skip("skipping integration test")
}
type args struct {
buildType string
buildFilePath string
}
tests := []struct {
name string
args args
wantErr bool
expectedErrMsg string
}{
{"type docker-compose", args{"docker-compose", ""}, false, ""},
{"type dockerfile", args{"dockerfile", ""}, false, ""},
{"type dockerfile should fail", args{"dockerfile", "fail.Dockerfile"}, true, "image build failed"},
{"type herokuish", args{"herokuish", ""}, false, ""},
}
// Setup
cli, err := NewDockerClient()
assert.Nil(t, err)
defer cli.Close()
// Run cases
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Clean up before test
cli.ContainersPrune(context.Background(), filters.Args{})
time.Sleep(5 * time.Second)
var (
// Generate random project name
testProjectName = "test_" + tt.args.buildType + "_" + strconv.Itoa(rand.Intn(100))
testProjectDir = path.Join(
os.Getenv("GOPATH"),
"/src/github.com/ubclaunchpad/inertia/test/build/"+tt.args.buildType,
)
b = NewBuilder(BuilderConfig{
DockerComposeVersion: DockerComposeVersion,
HerokuishVersion: HerokuishVersion,
})
out = os.Stdout
)
// Run build
deploy, err := b.Build(tt.args.buildType, Config{
Name: testProjectName,
BuildFilePath: tt.args.buildFilePath,
BuildDirectory: testProjectDir,
}, cli, out)
if tt.wantErr {
assert.NotNil(t, err)
assert.Contains(t, err.Error(), tt.expectedErrMsg)
return
}
assert.Nil(t, err)
// Run containers
err = deploy()
assert.Nil(t, err)
// Arbitrary wait for containers to start
time.Sleep(10 * time.Second)
// Check for containers
containers, err := cli.ContainerList(
context.Background(),
types.ContainerListOptions{},
)
assert.Nil(t, err)
foundP := false
for _, c := range containers {
if strings.Contains(c.Names[0], testProjectName) {
foundP = true
}
}
// Wait for project to come up
attempts := 0
for !foundP && attempts < 10 {
attempts++
time.Sleep(30 * time.Second)
containers, err = cli.ContainerList(
context.Background(),
types.ContainerListOptions{},
)
assert.Nil(t, err)
for _, c := range containers {
if strings.Contains(c.Names[0], testProjectName) {
foundP = true
}
}
}
assert.True(t, foundP, "project container should be active")
// clean up
err = killTestContainers(cli, nil)
assert.Nil(t, err)
cli.ContainersPrune(context.Background(), filters.Args{})
time.Sleep(5 * time.Second)
})
}
}