From 2f1f6513b6241b24352d69f796d67352b9bb962b Mon Sep 17 00:00:00 2001 From: niranjan-n1 Date: Tue, 21 Jan 2025 13:03:59 +0530 Subject: [PATCH] increased the coverage to 49.7% --- gofsutil_fs_test.go | 91 +++++++++++++++++++++++++++++++++++++++++++++ gofsutil_mock.go | 18 ++------- 2 files changed, 94 insertions(+), 15 deletions(-) diff --git a/gofsutil_fs_test.go b/gofsutil_fs_test.go index 5d0d71a..1974e0b 100644 --- a/gofsutil_fs_test.go +++ b/gofsutil_fs_test.go @@ -194,6 +194,19 @@ 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) @@ -201,3 +214,81 @@ func TestFSGetMounts(t *testing.T) { }) } } + +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) + }) + } +} diff --git a/gofsutil_mock.go b/gofsutil_mock.go index 7f1304b..4306ae2 100644 --- a/gofsutil_mock.go +++ b/gofsutil_mock.go @@ -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,