github.com/kaisenlinux/docker.io@v0.0.0-20230510090727-ea55db55fac7/engine/daemon/cluster/executor/container/validate_test.go (about) 1 package container // import "github.com/docker/docker/daemon/cluster/executor/container" 2 3 import ( 4 "os" 5 "strings" 6 "testing" 7 8 "github.com/docker/docker/daemon" 9 "github.com/docker/docker/pkg/stringid" 10 "github.com/docker/swarmkit/api" 11 ) 12 13 func newTestControllerWithMount(m api.Mount) (*controller, error) { 14 return newController(&daemon.Daemon{}, nil, nil, &api.Task{ 15 ID: stringid.GenerateRandomID(), 16 ServiceID: stringid.GenerateRandomID(), 17 Spec: api.TaskSpec{ 18 Runtime: &api.TaskSpec_Container{ 19 Container: &api.ContainerSpec{ 20 Image: "image_name", 21 Labels: map[string]string{ 22 "com.docker.swarm.task.id": "id", 23 }, 24 Mounts: []api.Mount{m}, 25 }, 26 }, 27 }, 28 }, nil, 29 nil) 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: testAbsNonExistent, 46 Target: testAbsPath, 47 }); err != nil { 48 t.Fatalf("controller should not error at creation: %v", err) 49 } 50 51 // with proper source 52 tmpdir, err := os.MkdirTemp("", "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 := os.MkdirTemp("", "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 }