github.com/secure-build/gitlab-runner@v12.5.0+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  		"bind propagation": {
    68  			volumeSpec:    "/source:/destination:rslave",
    69  			expectedParts: &Volume{Source: "/source", Destination: "/destination", BindPropagation: "rslave"},
    70  		},
    71  		"mode with bind propagation": {
    72  			volumeSpec:    "/source:/destination:ro,rslave",
    73  			expectedParts: &Volume{Source: "/source", Destination: "/destination", Mode: "ro", BindPropagation: "rslave"},
    74  		},
    75  		"unsupported bind propagation": {
    76  			volumeSpec:    "/source:/destination:unkown",
    77  			expectedError: NewInvalidVolumeSpecErr("/source:/destination:unkown"),
    78  		},
    79  		"unsupported bind propagation with mode": {
    80  			volumeSpec:    "/source:/destination:ro,unkown",
    81  			expectedError: NewInvalidVolumeSpecErr("/source:/destination:ro,unkown"),
    82  		},
    83  		"malformed bind propagation": {
    84  			volumeSpec:    "/source:/destination:,rslave",
    85  			expectedError: NewInvalidVolumeSpecErr("/source:/destination:,rslave"),
    86  		},
    87  		// This is not a valid syntax for Docker but GitLab Runner still parses
    88  		// for the sake of simplicity, check
    89  		// https://gitlab.com/gitlab-org/gitlab-runner/merge_requests/1632#note_240079623
    90  		// for the discussion and rationale.
    91  		"too much colons for bind propagation": {
    92  			volumeSpec:    "/source:/destination:rw:rslave",
    93  			expectedParts: &Volume{Source: "/source", Destination: "/destination", Mode: "rw", BindPropagation: "rslave"},
    94  		},
    95  	}
    96  
    97  	for testName, testCase := range testCases {
    98  		t.Run(testName, func(t *testing.T) {
    99  			parser := NewLinuxParser()
   100  			parts, err := parser.ParseVolume(testCase.volumeSpec)
   101  
   102  			if testCase.expectedError == nil {
   103  				assert.NoError(t, err)
   104  			} else {
   105  				assert.EqualError(t, err, testCase.expectedError.Error())
   106  			}
   107  
   108  			assert.Equal(t, testCase.expectedParts, parts)
   109  		})
   110  	}
   111  }