-
Notifications
You must be signed in to change notification settings - Fork 46
/
cloud_deploy_test.go
199 lines (160 loc) · 6.82 KB
/
cloud_deploy_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
package commands
import (
"kool-dev/kool/core/environment"
"kool-dev/kool/core/shell"
"kool-dev/kool/services/cloud/setup"
"os"
"path/filepath"
"strings"
"testing"
)
func TestNewKoolDeploy(t *testing.T) {
kd := NewKoolDeploy(NewCloud())
if _, is := kd.env.(*environment.DefaultEnvStorage); !is {
t.Error("failed asserting default env storage")
}
if _, is := kd.cloud.env.(*environment.DefaultEnvStorage); !is {
t.Error("failed asserting default cloud.env storage")
}
if _, is := kd.setupParser.(*setup.DefaultCloudSetupParser); !is {
t.Error("failed asserting default cloud setup parser")
}
}
func fakeKoolDeploy(pwd string) *KoolDeploy {
c := NewCloud()
c.Fake()
return &KoolDeploy{
*(newDefaultKoolService().Fake()),
c,
setup.NewDefaultCloudSetupParser(pwd),
&KoolCloudDeployFlags{},
environment.NewFakeEnvStorage(),
nil,
}
}
func TestCreateReleaseFileNoConfig(t *testing.T) {
fake := fakeKoolDeploy("")
if _, err := fake.createReleaseFile(); err == nil || !strings.Contains(err.Error(), "no kool.cloud.yml config files found") {
t.Errorf("expected error on createReleaseFile when no kool.deploy.yml exists in current working directory; got: %v", err)
}
}
func TestCreateReleaseFileCreatesTgz(t *testing.T) {
tmpDir := t.TempDir()
mockConfig(tmpDir, t, nil)
fake := fakeKoolDeploy(tmpDir)
fake.env.Set("PWD", tmpDir)
if err := fake.loadAndValidateConfig(); err != nil {
t.Errorf("unexpected error on loadAndValidateConfig; got: %v", err)
}
if tg, err := fake.createReleaseFile(); err != nil {
t.Errorf("unexpected error on createReleaseFile; got: %v", err)
} else if _, err := os.Stat(tg); err != nil {
t.Errorf("expected tgz file to be created; got: %v", err)
}
}
func TestCreateReleaseFileCreatesTgzWithEnvFile(t *testing.T) {
tmpDir := t.TempDir()
mockConfig(tmpDir, t, []byte("services:\n foo:\n image: bar\n env:\n source: 'foo.env'\n"))
if err := os.WriteFile(filepath.Join(tmpDir, "foo.env"), []byte("FOO=BAR"), os.ModePerm); err != nil {
t.Fatal(err)
}
fake := fakeKoolDeploy(tmpDir)
fake.env.Set("PWD", tmpDir)
if err := fake.loadAndValidateConfig(); err != nil {
t.Errorf("unexpected error on loadAndValidateConfig; got: %v", err)
}
if tg, err := fake.createReleaseFile(); err != nil {
t.Errorf("unexpected error on createReleaseFile; got: %v", err)
} else if _, err := os.Stat(tg); err != nil {
t.Errorf("expected tgz file to be created; got: %v", err)
}
if !fake.shell.(*shell.FakeShell).CalledPrintln {
t.Error("expected Println to have been called on shell")
} else if !strings.Contains(fake.shell.(*shell.FakeShell).OutLines[0], "Compressing files:") {
t.Error("expected to print 'Compressing files:'")
} else if !strings.Contains(fake.shell.(*shell.FakeShell).OutLines[1], "- " filepath.Join(tmpDir, "kool.cloud.yml")) {
t.Error("expected to print '- " filepath.Join(tmpDir, "kool.cloud.yml") "'")
} else if !strings.Contains(fake.shell.(*shell.FakeShell).OutLines[2], "- " filepath.Join(tmpDir, "docker-compose.yml")) {
t.Error("expected to print '- " filepath.Join(tmpDir, "docker-compose.yml") "'")
} else if !strings.Contains(fake.shell.(*shell.FakeShell).OutLines[3], "- " filepath.Join(tmpDir, "foo.env")) {
t.Error("expected to print '- " filepath.Join(tmpDir, "foo.env") "'")
}
}
func TestCreateReleaseFileCreatesTgzWithEnvironmentFile(t *testing.T) {
tmpDir := t.TempDir()
mockConfig(tmpDir, t, []byte("services:\n foo:\n image: bar\n environment: 'bar.env'\n"))
if err := os.WriteFile(filepath.Join(tmpDir, "bar.env"), []byte("BAR=FOO"), os.ModePerm); err != nil {
t.Fatal(err)
}
fake := fakeKoolDeploy(tmpDir)
fake.env.Set("PWD", tmpDir)
if err := fake.loadAndValidateConfig(); err != nil {
t.Errorf("unexpected error on loadAndValidateConfig; got: %v", err)
}
if tg, err := fake.createReleaseFile(); err != nil {
t.Errorf("unexpected error on createReleaseFile; got: %v", err)
} else if _, err := os.Stat(tg); err != nil {
t.Errorf("expected tgz file to be created; got: %v", err)
}
if !fake.shell.(*shell.FakeShell).CalledPrintln {
t.Error("expected Println to have been called on shell")
} else if !strings.Contains(fake.shell.(*shell.FakeShell).OutLines[0], "Compressing files:") {
t.Error("expected to print 'Compressing files:'")
} else if !strings.Contains(fake.shell.(*shell.FakeShell).OutLines[1], "- " filepath.Join(tmpDir, "kool.cloud.yml")) {
t.Error("expected to print '- " filepath.Join(tmpDir, "kool.cloud.yml") "'")
} else if !strings.Contains(fake.shell.(*shell.FakeShell).OutLines[2], "- " filepath.Join(tmpDir, "docker-compose.yml")) {
t.Error("expected to print '- " filepath.Join(tmpDir, "docker-compose.yml") "'")
} else if !strings.Contains(fake.shell.(*shell.FakeShell).OutLines[3], "- " filepath.Join(tmpDir, "bar.env")) {
t.Error("expected to print '- " filepath.Join(tmpDir, "bar.env") "'")
}
}
func TestCleanupReleaseFile(t *testing.T) {
tmpDir := t.TempDir()
mockConfig(tmpDir, t, nil)
fake := fakeKoolDeploy("")
fake.env.Set("PWD", tmpDir)
f := filepath.Join(tmpDir, "kool.cloud.yml")
fake.cleanupReleaseFile(f)
if _, err := os.Stat(f); !os.IsNotExist(err) {
t.Errorf("expected file to be removed")
}
fake.cleanupReleaseFile(f)
if !fake.shell.(*shell.FakeShell).CalledError {
t.Errorf("expected for Error to have been called on shell")
}
if !strings.Contains(fake.shell.(*shell.FakeShell).Err.Error(), "error trying to remove temporary tarball") {
t.Errorf("expected to print proper error message if file removal fails")
}
}
func TestLoadAndValidateConfig(t *testing.T) {
fake := fakeKoolDeploy("")
tmpDir := t.TempDir()
fake.env.Set("PWD", tmpDir)
if err := fake.loadAndValidateConfig(); err == nil || !strings.Contains(err.Error(), "could not find required file") {
t.Error("failed getting proper error out of loadAndValidateConfig when no kool.cloud.yml exists in current working directory")
}
mockConfig(tmpDir, t, []byte("services:\n\tfoo:\n"))
if err := fake.loadAndValidateConfig(); err == nil || !strings.Contains(err.Error(), "found character that cannot start") {
t.Errorf("unexpcted error on loadAndValidateConfig with bad config: %v", err)
}
mockConfig(tmpDir, t, nil)
if err := fake.loadAndValidateConfig(); err != nil {
t.Errorf("unexpcted error on loadAndValidateConfig when file exists: %v", err)
}
if fake.cloudConfig.Cloud.Services == nil {
t.Error("failed loading cloud config")
}
if len(fake.cloudConfig.Cloud.Services) != 1 {
t.Error("service count mismatch - should be 1")
} else if *fake.cloudConfig.Cloud.Services["foo"].Image != "bar" {
t.Error("failed loading service foo image 'bar'")
}
}
func mockConfig(tmpDir string, t *testing.T, mock []byte) {
if mock == nil {
mock = []byte("services:\n foo:\n image: bar\n")
}
if err := os.WriteFile(filepath.Join(tmpDir, "kool.cloud.yml"), mock, os.ModePerm); err != nil {
t.Fatal(err)
}
}