|
4 | 4 | "bytes"
|
5 | 5 | "encoding/json"
|
6 | 6 | "fmt"
|
| 7 | + "io/ioutil" |
| 8 | + "os" |
| 9 | + "path" |
7 | 10 | "reflect"
|
8 | 11 | "testing"
|
9 | 12 | "text/template"
|
@@ -54,6 +57,13 @@ func TestContainsInteger(t *testing.T) {
|
54 | 57 | assert.False(t, contains(env, 24))
|
55 | 58 | }
|
56 | 59 |
|
| 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 | + |
57 | 67 | func TestKeys(t *testing.T) {
|
58 | 68 | env := map[string]string{
|
59 | 69 | "VIRTUAL_HOST": "demo.local",
|
@@ -172,6 +182,45 @@ func TestGroupByAfterWhere(t *testing.T) {
|
172 | 182 | assert.Equal(t, "3", groups["demo2.localhost"][0].(RuntimeContainer).ID)
|
173 | 183 | }
|
174 | 184 |
|
| 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 | + |
175 | 224 | func TestGroupByLabel(t *testing.T) {
|
176 | 225 | containers := []*RuntimeContainer{
|
177 | 226 | {
|
@@ -213,6 +262,13 @@ func TestGroupByLabel(t *testing.T) {
|
213 | 262 | assert.Equal(t, "2", groups["two"][0].(RuntimeContainer).ID)
|
214 | 263 | }
|
215 | 264 |
|
| 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 | + |
216 | 272 | func TestGroupByMulti(t *testing.T) {
|
217 | 273 | containers := []*RuntimeContainer{
|
218 | 274 | {
|
@@ -835,3 +891,46 @@ func TestWhenFalse(t *testing.T) {
|
835 | 891 | t.Fatal("Expected second value")
|
836 | 892 | }
|
837 | 893 | }
|
| 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