@@ -8,17 +8,20 @@ import (
8
8
"testing"
9
9
)
10
10
11
- func TestGetCurrentContainerID (t * testing.T ) {
12
- hostname := os .Getenv ("HOSTNAME" )
13
- defer os .Setenv ("HOSTNAME" , hostname )
14
-
15
- ids := []string {
11
+ var (
12
+ ids = []string {
16
13
"0fa939e22e6938e7517f663de83e79a5087a18b1b997a36e0c933a917cddb295" ,
17
14
"e881f8c51a72db7da515e9d5cab8ed105b869579eb9923fdcf4ee80933160802" ,
18
15
"eede6bd9e72f5d783a4bfb845bd71f310e974cb26987328a5d15704e23a8d6cb" ,
19
16
}
20
17
21
- contents := map [string ]string {
18
+ fileKeys = []string {
19
+ "cpuset" ,
20
+ "cgroup" ,
21
+ "mountinfo" ,
22
+ }
23
+
24
+ contents = map [string ]string {
22
25
"cpuset" : fmt .Sprintf ("/docker/%v" , ids [0 ]),
23
26
"cgroup" : fmt .Sprintf (`13:name=systemd:/docker-ce/docker/%[1]v
24
27
12:pids:/docker-ce/docker/%[1]v
@@ -61,16 +64,15 @@ func TestGetCurrentContainerID(t *testing.T) {
61
64
674 706 0:111 / /proc/scsi ro,relatime - tmpfs tmpfs ro,inode64
62
65
675 709 0:112 / /sys/firmware ro,relatime - tmpfs tmpfs ro,inode64` , ids [2 ]),
63
66
}
67
+ )
64
68
65
- keys := []string {
66
- "cpuset" ,
67
- "cgroup" ,
68
- "mountinfo" ,
69
- }
69
+ func TestGetCurrentContainerID (t * testing.T ) {
70
+ hostname := os .Getenv ("HOSTNAME" )
71
+ defer os .Setenv ("HOSTNAME" , hostname )
70
72
71
73
var filepaths []string
72
74
// Create temporary files with test content
73
- for _ , key := range keys {
75
+ for _ , key := range fileKeys {
74
76
file , err := ioutil .TempFile ("" , key )
75
77
if err != nil {
76
78
log .Fatal (err )
@@ -96,3 +98,38 @@ func TestGetCurrentContainerID(t *testing.T) {
96
98
t .Fatalf ("id mismatch with custom HOSTNAME: got %v, exp %v" , got , exp )
97
99
}
98
100
}
101
+
102
+ func TestGetCurrentContainerIDMountInfo (t * testing.T ) {
103
+ // Test specific to cases like https://github.com/nginx-proxy/docker-gen/issues/355
104
+ // where only the /proc/<pid>/mountinfo file contains information
105
+ hostname := os .Getenv ("HOSTNAME" )
106
+ defer os .Setenv ("HOSTNAME" , hostname )
107
+ os .Setenv ("HOSTNAME" , "customhostname" )
108
+
109
+ id := ids [2 ]
110
+
111
+ content := map [string ]string {
112
+ "cpuset" : "/" ,
113
+ "cgroup" : "0::/" ,
114
+ "mountinfo" : contents ["mountinfo" ],
115
+ }
116
+
117
+ var filepaths []string
118
+ // Create temporary files with test content
119
+ for _ , key := range fileKeys {
120
+ file , err := ioutil .TempFile ("" , key )
121
+ if err != nil {
122
+ log .Fatal (err )
123
+ }
124
+ defer os .Remove (file .Name ())
125
+ if _ , err = file .WriteString (content [key ]); err != nil {
126
+ log .Fatal (err )
127
+ }
128
+ filepaths = append (filepaths , file .Name ())
129
+ }
130
+
131
+ // 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 )
134
+ }
135
+ }
0 commit comments