Skip to content

Commit 59c838a

Browse files
committed
tests: additional template tests
1 parent 4282c41 commit 59c838a

File tree

1 file changed

+99
-0
lines changed

1 file changed

+99
-0
lines changed

internal/dockergen/template_test.go

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ import (
44
"bytes"
55
"encoding/json"
66
"fmt"
7+
"io/ioutil"
8+
"os"
9+
"path"
710
"reflect"
811
"testing"
912
"text/template"
@@ -54,6 +57,13 @@ func TestContainsInteger(t *testing.T) {
5457
assert.False(t, contains(env, 24))
5558
}
5659

60+
func TestContainsNilInput(t *testing.T) {
61+
var env interface{} = nil
62+
63+
assert.False(t, contains(env, 0))
64+
assert.False(t, contains(env, ""))
65+
}
66+
5767
func TestKeys(t *testing.T) {
5868
env := map[string]string{
5969
"VIRTUAL_HOST": "demo.local",
@@ -172,6 +182,45 @@ func TestGroupByAfterWhere(t *testing.T) {
172182
assert.Equal(t, "3", groups["demo2.localhost"][0].(RuntimeContainer).ID)
173183
}
174184

185+
func TestGroupByKeys(t *testing.T) {
186+
containers := []*RuntimeContainer{
187+
{
188+
Env: map[string]string{
189+
"VIRTUAL_HOST": "demo1.localhost",
190+
},
191+
ID: "1",
192+
},
193+
{
194+
Env: map[string]string{
195+
"VIRTUAL_HOST": "demo1.localhost",
196+
},
197+
ID: "2",
198+
},
199+
{
200+
Env: map[string]string{
201+
"VIRTUAL_HOST": "demo2.localhost",
202+
},
203+
ID: "3",
204+
},
205+
}
206+
207+
expected := []string{"demo1.localhost", "demo2.localhost"}
208+
groups, err := groupByKeys(containers, "Env.VIRTUAL_HOST")
209+
assert.NoError(t, err)
210+
assert.ElementsMatch(t, expected, groups)
211+
212+
expected = []string{"1", "2", "3"}
213+
groups, err = groupByKeys(containers, "ID")
214+
assert.NoError(t, err)
215+
assert.ElementsMatch(t, expected, groups)
216+
}
217+
218+
func TestGeneralizedGroupByError(t *testing.T) {
219+
groups, err := groupBy("string", "")
220+
assert.Error(t, err)
221+
assert.Nil(t, groups)
222+
}
223+
175224
func TestGroupByLabel(t *testing.T) {
176225
containers := []*RuntimeContainer{
177226
{
@@ -213,6 +262,13 @@ func TestGroupByLabel(t *testing.T) {
213262
assert.Equal(t, "2", groups["two"][0].(RuntimeContainer).ID)
214263
}
215264

265+
func TestGroupByLabelError(t *testing.T) {
266+
strings := []string{"foo", "bar", "baz"}
267+
groups, err := groupByLabel(strings, "")
268+
assert.Error(t, err)
269+
assert.Nil(t, groups)
270+
}
271+
216272
func TestGroupByMulti(t *testing.T) {
217273
containers := []*RuntimeContainer{
218274
{
@@ -835,3 +891,46 @@ func TestWhenFalse(t *testing.T) {
835891
t.Fatal("Expected second value")
836892
}
837893
}
894+
895+
func TestDirList(t *testing.T) {
896+
dir, err := ioutil.TempDir("", "dirList")
897+
if err != nil {
898+
t.Fatal(err)
899+
}
900+
defer os.Remove(dir)
901+
902+
files := map[string]string{
903+
"aaa": "",
904+
"bbb": "",
905+
"ccc": "",
906+
}
907+
// Create temporary files
908+
for key := range files {
909+
file, err := ioutil.TempFile(dir, key)
910+
if err != nil {
911+
t.Fatal(err)
912+
}
913+
defer os.Remove(file.Name())
914+
files[key] = file.Name()
915+
}
916+
917+
expected := []string{
918+
path.Base(files["aaa"]),
919+
path.Base(files["bbb"]),
920+
path.Base(files["ccc"]),
921+
}
922+
923+
filesList, _ := dirList(dir)
924+
assert.Equal(t, expected, filesList)
925+
926+
filesList, _ = dirList("/wrong/path")
927+
assert.Equal(t, []string{}, filesList)
928+
}
929+
930+
func TestCoalesce(t *testing.T) {
931+
v := coalesce(nil, "second", "third")
932+
assert.Equal(t, "second", v, "Expected second value")
933+
934+
v = coalesce(nil, nil, nil)
935+
assert.Nil(t, v, "Expected nil value")
936+
}

0 commit comments

Comments
 (0)