github.com/iqoqo/nomad@v0.11.3-0.20200911112621-d7021c74d101/drivers/docker/utils_test.go (about) 1 package docker 2 3 import ( 4 "testing" 5 6 "github.com/stretchr/testify/require" 7 ) 8 9 func TestIsParentPath(t *testing.T) { 10 require.True(t, isParentPath("/a/b/c", "/a/b/c")) 11 require.True(t, isParentPath("/a/b/c", "/a/b/c/d")) 12 require.True(t, isParentPath("/a/b/c", "/a/b/c/d/e")) 13 14 require.False(t, isParentPath("/a/b/c", "/a/b/d")) 15 require.False(t, isParentPath("/a/b/c", "/a/b/cd")) 16 require.False(t, isParentPath("/a/b/c", "/a/d/c")) 17 require.False(t, isParentPath("/a/b/c", "/d/e/c")) 18 } 19 20 func TestParseVolumeSpec_Linux(t *testing.T) { 21 validCases := []struct { 22 name string 23 bindSpec string 24 hostPath string 25 containerPath string 26 mode string 27 }{ 28 { 29 "absolute paths with mode", 30 "/etc/host-path:/etc/container-path:rw", 31 "/etc/host-path", 32 "/etc/container-path", 33 "rw", 34 }, 35 { 36 "absolute paths without mode", 37 "/etc/host-path:/etc/container-path", 38 "/etc/host-path", 39 "/etc/container-path", 40 "", 41 }, 42 { 43 "relative paths with mode", 44 "etc/host-path:/etc/container-path:rw", 45 "etc/host-path", 46 "/etc/container-path", 47 "rw", 48 }, 49 { 50 "relative paths without mode", 51 "etc/host-path:/etc/container-path", 52 "etc/host-path", 53 "/etc/container-path", 54 "", 55 }, 56 } 57 58 for _, c := range validCases { 59 t.Run("valid:"+c.name, func(t *testing.T) { 60 hp, cp, m, err := parseVolumeSpec(c.bindSpec, "linux") 61 require.NoError(t, err) 62 require.Equal(t, c.hostPath, hp) 63 require.Equal(t, c.containerPath, cp) 64 require.Equal(t, c.mode, m) 65 }) 66 } 67 68 invalidCases := []string{ 69 "/single-path", 70 } 71 72 for _, c := range invalidCases { 73 t.Run("invalid:"+c, func(t *testing.T) { 74 hp, cp, m, err := parseVolumeSpec(c, "linux") 75 require.Errorf(t, err, "expected error but parsed as %s:%s:%s", hp, cp, m) 76 }) 77 } 78 }