| 1 | package mount |
| 2 | |
| 3 | import ( |
| 4 | "os" |
| 5 | "path/filepath" |
| 6 | "testing" |
| 7 | |
| 8 | "github.com/go-git/go-billy/v5" |
| 9 | "github.com/go-git/go-billy/v5/memfs" |
| 10 | "github.com/go-git/go-billy/v5/test" |
| 11 | "github.com/go-git/go-billy/v5/util" |
| 12 | |
| 13 | . "gopkg.in/check.v1" |
| 14 | ) |
| 15 | |
| 16 | func Test(t *testing.T) { TestingT(t) } |
| 17 | |
| 18 | var _ = Suite(&MountSuite{}) |
| 19 | |
| 20 | type MountSuite struct { |
| 21 | Helper *Mount |
| 22 | Underlying mock |
| 23 | Source mock |
| 24 | } |
| 25 | |
| 26 | type mock struct { |
| 27 | test.BasicMock |
| 28 | test.DirMock |
| 29 | test.SymlinkMock |
| 30 | } |
| 31 | |
| 32 | func (s *MountSuite) SetUpTest(c *C) { |
| 33 | s.Underlying.BasicMock = test.BasicMock{} |
| 34 | s.Underlying.DirMock = test.DirMock{} |
| 35 | s.Underlying.SymlinkMock = test.SymlinkMock{} |
| 36 | s.Source.BasicMock = test.BasicMock{} |
| 37 | s.Source.DirMock = test.DirMock{} |
| 38 | s.Source.SymlinkMock = test.SymlinkMock{} |
| 39 | |
| 40 | s.Helper = New(&s.Underlying, "/foo", &s.Source) |
| 41 | } |
| 42 | |
| 43 | func (s *MountSuite) TestCreate(c *C) { |
| 44 | f, err := s.Helper.Create("bar/qux") |
| 45 | c.Assert(err, IsNil) |
| 46 | c.Assert(f.Name(), Equals, filepath.Join("bar", "qux")) |
| 47 | |
| 48 | c.Assert(s.Underlying.CreateArgs, HasLen, 1) |
| 49 | c.Assert(s.Underlying.CreateArgs[0], Equals, filepath.Join("bar", "qux")) |
| 50 | c.Assert(s.Source.CreateArgs, HasLen, 0) |
| 51 | } |
| 52 | |
| 53 | func (s *MountSuite) TestCreateMountPoint(c *C) { |
| 54 | f, err := s.Helper.Create("foo") |
| 55 | c.Assert(f, IsNil) |
| 56 | c.Assert(err, Equals, os.ErrInvalid) |
| 57 | } |
| 58 | |
| 59 | func (s *MountSuite) TestCreateInMount(c *C) { |
| 60 | f, err := s.Helper.Create("foo/bar/qux") |
| 61 | c.Assert(err, IsNil) |
| 62 | c.Assert(f.Name(), Equals, filepath.Join("foo", "bar", "qux")) |
| 63 | |
| 64 | c.Assert(s.Underlying.CreateArgs, HasLen, 0) |
| 65 | c.Assert(s.Source.CreateArgs, HasLen, 1) |
| 66 | c.Assert(s.Source.CreateArgs[0], Equals, filepath.Join("bar", "qux")) |
| 67 | } |
| 68 | |
| 69 | func (s *MountSuite) TestOpen(c *C) { |
| 70 | f, err := s.Helper.Open("bar/qux") |
| 71 | c.Assert(err, IsNil) |
| 72 | c.Assert(f.Name(), Equals, filepath.Join("bar", "qux")) |
| 73 | |
| 74 | c.Assert(s.Underlying.OpenArgs, HasLen, 1) |
| 75 | c.Assert(s.Underlying.OpenArgs[0], Equals, filepath.Join("bar", "qux")) |
| 76 | c.Assert(s.Source.OpenArgs, HasLen, 0) |
| 77 | } |
| 78 | |
| 79 | func (s *MountSuite) TestOpenMountPoint(c *C) { |
| 80 | f, err := s.Helper.Open("foo") |
| 81 | c.Assert(f, IsNil) |
| 82 | c.Assert(err, Equals, os.ErrInvalid) |
| 83 | } |
| 84 | |
| 85 | func (s *MountSuite) TestOpenInMount(c *C) { |
| 86 | f, err := s.Helper.Open("foo/bar/qux") |
| 87 | c.Assert(err, IsNil) |
| 88 | c.Assert(f.Name(), Equals, filepath.Join("foo", "bar", "qux")) |
| 89 | |
| 90 | c.Assert(s.Underlying.OpenArgs, HasLen, 0) |
| 91 | c.Assert(s.Source.OpenArgs, HasLen, 1) |
| 92 | c.Assert(s.Source.OpenArgs[0], Equals, filepath.Join("bar", "qux")) |
| 93 | } |
| 94 | |
| 95 | func (s *MountSuite) TestOpenMountPointPrefix(c *C) { |
| 96 | f, err := s.Helper.Open("foobar/qux") |
| 97 | c.Assert(err, IsNil) |
| 98 | c.Assert(f.Name(), Equals, filepath.Join("foobar", "qux")) |
| 99 | |
| 100 | c.Assert(s.Underlying.OpenArgs, HasLen, 1) |
| 101 | c.Assert(s.Underlying.OpenArgs[0], Equals, filepath.Join("foobar", "qux")) |
| 102 | c.Assert(s.Source.OpenArgs, HasLen, 0) |
| 103 | } |
| 104 | |
| 105 | func (s *MountSuite) TestOpenFile(c *C) { |
| 106 | f, err := s.Helper.OpenFile("bar/qux", 42, 0777) |
| 107 | c.Assert(err, IsNil) |
| 108 | c.Assert(f.Name(), Equals, filepath.Join("bar", "qux")) |
| 109 | |
| 110 | c.Assert(s.Underlying.OpenFileArgs, HasLen, 1) |
| 111 | c.Assert(s.Underlying.OpenFileArgs[0], Equals, |
| 112 | [3]interface{}{filepath.Join("bar", "qux"), 42, os.FileMode(0777)}) |
| 113 | c.Assert(s.Source.OpenFileArgs, HasLen, 0) |
| 114 | } |
| 115 | |
| 116 | func (s *MountSuite) TestOpenFileMountPoint(c *C) { |
| 117 | f, err := s.Helper.OpenFile("foo", 42, 0777) |
| 118 | c.Assert(f, IsNil) |
| 119 | c.Assert(err, Equals, os.ErrInvalid) |
| 120 | } |
| 121 | |
| 122 | func (s *MountSuite) TestOpenFileInMount(c *C) { |
| 123 | f, err := s.Helper.OpenFile("foo/bar/qux", 42, 0777) |
| 124 | c.Assert(err, IsNil) |
| 125 | c.Assert(f.Name(), Equals, filepath.Join("foo", "bar", "qux")) |
| 126 | |
| 127 | c.Assert(s.Underlying.OpenFileArgs, HasLen, 0) |
| 128 | c.Assert(s.Source.OpenFileArgs, HasLen, 1) |
| 129 | c.Assert(s.Source.OpenFileArgs[0], Equals, |
| 130 | [3]interface{}{filepath.Join("bar", "qux"), 42, os.FileMode(0777)}) |
| 131 | } |
| 132 | |
| 133 | func (s *MountSuite) TestStat(c *C) { |
| 134 | _, err := s.Helper.Stat("bar/qux") |
| 135 | c.Assert(err, IsNil) |
| 136 | |
| 137 | c.Assert(s.Underlying.StatArgs, HasLen, 1) |
| 138 | c.Assert(s.Underlying.StatArgs[0], Equals, filepath.Join("bar", "qux")) |
| 139 | c.Assert(s.Source.StatArgs, HasLen, 0) |
| 140 | } |
| 141 | |
| 142 | func (s *MountSuite) TestStatInMount(c *C) { |
| 143 | _, err := s.Helper.Stat("foo/bar/qux") |
| 144 | c.Assert(err, IsNil) |
| 145 | |
| 146 | c.Assert(s.Underlying.StatArgs, HasLen, 0) |
| 147 | c.Assert(s.Source.StatArgs, HasLen, 1) |
| 148 | c.Assert(s.Source.StatArgs[0], Equals, filepath.Join("bar", "qux")) |
| 149 | } |
| 150 | |
| 151 | func (s *MountSuite) TestRename(c *C) { |
| 152 | err := s.Helper.Rename("bar/qux", "qux") |
| 153 | c.Assert(err, IsNil) |
| 154 | |
| 155 | c.Assert(s.Underlying.RenameArgs, HasLen, 1) |
| 156 | c.Assert(s.Underlying.RenameArgs[0], Equals, [2]string{"bar/qux", "qux"}) |
| 157 | c.Assert(s.Source.RenameArgs, HasLen, 0) |
| 158 | } |
| 159 | |
| 160 | func (s *MountSuite) TestRenameInMount(c *C) { |
| 161 | err := s.Helper.Rename("foo/bar/qux", "foo/qux") |
| 162 | c.Assert(err, IsNil) |
| 163 | |
| 164 | c.Assert(s.Underlying.RenameArgs, HasLen, 0) |
| 165 | c.Assert(s.Source.RenameArgs, HasLen, 1) |
| 166 | c.Assert(s.Source.RenameArgs[0], Equals, |
| 167 | [2]string{filepath.Join("bar", "qux"), "qux"}) |
| 168 | } |
| 169 | |
| 170 | func (s *MountSuite) TestRenameCross(c *C) { |
| 171 | underlying := memfs.New() |
| 172 | source := memfs.New() |
| 173 | |
| 174 | util.WriteFile(underlying, "file", []byte("foo"), 0777) |
| 175 | |
| 176 | fs := New(underlying, "/foo", source) |
| 177 | err := fs.Rename("file", "foo/file") |
| 178 | c.Assert(err, IsNil) |
| 179 | |
| 180 | _, err = underlying.Stat("file") |
| 181 | c.Assert(err, Equals, os.ErrNotExist) |
| 182 | |
| 183 | _, err = source.Stat("file") |
| 184 | c.Assert(err, IsNil) |
| 185 | |
| 186 | err = fs.Rename("foo/file", "file") |
| 187 | c.Assert(err, IsNil) |
| 188 | |
| 189 | _, err = underlying.Stat("file") |
| 190 | c.Assert(err, IsNil) |
| 191 | |
| 192 | _, err = source.Stat("file") |
| 193 | c.Assert(err, Equals, os.ErrNotExist) |
| 194 | } |
| 195 | |
| 196 | func (s *MountSuite) TestRemove(c *C) { |
| 197 | err := s.Helper.Remove("bar/qux") |
| 198 | c.Assert(err, IsNil) |
| 199 | |
| 200 | c.Assert(s.Underlying.RemoveArgs, HasLen, 1) |
| 201 | c.Assert(s.Underlying.RemoveArgs[0], Equals, filepath.Join("bar", "qux")) |
| 202 | c.Assert(s.Source.RemoveArgs, HasLen, 0) |
| 203 | } |
| 204 | |
| 205 | func (s *MountSuite) TestRemoveMountPoint(c *C) { |
| 206 | err := s.Helper.Remove("foo") |
| 207 | c.Assert(err, Equals, os.ErrInvalid) |
| 208 | } |
| 209 | |
| 210 | func (s *MountSuite) TestRemoveInMount(c *C) { |
| 211 | err := s.Helper.Remove("foo/bar/qux") |
| 212 | c.Assert(err, IsNil) |
| 213 | |
| 214 | c.Assert(s.Underlying.RemoveArgs, HasLen, 0) |
| 215 | c.Assert(s.Source.RemoveArgs, HasLen, 1) |
| 216 | c.Assert(s.Source.RemoveArgs[0], Equals, filepath.Join("bar", "qux")) |
| 217 | } |
| 218 | |
| 219 | func (s *MountSuite) TestReadDir(c *C) { |
| 220 | _, err := s.Helper.ReadDir("bar/qux") |
| 221 | c.Assert(err, IsNil) |
| 222 | |
| 223 | c.Assert(s.Underlying.ReadDirArgs, HasLen, 1) |
| 224 | c.Assert(s.Underlying.ReadDirArgs[0], Equals, filepath.Join("bar", "qux")) |
| 225 | c.Assert(s.Source.ReadDirArgs, HasLen, 0) |
| 226 | } |
| 227 | |
| 228 | func (s *MountSuite) TestJoin(c *C) { |
| 229 | s.Helper.Join("foo", "bar") |
| 230 | |
| 231 | c.Assert(s.Underlying.JoinArgs, HasLen, 1) |
| 232 | c.Assert(s.Underlying.JoinArgs[0], DeepEquals, []string{"foo", "bar"}) |
| 233 | c.Assert(s.Source.JoinArgs, HasLen, 0) |
| 234 | } |
| 235 | |
| 236 | func (s *MountSuite) TestReadDirInMount(c *C) { |
| 237 | _, err := s.Helper.ReadDir("foo/bar/qux") |
| 238 | c.Assert(err, IsNil) |
| 239 | |
| 240 | c.Assert(s.Underlying.ReadDirArgs, HasLen, 0) |
| 241 | c.Assert(s.Source.ReadDirArgs, HasLen, 1) |
| 242 | c.Assert(s.Source.ReadDirArgs[0], Equals, filepath.Join("bar", "qux")) |
| 243 | } |
| 244 | |
| 245 | func (s *MountSuite) TestMkdirAll(c *C) { |
| 246 | err := s.Helper.MkdirAll("bar/qux", 0777) |
| 247 | c.Assert(err, IsNil) |
| 248 | |
| 249 | c.Assert(s.Underlying.MkdirAllArgs, HasLen, 1) |
| 250 | c.Assert(s.Underlying.MkdirAllArgs[0], Equals, |
| 251 | [2]interface{}{filepath.Join("bar", "qux"), os.FileMode(0777)}) |
| 252 | c.Assert(s.Source.MkdirAllArgs, HasLen, 0) |
| 253 | } |
| 254 | |
| 255 | func (s *MountSuite) TestMkdirAllInMount(c *C) { |
| 256 | err := s.Helper.MkdirAll("foo/bar/qux", 0777) |
| 257 | c.Assert(err, IsNil) |
| 258 | |
| 259 | c.Assert(s.Underlying.MkdirAllArgs, HasLen, 0) |
| 260 | c.Assert(s.Source.MkdirAllArgs, HasLen, 1) |
| 261 | c.Assert(s.Source.MkdirAllArgs[0], Equals, |
| 262 | [2]interface{}{filepath.Join("bar", "qux"), os.FileMode(0777)}) |
| 263 | } |
| 264 | |
| 265 | func (s *MountSuite) TestLstat(c *C) { |
| 266 | _, err := s.Helper.Lstat("bar/qux") |
| 267 | c.Assert(err, IsNil) |
| 268 | |
| 269 | c.Assert(s.Underlying.LstatArgs, HasLen, 1) |
| 270 | c.Assert(s.Underlying.LstatArgs[0], Equals, filepath.Join("bar", "qux")) |
| 271 | c.Assert(s.Source.LstatArgs, HasLen, 0) |
| 272 | } |
| 273 | |
| 274 | func (s *MountSuite) TestLstatInMount(c *C) { |
| 275 | _, err := s.Helper.Lstat("foo/bar/qux") |
| 276 | c.Assert(err, IsNil) |
| 277 | |
| 278 | c.Assert(s.Underlying.LstatArgs, HasLen, 0) |
| 279 | c.Assert(s.Source.LstatArgs, HasLen, 1) |
| 280 | c.Assert(s.Source.LstatArgs[0], Equals, filepath.Join("bar", "qux")) |
| 281 | } |
| 282 | |
| 283 | func (s *MountSuite) TestSymlink(c *C) { |
| 284 | err := s.Helper.Symlink("../baz", "bar/qux") |
| 285 | c.Assert(err, IsNil) |
| 286 | |
| 287 | c.Assert(s.Underlying.SymlinkArgs, HasLen, 1) |
| 288 | c.Assert(s.Underlying.SymlinkArgs[0], Equals, |
| 289 | [2]string{"../baz", filepath.Join("bar", "qux")}) |
| 290 | c.Assert(s.Source.SymlinkArgs, HasLen, 0) |
| 291 | } |
| 292 | |
| 293 | func (s *MountSuite) TestSymlinkCrossMount(c *C) { |
| 294 | err := s.Helper.Symlink("../foo", "bar/qux") |
| 295 | c.Assert(err, NotNil) |
| 296 | |
| 297 | err = s.Helper.Symlink("../foo/qux", "bar/qux") |
| 298 | c.Assert(err, NotNil) |
| 299 | |
| 300 | err = s.Helper.Symlink("../baz", "foo") |
| 301 | c.Assert(err, NotNil) |
| 302 | |
| 303 | err = s.Helper.Symlink("../../../foo", "foo/bar/qux") |
| 304 | c.Assert(err, NotNil) |
| 305 | } |
| 306 | |
| 307 | func (s *MountSuite) TestSymlinkInMount(c *C) { |
| 308 | err := s.Helper.Symlink("../baz", "foo/bar/qux") |
| 309 | c.Assert(err, IsNil) |
| 310 | |
| 311 | c.Assert(s.Underlying.SymlinkArgs, HasLen, 0) |
| 312 | c.Assert(s.Source.SymlinkArgs, HasLen, 1) |
| 313 | c.Assert(s.Source.SymlinkArgs[0], Equals, |
| 314 | [2]string{"../baz", filepath.Join("bar", "qux")}) |
| 315 | } |
| 316 | |
| 317 | func (s *MountSuite) TestRadlink(c *C) { |
| 318 | _, err := s.Helper.Readlink("bar/qux") |
| 319 | c.Assert(err, IsNil) |
| 320 | |
| 321 | c.Assert(s.Underlying.ReadlinkArgs, HasLen, 1) |
| 322 | c.Assert(s.Underlying.ReadlinkArgs[0], Equals, filepath.Join("bar", "qux")) |
| 323 | c.Assert(s.Source.ReadlinkArgs, HasLen, 0) |
| 324 | } |
| 325 | |
| 326 | func (s *MountSuite) TestReadlinkInMount(c *C) { |
| 327 | _, err := s.Helper.Readlink("foo/bar/qux") |
| 328 | c.Assert(err, IsNil) |
| 329 | |
| 330 | c.Assert(s.Underlying.ReadlinkArgs, HasLen, 0) |
| 331 | c.Assert(s.Source.ReadlinkArgs, HasLen, 1) |
| 332 | c.Assert(s.Source.ReadlinkArgs[0], Equals, filepath.Join("bar", "qux")) |
| 333 | } |
| 334 | |
| 335 | func (s *MountSuite) TestIsMountpoint(c *C) { |
| 336 | tests := []struct { |
| 337 | path string |
| 338 | want bool |
| 339 | }{ |
| 340 | {path: "foo", want: true}, |
| 341 | {path: "/foo", want: true}, |
| 342 | {path: "foo/bar", want: true}, |
| 343 | {path: "/foo/bar", want: true}, |
| 344 | {path: "foobar", want: false}, |
| 345 | {path: "/foobar/qux", want: false}, |
| 346 | {path: "bar/foo", want: false}, |
| 347 | } |
| 348 | |
| 349 | for _, tt := range tests { |
| 350 | c.Assert(s.Helper.isMountpoint(tt.path), Equals, tt.want, Commentf("path: %q", tt.path)) |
| 351 | } |
| 352 | } |
| 353 | |
| 354 | func (s *MountSuite) TestUnderlyingNotSupported(c *C) { |
| 355 | h := New(&test.BasicMock{}, "/foo", &test.BasicMock{}) |
| 356 | _, err := h.ReadDir("qux") |
| 357 | c.Assert(err, Equals, billy.ErrNotSupported) |
| 358 | _, err = h.Readlink("qux") |
| 359 | c.Assert(err, Equals, billy.ErrNotSupported) |
| 360 | } |
| 361 | |
| 362 | func (s *MountSuite) TestSourceNotSupported(c *C) { |
| 363 | h := New(&s.Underlying, "/foo", &test.BasicMock{}) |
| 364 | _, err := h.ReadDir("foo") |
| 365 | c.Assert(err, Equals, billy.ErrNotSupported) |
| 366 | _, err = h.Readlink("foo") |
| 367 | c.Assert(err, Equals, billy.ErrNotSupported) |
| 368 | } |
| 369 | |
| 370 | func (s *MountSuite) TestCapabilities(c *C) { |
| 371 | testCapabilities(c, new(test.BasicMock), new(test.BasicMock)) |
| 372 | testCapabilities(c, new(test.BasicMock), new(test.OnlyReadCapFs)) |
| 373 | testCapabilities(c, new(test.BasicMock), new(test.NoLockCapFs)) |
| 374 | testCapabilities(c, new(test.OnlyReadCapFs), new(test.BasicMock)) |
| 375 | testCapabilities(c, new(test.OnlyReadCapFs), new(test.OnlyReadCapFs)) |
| 376 | testCapabilities(c, new(test.OnlyReadCapFs), new(test.NoLockCapFs)) |
| 377 | testCapabilities(c, new(test.NoLockCapFs), new(test.BasicMock)) |
| 378 | testCapabilities(c, new(test.NoLockCapFs), new(test.OnlyReadCapFs)) |
| 379 | testCapabilities(c, new(test.NoLockCapFs), new(test.NoLockCapFs)) |
| 380 | } |
| 381 | |
| 382 | func testCapabilities(c *C, a, b billy.Basic) { |
| 383 | aCapabilities := billy.Capabilities(a) |
| 384 | bCapabilities := billy.Capabilities(b) |
| 385 | |
| 386 | fs := New(a, "/foo", b) |
| 387 | capabilities := billy.Capabilities(fs) |
| 388 | |
| 389 | unionCapabilities := aCapabilities & bCapabilities |
| 390 | |
| 391 | c.Assert(capabilities, Equals, unionCapabilities) |
| 392 | |
| 393 | fs = New(b, "/foo", a) |
| 394 | capabilities = billy.Capabilities(fs) |
| 395 | |
| 396 | unionCapabilities = aCapabilities & bCapabilities |
| 397 | |
| 398 | c.Assert(capabilities, Equals, unionCapabilities) |
| 399 | } |