-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: refactor vgmanager into different interfaces and introduce tests
- Loading branch information
1 parent
dd8b3b7
commit 1dbd4ff
Showing
18 changed files
with
863 additions
and
509 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
/* | ||
Copyright © 2023 Red Hat, Inc. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package lsblk | ||
|
||
import ( | ||
"strings" | ||
) | ||
|
||
const ( | ||
// StateSuspended is a possible value of BlockDevice.State | ||
StateSuspended = "suspended" | ||
|
||
// DeviceTypeLoop is the device type for loop devices in lsblk output | ||
DeviceTypeLoop = "loop" | ||
|
||
// DeviceTypeROM is the device type for ROM devices in lsblk output | ||
DeviceTypeROM = "rom" | ||
|
||
// DeviceTypeLVM is the device type for lvm devices in lsblk output | ||
DeviceTypeLVM = "lvm" | ||
|
||
// filter names: | ||
notReadOnly = "notReadOnly" | ||
notSuspended = "notSuspended" | ||
noBiosBootInPartLabel = "noBiosBootInPartLabel" | ||
noReservedInPartLabel = "noReservedInPartLabel" | ||
noFilesystemSignature = "noFilesystemSignature" | ||
noBindMounts = "noBindMounts" | ||
noChildren = "noChildren" | ||
usableDeviceType = "usableDeviceType" | ||
) | ||
|
||
func (lsblk *HostLSBLK) ResetFilterMap() { | ||
lsblk.filterMap = lsblk.defaultFilterMap() | ||
} | ||
|
||
func (lsblk *HostLSBLK) GetFilterMap() map[string]func(BlockDevice) (bool, error) { | ||
return lsblk.filterMap | ||
} | ||
|
||
func (lsblk *HostLSBLK) SetFilterMap(newFilterMap map[string]func(BlockDevice) (bool, error)) { | ||
lsblk.filterMap = newFilterMap | ||
} | ||
|
||
func (lsblk *HostLSBLK) defaultFilterMap() map[string]func(BlockDevice) (bool, error) { | ||
return map[string]func(BlockDevice) (bool, error){ | ||
notReadOnly: func(dev BlockDevice) (bool, error) { | ||
return !dev.ReadOnly, nil | ||
}, | ||
|
||
notSuspended: func(dev BlockDevice) (bool, error) { | ||
matched := dev.State != StateSuspended | ||
return matched, nil | ||
}, | ||
|
||
noBiosBootInPartLabel: func(dev BlockDevice) (bool, error) { | ||
biosBootInPartLabel := strings.Contains(strings.ToLower(dev.PartLabel), strings.ToLower("bios")) || | ||
strings.Contains(strings.ToLower(dev.PartLabel), strings.ToLower("boot")) | ||
return !biosBootInPartLabel, nil | ||
}, | ||
|
||
noReservedInPartLabel: func(dev BlockDevice) (bool, error) { | ||
reservedInPartLabel := strings.Contains(strings.ToLower(dev.PartLabel), "reserved") | ||
return !reservedInPartLabel, nil | ||
}, | ||
|
||
noFilesystemSignature: func(dev BlockDevice) (bool, error) { | ||
matched := dev.FSType == "" | ||
return matched, nil | ||
}, | ||
|
||
noChildren: func(dev BlockDevice) (bool, error) { | ||
hasChildren := dev.HasChildren() | ||
return !hasChildren, nil | ||
}, | ||
|
||
noBindMounts: func(dev BlockDevice) (bool, error) { | ||
hasBindMounts, _, err := lsblk.HasBindMounts(dev) | ||
return !hasBindMounts, err | ||
}, | ||
|
||
usableDeviceType: func(dev BlockDevice) (bool, error) { | ||
switch dev.Type { | ||
case DeviceTypeLoop: | ||
// check loop device isn't being used by kubernetes | ||
return lsblk.IsUsableLoopDev(dev) | ||
case DeviceTypeROM: | ||
return false, nil | ||
case DeviceTypeLVM: | ||
return false, nil | ||
default: | ||
return true, nil | ||
} | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
package lsblk | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
type filterTestCase struct { | ||
label string | ||
device BlockDevice | ||
expected bool | ||
expectErr bool | ||
} | ||
|
||
func TestNotReadOnly(t *testing.T) { | ||
testcases := []filterTestCase{ | ||
{label: "tc false", device: BlockDevice{ReadOnly: false}, expected: true, expectErr: false}, | ||
{label: "tc true", device: BlockDevice{ReadOnly: true}, expected: false, expectErr: false}, | ||
} | ||
for _, tc := range testcases { | ||
result, err := NewHostLSBLK(nil, "", "").GetFilterMap()[notReadOnly](tc.device) | ||
assert.Equal(t, tc.expected, result) | ||
if tc.expectErr { | ||
assert.Error(t, err) | ||
} else { | ||
assert.NoError(t, err) | ||
} | ||
} | ||
} | ||
|
||
func TestNotSuspended(t *testing.T) { | ||
testcases := []filterTestCase{ | ||
{label: "tc suspended", device: BlockDevice{State: "suspended"}, expected: false, expectErr: false}, | ||
{label: "tc live", device: BlockDevice{State: "live"}, expected: true, expectErr: false}, | ||
{label: "tc running", device: BlockDevice{State: "running"}, expected: true, expectErr: false}, | ||
} | ||
for _, tc := range testcases { | ||
result, err := NewHostLSBLK(nil, "", "").GetFilterMap()[notSuspended](tc.device) | ||
assert.Equal(t, tc.expected, result) | ||
if tc.expectErr { | ||
assert.Error(t, err) | ||
} else { | ||
assert.NoError(t, err) | ||
} | ||
} | ||
} | ||
|
||
func TestNoFilesystemSignature(t *testing.T) { | ||
testcases := []filterTestCase{ | ||
{label: "tc no fs", device: BlockDevice{FSType: ""}, expected: true, expectErr: false}, | ||
{label: "tc xfs", device: BlockDevice{FSType: "xfs"}, expected: false, expectErr: false}, | ||
{label: "tc swap", device: BlockDevice{FSType: "swap"}, expected: false, expectErr: false}, | ||
} | ||
for _, tc := range testcases { | ||
result, err := NewHostLSBLK(nil, "", "").GetFilterMap()[noFilesystemSignature](tc.device) | ||
assert.Equal(t, tc.expected, result) | ||
if tc.expectErr { | ||
assert.Error(t, err) | ||
} else { | ||
assert.NoError(t, err) | ||
} | ||
} | ||
} | ||
|
||
func TestNoChildren(t *testing.T) { | ||
testcases := []filterTestCase{ | ||
{label: "tc child", device: BlockDevice{Name: "dev1", Children: []BlockDevice{{Name: "child1"}}}, expected: false, expectErr: false}, | ||
{label: "tc no child", device: BlockDevice{Name: "dev2", Children: []BlockDevice{}}, expected: true, expectErr: false}, | ||
} | ||
for _, tc := range testcases { | ||
result, err := NewHostLSBLK(nil, "", "").GetFilterMap()[noChildren](tc.device) | ||
assert.Equal(t, tc.expected, result) | ||
if tc.expectErr { | ||
assert.Error(t, err) | ||
} else { | ||
assert.NoError(t, err) | ||
} | ||
} | ||
} | ||
|
||
func TestIsUsableDeviceType(t *testing.T) { | ||
testcases := []filterTestCase{ | ||
{label: "tc ROM", device: BlockDevice{Name: "dev1", Type: "rom"}, expected: false, expectErr: false}, | ||
{label: "tc Disk", device: BlockDevice{Name: "dev2", Type: "disk"}, expected: true, expectErr: false}, | ||
} | ||
for _, tc := range testcases { | ||
result, err := NewHostLSBLK(nil, "", "").GetFilterMap()[usableDeviceType](tc.device) | ||
assert.Equal(t, tc.expected, result) | ||
if tc.expectErr { | ||
assert.Error(t, err) | ||
} else { | ||
assert.NoError(t, err) | ||
} | ||
} | ||
} | ||
|
||
func TestNoBiosBootInPartLabel(t *testing.T) { | ||
testcases := []filterTestCase{ | ||
{label: "tc 1", device: BlockDevice{Name: "dev1", PartLabel: ""}, expected: true, expectErr: false}, | ||
{label: "tc 2", device: BlockDevice{Name: "dev2", PartLabel: "abc"}, expected: true, expectErr: false}, | ||
{label: "tc 3", device: BlockDevice{Name: "dev3", PartLabel: "bios"}, expected: false, expectErr: false}, | ||
{label: "tc 4", device: BlockDevice{Name: "dev4", PartLabel: "BIOS"}, expected: false, expectErr: false}, | ||
{label: "tc 5", device: BlockDevice{Name: "dev5", PartLabel: "boot"}, expected: false, expectErr: false}, | ||
{label: "tc 6", device: BlockDevice{Name: "dev6", PartLabel: "BOOT"}, expected: false, expectErr: false}, | ||
} | ||
for _, tc := range testcases { | ||
result, err := NewHostLSBLK(nil, "", "").GetFilterMap()[noBiosBootInPartLabel](tc.device) | ||
assert.Equal(t, tc.expected, result) | ||
if tc.expectErr { | ||
assert.Error(t, err) | ||
} else { | ||
assert.NoError(t, err) | ||
} | ||
} | ||
} | ||
|
||
func TestNoReservedInPartLabel(t *testing.T) { | ||
testcases := []filterTestCase{ | ||
{label: "tc 1", device: BlockDevice{Name: "dev1", PartLabel: ""}, expected: true}, | ||
{label: "tc 2", device: BlockDevice{Name: "dev2", PartLabel: "abc"}, expected: true}, | ||
{label: "tc 3", device: BlockDevice{Name: "dev3", PartLabel: "reserved"}, expected: false}, | ||
{label: "tc 4", device: BlockDevice{Name: "dev4", PartLabel: "RESERVED"}, expected: false}, | ||
{label: "tc 5", device: BlockDevice{Name: "dev5", PartLabel: "Reserved"}, expected: false}, | ||
} | ||
for _, tc := range testcases { | ||
result, err := NewHostLSBLK(nil, "", "").GetFilterMap()[noReservedInPartLabel](tc.device) | ||
assert.NoError(t, err) | ||
assert.Equal(t, tc.expected, result) | ||
} | ||
} |
Oops, something went wrong.