forked from nginx-proxy/docker-gen
-
Notifications
You must be signed in to change notification settings - Fork 1
/
template_test.go
101 lines (89 loc) · 1.95 KB
/
template_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
package main
import "testing"
func TestContains(t *testing.T) {
env := map[string]string{
"PORT": "1234",
}
if !contains(env, "PORT") {
t.Fail()
}
if contains(env, "MISSING") {
t.Fail()
}
}
func TestGroupByExitingKey(t *testing.T) {
containers := []*RuntimeContainer{
&RuntimeContainer{
Env: map[string]string{
"VIRTUAL_HOST": "demo1.localhost",
},
ID: "1",
},
&RuntimeContainer{
Env: map[string]string{
"VIRTUAL_HOST": "demo1.localhost",
},
ID: "2",
},
&RuntimeContainer{
Env: map[string]string{
"VIRTUAL_HOST": "demo2.localhost",
},
ID: "3",
},
}
groups := groupBy(containers, "Env.VIRTUAL_HOST")
if len(groups) != 2 {
t.Fail()
}
if len(groups["demo1.localhost"]) != 2 {
t.Fail()
}
if len(groups["demo2.localhost"]) != 1 {
t.Fail()
}
if groups["demo2.localhost"][0].ID != "3" {
t.Fail()
}
}
func TestGroupByMulti(t *testing.T) {
containers := []*RuntimeContainer{
&RuntimeContainer{
Env: map[string]string{
"VIRTUAL_HOST": "demo1.localhost",
},
ID: "1",
},
&RuntimeContainer{
Env: map[string]string{
"VIRTUAL_HOST": "demo1.localhost,demo3.localhost",
},
ID: "2",
},
&RuntimeContainer{
Env: map[string]string{
"VIRTUAL_HOST": "demo2.localhost",
},
ID: "3",
},
}
groups := groupByMulti(containers, "Env.VIRTUAL_HOST", ",")
if len(groups) != 3 {
t.Fatalf("expected 3 got %d", len(groups))
}
if len(groups["demo1.localhost"]) != 2 {
t.Fatalf("expected 2 got %s", len(groups["demo1.localhost"]))
}
if len(groups["demo2.localhost"]) != 1 {
t.Fatalf("expected 1 got %s", len(groups["demo2.localhost"]))
}
if groups["demo2.localhost"][0].ID != "3" {
t.Fatalf("expected 2 got %s", groups["demo2.localhost"][0].ID)
}
if len(groups["demo3.localhost"]) != 1 {
t.Fatalf("expect 1 got %d", len(groups["demo3.localhost"]))
}
if groups["demo3.localhost"][0].ID != "2" {
t.Fatalf("expected 2 got %s", groups["demo3.localhost"][0].ID)
}
}