github.com/vieux/docker@v0.6.3-0.20161004191708-e097c2a938c7/daemon/cluster/executor/container/validate_test.go (about) 1 package 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{}, &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 }) 30 } 31 32 func TestControllerValidateMountBind(t *testing.T) { 33 // with improper source 34 if _, err := newTestControllerWithMount(api.Mount{ 35 Type: api.MountTypeBind, 36 Source: "foo", 37 Target: testAbsPath, 38 }); err == nil || !strings.Contains(err.Error(), "invalid bind mount source") { 39 t.Fatalf("expected error, got: %v", err) 40 } 41 42 // with non-existing source 43 if _, err := newTestControllerWithMount(api.Mount{ 44 Type: api.MountTypeBind, 45 Source: "/some-non-existing-host-path/", 46 Target: testAbsPath, 47 }); err == nil || !strings.Contains(err.Error(), "invalid bind mount source") { 48 t.Fatalf("expected error, got: %v", err) 49 } 50 51 // with proper source 52 tmpdir, err := ioutil.TempDir("", "TestControllerValidateMountBind") 53 if err != nil { 54 t.Fatalf("failed to create temp dir: %v", err) 55 } 56 defer os.Remove(tmpdir) 57 58 if _, err := newTestControllerWithMount(api.Mount{ 59 Type: api.MountTypeBind, 60 Source: tmpdir, 61 Target: testAbsPath, 62 }); err != nil { 63 t.Fatalf("expected error, got: %v", err) 64 } 65 } 66 67 func TestControllerValidateMountVolume(t *testing.T) { 68 // with improper source 69 if _, err := newTestControllerWithMount(api.Mount{ 70 Type: api.MountTypeVolume, 71 Source: testAbsPath, 72 Target: testAbsPath, 73 }); err == nil || !strings.Contains(err.Error(), "invalid volume mount source") { 74 t.Fatalf("expected error, got: %v", err) 75 } 76 77 // with proper source 78 if _, err := newTestControllerWithMount(api.Mount{ 79 Type: api.MountTypeVolume, 80 Source: "foo", 81 Target: testAbsPath, 82 }); err != nil { 83 t.Fatalf("expected error, got: %v", err) 84 } 85 } 86 87 func TestControllerValidateMountTarget(t *testing.T) { 88 tmpdir, err := ioutil.TempDir("", "TestControllerValidateMountTarget") 89 if err != nil { 90 t.Fatalf("failed to create temp dir: %v", err) 91 } 92 defer os.Remove(tmpdir) 93 94 // with improper target 95 if _, err := newTestControllerWithMount(api.Mount{ 96 Type: api.MountTypeBind, 97 Source: testAbsPath, 98 Target: "foo", 99 }); err == nil || !strings.Contains(err.Error(), "invalid mount target") { 100 t.Fatalf("expected error, got: %v", err) 101 } 102 103 // with proper target 104 if _, err := newTestControllerWithMount(api.Mount{ 105 Type: api.MountTypeBind, 106 Source: tmpdir, 107 Target: testAbsPath, 108 }); err != nil { 109 t.Fatalf("expected no error, got: %v", err) 110 } 111 } 112 113 func TestControllerValidateMountTmpfs(t *testing.T) { 114 // with improper target 115 if _, err := newTestControllerWithMount(api.Mount{ 116 Type: api.MountTypeTmpfs, 117 Source: "foo", 118 Target: testAbsPath, 119 }); err == nil || !strings.Contains(err.Error(), "invalid tmpfs source") { 120 t.Fatalf("expected error, got: %v", err) 121 } 122 123 // with proper target 124 if _, err := newTestControllerWithMount(api.Mount{ 125 Type: api.MountTypeTmpfs, 126 Target: testAbsPath, 127 }); err != nil { 128 t.Fatalf("expected no error, got: %v", err) 129 } 130 } 131 132 func TestControllerValidateMountInvalidType(t *testing.T) { 133 // with improper target 134 if _, err := newTestControllerWithMount(api.Mount{ 135 Type: api.Mount_MountType(9999), 136 Source: "foo", 137 Target: testAbsPath, 138 }); err == nil || !strings.Contains(err.Error(), "invalid mount type") { 139 t.Fatalf("expected error, got: %v", err) 140 } 141 }