2016-06-15 13:11:23 -04:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2016-06-16 17:55:50 -04:00
|
|
|
"github.com/docker/docker/pkg/testutil/assert"
|
2016-06-15 13:11:23 -04:00
|
|
|
"github.com/docker/engine-api/types/swarm"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestMemBytesString(t *testing.T) {
|
|
|
|
var mem memBytes = 1048576
|
2016-06-16 17:55:50 -04:00
|
|
|
assert.Equal(t, mem.String(), "1 MiB")
|
2016-06-15 13:11:23 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestMemBytesSetAndValue(t *testing.T) {
|
|
|
|
var mem memBytes
|
2016-06-16 17:55:50 -04:00
|
|
|
assert.NilError(t, mem.Set("5kb"))
|
|
|
|
assert.Equal(t, mem.Value(), int64(5120))
|
2016-06-15 13:11:23 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestNanoCPUsString(t *testing.T) {
|
|
|
|
var cpus nanoCPUs = 6100000000
|
2016-06-16 17:55:50 -04:00
|
|
|
assert.Equal(t, cpus.String(), "6.100")
|
2016-06-15 13:11:23 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestNanoCPUsSetAndValue(t *testing.T) {
|
|
|
|
var cpus nanoCPUs
|
2016-06-16 17:55:50 -04:00
|
|
|
assert.NilError(t, cpus.Set("0.35"))
|
|
|
|
assert.Equal(t, cpus.Value(), int64(350000000))
|
2016-06-15 13:11:23 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestDurationOptString(t *testing.T) {
|
|
|
|
dur := time.Duration(300 * 10e8)
|
|
|
|
duration := DurationOpt{value: &dur}
|
2016-06-16 17:55:50 -04:00
|
|
|
assert.Equal(t, duration.String(), "5m0s")
|
2016-06-15 13:11:23 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestDurationOptSetAndValue(t *testing.T) {
|
|
|
|
var duration DurationOpt
|
2016-06-16 17:55:50 -04:00
|
|
|
assert.NilError(t, duration.Set("300s"))
|
|
|
|
assert.Equal(t, *duration.Value(), time.Duration(300*10e8))
|
2016-06-15 13:11:23 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestUint64OptString(t *testing.T) {
|
|
|
|
value := uint64(2345678)
|
|
|
|
opt := Uint64Opt{value: &value}
|
2016-06-16 17:55:50 -04:00
|
|
|
assert.Equal(t, opt.String(), "2345678")
|
2016-06-15 13:11:23 -04:00
|
|
|
|
|
|
|
opt = Uint64Opt{}
|
2016-06-16 17:55:50 -04:00
|
|
|
assert.Equal(t, opt.String(), "none")
|
2016-06-15 13:11:23 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestUint64OptSetAndValue(t *testing.T) {
|
|
|
|
var opt Uint64Opt
|
2016-06-16 17:55:50 -04:00
|
|
|
assert.NilError(t, opt.Set("14445"))
|
|
|
|
assert.Equal(t, *opt.Value(), uint64(14445))
|
2016-06-15 13:11:23 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestMountOptString(t *testing.T) {
|
|
|
|
mount := MountOpt{
|
|
|
|
values: []swarm.Mount{
|
|
|
|
{
|
2016-07-20 19:04:51 -04:00
|
|
|
Type: swarm.MountTypeBind,
|
2016-06-15 13:11:23 -04:00
|
|
|
Source: "/home/path",
|
|
|
|
Target: "/target",
|
|
|
|
},
|
|
|
|
{
|
2016-07-20 19:04:51 -04:00
|
|
|
Type: swarm.MountTypeVolume,
|
2016-06-15 13:11:23 -04:00
|
|
|
Source: "foo",
|
|
|
|
Target: "/target/foo",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2016-07-20 19:04:51 -04:00
|
|
|
expected := "bind /home/path /target, volume foo /target/foo"
|
2016-06-16 17:55:50 -04:00
|
|
|
assert.Equal(t, mount.String(), expected)
|
2016-06-15 13:11:23 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestMountOptSetNoError(t *testing.T) {
|
2016-07-20 19:34:45 -04:00
|
|
|
for _, testcase := range []string{
|
|
|
|
// tests several aliases that should have same result.
|
|
|
|
"type=bind,target=/target,source=/source",
|
|
|
|
"type=bind,src=/source,dst=/target",
|
|
|
|
"type=bind,name=/source,dst=/target",
|
|
|
|
"type=bind,name=/source,path=/target",
|
|
|
|
} {
|
|
|
|
var mount MountOpt
|
|
|
|
|
|
|
|
assert.NilError(t, mount.Set(testcase))
|
|
|
|
|
|
|
|
mounts := mount.Value()
|
|
|
|
assert.Equal(t, len(mounts), 1)
|
|
|
|
assert.Equal(t, mounts[0], swarm.Mount{
|
|
|
|
Type: swarm.MountTypeBind,
|
|
|
|
Source: "/source",
|
|
|
|
Target: "/target",
|
|
|
|
})
|
|
|
|
}
|
2016-06-15 13:11:23 -04:00
|
|
|
}
|
|
|
|
|
2016-07-20 19:34:45 -04:00
|
|
|
// TestMountOptDefaultType ensures that a mount without the type defaults to a
|
|
|
|
// volume mount.
|
|
|
|
func TestMountOptDefaultType(t *testing.T) {
|
2016-06-15 13:11:23 -04:00
|
|
|
var mount MountOpt
|
2016-07-20 19:34:45 -04:00
|
|
|
assert.NilError(t, mount.Set("target=/target,source=/foo"))
|
|
|
|
assert.Equal(t, mount.values[0].Type, swarm.MountTypeVolume)
|
2016-06-15 13:11:23 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestMountOptSetErrorNoTarget(t *testing.T) {
|
|
|
|
var mount MountOpt
|
2016-07-20 19:04:51 -04:00
|
|
|
assert.Error(t, mount.Set("type=volume,source=/foo"), "target is required")
|
2016-06-15 13:11:23 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestMountOptSetErrorInvalidKey(t *testing.T) {
|
|
|
|
var mount MountOpt
|
2016-07-20 19:04:51 -04:00
|
|
|
assert.Error(t, mount.Set("type=volume,bogus=foo"), "unexpected key 'bogus'")
|
2016-06-15 13:11:23 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestMountOptSetErrorInvalidField(t *testing.T) {
|
|
|
|
var mount MountOpt
|
2016-07-20 19:04:51 -04:00
|
|
|
assert.Error(t, mount.Set("type=volume,bogus"), "invalid field 'bogus'")
|
2016-06-15 13:11:23 -04:00
|
|
|
}
|
|
|
|
|
2016-07-20 19:34:45 -04:00
|
|
|
func TestMountOptSetErrorInvalidReadOnly(t *testing.T) {
|
2016-06-15 13:11:23 -04:00
|
|
|
var mount MountOpt
|
2016-07-20 19:04:51 -04:00
|
|
|
assert.Error(t, mount.Set("type=volume,readonly=no"), "invalid value for readonly: no")
|
2016-07-20 19:34:45 -04:00
|
|
|
assert.Error(t, mount.Set("type=volume,readonly=invalid"), "invalid value for readonly: invalid")
|
2016-07-05 14:43:28 -04:00
|
|
|
}
|
|
|
|
|
2016-07-20 19:34:45 -04:00
|
|
|
func TestMountOptDefaultEnableReadOnly(t *testing.T) {
|
2016-07-05 14:43:28 -04:00
|
|
|
var m MountOpt
|
|
|
|
assert.NilError(t, m.Set("type=bind,target=/foo,source=/foo"))
|
|
|
|
assert.Equal(t, m.values[0].ReadOnly, false)
|
|
|
|
|
|
|
|
m = MountOpt{}
|
|
|
|
assert.NilError(t, m.Set("type=bind,target=/foo,source=/foo,readonly"))
|
|
|
|
assert.Equal(t, m.values[0].ReadOnly, true)
|
|
|
|
|
|
|
|
m = MountOpt{}
|
|
|
|
assert.NilError(t, m.Set("type=bind,target=/foo,source=/foo,readonly=1"))
|
|
|
|
assert.Equal(t, m.values[0].ReadOnly, true)
|
|
|
|
|
|
|
|
m = MountOpt{}
|
|
|
|
assert.NilError(t, m.Set("type=bind,target=/foo,source=/foo,readonly=0"))
|
|
|
|
assert.Equal(t, m.values[0].ReadOnly, false)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMountOptVolumeNoCopy(t *testing.T) {
|
|
|
|
var m MountOpt
|
|
|
|
assert.Error(t, m.Set("type=volume,target=/foo,volume-nocopy"), "source is required")
|
|
|
|
|
|
|
|
m = MountOpt{}
|
|
|
|
assert.NilError(t, m.Set("type=volume,target=/foo,source=foo"))
|
|
|
|
assert.Equal(t, m.values[0].VolumeOptions == nil, true)
|
|
|
|
|
|
|
|
m = MountOpt{}
|
|
|
|
assert.NilError(t, m.Set("type=volume,target=/foo,source=foo,volume-nocopy=true"))
|
|
|
|
assert.Equal(t, m.values[0].VolumeOptions != nil, true)
|
|
|
|
assert.Equal(t, m.values[0].VolumeOptions.NoCopy, true)
|
|
|
|
|
|
|
|
m = MountOpt{}
|
|
|
|
assert.NilError(t, m.Set("type=volume,target=/foo,source=foo,volume-nocopy"))
|
|
|
|
assert.Equal(t, m.values[0].VolumeOptions != nil, true)
|
|
|
|
assert.Equal(t, m.values[0].VolumeOptions.NoCopy, true)
|
|
|
|
|
|
|
|
m = MountOpt{}
|
|
|
|
assert.NilError(t, m.Set("type=volume,target=/foo,source=foo,volume-nocopy=1"))
|
|
|
|
assert.Equal(t, m.values[0].VolumeOptions != nil, true)
|
|
|
|
assert.Equal(t, m.values[0].VolumeOptions.NoCopy, true)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMountOptTypeConflict(t *testing.T) {
|
|
|
|
var m MountOpt
|
|
|
|
assert.Error(t, m.Set("type=bind,target=/foo,source=/foo,volume-nocopy=true"), "cannot mix")
|
|
|
|
assert.Error(t, m.Set("type=volume,target=/foo,source=/foo,bind-propagation=rprivate"), "cannot mix")
|
2016-06-15 13:11:23 -04:00
|
|
|
}
|