0ee771b020
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.
84 lines
1.8 KiB
Go
84 lines
1.8 KiB
Go
// +build windows
|
|
|
|
package docker
|
|
|
|
import (
|
|
"path/filepath"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestExpandPath(t *testing.T) {
|
|
cases := []struct {
|
|
base string
|
|
target string
|
|
expected string
|
|
}{
|
|
{"/tmp/alloc/task", ".", "/tmp/alloc/task"},
|
|
{"/tmp/alloc/task", "..", "/tmp/alloc"},
|
|
|
|
{"/tmp/alloc/task", "d1/d2", "/tmp/alloc/task/d1/d2"},
|
|
{"/tmp/alloc/task", "../d1/d2", "/tmp/alloc/d1/d2"},
|
|
{"/tmp/alloc/task", "../../d1/d2", "/tmp/d1/d2"},
|
|
|
|
{"/tmp/alloc/task", "c:/home/user", "c:/home/user"},
|
|
{"/tmp/alloc/task", "c:/home/user/..", "c:/home"},
|
|
}
|
|
|
|
for _, c := range cases {
|
|
t.Run(c.expected, func(t *testing.T) {
|
|
require.Equal(t, c.expected, filepath.ToSlash(expandPath(c.base, c.target)))
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestParseVolumeSpec_Windows(t *testing.T) {
|
|
validCases := []struct {
|
|
name string
|
|
bindSpec string
|
|
hostPath string
|
|
containerPath string
|
|
mode string
|
|
}{
|
|
{
|
|
"basic mount",
|
|
`c:\windows:e:\containerpath`,
|
|
`c:\windows`,
|
|
`e:\containerpath`,
|
|
"",
|
|
},
|
|
{
|
|
"relative path",
|
|
`relativepath:e:\containerpath`,
|
|
`relativepath`,
|
|
`e:\containerpath`,
|
|
"",
|
|
},
|
|
}
|
|
|
|
for _, c := range validCases {
|
|
t.Run("valid:"+c.name, func(t *testing.T) {
|
|
hp, cp, m, err := parseVolumeSpec(c.bindSpec, "windows")
|
|
require.NoError(t, err)
|
|
require.Equal(t, c.hostPath, hp)
|
|
require.Equal(t, c.containerPath, cp)
|
|
require.Equal(t, c.mode, m)
|
|
})
|
|
}
|
|
|
|
invalidCases := []string{
|
|
// linux path
|
|
"/linux-path",
|
|
// windows single path entry
|
|
`e:\containerpath`,
|
|
}
|
|
|
|
for _, c := range invalidCases {
|
|
t.Run("invalid:"+c, func(t *testing.T) {
|
|
hp, cp, m, err := parseVolumeSpec(c, "windows")
|
|
require.Errorf(t, err, "expected error but parsed as %s:%s:%s", hp, cp, m)
|
|
})
|
|
}
|
|
}
|