github.com/panekj/cli@v0.0.0-20230304125325-467dd2f3797e/opts/mount_test.go (about) 1 package opts 2 3 import ( 4 "os" 5 "path/filepath" 6 "testing" 7 8 mounttypes "github.com/docker/docker/api/types/mount" 9 "gotest.tools/v3/assert" 10 is "gotest.tools/v3/assert/cmp" 11 ) 12 13 func TestMountOptString(t *testing.T) { 14 mount := MountOpt{ 15 values: []mounttypes.Mount{ 16 { 17 Type: mounttypes.TypeBind, 18 Source: "/home/path", 19 Target: "/target", 20 }, 21 { 22 Type: mounttypes.TypeVolume, 23 Source: "foo", 24 Target: "/target/foo", 25 }, 26 }, 27 } 28 expected := "bind /home/path /target, volume foo /target/foo" 29 assert.Check(t, is.Equal(expected, mount.String())) 30 } 31 32 func TestMountRelative(t *testing.T) { 33 for _, testcase := range []struct { 34 name string 35 path string 36 bind string 37 }{ 38 { 39 name: "Current path", 40 path: ".", 41 bind: "type=bind,source=.,target=/target", 42 }, { 43 name: "Current path with slash", 44 path: "./", 45 bind: "type=bind,source=./,target=/target", 46 }, 47 } { 48 t.Run(testcase.name, func(t *testing.T) { 49 var mount MountOpt 50 assert.NilError(t, mount.Set(testcase.bind)) 51 52 mounts := mount.Value() 53 assert.Assert(t, is.Len(mounts, 1)) 54 abs, err := filepath.Abs(testcase.path) 55 assert.NilError(t, err) 56 assert.Check(t, is.DeepEqual(mounttypes.Mount{ 57 Type: mounttypes.TypeBind, 58 Source: abs, 59 Target: "/target", 60 }, mounts[0])) 61 }) 62 } 63 } 64 65 func TestMountOptSetBindNoErrorBind(t *testing.T) { 66 for _, testcase := range []string{ 67 // tests several aliases that should have same result. 68 "type=bind,target=/target,source=/source", 69 "type=bind,src=/source,dst=/target", 70 "type=bind,source=/source,dst=/target", 71 "type=bind,src=/source,target=/target", 72 } { 73 var mount MountOpt 74 75 assert.NilError(t, mount.Set(testcase)) 76 77 mounts := mount.Value() 78 assert.Assert(t, is.Len(mounts, 1)) 79 assert.Check(t, is.DeepEqual(mounttypes.Mount{ 80 Type: mounttypes.TypeBind, 81 Source: "/source", 82 Target: "/target", 83 }, mounts[0])) 84 } 85 } 86 87 func TestMountOptSetVolumeNoError(t *testing.T) { 88 for _, testcase := range []string{ 89 // tests several aliases that should have same result. 90 "type=volume,target=/target,source=/source", 91 "type=volume,src=/source,dst=/target", 92 "type=volume,source=/source,dst=/target", 93 "type=volume,src=/source,target=/target", 94 } { 95 var mount MountOpt 96 97 assert.NilError(t, mount.Set(testcase)) 98 99 mounts := mount.Value() 100 assert.Assert(t, is.Len(mounts, 1)) 101 assert.Check(t, is.DeepEqual(mounttypes.Mount{ 102 Type: mounttypes.TypeVolume, 103 Source: "/source", 104 Target: "/target", 105 }, mounts[0])) 106 } 107 } 108 109 // TestMountOptDefaultType ensures that a mount without the type defaults to a 110 // volume mount. 111 func TestMountOptDefaultType(t *testing.T) { 112 var mount MountOpt 113 assert.NilError(t, mount.Set("target=/target,source=/foo")) 114 assert.Check(t, is.Equal(mounttypes.TypeVolume, mount.values[0].Type)) 115 } 116 117 func TestMountOptSetErrorNoTarget(t *testing.T) { 118 var mount MountOpt 119 assert.Error(t, mount.Set("type=volume,source=/foo"), "target is required") 120 } 121 122 func TestMountOptSetErrorInvalidKey(t *testing.T) { 123 var mount MountOpt 124 assert.Error(t, mount.Set("type=volume,bogus=foo"), "unexpected key 'bogus' in 'bogus=foo'") 125 } 126 127 func TestMountOptSetErrorInvalidField(t *testing.T) { 128 var mount MountOpt 129 assert.Error(t, mount.Set("type=volume,bogus"), "invalid field 'bogus' must be a key=value pair") 130 } 131 132 func TestMountOptSetErrorInvalidReadOnly(t *testing.T) { 133 var mount MountOpt 134 assert.Error(t, mount.Set("type=volume,readonly=no"), "invalid value for readonly: no") 135 assert.Error(t, mount.Set("type=volume,readonly=invalid"), "invalid value for readonly: invalid") 136 } 137 138 func TestMountOptDefaultEnableReadOnly(t *testing.T) { 139 var m MountOpt 140 assert.NilError(t, m.Set("type=bind,target=/foo,source=/foo")) 141 assert.Check(t, !m.values[0].ReadOnly) 142 143 m = MountOpt{} 144 assert.NilError(t, m.Set("type=bind,target=/foo,source=/foo,readonly")) 145 assert.Check(t, m.values[0].ReadOnly) 146 147 m = MountOpt{} 148 assert.NilError(t, m.Set("type=bind,target=/foo,source=/foo,readonly=1")) 149 assert.Check(t, m.values[0].ReadOnly) 150 151 m = MountOpt{} 152 assert.NilError(t, m.Set("type=bind,target=/foo,source=/foo,readonly=true")) 153 assert.Check(t, m.values[0].ReadOnly) 154 155 m = MountOpt{} 156 assert.NilError(t, m.Set("type=bind,target=/foo,source=/foo,readonly=0")) 157 assert.Check(t, !m.values[0].ReadOnly) 158 } 159 160 func TestMountOptVolumeNoCopy(t *testing.T) { 161 var m MountOpt 162 assert.NilError(t, m.Set("type=volume,target=/foo,volume-nocopy")) 163 assert.Check(t, is.Equal("", m.values[0].Source)) 164 165 m = MountOpt{} 166 assert.NilError(t, m.Set("type=volume,target=/foo,source=foo")) 167 assert.Check(t, m.values[0].VolumeOptions == nil) 168 169 m = MountOpt{} 170 assert.NilError(t, m.Set("type=volume,target=/foo,source=foo,volume-nocopy=true")) 171 assert.Check(t, m.values[0].VolumeOptions != nil) 172 assert.Check(t, m.values[0].VolumeOptions.NoCopy) 173 174 m = MountOpt{} 175 assert.NilError(t, m.Set("type=volume,target=/foo,source=foo,volume-nocopy")) 176 assert.Check(t, m.values[0].VolumeOptions != nil) 177 assert.Check(t, m.values[0].VolumeOptions.NoCopy) 178 179 m = MountOpt{} 180 assert.NilError(t, m.Set("type=volume,target=/foo,source=foo,volume-nocopy=1")) 181 assert.Check(t, m.values[0].VolumeOptions != nil) 182 assert.Check(t, m.values[0].VolumeOptions.NoCopy) 183 } 184 185 func TestMountOptTypeConflict(t *testing.T) { 186 var m MountOpt 187 assert.ErrorContains(t, m.Set("type=bind,target=/foo,source=/foo,volume-nocopy=true"), "cannot mix") 188 assert.ErrorContains(t, m.Set("type=volume,target=/foo,source=/foo,bind-propagation=rprivate"), "cannot mix") 189 } 190 191 func TestMountOptSetTmpfsNoError(t *testing.T) { 192 for _, testcase := range []string{ 193 // tests several aliases that should have same result. 194 "type=tmpfs,target=/target,tmpfs-size=1m,tmpfs-mode=0700", 195 "type=tmpfs,target=/target,tmpfs-size=1MB,tmpfs-mode=700", 196 } { 197 var mount MountOpt 198 199 assert.NilError(t, mount.Set(testcase)) 200 201 mounts := mount.Value() 202 assert.Assert(t, is.Len(mounts, 1)) 203 assert.Check(t, is.DeepEqual(mounttypes.Mount{ 204 Type: mounttypes.TypeTmpfs, 205 Target: "/target", 206 TmpfsOptions: &mounttypes.TmpfsOptions{ 207 SizeBytes: 1024 * 1024, // not 1000 * 1000 208 Mode: os.FileMode(0o700), 209 }, 210 }, mounts[0])) 211 } 212 } 213 214 func TestMountOptSetTmpfsError(t *testing.T) { 215 var m MountOpt 216 assert.ErrorContains(t, m.Set("type=tmpfs,target=/foo,tmpfs-size=foo"), "invalid value for tmpfs-size") 217 assert.ErrorContains(t, m.Set("type=tmpfs,target=/foo,tmpfs-mode=foo"), "invalid value for tmpfs-mode") 218 assert.ErrorContains(t, m.Set("type=tmpfs"), "target is required") 219 }