github.com/rawahars/moby@v24.0.4+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  )
     9  
    10  type mockFiProvider struct{}
    11  
    12  func (mockFiProvider) fileInfo(path string) (exists, isDir bool, err error) {
    13  	dirs := map[string]struct{}{
    14  		`c:\`:                    {},
    15  		`c:\windows\`:            {},
    16  		`c:\windows`:             {},
    17  		`c:\program files`:       {},
    18  		`c:\Windows`:             {},
    19  		`c:\Program Files (x86)`: {},
    20  		`\\?\c:\windows\`:        {},
    21  	}
    22  	files := map[string]struct{}{
    23  		`c:\windows\system32\ntdll.dll`: {},
    24  	}
    25  	if _, ok := dirs[path]; ok {
    26  		return true, true, nil
    27  	}
    28  	if _, ok := files[path]; ok {
    29  		return true, false, nil
    30  	}
    31  	return false, false, nil
    32  }
    33  
    34  // always returns the configured error
    35  // this is used to test error handling
    36  type mockFiProviderWithError struct{ err error }
    37  
    38  func (m mockFiProviderWithError) fileInfo(path string) (bool, bool, error) {
    39  	return false, false, m.err
    40  }
    41  
    42  func TestParseMountSpec(t *testing.T) {
    43  	testDir, err := os.MkdirTemp("", "test-mount-config")
    44  	if err != nil {
    45  		t.Fatal(err)
    46  	}
    47  	defer os.RemoveAll(testDir)
    48  	parser := NewParser()
    49  	cases := []struct {
    50  		input    mount.Mount
    51  		expected MountPoint
    52  	}{
    53  		{mount.Mount{Type: mount.TypeBind, Source: testDir, Target: testDestinationPath, ReadOnly: true}, MountPoint{Type: mount.TypeBind, Source: testDir, Destination: testDestinationPath, Propagation: parser.DefaultPropagationMode()}},
    54  		{mount.Mount{Type: mount.TypeBind, Source: testDir, Target: testDestinationPath}, MountPoint{Type: mount.TypeBind, Source: testDir, Destination: testDestinationPath, RW: true, Propagation: parser.DefaultPropagationMode()}},
    55  		{mount.Mount{Type: mount.TypeBind, Source: testDir + string(os.PathSeparator), Target: testDestinationPath, ReadOnly: true}, MountPoint{Type: mount.TypeBind, Source: testDir, Destination: testDestinationPath, Propagation: parser.DefaultPropagationMode()}},
    56  		{mount.Mount{Type: mount.TypeBind, Source: testDir, Target: testDestinationPath + string(os.PathSeparator), ReadOnly: true}, MountPoint{Type: mount.TypeBind, Source: testDir, Destination: testDestinationPath, Propagation: parser.DefaultPropagationMode()}},
    57  		{mount.Mount{Type: mount.TypeVolume, Target: testDestinationPath}, MountPoint{Type: mount.TypeVolume, Destination: testDestinationPath, RW: true, CopyData: parser.DefaultCopyMode()}},
    58  		{mount.Mount{Type: mount.TypeVolume, Target: testDestinationPath + string(os.PathSeparator)}, MountPoint{Type: mount.TypeVolume, Destination: testDestinationPath, RW: true, CopyData: parser.DefaultCopyMode()}},
    59  	}
    60  
    61  	for i, c := range cases {
    62  		t.Logf("case %d", i)
    63  		mp, err := parser.ParseMountSpec(c.input)
    64  		if err != nil {
    65  			t.Error(err)
    66  		}
    67  
    68  		if c.expected.Type != mp.Type {
    69  			t.Errorf("Expected mount types to match. Expected: '%s', Actual: '%s'", c.expected.Type, mp.Type)
    70  		}
    71  		if c.expected.Destination != mp.Destination {
    72  			t.Errorf("Expected mount destination to match. Expected: '%s', Actual: '%s'", c.expected.Destination, mp.Destination)
    73  		}
    74  		if c.expected.Source != mp.Source {
    75  			t.Errorf("Expected mount source to match. Expected: '%s', Actual: '%s'", c.expected.Source, mp.Source)
    76  		}
    77  		if c.expected.RW != mp.RW {
    78  			t.Errorf("Expected mount writable to match. Expected: '%v', Actual: '%v'", c.expected.RW, mp.RW)
    79  		}
    80  		if c.expected.Propagation != mp.Propagation {
    81  			t.Errorf("Expected mount propagation to match. Expected: '%v', Actual: '%s'", c.expected.Propagation, mp.Propagation)
    82  		}
    83  		if c.expected.Driver != mp.Driver {
    84  			t.Errorf("Expected mount driver to match. Expected: '%v', Actual: '%s'", c.expected.Driver, mp.Driver)
    85  		}
    86  		if c.expected.CopyData != mp.CopyData {
    87  			t.Errorf("Expected mount copy data to match. Expected: '%v', Actual: '%v'", c.expected.CopyData, mp.CopyData)
    88  		}
    89  	}
    90  }