github.com/vieux/docker@v0.6.3-0.20161004191708-e097c2a938c7/cli/command/service/opts_test.go (about) 1 package service 2 3 import ( 4 "testing" 5 "time" 6 7 mounttypes "github.com/docker/docker/api/types/mount" 8 "github.com/docker/docker/pkg/testutil/assert" 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: []mounttypes.Mount{ 63 { 64 Type: mounttypes.TypeBind, 65 Source: "/home/path", 66 Target: "/target", 67 }, 68 { 69 Type: mounttypes.TypeVolume, 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 TestMountOptSetBindNoErrorBind(t *testing.T) { 80 for _, testcase := range []string{ 81 // tests several aliases that should have same result. 82 "type=bind,target=/target,source=/source", 83 "type=bind,src=/source,dst=/target", 84 "type=bind,source=/source,dst=/target", 85 "type=bind,src=/source,target=/target", 86 } { 87 var mount MountOpt 88 89 assert.NilError(t, mount.Set(testcase)) 90 91 mounts := mount.Value() 92 assert.Equal(t, len(mounts), 1) 93 assert.Equal(t, mounts[0], mounttypes.Mount{ 94 Type: mounttypes.TypeBind, 95 Source: "/source", 96 Target: "/target", 97 }) 98 } 99 } 100 101 func TestMountOptSetVolumeNoError(t *testing.T) { 102 for _, testcase := range []string{ 103 // tests several aliases that should have same result. 104 "type=volume,target=/target,source=/source", 105 "type=volume,src=/source,dst=/target", 106 "type=volume,source=/source,dst=/target", 107 "type=volume,src=/source,target=/target", 108 } { 109 var mount MountOpt 110 111 assert.NilError(t, mount.Set(testcase)) 112 113 mounts := mount.Value() 114 assert.Equal(t, len(mounts), 1) 115 assert.Equal(t, mounts[0], mounttypes.Mount{ 116 Type: mounttypes.TypeVolume, 117 Source: "/source", 118 Target: "/target", 119 }) 120 } 121 } 122 123 // TestMountOptDefaultType ensures that a mount without the type defaults to a 124 // volume mount. 125 func TestMountOptDefaultType(t *testing.T) { 126 var mount MountOpt 127 assert.NilError(t, mount.Set("target=/target,source=/foo")) 128 assert.Equal(t, mount.values[0].Type, mounttypes.TypeVolume) 129 } 130 131 func TestMountOptSetErrorNoTarget(t *testing.T) { 132 var mount MountOpt 133 assert.Error(t, mount.Set("type=volume,source=/foo"), "target is required") 134 } 135 136 func TestMountOptSetErrorInvalidKey(t *testing.T) { 137 var mount MountOpt 138 assert.Error(t, mount.Set("type=volume,bogus=foo"), "unexpected key 'bogus'") 139 } 140 141 func TestMountOptSetErrorInvalidField(t *testing.T) { 142 var mount MountOpt 143 assert.Error(t, mount.Set("type=volume,bogus"), "invalid field 'bogus'") 144 } 145 146 func TestMountOptSetErrorInvalidReadOnly(t *testing.T) { 147 var mount MountOpt 148 assert.Error(t, mount.Set("type=volume,readonly=no"), "invalid value for readonly: no") 149 assert.Error(t, mount.Set("type=volume,readonly=invalid"), "invalid value for readonly: invalid") 150 } 151 152 func TestMountOptDefaultEnableReadOnly(t *testing.T) { 153 var m MountOpt 154 assert.NilError(t, m.Set("type=bind,target=/foo,source=/foo")) 155 assert.Equal(t, m.values[0].ReadOnly, false) 156 157 m = MountOpt{} 158 assert.NilError(t, m.Set("type=bind,target=/foo,source=/foo,readonly")) 159 assert.Equal(t, m.values[0].ReadOnly, true) 160 161 m = MountOpt{} 162 assert.NilError(t, m.Set("type=bind,target=/foo,source=/foo,readonly=1")) 163 assert.Equal(t, m.values[0].ReadOnly, true) 164 165 m = MountOpt{} 166 assert.NilError(t, m.Set("type=bind,target=/foo,source=/foo,readonly=true")) 167 assert.Equal(t, m.values[0].ReadOnly, true) 168 169 m = MountOpt{} 170 assert.NilError(t, m.Set("type=bind,target=/foo,source=/foo,readonly=0")) 171 assert.Equal(t, m.values[0].ReadOnly, false) 172 } 173 174 func TestMountOptVolumeNoCopy(t *testing.T) { 175 var m MountOpt 176 assert.NilError(t, m.Set("type=volume,target=/foo,volume-nocopy")) 177 assert.Equal(t, m.values[0].Source, "") 178 179 m = MountOpt{} 180 assert.NilError(t, m.Set("type=volume,target=/foo,source=foo")) 181 assert.Equal(t, m.values[0].VolumeOptions == nil, true) 182 183 m = MountOpt{} 184 assert.NilError(t, m.Set("type=volume,target=/foo,source=foo,volume-nocopy=true")) 185 assert.Equal(t, m.values[0].VolumeOptions != nil, true) 186 assert.Equal(t, m.values[0].VolumeOptions.NoCopy, true) 187 188 m = MountOpt{} 189 assert.NilError(t, m.Set("type=volume,target=/foo,source=foo,volume-nocopy")) 190 assert.Equal(t, m.values[0].VolumeOptions != nil, true) 191 assert.Equal(t, m.values[0].VolumeOptions.NoCopy, true) 192 193 m = MountOpt{} 194 assert.NilError(t, m.Set("type=volume,target=/foo,source=foo,volume-nocopy=1")) 195 assert.Equal(t, m.values[0].VolumeOptions != nil, true) 196 assert.Equal(t, m.values[0].VolumeOptions.NoCopy, true) 197 } 198 199 func TestMountOptTypeConflict(t *testing.T) { 200 var m MountOpt 201 assert.Error(t, m.Set("type=bind,target=/foo,source=/foo,volume-nocopy=true"), "cannot mix") 202 assert.Error(t, m.Set("type=volume,target=/foo,source=/foo,bind-propagation=rprivate"), "cannot mix") 203 }