github.com/kunnos/engine@v1.13.1/volume/validate_test.go (about)

     1  package volume
     2  
     3  import (
     4  	"errors"
     5  	"io/ioutil"
     6  	"os"
     7  	"strings"
     8  	"testing"
     9  
    10  	"github.com/docker/docker/api/types/mount"
    11  )
    12  
    13  func TestValidateMount(t *testing.T) {
    14  	testDir, err := ioutil.TempDir("", "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  		{mount.Mount{Type: mount.TypeBind, Source: testSourcePath, Target: testDestinationPath}, errBindNotExist},
    31  		{mount.Mount{Type: mount.TypeBind, Source: testDir, Target: testDestinationPath}, nil},
    32  		{mount.Mount{Type: "invalid", Target: testDestinationPath}, errors.New("mount type unknown")},
    33  	}
    34  	for i, x := range cases {
    35  		err := validateMountConfig(&x.input)
    36  		if err == nil && x.expected == nil {
    37  			continue
    38  		}
    39  		if (err == nil && x.expected != nil) || (x.expected == nil && err != nil) || !strings.Contains(err.Error(), x.expected.Error()) {
    40  			t.Fatalf("expected %q, got %q, case: %d", x.expected, err, i)
    41  		}
    42  	}
    43  }