@@ -3,9 +3,10 @@ package dockergen
3
3
import (
4
4
"fmt"
5
5
"io/ioutil"
6
- "log"
7
6
"os"
8
7
"testing"
8
+
9
+ "github.com/stretchr/testify/assert"
9
10
)
10
11
11
12
var (
@@ -75,28 +76,24 @@ func TestGetCurrentContainerID(t *testing.T) {
75
76
for _ , key := range fileKeys {
76
77
file , err := ioutil .TempFile ("" , key )
77
78
if err != nil {
78
- log .Fatal (err )
79
+ t .Fatal (err )
79
80
}
80
81
defer os .Remove (file .Name ())
81
82
if _ , err = file .WriteString (contents [key ]); err != nil {
82
- log .Fatal (err )
83
+ t .Fatal (err )
83
84
}
84
85
filepaths = append (filepaths , file .Name ())
85
86
}
86
87
87
88
// Each time the HOSTNAME is set to a short form ID, GetCurrentContainerID() should match and return the corresponding full ID
88
89
for _ , id := range ids {
89
90
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" )
93
92
}
94
93
95
94
// If the Hostname isn't a short form ID, we should match the first valid ID (64 character hex string) instead
96
95
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" )
100
97
}
101
98
102
99
func TestGetCurrentContainerIDMountInfo (t * testing.T ) {
@@ -119,17 +116,50 @@ func TestGetCurrentContainerIDMountInfo(t *testing.T) {
119
116
for _ , key := range fileKeys {
120
117
file , err := ioutil .TempFile ("" , key )
121
118
if err != nil {
122
- log .Fatal (err )
119
+ t .Fatal (err )
123
120
}
124
121
defer os .Remove (file .Name ())
125
122
if _ , err = file .WriteString (content [key ]); err != nil {
126
- log .Fatal (err )
123
+ t .Fatal (err )
127
124
}
128
125
filepaths = append (filepaths , file .Name ())
129
126
}
130
127
131
128
// 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
+ },
134
151
}
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 ())
135
165
}
0 commit comments