github.com/anth0d/nomad@v0.0.0-20221214183521-ae3a0a2cad06/drivers/docker/utils_test.go (about)

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