github.com/moby/docker@v26.1.3+incompatible/volume/mounts/validate_test.go (about)

     1  package mounts // import "github.com/docker/docker/volume/mounts"
     2  
     3  import (
     4  	"errors"
     5  	"runtime"
     6  	"testing"
     7  
     8  	"github.com/docker/docker/api/types/mount"
     9  	"gotest.tools/v3/assert"
    10  	is "gotest.tools/v3/assert/cmp"
    11  )
    12  
    13  func TestValidateMount(t *testing.T) {
    14  	testDir := t.TempDir()
    15  	parser := NewParser()
    16  
    17  	tests := []struct {
    18  		input    mount.Mount
    19  		expected error
    20  	}{
    21  		{
    22  			input:    mount.Mount{Type: mount.TypeVolume},
    23  			expected: errMissingField("Target"),
    24  		},
    25  		{
    26  			input: mount.Mount{Type: mount.TypeVolume, Target: testDestinationPath, Source: "hello"},
    27  		},
    28  		{
    29  			input: mount.Mount{Type: mount.TypeVolume, Target: testDestinationPath},
    30  		},
    31  		{
    32  			input: mount.Mount{Type: mount.TypeVolume, Target: testDestinationPath, Source: "hello", VolumeOptions: &mount.VolumeOptions{Subpath: "world"}},
    33  		},
    34  		{
    35  			input:    mount.Mount{Type: mount.TypeBind},
    36  			expected: errMissingField("Target"),
    37  		},
    38  		{
    39  			input:    mount.Mount{Type: mount.TypeBind, Target: testDestinationPath},
    40  			expected: errMissingField("Source"),
    41  		},
    42  		{
    43  			input:    mount.Mount{Type: mount.TypeBind, Target: testDestinationPath, Source: testSourcePath, VolumeOptions: &mount.VolumeOptions{}},
    44  			expected: errExtraField("VolumeOptions"),
    45  		},
    46  		{
    47  			input: mount.Mount{Type: mount.TypeBind, Source: testDir, Target: testDestinationPath},
    48  		},
    49  		{
    50  			input:    mount.Mount{Type: "invalid", Target: testDestinationPath},
    51  			expected: errors.New("mount type unknown"),
    52  		},
    53  		{
    54  			input:    mount.Mount{Type: mount.TypeBind, Source: testSourcePath, Target: testDestinationPath},
    55  			expected: errBindSourceDoesNotExist(testSourcePath),
    56  		},
    57  	}
    58  	for _, tc := range tests {
    59  		tc := tc
    60  		t.Run("", func(t *testing.T) {
    61  			err := parser.ValidateMountConfig(&tc.input)
    62  			if tc.expected != nil {
    63  				assert.Check(t, is.ErrorContains(err, tc.expected.Error()))
    64  			} else {
    65  				assert.Check(t, err)
    66  			}
    67  		})
    68  	}
    69  }
    70  
    71  func TestValidateLCOWMount(t *testing.T) {
    72  	if runtime.GOOS != "windows" {
    73  		t.Skip("only tested on Windows")
    74  	}
    75  	testDir := t.TempDir()
    76  	parser := NewLCOWParser()
    77  
    78  	tests := []struct {
    79  		input    mount.Mount
    80  		expected error
    81  	}{
    82  		{
    83  			input:    mount.Mount{Type: mount.TypeVolume},
    84  			expected: errMissingField("Target"),
    85  		},
    86  		{
    87  			input: mount.Mount{Type: mount.TypeVolume, Target: "/foo", Source: "hello"},
    88  		},
    89  		{
    90  			input: mount.Mount{Type: mount.TypeVolume, Target: "/foo"},
    91  		},
    92  		{
    93  			input:    mount.Mount{Type: mount.TypeBind},
    94  			expected: errMissingField("Target"),
    95  		},
    96  		{
    97  			input:    mount.Mount{Type: mount.TypeBind, Target: "/foo"},
    98  			expected: errMissingField("Source"),
    99  		},
   100  		{
   101  			input:    mount.Mount{Type: mount.TypeBind, Target: "/foo", Source: "c:\\foo", VolumeOptions: &mount.VolumeOptions{}},
   102  			expected: errExtraField("VolumeOptions"),
   103  		},
   104  		{
   105  			input:    mount.Mount{Type: mount.TypeBind, Source: "c:\\foo", Target: "/foo"},
   106  			expected: errBindSourceDoesNotExist("c:\\foo"),
   107  		},
   108  		{
   109  			input: mount.Mount{Type: mount.TypeBind, Source: testDir, Target: "/foo"},
   110  		},
   111  		{
   112  			input:    mount.Mount{Type: "invalid", Target: "/foo"},
   113  			expected: errors.New("mount type unknown"),
   114  		},
   115  	}
   116  	for _, tc := range tests {
   117  		tc := tc
   118  		t.Run("", func(t *testing.T) {
   119  			err := parser.ValidateMountConfig(&tc.input)
   120  			if tc.expected != nil {
   121  				assert.Check(t, is.ErrorContains(err, tc.expected.Error()))
   122  			} else {
   123  				assert.Check(t, err)
   124  			}
   125  		})
   126  	}
   127  }