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