Skip to content

Commit 7af5fc0

Browse files
Unit test fixes
1 parent 2b82b03 commit 7af5fc0

2 files changed

Lines changed: 30 additions & 6 deletions

File tree

plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtRestoreBackupCommandWrapper.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,13 @@ private String mountBackupDirectory(String backupRepoAddress, String backupRepoT
214214
}
215215
}
216216

217+
executeMount(backupRepoAddress, backupRepoType, mountOptions, mountDirectory, mountTimeout);
218+
219+
return mountDirectory;
220+
}
221+
222+
private void executeMount(String backupRepoAddress, String backupRepoType, String mountOptions,
223+
String mountDirectory, Integer mountTimeout) {
217224
List<String[]> commands = new ArrayList<>();
218225
List<String> cmd = new ArrayList<>();
219226
cmd.add("sudo");
@@ -234,8 +241,6 @@ private String mountBackupDirectory(String backupRepoAddress, String backupRepoT
234241
backupRepoAddress, backupRepoType, mountDirectory, result.second());
235242
throw new CloudRuntimeException("Failed to mount the backup repository on the KVM host");
236243
}
237-
238-
return mountDirectory;
239244
}
240245

241246
private void unmountBackupDirectory(String backupDirectory) {

plugins/hypervisors/kvm/src/test/java/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtRestoreBackupCommandWrapperTest.java

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import com.cloud.agent.api.Answer;
2020
import com.cloud.hypervisor.kvm.resource.LibvirtComputingResource;
21+
import com.cloud.utils.Pair;
2122
import com.cloud.storage.Storage;
2223
import com.cloud.utils.script.Script;
2324
import com.cloud.vm.VirtualMachine;
@@ -83,6 +84,8 @@ public void testExecuteWithVmExistsNull() throws Exception {
8384
filesMock.when(() -> Files.createTempDirectory(anyString())).thenReturn(tempPath);
8485

8586
try (MockedStatic<Script> scriptMock = mockStatic(Script.class)) {
87+
Mockito.when(Script.executePipedCommands(Mockito.anyList(), Mockito.anyLong()))
88+
.thenReturn(new Pair<>(0, "success"));
8689
scriptMock.when(() -> Script.runSimpleBashScriptForExitValue(anyString(), anyInt(), any(Boolean.class)))
8790
.thenReturn(0); // Mount success
8891
scriptMock.when(() -> Script.runSimpleBashScriptForExitValue(anyString()))
@@ -126,6 +129,8 @@ public void testExecuteWithVmExistsTrue() throws Exception {
126129
filesMock.when(() -> Files.createTempDirectory(anyString())).thenReturn(tempPath);
127130

128131
try (MockedStatic<Script> scriptMock = mockStatic(Script.class)) {
132+
Mockito.when(Script.executePipedCommands(Mockito.anyList(), Mockito.anyLong()))
133+
.thenReturn(new Pair<>(0, "success"));
129134
scriptMock.when(() -> Script.runSimpleBashScriptForExitValue(anyString(), anyInt(), any(Boolean.class)))
130135
.thenReturn(0); // Mount success
131136
scriptMock.when(() -> Script.runSimpleBashScriptForExitValue(anyString()))
@@ -165,6 +170,8 @@ public void testExecuteWithVmExistsFalse() throws Exception {
165170
filesMock.when(() -> Files.createTempDirectory(anyString())).thenReturn(tempPath);
166171

167172
try (MockedStatic<Script> scriptMock = mockStatic(Script.class)) {
173+
Mockito.when(Script.executePipedCommands(Mockito.anyList(), Mockito.anyLong()))
174+
.thenReturn(new Pair<>(0, "success"));
168175
scriptMock.when(() -> Script.runSimpleBashScriptForExitValue(anyString(), anyInt(), any(Boolean.class)))
169176
.thenReturn(0); // Mount success
170177
scriptMock.when(() -> Script.runSimpleBashScriptForExitValue(anyString()))
@@ -207,6 +214,8 @@ public void testExecuteWithCifsMountType() throws Exception {
207214
filesMock.when(() -> Files.createTempDirectory(anyString())).thenReturn(tempPath);
208215

209216
try (MockedStatic<Script> scriptMock = mockStatic(Script.class)) {
217+
Mockito.when(Script.executePipedCommands(Mockito.anyList(), Mockito.anyLong()))
218+
.thenReturn(new Pair<>(0, "success"));
210219
scriptMock.when(() -> Script.runSimpleBashScriptForExitValue(anyString(), anyInt(), any(Boolean.class)))
211220
.thenReturn(0); // Mount success
212221
scriptMock.when(() -> Script.runSimpleBashScriptForExitValue(anyString()))
@@ -251,8 +260,8 @@ public void testExecuteWithMountFailure() throws Exception {
251260
filesMock.when(() -> Files.createTempDirectory(anyString())).thenReturn(tempPath);
252261

253262
try (MockedStatic<Script> scriptMock = mockStatic(Script.class)) {
254-
scriptMock.when(() -> Script.runSimpleBashScriptForExitValue(anyString(), anyInt(), any(Boolean.class)))
255-
.thenReturn(1); // Mount failure
263+
Mockito.when(Script.executePipedCommands(Mockito.anyList(), Mockito.anyLong()))
264+
.thenReturn(new Pair<>(1, "Failed"));
256265

257266
Answer result = wrapper.execute(command, libvirtComputingResource);
258267

@@ -290,6 +299,8 @@ public void testExecuteWithBackupFileNotFound() throws Exception {
290299
filesMock.when(() -> Files.createTempDirectory(anyString())).thenReturn(tempPath);
291300

292301
try (MockedStatic<Script> scriptMock = mockStatic(Script.class)) {
302+
Mockito.when(Script.executePipedCommands(Mockito.anyList(), Mockito.anyLong()))
303+
.thenReturn(new Pair<>(0, "success"));
293304
scriptMock.when(() -> Script.runSimpleBashScriptForExitValue(anyString(), anyInt(), any(Boolean.class)))
294305
.thenReturn(0); // Mount success
295306
scriptMock.when(() -> Script.runSimpleBashScriptForExitValue(anyString()))
@@ -339,8 +350,8 @@ public void testExecuteWithCorruptBackupFile() throws Exception {
339350
filesMock.when(() -> Files.createTempDirectory(anyString())).thenReturn(tempPath);
340351

341352
try (MockedStatic<Script> scriptMock = mockStatic(Script.class)) {
342-
scriptMock.when(() -> Script.runSimpleBashScriptForExitValue(anyString(), anyInt(), any(Boolean.class)))
343-
.thenReturn(0); // Mount success
353+
Mockito.when(Script.executePipedCommands(Mockito.anyList(), Mockito.anyLong()))
354+
.thenReturn(new Pair<>(0, "success"));
344355
scriptMock.when(() -> Script.runSimpleBashScriptForExitValue(anyString()))
345356
.thenAnswer(invocation -> {
346357
String command = invocation.getArgument(0);
@@ -390,6 +401,8 @@ public void testExecuteWithRsyncFailure() throws Exception {
390401
filesMock.when(() -> Files.createTempDirectory(anyString())).thenReturn(tempPath);
391402

392403
try (MockedStatic<Script> scriptMock = mockStatic(Script.class)) {
404+
Mockito.when(Script.executePipedCommands(Mockito.anyList(), Mockito.anyLong()))
405+
.thenReturn(new Pair<>(0, "success"));
393406
scriptMock.when(() -> Script.runSimpleBashScriptForExitValue(anyString(), anyInt(), any(Boolean.class)))
394407
.thenAnswer(invocation -> {
395408
String command = invocation.getArgument(0);
@@ -449,6 +462,8 @@ public void testExecuteWithAttachVolumeFailure() throws Exception {
449462
filesMock.when(() -> Files.createTempDirectory(anyString())).thenReturn(tempPath);
450463

451464
try (MockedStatic<Script> scriptMock = mockStatic(Script.class)) {
465+
Mockito.when(Script.executePipedCommands(Mockito.anyList(), Mockito.anyLong()))
466+
.thenReturn(new Pair<>(0, "success"));
452467
scriptMock.when(() -> Script.runSimpleBashScriptForExitValue(anyString(), anyInt(), any(Boolean.class)))
453468
.thenAnswer(invocation -> {
454469
String command = invocation.getArgument(0);
@@ -551,6 +566,10 @@ public void testExecuteWithMultipleVolumes() throws Exception {
551566
filesMock.when(() -> Files.createTempDirectory(anyString())).thenReturn(tempPath);
552567

553568
try (MockedStatic<Script> scriptMock = mockStatic(Script.class)) {
569+
Mockito.when(Script.executePipedCommands(Mockito.anyList(), Mockito.anyLong()))
570+
.thenReturn(new Pair<>(0, "success"));
571+
scriptMock.when(() -> Script.runSimpleBashScriptForExitValue(anyString(), anyInt(), any(Boolean.class)))
572+
.thenReturn(0); // Mount success
554573
scriptMock.when(() -> Script.runSimpleBashScriptForExitValue(anyString()))
555574
.thenReturn(0); // All commands success
556575
scriptMock.when(() -> Script.runSimpleBashScriptForExitValue(anyString(), anyInt(), any(Boolean.class)))

0 commit comments

Comments
 (0)