github.com/rawahars/moby@v24.0.4+incompatible/volume/mounts/validate_test.go (about)

     1  package mounts // import "github.com/docker/docker/volume/mounts"
     2  
     3  import (
     4  	"errors"
     5  	"os"
     6  	"runtime"
     7  	"strings"
     8  	"testing"
     9  
    10  	"github.com/docker/docker/api/types/mount"
    11  )
    12  
    13  func TestValidateMount(t *testing.T) {
    14  	testDir, err := os.MkdirTemp("", "test-validate-mount")
    15  	if err != nil {
    16  		t.Fatal(err)
    17  	}
    18  	defer os.RemoveAll(testDir)
    19  
    20  	cases := []struct {
    21  		input    mount.Mount
    22  		expected error
    23  	}{
    24  		{mount.Mount{Type: mount.TypeVolume}, errMissingField("Target")},
    25  		{mount.Mount{Type: mount.TypeVolume, Target: testDestinationPath, Source: "hello"}, nil},
    26  		{mount.Mount{Type: mount.TypeVolume, Target: testDestinationPath}, nil},
    27  		{mount.Mount{Type: mount.TypeBind}, errMissingField("Target")},
    28  		{mount.Mount{Type: mount.TypeBind, Target: testDestinationPath}, errMissingField("Source")},
    29  		{mount.Mount{Type: mount.TypeBind, Target: testDestinationPath, Source: testSourcePath, VolumeOptions: &mount.VolumeOptions{}}, errExtraField("VolumeOptions")},
    30  
    31  		{mount.Mount{Type: mount.TypeBind, Source: testDir, Target: testDestinationPath}, nil},
    32  		{mount.Mount{Type: "invalid", Target: testDestinationPath}, errors.New("mount type unknown")},
    33  		{mount.Mount{Type: mount.TypeBind, Source: testSourcePath, Target: testDestinationPath}, errBindSourceDoesNotExist(testSourcePath)},
    34  	}
    35  
    36  	lcowCases := []struct {
    37  		input    mount.Mount
    38  		expected error
    39  	}{
    40  		{mount.Mount{Type: mount.TypeVolume}, errMissingField("Target")},
    41  		{mount.Mount{Type: mount.TypeVolume, Target: "/foo", Source: "hello"}, nil},
    42  		{mount.Mount{Type: mount.TypeVolume, Target: "/foo"}, nil},
    43  		{mount.Mount{Type: mount.TypeBind}, errMissingField("Target")},
    44  		{mount.Mount{Type: mount.TypeBind, Target: "/foo"}, errMissingField("Source")},
    45  		{mount.Mount{Type: mount.TypeBind, Target: "/foo", Source: "c:\\foo", VolumeOptions: &mount.VolumeOptions{}}, errExtraField("VolumeOptions")},
    46  		{mount.Mount{Type: mount.TypeBind, Source: "c:\\foo", Target: "/foo"}, errBindSourceDoesNotExist("c:\\foo")},
    47  		{mount.Mount{Type: mount.TypeBind, Source: testDir, Target: "/foo"}, nil},
    48  		{mount.Mount{Type: "invalid", Target: "/foo"}, errors.New("mount type unknown")},
    49  	}
    50  	parser := NewParser()
    51  	for i, x := range cases {
    52  		err := parser.ValidateMountConfig(&x.input)
    53  		if err == nil && x.expected == nil {
    54  			continue
    55  		}
    56  		if (err == nil && x.expected != nil) || (x.expected == nil && err != nil) || !strings.Contains(err.Error(), x.expected.Error()) {
    57  			t.Errorf("expected %q, got %q, case: %d", x.expected, err, i)
    58  		}
    59  	}
    60  	if runtime.GOOS == "windows" {
    61  		parser = NewLCOWParser()
    62  		for i, x := range lcowCases {
    63  			err := parser.ValidateMountConfig(&x.input)
    64  			if err == nil && x.expected == nil {
    65  				continue
    66  			}
    67  			if (err == nil && x.expected != nil) || (x.expected == nil && err != nil) || !strings.Contains(err.Error(), x.expected.Error()) {
    68  				t.Errorf("expected %q, got %q, case: %d", x.expected, err, i)
    69  			}
    70  		}
    71  	}
    72  }