github.com/ssdev-go/moby@v17.12.1-ce-rc2+incompatible/volume/validate_test.go (about)

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