Skip to content

Commit dac626c

Browse files
committed
add test to including env referenced files to deploy tgz
1 parent 4ce617a commit dac626c

File tree

2 files changed

+70
-2
lines changed

2 files changed

+70
-2
lines changed

commands/cloud_deploy_test.go

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,74 @@ func TestCreateReleaseFileCreatesTgz(t *testing.T) {
6565
}
6666
}
6767

68+
func TestCreateReleaseFileCreatesTgzWithEnvFile(t *testing.T) {
69+
tmpDir := t.TempDir()
70+
mockConfig(tmpDir, t, []byte("services:\n foo:\n image: bar\n env:\n source: 'foo.env'\n"))
71+
72+
if err := os.WriteFile(filepath.Join(tmpDir, "foo.env"), []byte("FOO=BAR"), os.ModePerm); err != nil {
73+
t.Fatal(err)
74+
}
75+
76+
fake := fakeKoolDeploy(tmpDir)
77+
fake.env.Set("PWD", tmpDir)
78+
79+
if err := fake.loadAndValidateConfig(); err != nil {
80+
t.Errorf("unexpected error on loadAndValidateConfig; got: %v", err)
81+
}
82+
83+
if tg, err := fake.createReleaseFile(); err != nil {
84+
t.Errorf("unexpected error on createReleaseFile; got: %v", err)
85+
} else if _, err := os.Stat(tg); err != nil {
86+
t.Errorf("expected tgz file to be created; got: %v", err)
87+
}
88+
89+
if !fake.shell.(*shell.FakeShell).CalledPrintln {
90+
t.Error("expected Println to have been called on shell")
91+
} else if !strings.Contains(fake.shell.(*shell.FakeShell).OutLines[0], "Compressing files:") {
92+
t.Error("expected to print 'Compressing files:'")
93+
} else if !strings.Contains(fake.shell.(*shell.FakeShell).OutLines[1], "- "+filepath.Join(tmpDir, "kool.cloud.yml")) {
94+
t.Error("expected to print '- " + filepath.Join(tmpDir, "kool.cloud.yml") + "'")
95+
} else if !strings.Contains(fake.shell.(*shell.FakeShell).OutLines[2], "- "+filepath.Join(tmpDir, "docker-compose.yml")) {
96+
t.Error("expected to print '- " + filepath.Join(tmpDir, "docker-compose.yml") + "'")
97+
} else if !strings.Contains(fake.shell.(*shell.FakeShell).OutLines[3], "- "+filepath.Join(tmpDir, "foo.env")) {
98+
t.Error("expected to print '- " + filepath.Join(tmpDir, "foo.env") + "'")
99+
}
100+
}
101+
102+
func TestCreateReleaseFileCreatesTgzWithEnvironmentFile(t *testing.T) {
103+
tmpDir := t.TempDir()
104+
mockConfig(tmpDir, t, []byte("services:\n foo:\n image: bar\n environment: 'bar.env'\n"))
105+
106+
if err := os.WriteFile(filepath.Join(tmpDir, "bar.env"), []byte("BAR=FOO"), os.ModePerm); err != nil {
107+
t.Fatal(err)
108+
}
109+
110+
fake := fakeKoolDeploy(tmpDir)
111+
fake.env.Set("PWD", tmpDir)
112+
113+
if err := fake.loadAndValidateConfig(); err != nil {
114+
t.Errorf("unexpected error on loadAndValidateConfig; got: %v", err)
115+
}
116+
117+
if tg, err := fake.createReleaseFile(); err != nil {
118+
t.Errorf("unexpected error on createReleaseFile; got: %v", err)
119+
} else if _, err := os.Stat(tg); err != nil {
120+
t.Errorf("expected tgz file to be created; got: %v", err)
121+
}
122+
123+
if !fake.shell.(*shell.FakeShell).CalledPrintln {
124+
t.Error("expected Println to have been called on shell")
125+
} else if !strings.Contains(fake.shell.(*shell.FakeShell).OutLines[0], "Compressing files:") {
126+
t.Error("expected to print 'Compressing files:'")
127+
} else if !strings.Contains(fake.shell.(*shell.FakeShell).OutLines[1], "- "+filepath.Join(tmpDir, "kool.cloud.yml")) {
128+
t.Error("expected to print '- " + filepath.Join(tmpDir, "kool.cloud.yml") + "'")
129+
} else if !strings.Contains(fake.shell.(*shell.FakeShell).OutLines[2], "- "+filepath.Join(tmpDir, "docker-compose.yml")) {
130+
t.Error("expected to print '- " + filepath.Join(tmpDir, "docker-compose.yml") + "'")
131+
} else if !strings.Contains(fake.shell.(*shell.FakeShell).OutLines[3], "- "+filepath.Join(tmpDir, "bar.env")) {
132+
t.Error("expected to print '- " + filepath.Join(tmpDir, "bar.env") + "'")
133+
}
134+
}
135+
68136
func TestCleanupReleaseFile(t *testing.T) {
69137
tmpDir := t.TempDir()
70138
mockConfig(tmpDir, t, nil)

services/cloud/deploy_validator.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ func (c *DeployConfig) GetEnvFiles() []string {
2424

2525
for _, srv := range c.Cloud.Services {
2626
if srv.Env != nil {
27-
if env, ok := srv.Env.(map[string]interface{}); ok {
27+
if env, ok := srv.Env.(map[interface{}]interface{}); ok {
2828
if envFile, okSrc := env["source"].(string); okSrc {
2929
files[envFile] = true
3030
}
@@ -38,7 +38,7 @@ func (c *DeployConfig) GetEnvFiles() []string {
3838
}
3939
}
4040

41-
envs := make([]string, len(files))
41+
envs := make([]string, 0, len(files))
4242

4343
for envFile := range files {
4444
envs = append(envs, filepath.Join(c.Meta.WorkingDir, envFile))

0 commit comments

Comments
 (0)