github.com/bigcommerce/nomad@v0.9.3-bc/drivers/docker/utils_windows_test.go (about)

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