github.com/docker/docker@v299999999.0.0-20200612211812-aaf470eca7b5+incompatible/daemon/cluster/executor/container/validate_test.go (about)

     1  package container // import "github.com/docker/docker/daemon/cluster/executor/container"
     2  
     3  import (
     4  	"io/ioutil"
     5  	"os"
     6  	"strings"
     7  	"testing"
     8  
     9  	"github.com/docker/docker/daemon"
    10  	"github.com/docker/docker/pkg/stringid"
    11  	"github.com/docker/swarmkit/api"
    12  )
    13  
    14  func newTestControllerWithMount(m api.Mount) (*controller, error) {
    15  	return newController(&daemon.Daemon{}, nil, nil, &api.Task{
    16  		ID:        stringid.GenerateRandomID(),
    17  		ServiceID: stringid.GenerateRandomID(),
    18  		Spec: api.TaskSpec{
    19  			Runtime: &api.TaskSpec_Container{
    20  				Container: &api.ContainerSpec{
    21  					Image: "image_name",
    22  					Labels: map[string]string{
    23  						"com.docker.swarm.task.id": "id",
    24  					},
    25  					Mounts: []api.Mount{m},
    26  				},
    27  			},
    28  		},
    29  	}, nil,
    30  		nil)
    31  }
    32  
    33  func TestControllerValidateMountBind(t *testing.T) {
    34  	// with improper source
    35  	if _, err := newTestControllerWithMount(api.Mount{
    36  		Type:   api.MountTypeBind,
    37  		Source: "foo",
    38  		Target: testAbsPath,
    39  	}); err == nil || !strings.Contains(err.Error(), "invalid bind mount source") {
    40  		t.Fatalf("expected  error, got: %v", err)
    41  	}
    42  
    43  	// with non-existing source
    44  	if _, err := newTestControllerWithMount(api.Mount{
    45  		Type:   api.MountTypeBind,
    46  		Source: testAbsNonExistent,
    47  		Target: testAbsPath,
    48  	}); err != nil {
    49  		t.Fatalf("controller should not error at creation: %v", err)
    50  	}
    51  
    52  	// with proper source
    53  	tmpdir, err := ioutil.TempDir("", "TestControllerValidateMountBind")
    54  	if err != nil {
    55  		t.Fatalf("failed to create temp dir: %v", err)
    56  	}
    57  	defer os.Remove(tmpdir)
    58  
    59  	if _, err := newTestControllerWithMount(api.Mount{
    60  		Type:   api.MountTypeBind,
    61  		Source: tmpdir,
    62  		Target: testAbsPath,
    63  	}); err != nil {
    64  		t.Fatalf("expected  error, got: %v", err)
    65  	}
    66  }
    67  
    68  func TestControllerValidateMountVolume(t *testing.T) {
    69  	// with improper source
    70  	if _, err := newTestControllerWithMount(api.Mount{
    71  		Type:   api.MountTypeVolume,
    72  		Source: testAbsPath,
    73  		Target: testAbsPath,
    74  	}); err == nil || !strings.Contains(err.Error(), "invalid volume mount source") {
    75  		t.Fatalf("expected error, got: %v", err)
    76  	}
    77  
    78  	// with proper source
    79  	if _, err := newTestControllerWithMount(api.Mount{
    80  		Type:   api.MountTypeVolume,
    81  		Source: "foo",
    82  		Target: testAbsPath,
    83  	}); err != nil {
    84  		t.Fatalf("expected error, got: %v", err)
    85  	}
    86  }
    87  
    88  func TestControllerValidateMountTarget(t *testing.T) {
    89  	tmpdir, err := ioutil.TempDir("", "TestControllerValidateMountTarget")
    90  	if err != nil {
    91  		t.Fatalf("failed to create temp dir: %v", err)
    92  	}
    93  	defer os.Remove(tmpdir)
    94  
    95  	// with improper target
    96  	if _, err := newTestControllerWithMount(api.Mount{
    97  		Type:   api.MountTypeBind,
    98  		Source: testAbsPath,
    99  		Target: "foo",
   100  	}); err == nil || !strings.Contains(err.Error(), "invalid mount target") {
   101  		t.Fatalf("expected error, got: %v", err)
   102  	}
   103  
   104  	// with proper target
   105  	if _, err := newTestControllerWithMount(api.Mount{
   106  		Type:   api.MountTypeBind,
   107  		Source: tmpdir,
   108  		Target: testAbsPath,
   109  	}); err != nil {
   110  		t.Fatalf("expected no error, got: %v", err)
   111  	}
   112  }
   113  
   114  func TestControllerValidateMountTmpfs(t *testing.T) {
   115  	// with improper target
   116  	if _, err := newTestControllerWithMount(api.Mount{
   117  		Type:   api.MountTypeTmpfs,
   118  		Source: "foo",
   119  		Target: testAbsPath,
   120  	}); err == nil || !strings.Contains(err.Error(), "invalid tmpfs source") {
   121  		t.Fatalf("expected error, got: %v", err)
   122  	}
   123  
   124  	// with proper target
   125  	if _, err := newTestControllerWithMount(api.Mount{
   126  		Type:   api.MountTypeTmpfs,
   127  		Target: testAbsPath,
   128  	}); err != nil {
   129  		t.Fatalf("expected no error, got: %v", err)
   130  	}
   131  }
   132  
   133  func TestControllerValidateMountInvalidType(t *testing.T) {
   134  	// with improper target
   135  	if _, err := newTestControllerWithMount(api.Mount{
   136  		Type:   api.Mount_MountType(9999),
   137  		Source: "foo",
   138  		Target: testAbsPath,
   139  	}); err == nil || !strings.Contains(err.Error(), "invalid mount type") {
   140  		t.Fatalf("expected error, got: %v", err)
   141  	}
   142  }