Skip to content

Commit 342744e

Browse files
authored
Merge pull request #371 from nginx-proxy/tests
Additional tests and incremental enhancements
2 parents e23b149 + 48c39ac commit 342744e

File tree

3 files changed

+63
-15
lines changed

3 files changed

+63
-15
lines changed

internal/dockergen/context_test.go

Lines changed: 43 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@ package dockergen
33
import (
44
"fmt"
55
"io/ioutil"
6-
"log"
76
"os"
87
"testing"
8+
9+
"github.com/stretchr/testify/assert"
910
)
1011

1112
var (
@@ -75,28 +76,24 @@ func TestGetCurrentContainerID(t *testing.T) {
7576
for _, key := range fileKeys {
7677
file, err := ioutil.TempFile("", key)
7778
if err != nil {
78-
log.Fatal(err)
79+
t.Fatal(err)
7980
}
8081
defer os.Remove(file.Name())
8182
if _, err = file.WriteString(contents[key]); err != nil {
82-
log.Fatal(err)
83+
t.Fatal(err)
8384
}
8485
filepaths = append(filepaths, file.Name())
8586
}
8687

8788
// Each time the HOSTNAME is set to a short form ID, GetCurrentContainerID() should match and return the corresponding full ID
8889
for _, id := range ids {
8990
os.Setenv("HOSTNAME", id[0:12])
90-
if got, exp := GetCurrentContainerID(filepaths...), id; got != exp {
91-
t.Fatalf("id mismatch with HOSTNAME %v: got %v, exp %v", id[0:12], got, exp)
92-
}
91+
assert.Equal(t, id, GetCurrentContainerID(filepaths...), "id mismatch with default HOSTNAME")
9392
}
9493

9594
// If the Hostname isn't a short form ID, we should match the first valid ID (64 character hex string) instead
9695
os.Setenv("HOSTNAME", "customhostname")
97-
if got, exp := GetCurrentContainerID(filepaths...), ids[0]; got != exp {
98-
t.Fatalf("id mismatch with custom HOSTNAME: got %v, exp %v", got, exp)
99-
}
96+
assert.Equal(t, ids[0], GetCurrentContainerID(filepaths...), "id mismatch with custom HOSTNAME")
10097
}
10198

10299
func TestGetCurrentContainerIDMountInfo(t *testing.T) {
@@ -119,17 +116,50 @@ func TestGetCurrentContainerIDMountInfo(t *testing.T) {
119116
for _, key := range fileKeys {
120117
file, err := ioutil.TempFile("", key)
121118
if err != nil {
122-
log.Fatal(err)
119+
t.Fatal(err)
123120
}
124121
defer os.Remove(file.Name())
125122
if _, err = file.WriteString(content[key]); err != nil {
126-
log.Fatal(err)
123+
t.Fatal(err)
127124
}
128125
filepaths = append(filepaths, file.Name())
129126
}
130127

131128
// We should match the correct 64 characters long ID in mountinfo, not the first encountered
132-
if got, exp := GetCurrentContainerID(filepaths...), id; got != exp {
133-
t.Fatalf("id mismatch on mountinfo: got %v, exp %v", got, exp)
129+
assert.Equal(t, id, GetCurrentContainerID(filepaths...), "id mismatch on mountinfo")
130+
}
131+
132+
func TestGetCurrentContainerEmpty(t *testing.T) {
133+
assert.Equal(t, "", GetCurrentContainerID())
134+
}
135+
136+
func TestPublishedAddresses(t *testing.T) {
137+
container := &RuntimeContainer{
138+
Addresses: []Address{
139+
{
140+
IP: "172.19.0.1",
141+
HostPort: "80",
142+
},
143+
{
144+
IP: "172.19.0.2",
145+
},
146+
{
147+
IP: "172.19.0.3",
148+
HostPort: "8080",
149+
},
150+
},
134151
}
152+
153+
expected := []Address{
154+
{
155+
IP: "172.19.0.1",
156+
HostPort: "80",
157+
},
158+
{
159+
IP: "172.19.0.3",
160+
HostPort: "8080",
161+
},
162+
}
163+
164+
assert.ElementsMatch(t, expected, container.PublishedAddresses())
135165
}

internal/dockergen/docker_client_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package dockergen
33
import (
44
"fmt"
55
"io/ioutil"
6-
"log"
76
"os"
87
"testing"
98

@@ -182,7 +181,7 @@ func TestTlsEnabled(t *testing.T) {
182181
for key := range filepaths {
183182
file, err := ioutil.TempFile("", key)
184183
if err != nil {
185-
log.Fatal(err)
184+
t.Fatal(err)
186185
}
187186
defer os.Remove(file.Name())
188187
filepaths[key] = file.Name()

internal/dockergen/template_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,25 @@ func (tests templateTestList) run(t *testing.T, prefix string) {
3838
}
3939
}
4040

41+
func TestGetArrayValues(t *testing.T) {
42+
values := []string{"foor", "bar", "baz"}
43+
var expectedType *reflect.Value
44+
45+
arrayValues, err := getArrayValues("testFunc", values)
46+
assert.NoError(t, err)
47+
assert.IsType(t, expectedType, arrayValues)
48+
assert.Equal(t, "bar", arrayValues.Index(1).String())
49+
50+
arrayValues, err = getArrayValues("testFunc", &values)
51+
assert.NoError(t, err)
52+
assert.IsType(t, expectedType, arrayValues)
53+
assert.Equal(t, "baz", arrayValues.Index(2).String())
54+
55+
arrayValues, err = getArrayValues("testFunc", "foo")
56+
assert.Error(t, err)
57+
assert.Nil(t, arrayValues)
58+
}
59+
4160
func TestContainsString(t *testing.T) {
4261
env := map[string]string{
4362
"PORT": "1234",

0 commit comments

Comments
 (0)