github.com/rish1988/moby@v25.0.2+incompatible/volume/mounts/parser_test.go (about)

     1  package mounts // import "github.com/docker/docker/volume/mounts"
     2  
     3  import (
     4  	"os"
     5  	"testing"
     6  
     7  	"github.com/docker/docker/api/types/mount"
     8  	"gotest.tools/v3/assert"
     9  	is "gotest.tools/v3/assert/cmp"
    10  )
    11  
    12  type mockFiProvider struct{}
    13  
    14  func (mockFiProvider) fileInfo(path string) (exists, isDir bool, err error) {
    15  	dirs := map[string]struct{}{
    16  		`c:\`:                    {},
    17  		`c:\windows\`:            {},
    18  		`c:\windows`:             {},
    19  		`c:\program files`:       {},
    20  		`c:\Windows`:             {},
    21  		`c:\Program Files (x86)`: {},
    22  		`\\?\c:\windows\`:        {},
    23  	}
    24  	files := map[string]struct{}{
    25  		`c:\windows\system32\ntdll.dll`: {},
    26  	}
    27  	if _, ok := dirs[path]; ok {
    28  		return true, true, nil
    29  	}
    30  	if _, ok := files[path]; ok {
    31  		return true, false, nil
    32  	}
    33  	return false, false, nil
    34  }
    35  
    36  // always returns the configured error
    37  // this is used to test error handling
    38  type mockFiProviderWithError struct{ err error }
    39  
    40  func (m mockFiProviderWithError) fileInfo(path string) (bool, bool, error) {
    41  	return false, false, m.err
    42  }
    43  
    44  func TestParseMountSpec(t *testing.T) {
    45  	testDir, err := os.MkdirTemp("", "test-mount-config")
    46  	if err != nil {
    47  		t.Fatal(err)
    48  	}
    49  	defer os.RemoveAll(testDir)
    50  	parser := NewParser()
    51  	cases := []struct {
    52  		input    mount.Mount
    53  		expected MountPoint
    54  	}{
    55  		{
    56  			input:    mount.Mount{Type: mount.TypeBind, Source: testDir, Target: testDestinationPath, ReadOnly: true},
    57  			expected: MountPoint{Type: mount.TypeBind, Source: testDir, Destination: testDestinationPath, Propagation: parser.DefaultPropagationMode()},
    58  		},
    59  		{
    60  			input:    mount.Mount{Type: mount.TypeBind, Source: testDir, Target: testDestinationPath},
    61  			expected: MountPoint{Type: mount.TypeBind, Source: testDir, Destination: testDestinationPath, RW: true, Propagation: parser.DefaultPropagationMode()},
    62  		},
    63  		{
    64  			input:    mount.Mount{Type: mount.TypeBind, Source: testDir + string(os.PathSeparator), Target: testDestinationPath, ReadOnly: true},
    65  			expected: MountPoint{Type: mount.TypeBind, Source: testDir, Destination: testDestinationPath, Propagation: parser.DefaultPropagationMode()},
    66  		},
    67  		{
    68  			input:    mount.Mount{Type: mount.TypeBind, Source: testDir, Target: testDestinationPath + string(os.PathSeparator), ReadOnly: true},
    69  			expected: MountPoint{Type: mount.TypeBind, Source: testDir, Destination: testDestinationPath, Propagation: parser.DefaultPropagationMode()},
    70  		},
    71  		{
    72  			input:    mount.Mount{Type: mount.TypeVolume, Target: testDestinationPath},
    73  			expected: MountPoint{Type: mount.TypeVolume, Destination: testDestinationPath, RW: true, CopyData: parser.DefaultCopyMode()},
    74  		},
    75  		{
    76  			input:    mount.Mount{Type: mount.TypeVolume, Target: testDestinationPath + string(os.PathSeparator)},
    77  			expected: MountPoint{Type: mount.TypeVolume, Destination: testDestinationPath, RW: true, CopyData: parser.DefaultCopyMode()},
    78  		},
    79  	}
    80  
    81  	for _, tc := range cases {
    82  		tc := tc
    83  		t.Run("", func(t *testing.T) {
    84  			mp, err := parser.ParseMountSpec(tc.input)
    85  			assert.NilError(t, err)
    86  
    87  			assert.Check(t, is.Equal(mp.Type, tc.expected.Type))
    88  			assert.Check(t, is.Equal(mp.Destination, tc.expected.Destination))
    89  			assert.Check(t, is.Equal(mp.Source, tc.expected.Source))
    90  			assert.Check(t, is.Equal(mp.RW, tc.expected.RW))
    91  			assert.Check(t, is.Equal(mp.Propagation, tc.expected.Propagation))
    92  			assert.Check(t, is.Equal(mp.Driver, tc.expected.Driver))
    93  			assert.Check(t, is.Equal(mp.CopyData, tc.expected.CopyData))
    94  		})
    95  	}
    96  }