From 7e132eee02ce8588153003f592b4b9d7aebc5529 Mon Sep 17 00:00:00 2001 From: Antonio Murdaca Date: Fri, 26 Feb 2016 14:49:43 +0100 Subject: [PATCH] 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 --- idtools/idtools.go | 2 +- idtools/idtools_unix_test.go | 28 ++++++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/idtools/idtools.go b/idtools/idtools.go index a1301ee..7341640 100644 --- a/idtools/idtools.go +++ b/idtools/idtools.go @@ -171,7 +171,7 @@ func parseSubidFile(path, username string) (ranges, error) { } text := strings.TrimSpace(s.Text()) - if text == "" { + if text == "" || strings.HasPrefix(text, "#") { continue } parts := strings.Split(text, ":") diff --git a/idtools/idtools_unix_test.go b/idtools/idtools_unix_test.go index 55b338c..540d307 100644 --- a/idtools/idtools_unix_test.go +++ b/idtools/idtools_unix_test.go @@ -241,3 +241,31 @@ func compareTrees(left, right map[string]node) error { } return nil } + +func TestParseSubidFileWithNewlinesAndComments(t *testing.T) { + tmpDir, err := ioutil.TempDir("", "parsesubid") + if err != nil { + t.Fatal(err) + } + fnamePath := filepath.Join(tmpDir, "testsubuid") + fcontent := `tss:100000:65536 +# empty default subuid/subgid file + +dockremap:231072:65536` + if err := ioutil.WriteFile(fnamePath, []byte(fcontent), 0644); err != nil { + t.Fatal(err) + } + ranges, err := parseSubidFile(fnamePath, "dockremap") + if err != nil { + t.Fatal(err) + } + if len(ranges) != 1 { + t.Fatalf("wanted 1 element in ranges, got %d instead", len(ranges)) + } + if ranges[0].Start != 231072 { + t.Fatalf("wanted 231072, got %d instead", ranges[0].Start) + } + if ranges[0].Length != 65536 { + t.Fatalf("wanted 65536, got %d instead", ranges[0].Length) + } +}