open-nomad/drivers/docker/utils_test.go
Mahmood Ali 0ee771b020 driver/docker: Support volumes field in Windows
Support Docker `volumes` field in Windows.  Previously, volumes parser
assumed some Unix-ism (e.g. didn't expect `:` in mount paths).
Here, we use the Docker parser to identify host and container paths.

Docker parsers use different validation logic from our previous unix
implementation: Docker parser accepts single path as a volume entry
(parsing it as a container path with auto-created volume) and enforces
additional checks (e.g. validity of mode).  Thereforce, I opted to use
Docker parser only for Windows, and keep Nomad's linux parser to
preserve current behavior.
2019-04-25 09:02:44 -04:00

79 lines
1.7 KiB
Go

package docker
import (
"testing"
"github.com/stretchr/testify/require"
)
func TestIsParentPath(t *testing.T) {
require.True(t, isParentPath("/a/b/c", "/a/b/c"))
require.True(t, isParentPath("/a/b/c", "/a/b/c/d"))
require.True(t, isParentPath("/a/b/c", "/a/b/c/d/e"))
require.False(t, isParentPath("/a/b/c", "/a/b/d"))
require.False(t, isParentPath("/a/b/c", "/a/b/cd"))
require.False(t, isParentPath("/a/b/c", "/a/d/c"))
require.False(t, isParentPath("/a/b/c", "/d/e/c"))
}
func TestParseVolumeSpec_Linux(t *testing.T) {
validCases := []struct {
name string
bindSpec string
hostPath string
containerPath string
mode string
}{
{
"absolute paths with mode",
"/etc/host-path:/etc/container-path:rw",
"/etc/host-path",
"/etc/container-path",
"rw",
},
{
"absolute paths without mode",
"/etc/host-path:/etc/container-path",
"/etc/host-path",
"/etc/container-path",
"",
},
{
"relative paths with mode",
"etc/host-path:/etc/container-path:rw",
"etc/host-path",
"/etc/container-path",
"rw",
},
{
"relative paths without mode",
"etc/host-path:/etc/container-path",
"etc/host-path",
"/etc/container-path",
"",
},
}
for _, c := range validCases {
t.Run("valid:"+c.name, func(t *testing.T) {
hp, cp, m, err := parseVolumeSpec(c.bindSpec, "linux")
require.NoError(t, err)
require.Equal(t, c.hostPath, hp)
require.Equal(t, c.containerPath, cp)
require.Equal(t, c.mode, m)
})
}
invalidCases := []string{
"/single-path",
}
for _, c := range invalidCases {
t.Run("invalid:"+c, func(t *testing.T) {
hp, cp, m, err := parseVolumeSpec(c, "linux")
require.Errorf(t, err, "expected error but parsed as %s:%s:%s", hp, cp, m)
})
}
}