From dd5975f3d579e64493f9b005eb304b86c478829e Mon Sep 17 00:00:00 2001 From: chenkang Date: Mon, 11 Oct 2021 09:17:57 +0800 Subject: Support readonly rootfs contains colon Fix: https://github.com/containers/podman/issues/11913 Signed-off-by: chenkang --- pkg/specgen/specgen.go | 6 +++--- pkg/specgen/specgen_test.go | 25 +++++++++++++++++++++++++ 2 files changed, 28 insertions(+), 3 deletions(-) create mode 100644 pkg/specgen/specgen_test.go (limited to 'pkg/specgen') diff --git a/pkg/specgen/specgen.go b/pkg/specgen/specgen.go index dbb669291..07995b2df 100644 --- a/pkg/specgen/specgen.go +++ b/pkg/specgen/specgen.go @@ -552,10 +552,10 @@ func NewSpecGenerator(arg string, rootfs bool) *SpecGenerator { if rootfs { csc.Rootfs = arg // check if rootfs is actually overlayed - parts := strings.SplitN(csc.Rootfs, ":", 2) - if len(parts) > 1 && parts[1] == "O" { + lastColonIndex := strings.LastIndex(csc.Rootfs, ":") + if lastColonIndex != -1 && lastColonIndex+1 < len(csc.Rootfs) && csc.Rootfs[lastColonIndex+1:] == "O" { csc.RootfsOverlay = true - csc.Rootfs = parts[0] + csc.Rootfs = csc.Rootfs[:lastColonIndex] } } else { csc.Image = arg diff --git a/pkg/specgen/specgen_test.go b/pkg/specgen/specgen_test.go new file mode 100644 index 000000000..8e00bd4b6 --- /dev/null +++ b/pkg/specgen/specgen_test.go @@ -0,0 +1,25 @@ +package specgen + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestNewSpecGeneratorWithRootfs(t *testing.T) { + tests := []struct { + rootfs string + expectedRootfsOverlay bool + expectedRootfs string + }{ + {"/root/a:b:O", true, "/root/a:b"}, + {"/root/a:b/c:O", true, "/root/a:b/c"}, + {"/root/a:b/c:", false, "/root/a:b/c:"}, + {"/root/a/b", false, "/root/a/b"}, + } + for _, args := range tests { + val := NewSpecGenerator(args.rootfs, true) + assert.Equal(t, val.RootfsOverlay, args.rootfs) + assert.Equal(t, val.Rootfs, args.rootfs) + } +} -- cgit v1.2.3-54-g00ecf