Browse Source

Add capability tests to chroot, mount and polyfill

Also use mock FS instead of memfs to test capabilities

Signed-off-by: Javi Fontan <jfontan@gmail.com>
tags/v4.2.0^2
Javi Fontan 1 year ago
parent
commit
c1e3d52cfc
5 changed files with 87 additions and 3 deletions
  1. 6
    3
      fs_test.go
  2. 15
    0
      helper/chroot/chroot_test.go
  3. 31
    0
      helper/mount/mount_test.go
  4. 15
    0
      helper/polyfill/polyfill_test.go
  5. 20
    0
      test/mock.go

+ 6
- 3
fs_test.go View File

@@ -4,7 +4,7 @@ import (
"testing"

. "gopkg.in/src-d/go-billy.v4"
"gopkg.in/src-d/go-billy.v4/memfs"
"gopkg.in/src-d/go-billy.v4/test"

. "gopkg.in/check.v1"
)
@@ -29,9 +29,12 @@ func (s *FSSuite) TestCapabilities(c *C) {
}

// This filesystem supports all capabilities except for LockCapability
mem := memfs.New()
fs := new(test.NoLockCapFs)

for _, e := range cases {
c.Assert(CapabilityCheck(mem, e.caps), Equals, e.expected)
c.Assert(CapabilityCheck(fs, e.caps), Equals, e.expected)
}

dummy := new(test.BasicMock)
c.Assert(Capabilities(dummy), Equals, DefaultCapabilities)
}

+ 15
- 0
helper/chroot/chroot_test.go View File

@@ -351,3 +351,18 @@ func (s *ChrootSuite) TestReadlinkWithBasic(c *C) {
_, err := fs.Readlink("")
c.Assert(err, Equals, billy.ErrNotSupported)
}

func (s *ChrootSuite) TestCapabilities(c *C) {
testCapabilities(c, new(test.BasicMock))
testCapabilities(c, new(test.OnlyReadCapFs))
testCapabilities(c, new(test.NoLockCapFs))
}

func testCapabilities(c *C, basic billy.Basic) {
baseCapabilities := billy.Capabilities(basic)

fs := New(basic, "/foo")
capabilities := billy.Capabilities(fs)

c.Assert(capabilities, Equals, baseCapabilities)
}

+ 31
- 0
helper/mount/mount_test.go View File

@@ -337,3 +337,34 @@ func (s *MountSuite) TestSourceNotSupported(c *C) {
_, err = h.Readlink("foo")
c.Assert(err, Equals, billy.ErrNotSupported)
}

func (s *MountSuite) TestCapabilities(c *C) {
testCapabilities(c, new(test.BasicMock), new(test.BasicMock))
testCapabilities(c, new(test.BasicMock), new(test.OnlyReadCapFs))
testCapabilities(c, new(test.BasicMock), new(test.NoLockCapFs))
testCapabilities(c, new(test.OnlyReadCapFs), new(test.BasicMock))
testCapabilities(c, new(test.OnlyReadCapFs), new(test.OnlyReadCapFs))
testCapabilities(c, new(test.OnlyReadCapFs), new(test.NoLockCapFs))
testCapabilities(c, new(test.NoLockCapFs), new(test.BasicMock))
testCapabilities(c, new(test.NoLockCapFs), new(test.OnlyReadCapFs))
testCapabilities(c, new(test.NoLockCapFs), new(test.NoLockCapFs))
}

func testCapabilities(c *C, a, b billy.Basic) {
aCapabilities := billy.Capabilities(a)
bCapabilities := billy.Capabilities(b)

fs := New(a, "/foo", b)
capabilities := billy.Capabilities(fs)

unionCapabilities := aCapabilities & bCapabilities

c.Assert(capabilities, Equals, unionCapabilities)

fs = New(b, "/foo", a)
capabilities = billy.Capabilities(fs)

unionCapabilities = aCapabilities & bCapabilities

c.Assert(capabilities, Equals, unionCapabilities)
}

+ 15
- 0
helper/polyfill/polyfill_test.go View File

@@ -61,3 +61,18 @@ func (s *PolyfillSuite) TestChroot(c *C) {
func (s *PolyfillSuite) TestRoot(c *C) {
c.Assert(s.Helper.Root(), Equals, string(filepath.Separator))
}

func (s *PolyfillSuite) TestCapabilities(c *C) {
testCapabilities(c, new(test.BasicMock))
testCapabilities(c, new(test.OnlyReadCapFs))
testCapabilities(c, new(test.NoLockCapFs))
}

func testCapabilities(c *C, basic billy.Basic) {
baseCapabilities := billy.Capabilities(basic)

fs := New(basic)
capabilities := billy.Capabilities(fs)

c.Assert(capabilities, Equals, baseCapabilities)
}

+ 20
- 0
test/mock.go View File

@@ -134,3 +134,23 @@ func (*FileMock) Unlock() error {
func (*FileMock) Truncate(size int64) error {
return nil
}

type OnlyReadCapFs struct {
BasicMock
}

func (o *OnlyReadCapFs) Capabilities() billy.Capability {
return billy.ReadCapability
}

type NoLockCapFs struct {
BasicMock
}

func (o *NoLockCapFs) Capabilities() billy.Capability {
return billy.WriteCapability |
billy.ReadCapability |
billy.ReadAndWriteCapability |
billy.SeekCapability |
billy.TruncateCapability
}

Loading…
Cancel
Save