@@ -3,9 +3,10 @@ package dockergen
33import (
44 "fmt"
55 "io/ioutil"
6- "log"
76 "os"
87 "testing"
8+
9+ "github.com/stretchr/testify/assert"
910)
1011
1112var (
@@ -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
10299func 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}
0 commit comments