gitlab.com/jfprevost/gitlab-runner-notlscheck@v11.11.4+incompatible/executors/docker/internal/volumes/parser/linux_parser_test.go (about)

     1  package parser
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/assert"
     7  )
     8  
     9  func TestLinuxParser_ParseVolume(t *testing.T) {
    10  	testCases := map[string]struct {
    11  		volumeSpec    string
    12  		expectedParts *Volume
    13  		expectedError error
    14  	}{
    15  		"empty": {
    16  			volumeSpec:    "",
    17  			expectedError: NewInvalidVolumeSpecErr(""),
    18  		},
    19  		"destination only": {
    20  			volumeSpec:    "/destination",
    21  			expectedParts: &Volume{Destination: "/destination"},
    22  		},
    23  		"source and destination": {
    24  			volumeSpec:    "/source:/destination",
    25  			expectedParts: &Volume{Source: "/source", Destination: "/destination"},
    26  		},
    27  		"destination and mode": {
    28  			volumeSpec:    "/destination:rw",
    29  			expectedParts: &Volume{Destination: "/destination", Mode: "rw"},
    30  		},
    31  		"all values": {
    32  			volumeSpec:    "/source:/destination:rw",
    33  			expectedParts: &Volume{Source: "/source", Destination: "/destination", Mode: "rw"},
    34  		},
    35  		"read only": {
    36  			volumeSpec:    "/source:/destination:ro",
    37  			expectedParts: &Volume{Source: "/source", Destination: "/destination", Mode: "ro"},
    38  		},
    39  		"volume case sensitive": {
    40  			volumeSpec:    "/Source:/Destination:rw",
    41  			expectedParts: &Volume{Source: "/Source", Destination: "/Destination", Mode: "rw"},
    42  		},
    43  		"support SELinux label bind mount content is shared among multiple containers": {
    44  			volumeSpec:    "/source:/destination:z",
    45  			expectedParts: &Volume{Source: "/source", Destination: "/destination", Mode: "z"},
    46  		},
    47  		"support SELinux label bind mount content is private and unshare": {
    48  			volumeSpec:    "/source:/destination:Z",
    49  			expectedParts: &Volume{Source: "/source", Destination: "/destination", Mode: "Z"},
    50  		},
    51  		"unsupported mode": {
    52  			volumeSpec:    "/source:/destination:T",
    53  			expectedError: NewInvalidVolumeSpecErr("/source:/destination:T"),
    54  		},
    55  		"too much colons": {
    56  			volumeSpec:    "/source:/destination:rw:something",
    57  			expectedError: NewInvalidVolumeSpecErr("/source:/destination:rw:something"),
    58  		},
    59  		"invalid source": {
    60  			volumeSpec:    ":/destination",
    61  			expectedError: NewInvalidVolumeSpecErr(":/destination"),
    62  		},
    63  		"named source": {
    64  			volumeSpec:    "volume_name:/destination",
    65  			expectedParts: &Volume{Source: "volume_name", Destination: "/destination"},
    66  		},
    67  	}
    68  
    69  	for testName, testCase := range testCases {
    70  		t.Run(testName, func(t *testing.T) {
    71  			parser := NewLinuxParser()
    72  			parts, err := parser.ParseVolume(testCase.volumeSpec)
    73  
    74  			if testCase.expectedError == nil {
    75  				assert.NoError(t, err)
    76  			} else {
    77  				assert.EqualError(t, err, testCase.expectedError.Error())
    78  			}
    79  
    80  			assert.Equal(t, testCase.expectedParts, parts)
    81  		})
    82  	}
    83  }