github.com/noxiouz/docker@v0.7.3-0.20160629055221-3d231c78e8c5/api/client/service/opts_test.go (about) 1 package service 2 3 import ( 4 "testing" 5 "time" 6 7 "github.com/docker/docker/pkg/testutil/assert" 8 "github.com/docker/engine-api/types/swarm" 9 ) 10 11 func TestMemBytesString(t *testing.T) { 12 var mem memBytes = 1048576 13 assert.Equal(t, mem.String(), "1 MiB") 14 } 15 16 func TestMemBytesSetAndValue(t *testing.T) { 17 var mem memBytes 18 assert.NilError(t, mem.Set("5kb")) 19 assert.Equal(t, mem.Value(), int64(5120)) 20 } 21 22 func TestNanoCPUsString(t *testing.T) { 23 var cpus nanoCPUs = 6100000000 24 assert.Equal(t, cpus.String(), "6.100") 25 } 26 27 func TestNanoCPUsSetAndValue(t *testing.T) { 28 var cpus nanoCPUs 29 assert.NilError(t, cpus.Set("0.35")) 30 assert.Equal(t, cpus.Value(), int64(350000000)) 31 } 32 33 func TestDurationOptString(t *testing.T) { 34 dur := time.Duration(300 * 10e8) 35 duration := DurationOpt{value: &dur} 36 assert.Equal(t, duration.String(), "5m0s") 37 } 38 39 func TestDurationOptSetAndValue(t *testing.T) { 40 var duration DurationOpt 41 assert.NilError(t, duration.Set("300s")) 42 assert.Equal(t, *duration.Value(), time.Duration(300*10e8)) 43 } 44 45 func TestUint64OptString(t *testing.T) { 46 value := uint64(2345678) 47 opt := Uint64Opt{value: &value} 48 assert.Equal(t, opt.String(), "2345678") 49 50 opt = Uint64Opt{} 51 assert.Equal(t, opt.String(), "none") 52 } 53 54 func TestUint64OptSetAndValue(t *testing.T) { 55 var opt Uint64Opt 56 assert.NilError(t, opt.Set("14445")) 57 assert.Equal(t, *opt.Value(), uint64(14445)) 58 } 59 60 func TestMountOptString(t *testing.T) { 61 mount := MountOpt{ 62 values: []swarm.Mount{ 63 { 64 Type: swarm.MountType("BIND"), 65 Source: "/home/path", 66 Target: "/target", 67 }, 68 { 69 Type: swarm.MountType("VOLUME"), 70 Source: "foo", 71 Target: "/target/foo", 72 }, 73 }, 74 } 75 expected := "BIND /home/path /target, VOLUME foo /target/foo" 76 assert.Equal(t, mount.String(), expected) 77 } 78 79 func TestMountOptSetNoError(t *testing.T) { 80 var mount MountOpt 81 assert.NilError(t, mount.Set("type=bind,target=/target,source=/foo")) 82 83 mounts := mount.Value() 84 assert.Equal(t, len(mounts), 1) 85 assert.Equal(t, mounts[0], swarm.Mount{ 86 Type: swarm.MountType("BIND"), 87 Source: "/foo", 88 Target: "/target", 89 }) 90 } 91 92 func TestMountOptSetErrorNoType(t *testing.T) { 93 var mount MountOpt 94 assert.Error(t, mount.Set("target=/target,source=/foo"), "type is required") 95 } 96 97 func TestMountOptSetErrorNoTarget(t *testing.T) { 98 var mount MountOpt 99 assert.Error(t, mount.Set("type=VOLUME,source=/foo"), "target is required") 100 } 101 102 func TestMountOptSetErrorInvalidKey(t *testing.T) { 103 var mount MountOpt 104 assert.Error(t, mount.Set("type=VOLUME,bogus=foo"), "unexpected key 'bogus'") 105 } 106 107 func TestMountOptSetErrorInvalidField(t *testing.T) { 108 var mount MountOpt 109 assert.Error(t, mount.Set("type=VOLUME,bogus"), "invalid field 'bogus'") 110 } 111 112 func TestMountOptSetErrorInvalidWritable(t *testing.T) { 113 var mount MountOpt 114 assert.Error(t, mount.Set("type=VOLUME,writable=yes"), "invalid value for writable: yes") 115 }