forked from nginx-proxy/docker-gen
-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils_test.go
89 lines (75 loc) · 1.81 KB
/
utils_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
package main
import (
"flag"
"os"
"testing"
)
func TestDefaultEndpoint(t *testing.T) {
err := os.Unsetenv("DOCKER_HOST")
if err != nil {
t.Fatalf("Unable to unset DOCKER_HOST: %s", err)
}
endpoint, err := getEndpoint()
if err != nil {
t.Fatalf("%s", err)
}
if endpoint != "unix:///var/run/docker.sock" {
t.Fatalf("Expected unix:///var/run/docker.sock, got %s", endpoint)
}
}
func TestDockerHostEndpoint(t *testing.T) {
err := os.Setenv("DOCKER_HOST", "tcp://127.0.0.1:4243")
if err != nil {
t.Fatalf("Unable to set DOCKER_HOST: %s", err)
}
endpoint, err := getEndpoint()
if err != nil {
t.Fatal("%s", err)
}
if endpoint != "tcp://127.0.0.1:4243" {
t.Fatalf("Expected tcp://127.0.0.1:4243, got %s", endpoint)
}
}
func TestDockerFlagEndpoint(t *testing.T) {
initFlags()
err := os.Setenv("DOCKER_HOST", "tcp://127.0.0.1:4243")
if err != nil {
t.Fatalf("Unable to set DOCKER_HOST: %s", err)
}
// flag value should override DOCKER_HOST and default value
err = flag.Set("endpoint", "tcp://127.0.0.1:5555")
if err != nil {
t.Fatalf("Unable to set endpoint flag: %s", err)
}
endpoint, err := getEndpoint()
if err != nil {
t.Fatal("%s", err)
}
if endpoint != "tcp://127.0.0.1:5555" {
t.Fatalf("Expected tcp://127.0.0.1:5555, got %s", endpoint)
}
}
func TestUnixBadFormat(t *testing.T) {
endpoint = "unix:/var/run/docker.sock"
_, err := getEndpoint()
if err == nil {
t.Fatal("endpoint should have failed")
}
}
func TestSplitKeyValueSlice(t *testing.T) {
tests := []struct {
input []string
expected string
}{
{[]string{"K"}, ""},
{[]string{"K="}, ""},
{[]string{"K=V3"}, "V3"},
{[]string{"K=V4=V5"}, "V4=V5"},
}
for _, i := range tests {
v := splitKeyValueSlice(i.input)
if v["K"] != i.expected {
t.Fatalf("expected K='%s'. got '%s'", i.expected, v["K"])
}
}
}