|
| 1 | +package machine |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "fmt" |
| 6 | + "strings" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/containers/common/pkg/strongunits" |
| 10 | + "github.com/stretchr/testify/assert" |
| 11 | +) |
| 12 | + |
| 13 | +type MockedSSHRunner struct { |
| 14 | + mockedSSHCommandToOutput map[string]string |
| 15 | + mockedSSHCommandToError map[string]error |
| 16 | + mockedSSHCommandToArgs map[string]string |
| 17 | +} |
| 18 | + |
| 19 | +func (r *MockedSSHRunner) RunPrivileged(reason string, cmdAndArgs ...string) (string, string, error) { |
| 20 | + if r.mockedSSHCommandToError[reason] != nil { |
| 21 | + return "", "", r.mockedSSHCommandToError[reason] |
| 22 | + } |
| 23 | + r.mockedSSHCommandToArgs[reason] = strings.Join(cmdAndArgs, ",") |
| 24 | + output, ok := r.mockedSSHCommandToOutput[reason] |
| 25 | + if !ok { |
| 26 | + r.mockedSSHCommandToOutput[reason] = "" |
| 27 | + } |
| 28 | + return output, "", nil |
| 29 | +} |
| 30 | + |
| 31 | +func (r *MockedSSHRunner) Run(_ string, _ ...string) (string, string, error) { |
| 32 | + // No-op |
| 33 | + return "", "", nil |
| 34 | +} |
| 35 | + |
| 36 | +func (r *MockedSSHRunner) RunPrivate(_ string, _ ...string) (string, string, error) { |
| 37 | + // No-op |
| 38 | + return "", "", nil |
| 39 | +} |
| 40 | + |
| 41 | +func NewMockedSSHRunner() *MockedSSHRunner { |
| 42 | + return &MockedSSHRunner{ |
| 43 | + mockedSSHCommandToOutput: map[string]string{}, |
| 44 | + mockedSSHCommandToArgs: map[string]string{}, |
| 45 | + mockedSSHCommandToError: map[string]error{}, |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +func TestGrowLVForMicroShift_WhenResizeAttemptFails_ThenThrowErrorExplainingWhereItFailed(t *testing.T) { |
| 50 | + testCases := []struct { |
| 51 | + name string |
| 52 | + volumeGroupSizeOutput string |
| 53 | + logicalVolumeSizeOutput string |
| 54 | + physicalVolumeListCommandFailed error |
| 55 | + physicalVolumeGroupSizeCommandFailed error |
| 56 | + logicalVolumeSizeCommandFailed error |
| 57 | + extendingLogicalVolumeSizeCommandFailed error |
| 58 | + expectedReturnedError error |
| 59 | + }{ |
| 60 | + {"when listing physical volume devices failed, then throw error", "", "", errors.New("listing volumes failed"), nil, nil, nil, errors.New("listing volumes failed")}, |
| 61 | + {"when fetching volume group size failed, then throw error", "", "", nil, errors.New("fetching volume group size failed"), nil, nil, errors.New("fetching volume group size failed")}, |
| 62 | + {"when parsing volume group size failed, then throw error", "invalid", "", nil, nil, nil, nil, errors.New("strconv.Atoi: parsing \"invalid\": invalid syntax")}, |
| 63 | + {"when fetching lv size failed, then throw error", "42966450176", "", nil, nil, errors.New("fetching lvm size failed"), nil, errors.New("fetching lvm size failed")}, |
| 64 | + {"when parsing lv size failed, then throw error", "42966450176", "invalid", nil, nil, nil, nil, errors.New("strconv.Atoi: parsing \"invalid\": invalid syntax")}, |
| 65 | + {"when extending lv size failed, then throw error", "42966450176", "16106127360", nil, nil, nil, errors.New("extending lv failed"), errors.New("extending lv failed")}, |
| 66 | + } |
| 67 | + |
| 68 | + // Loop through each test case |
| 69 | + for _, tc := range testCases { |
| 70 | + t.Run(tc.name, func(t *testing.T) { |
| 71 | + // Given |
| 72 | + sshRunner := NewMockedSSHRunner() |
| 73 | + sshRunner.mockedSSHCommandToError["Resizing the physical volume(PV)"] = tc.physicalVolumeListCommandFailed |
| 74 | + sshRunner.mockedSSHCommandToError["Get the volume group size"] = tc.physicalVolumeGroupSizeCommandFailed |
| 75 | + sshRunner.mockedSSHCommandToOutput["Get the volume group size"] = tc.volumeGroupSizeOutput |
| 76 | + sshRunner.mockedSSHCommandToError["Get the size of root logical volume"] = tc.logicalVolumeSizeCommandFailed |
| 77 | + sshRunner.mockedSSHCommandToOutput["Get the size of root logical volume"] = tc.logicalVolumeSizeOutput |
| 78 | + sshRunner.mockedSSHCommandToError["Extending and resizing the logical volume(LV)"] = tc.extendingLogicalVolumeSizeCommandFailed |
| 79 | + |
| 80 | + // When |
| 81 | + err := growLVForMicroshift(sshRunner, "rhel/root", "/dev/vda5", 15) |
| 82 | + |
| 83 | + // Then |
| 84 | + assert.EqualError(t, err, tc.expectedReturnedError.Error(), "Error messages should match") |
| 85 | + }) |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +func TestGrowLVForMicroShift_WhenPhysicalVolumeAvailableForResize_ThenSizeToIncreaseIsCalculatedAndGrown(t *testing.T) { |
| 90 | + testCases := []struct { |
| 91 | + name string |
| 92 | + existingVolumeGroupSize strongunits.B |
| 93 | + oldPersistentVolumeSize strongunits.B |
| 94 | + persistentVolumeSize int |
| 95 | + expectPVSizeToGrow bool |
| 96 | + expectedIncreaseInSize strongunits.B |
| 97 | + }{ |
| 98 | + {"when disk size can not accommodate persistent volume size growth, then do NOT grow lv", strongunits.GiB(31).ToBytes(), strongunits.GiB(15).ToBytes(), 20, false, strongunits.B(0)}, |
| 99 | + {"when disk size can accommodate persistent volume size growth, then grow lv", strongunits.GiB(41).ToBytes(), strongunits.GiB(15).ToBytes(), 20, true, strongunits.GiB(6).ToBytes()}, |
| 100 | + {"when requested persistent volume size less than present persistent volume size, then do NOT grow lv", strongunits.GiB(31).ToBytes(), strongunits.GiB(20).ToBytes(), 15, false, strongunits.B(0)}, |
| 101 | + {"when requested disk size less than present persistent volume size, then do NOT grow lv", strongunits.GiB(31).ToBytes(), strongunits.GiB(20).ToBytes(), 41, false, strongunits.B(0)}, |
| 102 | + // https://github.com/crc-org/crc/issues/4186#issuecomment-2656129015 |
| 103 | + {"when disk size has more space than requested persistent volume growth, then lv growth larger than requested", strongunits.GiB(45).ToBytes(), strongunits.GiB(15).ToBytes(), 20, true, strongunits.GiB(10).ToBytes()}, |
| 104 | + } |
| 105 | + |
| 106 | + // Loop through each test case |
| 107 | + for _, tc := range testCases { |
| 108 | + t.Run(tc.name, func(t *testing.T) { |
| 109 | + // Given |
| 110 | + sshRunner := NewMockedSSHRunner() |
| 111 | + sshRunner.mockedSSHCommandToOutput["resizing the physical volume(PV)"] = "Physical volume \"/dev/vda5\" changed" |
| 112 | + sshRunner.mockedSSHCommandToOutput["Get the volume group size"] = fmt.Sprintf("%d", tc.existingVolumeGroupSize.ToBytes()) |
| 113 | + sshRunner.mockedSSHCommandToOutput["Get the size of root logical volume"] = fmt.Sprintf("%d", tc.oldPersistentVolumeSize.ToBytes()) |
| 114 | + |
| 115 | + // When |
| 116 | + err := growLVForMicroshift(sshRunner, "rhel/root", "/dev/vda5", tc.persistentVolumeSize) |
| 117 | + |
| 118 | + // Then |
| 119 | + assert.NoError(t, err) |
| 120 | + _, lvExpanded := sshRunner.mockedSSHCommandToOutput["Extending and resizing the logical volume(LV)"] |
| 121 | + assert.Equal(t, tc.expectPVSizeToGrow, lvExpanded) |
| 122 | + if tc.expectPVSizeToGrow { |
| 123 | + assert.Contains(t, sshRunner.mockedSSHCommandToArgs["Extending and resizing the logical volume(LV)"], fmt.Sprintf("+%db", tc.expectedIncreaseInSize.ToBytes())) |
| 124 | + } |
| 125 | + }) |
| 126 | + } |
| 127 | +} |
0 commit comments