Skip to content

Commit

Permalink
increased the coverage to 49.7%
Browse files Browse the repository at this point in the history
  • Loading branch information
niranjan-n1 committed Jan 21, 2025
1 parent 996749b commit 2f1f651
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 15 deletions.
91 changes: 91 additions & 0 deletions gofsutil_fs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,10 +194,101 @@ func TestFSGetMounts(t *testing.T) {
t.Run(tt.testname, func(t *testing.T) {
fs := &mockfs{}
GOFSMock.InduceGetMountsError = tt.induceErr
GOFSMockMounts = []Info{
{
Path: "/mnt/volume1",
Type: "ext4",
Opts: []string{"rw", "relatime"},
},
{
Path: "/mnt/volume2",
Type: "xfs",
Opts: []string{"rw", "noexec"},
},
}

mounts, err := fs.GetMounts(tt.ctx)

assert.Equal(t, tt.expected.err, err)
assert.Equal(t, tt.expected.mounts, mounts)
})
}
}

func TestFSRemoveBlockDevice(t *testing.T) {
tests := []struct {
testname string
ctx context.Context
blockDevicePath string
induceErr bool
expectedErr error
}{
{
testname: "Normal operation",
blockDevicePath: "/dev/sda1",
induceErr: false,
expectedErr: nil,
},
{
testname: "Induced error",
blockDevicePath: "/dev/sda1",
induceErr: true,
expectedErr: errors.New("remove block device induced error"),
},
}

for _, tt := range tests {
t.Run(tt.testname, func(t *testing.T) {
fs := &mockfs{}
GOFSMock.InduceRemoveBlockDeviceError = tt.induceErr
err := fs.RemoveBlockDevice(tt.ctx, tt.blockDevicePath)

assert.Equal(t, tt.expectedErr, err)
})
}
}

func TestFSWWNToDevicePath(t *testing.T) {
tests := []struct {
testname string
ctx context.Context
wwn string
induceErr bool
expectedDev string
expectedPath string
expectedErr error
}{
{
testname: "Normal operation",
wwn: "wwn-0x5000c500a0b1c2d3",
induceErr: false,
expectedDev: "/dev/sda/wwn-0x5000c500a0b1c2d3",
expectedPath: "/dev/sda",
expectedErr: nil,
},
{
testname: "Induced error",
wwn: "wwn-0x5000c500a0b1c2d3",
induceErr: true,
expectedDev: "",
expectedPath: "",
expectedErr: errors.New("induced error"),
},
}

for _, tt := range tests {
t.Run(tt.testname, func(t *testing.T) {
fs := &mockfs{}
GOFSMock.InduceWWNToDevicePathError = tt.induceErr
GOFSMockWWNToDevice = map[string]string{
"wwn-0x5000c500a0b1c2d3": "/dev/sda",
}
GOFSWWNPath = "/dev/sda/"
dev, path, err := fs.WWNToDevicePath(tt.ctx, tt.wwn)

assert.Equal(t, tt.expectedErr, err)
assert.Equal(t, tt.expectedDev, dev)
assert.Equal(t, tt.expectedPath, path)
})
}
}
18 changes: 3 additions & 15 deletions gofsutil_mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,21 +222,9 @@ func (fs *mockfs) resizeMultipath(_ context.Context, _ string) error {

func (fs *mockfs) getMounts(_ context.Context) ([]Info, error) {
if GOFSMock.InduceGetMountsError {
return GOFSMockMounts, errors.New("getMounts induced error")
}
var mockMounts = []Info{
{
Path: "/mnt/volume1",
Type: "ext4",
Opts: []string{"rw", "relatime"},
},
{
Path: "/mnt/volume2",
Type: "xfs",
Opts: []string{"rw", "noexec"},
},
}
return append(GOFSMockMounts, mockMounts...), nil
return nil, errors.New("getMounts induced error")
}
return GOFSMockMounts, nil
}

func (fs *mockfs) readProcMounts(_ context.Context,
Expand Down

0 comments on commit 2f1f651

Please sign in to comment.