Browse code

pkg: idtools: fix subid files parsing

Since Docker is already skipping newlines in /etc/sub{uid,gid},
this patch skips commented out lines - otherwise Docker fails to start.
Add unit test also.

Signed-off-by: Antonio Murdaca <runcom@redhat.com>

Antonio Murdaca authored on 2016/02/26 22:49:43
Showing 2 changed files
... ...
@@ -171,7 +171,7 @@ func parseSubidFile(path, username string) (ranges, error) {
171 171
 		}
172 172
 
173 173
 		text := strings.TrimSpace(s.Text())
174
-		if text == "" {
174
+		if text == "" || strings.HasPrefix(text, "#") {
175 175
 			continue
176 176
 		}
177 177
 		parts := strings.Split(text, ":")
... ...
@@ -241,3 +241,31 @@ func compareTrees(left, right map[string]node) error {
241 241
 	}
242 242
 	return nil
243 243
 }
244
+
245
+func TestParseSubidFileWithNewlinesAndComments(t *testing.T) {
246
+	tmpDir, err := ioutil.TempDir("", "parsesubid")
247
+	if err != nil {
248
+		t.Fatal(err)
249
+	}
250
+	fnamePath := filepath.Join(tmpDir, "testsubuid")
251
+	fcontent := `tss:100000:65536
252
+# empty default subuid/subgid file
253
+
254
+dockremap:231072:65536`
255
+	if err := ioutil.WriteFile(fnamePath, []byte(fcontent), 0644); err != nil {
256
+		t.Fatal(err)
257
+	}
258
+	ranges, err := parseSubidFile(fnamePath, "dockremap")
259
+	if err != nil {
260
+		t.Fatal(err)
261
+	}
262
+	if len(ranges) != 1 {
263
+		t.Fatalf("wanted 1 element in ranges, got %d instead", len(ranges))
264
+	}
265
+	if ranges[0].Start != 231072 {
266
+		t.Fatalf("wanted 231072, got %d instead", ranges[0].Start)
267
+	}
268
+	if ranges[0].Length != 65536 {
269
+		t.Fatalf("wanted 65536, got %d instead", ranges[0].Length)
270
+	}
271
+}