Skip to content

Commit

Permalink
increased coverage to 61.5%
Browse files Browse the repository at this point in the history
  • Loading branch information
niranjan-n1 committed Jan 22, 2025
1 parent b6f0474 commit b8603e4
Show file tree
Hide file tree
Showing 3 changed files with 187 additions and 10 deletions.
177 changes: 177 additions & 0 deletions gofsutil_fs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -718,3 +718,180 @@ func TestFSTargetIPLUNToDevicePath(t *testing.T) {
})
}
}

func TestFSGetMpathNameFromDevice(t *testing.T) {
tests := []struct {
testname string
ctx context.Context
device string
induceErr bool
expectedMpath string
expectedErr error
}{
{
testname: "Normal operation",
device: "sda",
induceErr: false,
expectedMpath: "mpatha",
expectedErr: nil,
},
{
testname: "Induced error",
device: "sda",
induceErr: true,
expectedMpath: "",
expectedErr: errors.New("getMpathNameFromDevice induced error: Failed to find mount information"),
},
}

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

mpath, err := fs.GetMpathNameFromDevice(tt.ctx, tt.device)

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

func TestFSGetDevMounts(t *testing.T) {
tests := []struct {
testname string
ctx context.Context
dev string
induceErr bool
expectedMounts []Info
expectedErr error
}{
{
testname: "Normal operation",
dev: "sda",
induceErr: false,
expectedMounts: []Info{
{Device: "/dev/sda1", Path: "/mnt/volume1", Opts: []string{"rw", "relatime"}},
{Device: "/dev/sda2", Path: "/mnt/volume2", Opts: []string{"rw", "noexec"}},
},
expectedErr: nil,
},
{
testname: "Induced error",
dev: "sda",
induceErr: true,
expectedMounts: nil,
expectedErr: errors.New("dev mount induced error"),
},
}

for _, tt := range tests {
t.Run(tt.testname, func(t *testing.T) {
fs := &mockfs{}
GOFSMock.InduceDevMountsError = tt.induceErr
GOFSMockMounts = []Info{
{Device: "/dev/sda1", Path: "/mnt/volume1", Opts: []string{"rw", "relatime"}},
{Device: "/dev/sda2", Path: "/mnt/volume2", Opts: []string{"rw", "noexec"}},
}

mounts, err := fs.getDevMounts(tt.ctx, tt.dev)

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

func TestFSBindMount(t *testing.T) {
tests := []struct {
testname string
ctx context.Context
source string
target string
options []string
induceErr bool
expectedErr error
}{
{
testname: "Normal operation with options",
source: "/source",
target: "/target",
options: []string{"ro"},
induceErr: false,
expectedErr: nil,
},
{
testname: "Normal operation without options",
source: "/source",
target: "/target",
options: nil,
induceErr: false,
expectedErr: nil,
},
{
testname: "Induced error",
source: "/source",
target: "/target",
options: []string{"ro"},
induceErr: true,
expectedErr: errors.New("mount induced error"),
},
}

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

err := fs.BindMount(tt.ctx, tt.source, tt.target, tt.options...)

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

func TestFSRescanSCSIHost(t *testing.T) {
tests := []struct {
testname string
ctx context.Context
hosts []string
lun string
induceErr bool
expectedErr error
}{
{
testname: "Normal operation",
hosts: []string{"host1", "host2"},
lun: "0",
induceErr: false,
expectedErr: nil,
},
{
testname: "Induced error",
hosts: []string{"host1", "host2"},
lun: "0",
induceErr: true,
expectedErr: errors.New("induced rescan error"),
},
}

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

var callbackCalled bool
GOFSRescanCallback = func(scanString string) {
callbackCalled = true
assert.Equal(t, tt.lun, scanString)
}

err := fs.rescanSCSIHost(tt.ctx, tt.hosts, tt.lun)

assert.Equal(t, tt.expectedErr, err)
if !tt.induceErr {
assert.True(t, callbackCalled)
}
})
}
}
2 changes: 1 addition & 1 deletion gofsutil_mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ func (fs *mockfs) unmount(_ context.Context, target string) error {

func (fs *mockfs) getDevMounts(_ context.Context, _ string) ([]Info, error) {
if GOFSMock.InduceDevMountsError {
return GOFSMockMounts, errors.New("dev mount induced error")
return nil, errors.New("dev mount induced error")
}
return GOFSMockMounts, nil
}
Expand Down
18 changes: 9 additions & 9 deletions gofsutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,16 +165,16 @@ func TestUnmount(t *testing.T) {
}
}

// func TestGetMountInfoFromDevice(t *testing.T) {
// ctx := context.Background()
// devID := "sda1"
func TestGetMountInfoFromDevice(t *testing.T) {
ctx := context.Background()
devID := "sda1"

// result, err := GetMountInfoFromDevice(ctx, devID)
// if err != nil {
// t.Errorf("expected no error, got %v", err)
// }
// t.Logf("Mount info: %+v", result)
// }
result, err := GetMountInfoFromDevice(ctx, devID)
if err != nil {
t.Errorf("expected no error, got %v", err)
}
t.Logf("Mount info: %+v", result)
}

func TestGetMpathNameFromDevice(t *testing.T) {
ctx := context.Background()
Expand Down

0 comments on commit b8603e4

Please sign in to comment.