github.com/hernad/nomad@v1.6.112/drivers/docker/utils_windows_test.go (about) 1 // Copyright (c) HashiCorp, Inc. 2 // SPDX-License-Identifier: MPL-2.0 3 4 //go:build windows 5 6 package docker 7 8 import ( 9 "path/filepath" 10 "testing" 11 12 "github.com/stretchr/testify/require" 13 ) 14 15 func TestExpandPath(t *testing.T) { 16 cases := []struct { 17 base string 18 target string 19 expected string 20 }{ 21 {"/tmp/alloc/task", ".", "/tmp/alloc/task"}, 22 {"/tmp/alloc/task", "..", "/tmp/alloc"}, 23 24 {"/tmp/alloc/task", "d1/d2", "/tmp/alloc/task/d1/d2"}, 25 {"/tmp/alloc/task", "../d1/d2", "/tmp/alloc/d1/d2"}, 26 {"/tmp/alloc/task", "../../d1/d2", "/tmp/d1/d2"}, 27 28 {"/tmp/alloc/task", "c:/home/user", "c:/home/user"}, 29 {"/tmp/alloc/task", "c:/home/user/..", "c:/home"}, 30 31 {"/tmp/alloc/task", `//./pipe/named_pipe`, `//./pipe/named_pipe`}, 32 } 33 34 for _, c := range cases { 35 t.Run(c.expected, func(t *testing.T) { 36 require.Equal(t, c.expected, filepath.ToSlash(expandPath(c.base, c.target))) 37 }) 38 } 39 } 40 41 func TestParseVolumeSpec_Windows(t *testing.T) { 42 validCases := []struct { 43 name string 44 bindSpec string 45 hostPath string 46 containerPath string 47 mode string 48 }{ 49 { 50 "basic mount", 51 `c:\windows:e:\containerpath`, 52 `c:\windows`, 53 `e:\containerpath`, 54 "", 55 }, 56 { 57 "relative path", 58 `relativepath:e:\containerpath`, 59 `relativepath`, 60 `e:\containerpath`, 61 "", 62 }, 63 { 64 "named pipe", 65 `//./pipe/named_pipe://./pipe/named_pipe`, 66 `\\.\pipe\named_pipe`, 67 `//./pipe/named_pipe`, 68 "", 69 }, 70 } 71 72 for _, c := range validCases { 73 t.Run("valid:"+c.name, func(t *testing.T) { 74 hp, cp, m, err := parseVolumeSpec(c.bindSpec, "windows") 75 require.NoError(t, err) 76 require.Equal(t, c.hostPath, hp) 77 require.Equal(t, c.containerPath, cp) 78 require.Equal(t, c.mode, m) 79 }) 80 } 81 82 invalidCases := []string{ 83 // linux path 84 "/linux-path", 85 // windows single path entry 86 `e:\containerpath`, 87 } 88 89 for _, c := range invalidCases { 90 t.Run("invalid:"+c, func(t *testing.T) { 91 hp, cp, m, err := parseVolumeSpec(c, "windows") 92 require.Errorf(t, err, "expected error but parsed as %s:%s:%s", hp, cp, m) 93 }) 94 } 95 }