Skip to content

Commit 27cd68c

Browse files
authored
test (machine) : add some unit tests for growLVForMicroshift (#4628)
+ Replace sshRunner argument in growLVForMicroshift with an interface + Add unit tests for abovementioned method with various combinations of persistent-volume size and disk size + Add unit test for validatePersistentVolumeSize Signed-off-by: Rohan Kumar <rohaan@redhat.com>
1 parent e4a8966 commit 27cd68c

File tree

3 files changed

+150
-1
lines changed

3 files changed

+150
-1
lines changed

pkg/crc/config/validations_test.go

+22
Original file line numberDiff line numberDiff line change
@@ -136,3 +136,25 @@ func TestValidateNoProxy(t *testing.T) {
136136
})
137137
}
138138
}
139+
140+
func TestValidatePersistentVolumeSize(t *testing.T) {
141+
tests := []struct {
142+
name string
143+
persistentVolumeSize string
144+
expectedValidationResult bool
145+
}{
146+
{"equal to 15G", "15", true},
147+
{"greater than 15G", "20", true},
148+
{"less than 15G", "7", false},
149+
{"invalid integer value", "an-elephant", false},
150+
}
151+
152+
for _, tt := range tests {
153+
t.Run(tt.name, func(t *testing.T) {
154+
actualValidationResult, _ := validatePersistentVolumeSize(tt.persistentVolumeSize)
155+
if actualValidationResult != tt.expectedValidationResult {
156+
t.Errorf("validatePersistentVolumeSize(%s) : got %v, want %v", tt.persistentVolumeSize, actualValidationResult, tt.expectedValidationResult)
157+
}
158+
})
159+
}
160+
}

pkg/crc/machine/start.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ func getrootPartition(sshRunner *crcssh.Runner, preset crcPreset.Preset) (string
169169
return rootPart, nil
170170
}
171171

172-
func growLVForMicroshift(sshRunner *crcssh.Runner, lvFullName string, rootPart string, persistentVolumeSize int) error {
172+
func growLVForMicroshift(sshRunner crcos.CommandRunner, lvFullName string, rootPart string, persistentVolumeSize int) error {
173173
if _, _, err := sshRunner.RunPrivileged("Resizing the physical volume(PV)", "/usr/sbin/pvresize", "--devices", rootPart, rootPart); err != nil {
174174
return err
175175
}

pkg/crc/machine/start_test.go

+127
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
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

Comments
 (0)