Skip to content

Commit

Permalink
replica: add unmap unittest
Browse files Browse the repository at this point in the history
Signed-off-by: Vicente Cheng <vicente.cheng@suse.com>
  • Loading branch information
Vicente-Cheng committed Mar 22, 2024
1 parent 3058239 commit bfb7997
Showing 1 changed file with 62 additions and 0 deletions.
62 changes: 62 additions & 0 deletions pkg/replica/replica_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1119,3 +1119,65 @@ func (s *TestSuite) TestUnmapMarkDiskRemoved(c *C) {
c.Assert(r.activeDiskData[1].Removed, Equals, false)
c.Assert(r.activeDiskData[1].Parent, Equals, "")
}

func (s *TestSuite) TestUnmap(c *C) {
dir, err := os.MkdirTemp("", "replica")
c.Assert(err, IsNil)
defer os.RemoveAll(dir)

r, err := New(8*b, b, dir, nil, false, false, 250, 0)
c.Assert(err, IsNil)
defer r.Close()

buf := make([]byte, 8*b)
fill(buf, 1)
_, err = r.WriteAt(buf, 0)
c.Assert(err, IsNil)

// default sector size is 4096, we follow this assumption in the test
// offset = 0, unmap length = 0: 0 sectors can be unmapped
ret, err := r.UnmapAt(0, 0)
c.Assert(err, IsNil)
c.Assert(ret, Equals, 0)

// offset = 0, unmap length = 512: 0 sectors can be unmapped
ret, err = r.UnmapAt(512, 0)
c.Assert(err, IsNil)
c.Assert(ret, Equals, 0)

// offset = 0, unmap length = 4096: 1 sector can be unmapped
ret, err = r.UnmapAt(b, 0)
c.Assert(err, IsNil)
c.Assert(ret, Equals, 4096)
_, err = r.WriteAt(buf, 0)
c.Assert(err, IsNil)

// offset = 0, unmap length = 4097: 1 sector can be unmapped
ret, err = r.UnmapAt(b+1, 0)
c.Assert(err, IsNil)
c.Assert(ret, Equals, 4096)
_, err = r.WriteAt(buf, 0)
c.Assert(err, IsNil)

// offset = 1, unmap length = 512: 0 sectors can be unmapped
ret, err = r.UnmapAt(512, 1)
c.Assert(err, IsNil)
c.Assert(ret, Equals, 0)

// offset = 1, unmap length = 4096: 0 sector can be unmapped
ret, err = r.UnmapAt(b, 1)
c.Assert(err, IsNil)
c.Assert(ret, Equals, 0)

// offset = 2048, unmap length = 4096: 1 sector can be unmapped
ret, err = r.UnmapAt(2*b, 2048)
c.Assert(err, IsNil)
c.Assert(ret, Equals, 4096)
_, err = r.WriteAt(buf, 0)
c.Assert(err, IsNil)

// offset = 2048, unmap length = 3072: 0 sector can be unmapped
ret, err = r.UnmapAt(b+1024, 2048)
c.Assert(err, IsNil)
c.Assert(ret, Equals, 0)
}

0 comments on commit bfb7997

Please sign in to comment.