Skip to content

Commit 41f48ce

Browse files
authored
Merge pull request #366 from nginx-proxy/tests
Additional tests and use of github.com/stretchr/testify/assert
2 parents de17d67 + f13650e commit 41f48ce

File tree

4 files changed

+158
-126
lines changed

4 files changed

+158
-126
lines changed

config_test.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package dockergen
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
)
8+
9+
func TestParseWait(t *testing.T) {
10+
incorrectIntervals := []string{
11+
"500x", // Incorrect min interval
12+
"500s:4x", // Incorrect max interval
13+
"1m:1s", // Min interval larger than max interval
14+
}
15+
16+
for _, intervalString := range incorrectIntervals {
17+
wait, err := ParseWait(intervalString)
18+
assert.Error(t, err)
19+
assert.Nil(t, wait)
20+
}
21+
22+
correctIntervals := map[string]Wait{
23+
"": {0, 0}, // Empty time interval string
24+
"1ms": {1000000, 4000000}, // Correct min interval without max
25+
"1ms:111ms": {1000000, 111000000}, // Correct min:max time interval
26+
}
27+
28+
for intervalString, expectedWait := range correctIntervals {
29+
wait, err := ParseWait(intervalString)
30+
assert.NoError(t, err)
31+
assert.Equal(t, &expectedWait, wait)
32+
}
33+
}
34+
35+
func TestWaitUnmarshalText(t *testing.T) {
36+
// Correct min:max time interval
37+
intervalBytes := []byte("1ms:2ms")
38+
expectedWait := &Wait{1000000, 2000000}
39+
wait := new(Wait)
40+
err := wait.UnmarshalText(intervalBytes)
41+
assert.NoError(t, err)
42+
assert.Equal(t, expectedWait, wait)
43+
}

docker_client_test.go

Lines changed: 103 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -1,196 +1,193 @@
11
package dockergen
22

33
import (
4+
"fmt"
5+
"io/ioutil"
6+
"log"
7+
"os"
48
"testing"
9+
10+
"github.com/stretchr/testify/assert"
511
)
612

713
func TestSplitDockerImageRepository(t *testing.T) {
814
registry, repository, tag := splitDockerImage("ubuntu")
915

10-
if registry != "" {
11-
t.Fail()
12-
}
13-
if repository != "ubuntu" {
14-
t.Fail()
15-
}
16-
if tag != "" {
17-
t.Fail()
18-
}
16+
assert.Equal(t, "", registry)
17+
assert.Equal(t, "ubuntu", repository)
18+
assert.Equal(t, "", tag)
1919

2020
dockerImage := DockerImage{
2121
Registry: registry,
2222
Repository: repository,
2323
Tag: tag,
2424
}
25-
if "ubuntu" != dockerImage.String() {
26-
t.Fail()
27-
}
25+
assert.Equal(t, "ubuntu", dockerImage.String())
2826
}
2927

3028
func TestSplitDockerImageWithRegistry(t *testing.T) {
3129
registry, repository, tag := splitDockerImage("custom.registry/ubuntu")
3230

33-
if registry != "custom.registry" {
34-
t.Fail()
35-
}
36-
if repository != "ubuntu" {
37-
t.Fail()
38-
}
39-
if tag != "" {
40-
t.Fail()
41-
}
31+
assert.Equal(t, "custom.registry", registry)
32+
assert.Equal(t, "ubuntu", repository)
33+
assert.Equal(t, "", tag)
34+
4235
dockerImage := DockerImage{
4336
Registry: registry,
4437
Repository: repository,
4538
Tag: tag,
4639
}
47-
if "custom.registry/ubuntu" != dockerImage.String() {
48-
t.Fail()
49-
}
50-
40+
assert.Equal(t, "custom.registry/ubuntu", dockerImage.String())
5141
}
5242

5343
func TestSplitDockerImageWithRegistryAndTag(t *testing.T) {
5444
registry, repository, tag := splitDockerImage("custom.registry/ubuntu:12.04")
5545

56-
if registry != "custom.registry" {
57-
t.Fail()
58-
}
59-
if repository != "ubuntu" {
60-
t.Fail()
61-
}
62-
if tag != "12.04" {
63-
t.Fail()
64-
}
46+
assert.Equal(t, "custom.registry", registry)
47+
assert.Equal(t, "ubuntu", repository)
48+
assert.Equal(t, "12.04", tag)
49+
6550
dockerImage := DockerImage{
6651
Registry: registry,
6752
Repository: repository,
6853
Tag: tag,
6954
}
70-
if "custom.registry/ubuntu:12.04" != dockerImage.String() {
71-
t.Fail()
72-
}
73-
55+
assert.Equal(t, "custom.registry/ubuntu:12.04", dockerImage.String())
7456
}
7557

7658
func TestSplitDockerImageWithRepositoryAndTag(t *testing.T) {
7759
registry, repository, tag := splitDockerImage("ubuntu:12.04")
7860

79-
if registry != "" {
80-
t.Fail()
81-
}
61+
assert.Equal(t, "", registry)
62+
assert.Equal(t, "ubuntu", repository)
63+
assert.Equal(t, "12.04", tag)
8264

83-
if repository != "ubuntu" {
84-
t.Fail()
85-
}
86-
87-
if tag != "12.04" {
88-
t.Fail()
89-
}
9065
dockerImage := DockerImage{
9166
Registry: registry,
9267
Repository: repository,
9368
Tag: tag,
9469
}
95-
if "ubuntu:12.04" != dockerImage.String() {
96-
t.Fail()
97-
}
70+
assert.Equal(t, "ubuntu:12.04", dockerImage.String())
9871
}
9972

10073
func TestSplitDockerImageWithPrivateRegistryPath(t *testing.T) {
10174
registry, repository, tag := splitDockerImage("localhost:8888/ubuntu/foo:12.04")
10275

103-
if registry != "localhost:8888" {
104-
t.Fail()
105-
}
106-
107-
if repository != "ubuntu/foo" {
108-
t.Fail()
109-
}
76+
assert.Equal(t, "localhost:8888", registry)
77+
assert.Equal(t, "ubuntu/foo", repository)
78+
assert.Equal(t, "12.04", tag)
11079

111-
if tag != "12.04" {
112-
t.Fail()
113-
}
11480
dockerImage := DockerImage{
11581
Registry: registry,
11682
Repository: repository,
11783
Tag: tag,
11884
}
119-
if "localhost:8888/ubuntu/foo:12.04" != dockerImage.String() {
120-
t.Fail()
121-
}
85+
assert.Equal(t, "localhost:8888/ubuntu/foo:12.04", dockerImage.String())
12286
}
12387
func TestSplitDockerImageWithLocalRepositoryAndTag(t *testing.T) {
12488
registry, repository, tag := splitDockerImage("localhost:8888/ubuntu:12.04")
12589

126-
if registry != "localhost:8888" {
127-
t.Fatalf("registry does not match: expected %s got %s", "localhost:8888", registry)
128-
}
129-
130-
if repository != "ubuntu" {
131-
t.Fatalf("repository does not match: expected %s got %s", "ubuntu", repository)
132-
}
90+
assert.Equal(t, "localhost:8888", registry)
91+
assert.Equal(t, "ubuntu", repository)
92+
assert.Equal(t, "12.04", tag)
13393

134-
if tag != "12.04" {
135-
t.Fatalf("tag does not match: expected %s got %s", "12.04", tag)
136-
}
13794
dockerImage := DockerImage{
13895
Registry: registry,
13996
Repository: repository,
14097
Tag: tag,
14198
}
142-
if "localhost:8888/ubuntu:12.04" != dockerImage.String() {
143-
t.Fail()
144-
}
145-
99+
assert.Equal(t, "localhost:8888/ubuntu:12.04", dockerImage.String())
146100
}
147101

148102
func TestParseHostUnix(t *testing.T) {
149103
proto, addr, err := parseHost("unix:///var/run/docker.sock")
150-
if err != nil {
151-
t.Fatalf("%s", err)
152-
}
153-
if proto != "unix" || addr != "/var/run/docker.sock" {
154-
t.Fatal("failed to parse unix:///var/run/docker.sock")
155-
}
104+
assert.NoError(t, err)
105+
assert.Equal(t, "unix", proto, "failed to parse unix:///var/run/docker.sock")
106+
assert.Equal(t, "/var/run/docker.sock", addr, "failed to parse unix:///var/run/docker.sock")
156107
}
157108

158109
func TestParseHostUnixDefault(t *testing.T) {
159110
proto, addr, err := parseHost("")
160-
if err != nil {
161-
t.Fatalf("%s", err)
162-
}
163-
if proto != "unix" || addr != "/var/run/docker.sock" {
164-
t.Fatal("failed to parse ''")
165-
}
111+
assert.NoError(t, err)
112+
assert.Equal(t, "unix", proto, "failed to parse ''")
113+
assert.Equal(t, "/var/run/docker.sock", addr, "failed to parse ''")
166114
}
167115

168116
func TestParseHostUnixDefaultNoPath(t *testing.T) {
169117
proto, addr, err := parseHost("unix://")
170-
if err != nil {
171-
t.Fatalf("%s", err)
172-
}
173-
if proto != "unix" || addr != "/var/run/docker.sock" {
174-
t.Fatal("failed to parse unix://")
175-
}
118+
assert.NoError(t, err)
119+
assert.Equal(t, "unix", proto, "failed to parse unix://")
120+
assert.Equal(t, "/var/run/docker.sock", addr, "failed to parse unix://")
176121
}
177122

178123
func TestParseHostTCP(t *testing.T) {
179124
proto, addr, err := parseHost("tcp://127.0.0.1:4243")
180-
if err != nil {
181-
t.Fatalf("%s", err)
182-
}
183-
if proto != "tcp" || addr != "127.0.0.1:4243" {
184-
t.Fatal("failed to parse tcp://127.0.0.1:4243")
185-
}
125+
assert.NoError(t, err)
126+
assert.Equal(t, "tcp", proto, "failed to parse tcp://127.0.0.1:4243")
127+
assert.Equal(t, "127.0.0.1:4243", addr, "failed to parse tcp://127.0.0.1:4243")
186128
}
187129

188130
func TestParseHostTCPDefault(t *testing.T) {
189131
proto, addr, err := parseHost("tcp://:4243")
190-
if err != nil {
191-
t.Fatalf("%s", err)
132+
assert.NoError(t, err)
133+
assert.Equal(t, "tcp", proto, "failed to parse tcp://:4243")
134+
assert.Equal(t, "127.0.0.1:4243", addr, "failed to parse tcp://:4243")
135+
}
136+
137+
func TestParseHostSystemd(t *testing.T) {
138+
proto, addr, err := parseHost("fd://")
139+
assert.NoError(t, err)
140+
assert.Equal(t, "fd", proto, "failed to parse fd://")
141+
assert.Equal(t, "fd://", addr, "failed to parse fd://")
142+
}
143+
144+
func assertParseHostError(t *testing.T, address string) {
145+
proto, addr, err := parseHost(address)
146+
message := fmt.Sprintf("should have failed to parse %v", address)
147+
assert.Error(t, err, message)
148+
assert.Equal(t, "", proto, message)
149+
assert.Equal(t, "", addr, message)
150+
}
151+
152+
func TestParseHostTCPNoAddressError(t *testing.T) {
153+
assertParseHostError(t, "tcp://")
154+
}
155+
156+
func TestParseHostTCPIncorrectBindAddressError(t *testing.T) {
157+
incorrectBindAdresses := []string{
158+
"tcp://127.0.0.1:4243:80",
159+
"tcp://127.0.0.1:",
160+
"tcp://127.0.0.1",
161+
}
162+
163+
for _, address := range incorrectBindAdresses {
164+
assertParseHostError(t, address)
192165
}
193-
if proto != "tcp" || addr != "127.0.0.1:4243" {
194-
t.Fatal("failed to parse unix:///var/run/docker.sock")
166+
}
167+
168+
func TestParseHostWrongProtocolError(t *testing.T) {
169+
assertParseHostError(t, "foo://")
170+
}
171+
172+
func TestTlsEnabled(t *testing.T) {
173+
tls := tlsEnabled("foo", "bar", "baz")
174+
assert.False(t, tls)
175+
176+
filepaths := map[string]string{
177+
"cert": "",
178+
"caCert": "",
179+
"key": "",
180+
}
181+
// Create temporary files
182+
for key := range filepaths {
183+
file, err := ioutil.TempFile("", key)
184+
if err != nil {
185+
log.Fatal(err)
186+
}
187+
defer os.Remove(file.Name())
188+
filepaths[key] = file.Name()
195189
}
190+
191+
tls = tlsEnabled(filepaths["cert"], filepaths["caCert"], filepaths["key"])
192+
assert.True(t, tls)
196193
}

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ require (
2121
github.com/opencontainers/runc v0.1.1 // indirect
2222
github.com/opencontainers/selinux v1.8.0 // indirect
2323
github.com/sirupsen/logrus v1.8.1 // indirect
24-
github.com/stretchr/testify v1.7.0 // indirect
24+
github.com/stretchr/testify v1.7.0
2525
golang.org/x/sys v0.0.0-20210331175145-43e1dd70ce54 // indirect
2626
gotest.tools v2.2.0+incompatible // indirect
2727
)

0 commit comments

Comments
 (0)