mirror of https://github.com/apache/cloudstack.git
Add Libvirt wrapper tests
This commit is contained in:
parent
0277fd1a71
commit
b49453e41c
|
|
@ -0,0 +1,121 @@
|
|||
// Licensed to the Apache Software Foundation (ASF) under one
|
||||
// or more contributor license agreements. See the NOTICE file
|
||||
// distributed with this work for additional information
|
||||
// regarding copyright ownership. The ASF licenses this file
|
||||
// to you 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 com.cloud.hypervisor.kvm.resource.wrapper;
|
||||
|
||||
import static org.mockito.ArgumentMatchers.anyMap;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
|
||||
import org.apache.cloudstack.backup.CreateImageTransferAnswer;
|
||||
import org.apache.cloudstack.backup.CreateImageTransferCommand;
|
||||
import org.apache.cloudstack.backup.ImageTransfer;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.MockedConstruction;
|
||||
import org.mockito.MockedStatic;
|
||||
import org.mockito.Mockito;
|
||||
import org.mockito.junit.MockitoJUnitRunner;
|
||||
|
||||
import com.cloud.agent.api.Answer;
|
||||
import com.cloud.hypervisor.kvm.resource.ImageServerControlSocket;
|
||||
import com.cloud.hypervisor.kvm.resource.LibvirtComputingResource;
|
||||
import com.cloud.utils.script.Script;
|
||||
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class LibvirtCreateImageTransferCommandWrapperTest {
|
||||
|
||||
private LibvirtCreateImageTransferCommandWrapper wrapper;
|
||||
private CreateImageTransferCommand command;
|
||||
private LibvirtComputingResource resource;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
wrapper = new LibvirtCreateImageTransferCommandWrapper();
|
||||
command = Mockito.mock(CreateImageTransferCommand.class);
|
||||
resource = Mockito.mock(LibvirtComputingResource.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testExecuteBlankTransferIdReturnsFailure() {
|
||||
Mockito.when(command.getTransferId()).thenReturn("");
|
||||
Mockito.when(command.getBackend()).thenReturn(ImageTransfer.Backend.nbd);
|
||||
|
||||
Answer answer = wrapper.execute(command, resource);
|
||||
|
||||
Assert.assertFalse(answer.getResult());
|
||||
Assert.assertEquals("transferId is empty.", answer.getDetails());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testExecuteNbdBackendSuccessReturnsUrl() {
|
||||
Mockito.when(command.getTransferId()).thenReturn("tr-1");
|
||||
Mockito.when(command.getBackend()).thenReturn(ImageTransfer.Backend.nbd);
|
||||
Mockito.when(command.getIdleTimeoutSeconds()).thenReturn(120);
|
||||
Mockito.when(command.getSocket()).thenReturn("sock-1");
|
||||
Mockito.when(command.getExportName()).thenReturn("vol-1");
|
||||
Mockito.when(command.getCheckpointId()).thenReturn("cp-1");
|
||||
|
||||
Mockito.when(resource.getImageServerPath()).thenReturn("/opt/cloudstack/image/server.py");
|
||||
Mockito.when(resource.getImageServerListenAddress()).thenReturn("");
|
||||
Mockito.when(resource.getPrivateIp()).thenReturn("10.0.0.10");
|
||||
Mockito.when(resource.isImageServerTlsEnabled()).thenReturn(false);
|
||||
|
||||
try (MockedConstruction<Script> ignored = Mockito.mockConstruction(Script.class,
|
||||
(mock, context) -> Mockito.when(mock.execute()).thenReturn(null));
|
||||
MockedStatic<ImageServerControlSocket> imageServerControlMock = Mockito.mockStatic(ImageServerControlSocket.class)) {
|
||||
imageServerControlMock.when(ImageServerControlSocket::isReady).thenReturn(true);
|
||||
imageServerControlMock.when(() -> ImageServerControlSocket.registerTransfer(eq("tr-1"), anyMap())).thenReturn(true);
|
||||
|
||||
Answer answer = wrapper.execute(command, resource);
|
||||
|
||||
Assert.assertTrue(answer.getResult());
|
||||
Assert.assertTrue(answer instanceof CreateImageTransferAnswer);
|
||||
CreateImageTransferAnswer transferAnswer = (CreateImageTransferAnswer) answer;
|
||||
Assert.assertEquals("tr-1", transferAnswer.getImageTransferId());
|
||||
Assert.assertEquals(
|
||||
"http://10.0.0.10:" + LibvirtComputingResource.IMAGE_SERVER_DEFAULT_PORT + "/images/tr-1",
|
||||
transferAnswer.getTransferUrl());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testExecuteRegisterTransferFailureReturnsFailure() {
|
||||
Mockito.when(command.getTransferId()).thenReturn("tr-2");
|
||||
Mockito.when(command.getBackend()).thenReturn(ImageTransfer.Backend.nbd);
|
||||
Mockito.when(command.getIdleTimeoutSeconds()).thenReturn(120);
|
||||
Mockito.when(command.getSocket()).thenReturn("sock-2");
|
||||
Mockito.when(command.getExportName()).thenReturn("vol-2");
|
||||
Mockito.when(command.getCheckpointId()).thenReturn(null);
|
||||
|
||||
Mockito.when(resource.getImageServerPath()).thenReturn("/opt/cloudstack/image/server.py");
|
||||
Mockito.when(resource.getImageServerListenAddress()).thenReturn("192.168.10.10");
|
||||
Mockito.when(resource.isImageServerTlsEnabled()).thenReturn(true);
|
||||
|
||||
try (MockedConstruction<Script> ignored = Mockito.mockConstruction(Script.class,
|
||||
(mock, context) -> Mockito.when(mock.execute()).thenReturn(null));
|
||||
MockedStatic<ImageServerControlSocket> imageServerControlMock = Mockito.mockStatic(ImageServerControlSocket.class)) {
|
||||
imageServerControlMock.when(ImageServerControlSocket::isReady).thenReturn(true);
|
||||
imageServerControlMock.when(() -> ImageServerControlSocket.registerTransfer(eq("tr-2"), anyMap())).thenReturn(false);
|
||||
|
||||
Answer answer = wrapper.execute(command, resource);
|
||||
|
||||
Assert.assertFalse(answer.getResult());
|
||||
Assert.assertEquals("Failed to register transfer with image server.", answer.getDetails());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,88 @@
|
|||
// Licensed to the Apache Software Foundation (ASF) under one
|
||||
// or more contributor license agreements. See the NOTICE file
|
||||
// distributed with this work for additional information
|
||||
// regarding copyright ownership. The ASF licenses this file
|
||||
// to you 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 com.cloud.hypervisor.kvm.resource.wrapper;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.Map;
|
||||
|
||||
import org.apache.cloudstack.backup.DeleteVmCheckpointCommand;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.MockedConstruction;
|
||||
import org.mockito.Mockito;
|
||||
import org.mockito.junit.MockitoJUnitRunner;
|
||||
|
||||
import com.cloud.agent.api.Answer;
|
||||
import com.cloud.hypervisor.kvm.resource.LibvirtComputingResource;
|
||||
import com.cloud.utils.script.Script;
|
||||
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class LibvirtDeleteVmCheckpointCommandWrapperTest {
|
||||
|
||||
private LibvirtDeleteVmCheckpointCommandWrapper wrapper;
|
||||
private DeleteVmCheckpointCommand command;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
wrapper = new LibvirtDeleteVmCheckpointCommandWrapper();
|
||||
command = Mockito.mock(DeleteVmCheckpointCommand.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testExecuteStoppedVmWithoutDisksReturnsFailure() {
|
||||
Mockito.when(command.isStoppedVM()).thenReturn(true);
|
||||
Mockito.when(command.getCheckpointId()).thenReturn("cp-1");
|
||||
Mockito.when(command.getDiskPathUuidMap()).thenReturn(Collections.emptyMap());
|
||||
|
||||
Answer answer = wrapper.execute(command, Mockito.mock(LibvirtComputingResource.class));
|
||||
|
||||
Assert.assertFalse(answer.getResult());
|
||||
Assert.assertEquals("No disks provided for bitmap removal", answer.getDetails());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testExecuteStoppedVmBitmapRemovalFailureReturnsFailure() {
|
||||
Mockito.when(command.isStoppedVM()).thenReturn(true);
|
||||
Mockito.when(command.getCheckpointId()).thenReturn("cp-2");
|
||||
Mockito.when(command.getDiskPathUuidMap()).thenReturn(Map.of("/path/disk1.qcow2", "vol-1"));
|
||||
|
||||
try (MockedConstruction<Script> ignored = Mockito.mockConstruction(Script.class,
|
||||
(mock, context) -> Mockito.when(mock.execute()).thenReturn("bitmap remove error"))) {
|
||||
Answer answer = wrapper.execute(command, Mockito.mock(LibvirtComputingResource.class));
|
||||
|
||||
Assert.assertFalse(answer.getResult());
|
||||
Assert.assertTrue(answer.getDetails().contains("Failed to remove bitmap cp-2"));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testExecuteRunningVmDeleteCheckpointSuccess() {
|
||||
Mockito.when(command.isStoppedVM()).thenReturn(false);
|
||||
Mockito.when(command.getVmName()).thenReturn("i-2-3-VM");
|
||||
Mockito.when(command.getCheckpointId()).thenReturn("cp-3");
|
||||
|
||||
try (MockedConstruction<Script> ignored = Mockito.mockConstruction(Script.class,
|
||||
(mock, context) -> Mockito.when(mock.execute()).thenReturn(null))) {
|
||||
Answer answer = wrapper.execute(command, Mockito.mock(LibvirtComputingResource.class));
|
||||
|
||||
Assert.assertTrue(answer.getResult());
|
||||
Assert.assertEquals("Checkpoint deleted", answer.getDetails());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,87 @@
|
|||
// Licensed to the Apache Software Foundation (ASF) under one
|
||||
// or more contributor license agreements. See the NOTICE file
|
||||
// distributed with this work for additional information
|
||||
// regarding copyright ownership. The ASF licenses this file
|
||||
// to you 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 com.cloud.hypervisor.kvm.resource.wrapper;
|
||||
|
||||
import org.apache.cloudstack.backup.FinalizeImageTransferCommand;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.MockedConstruction;
|
||||
import org.mockito.MockedStatic;
|
||||
import org.mockito.Mockito;
|
||||
import org.mockito.junit.MockitoJUnitRunner;
|
||||
|
||||
import com.cloud.agent.api.Answer;
|
||||
import com.cloud.hypervisor.kvm.resource.ImageServerControlSocket;
|
||||
import com.cloud.hypervisor.kvm.resource.LibvirtComputingResource;
|
||||
import com.cloud.utils.script.Script;
|
||||
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class LibvirtFinalizeImageTransferCommandWrapperTest {
|
||||
|
||||
private LibvirtFinalizeImageTransferCommandWrapper wrapper;
|
||||
private FinalizeImageTransferCommand command;
|
||||
private LibvirtComputingResource resource;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
wrapper = new LibvirtFinalizeImageTransferCommandWrapper();
|
||||
command = Mockito.mock(FinalizeImageTransferCommand.class);
|
||||
resource = Mockito.mock(LibvirtComputingResource.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testExecuteBlankTransferIdReturnsFailure() {
|
||||
Mockito.when(command.getTransferId()).thenReturn("");
|
||||
|
||||
Answer answer = wrapper.execute(command, resource);
|
||||
|
||||
Assert.assertFalse(answer.getResult());
|
||||
Assert.assertEquals("transferId is empty.", answer.getDetails());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testExecuteWithActiveTransfersReturnsSuccessWithoutStoppingServer() {
|
||||
Mockito.when(command.getTransferId()).thenReturn("tr-1");
|
||||
|
||||
try (MockedStatic<ImageServerControlSocket> imageServerControlMock = Mockito.mockStatic(ImageServerControlSocket.class)) {
|
||||
imageServerControlMock.when(() -> ImageServerControlSocket.unregisterTransfer("tr-1")).thenReturn(2);
|
||||
|
||||
Answer answer = wrapper.execute(command, resource);
|
||||
|
||||
Assert.assertTrue(answer.getResult());
|
||||
Assert.assertEquals("Image transfer finalized.", answer.getDetails());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testExecuteServerUnreachableReturnsSuccessAndForcesStop() {
|
||||
Mockito.when(command.getTransferId()).thenReturn("tr-2");
|
||||
|
||||
try (MockedConstruction<Script> ignored = Mockito.mockConstruction(Script.class,
|
||||
(mock, context) -> Mockito.when(mock.execute()).thenReturn("inactive"));
|
||||
MockedStatic<ImageServerControlSocket> imageServerControlMock = Mockito.mockStatic(ImageServerControlSocket.class)) {
|
||||
imageServerControlMock.when(() -> ImageServerControlSocket.unregisterTransfer("tr-2")).thenReturn(-1);
|
||||
|
||||
Answer answer = wrapper.execute(command, resource);
|
||||
|
||||
Assert.assertTrue(answer.getResult());
|
||||
Assert.assertEquals("Image transfer finalized (server unreachable, forced stop).", answer.getDetails());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,118 @@
|
|||
// Licensed to the Apache Software Foundation (ASF) under one
|
||||
// or more contributor license agreements. See the NOTICE file
|
||||
// distributed with this work for additional information
|
||||
// regarding copyright ownership. The ASF licenses this file
|
||||
// to you 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 com.cloud.hypervisor.kvm.resource.wrapper;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import org.apache.cloudstack.backup.StartBackupAnswer;
|
||||
import org.apache.cloudstack.backup.StartBackupCommand;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.MockedConstruction;
|
||||
import org.mockito.Mockito;
|
||||
import org.mockito.junit.MockitoJUnitRunner;
|
||||
|
||||
import com.cloud.agent.api.Answer;
|
||||
import com.cloud.hypervisor.kvm.resource.LibvirtComputingResource;
|
||||
import com.cloud.utils.script.Script;
|
||||
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class LibvirtStartBackupCommandWrapperTest {
|
||||
|
||||
private LibvirtStartBackupCommandWrapper wrapper;
|
||||
private StartBackupCommand command;
|
||||
private LibvirtComputingResource resource;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
wrapper = new LibvirtStartBackupCommandWrapper();
|
||||
command = Mockito.mock(StartBackupCommand.class);
|
||||
resource = Mockito.mock(LibvirtComputingResource.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testExecuteStoppedVmBitmapAddSuccess() {
|
||||
Mockito.when(command.isStoppedVM()).thenReturn(true);
|
||||
Mockito.when(command.getVmName()).thenReturn("i-2-3-VM");
|
||||
Mockito.when(command.getToCheckpointId()).thenReturn("cp-stopped-1");
|
||||
Mockito.when(command.getDiskPathUuidMap()).thenReturn(Map.of("/path/disk1.qcow2", "vol-1"));
|
||||
|
||||
try (MockedConstruction<Script> ignored = Mockito.mockConstruction(Script.class,
|
||||
(mock, context) -> Mockito.when(mock.execute()).thenReturn(null))) {
|
||||
Answer answer = wrapper.execute(command, resource);
|
||||
|
||||
Assert.assertTrue(answer.getResult());
|
||||
Assert.assertTrue(answer instanceof StartBackupAnswer);
|
||||
Assert.assertTrue(answer.getDetails().contains("checkpoint bitmap added successfully"));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testExecuteStoppedVmBitmapAddFailure() {
|
||||
Mockito.when(command.isStoppedVM()).thenReturn(true);
|
||||
Mockito.when(command.getVmName()).thenReturn("i-2-3-VM");
|
||||
Mockito.when(command.getToCheckpointId()).thenReturn("cp-stopped-2");
|
||||
Mockito.when(command.getDiskPathUuidMap()).thenReturn(Map.of("/path/disk1.qcow2", "vol-1"));
|
||||
|
||||
try (MockedConstruction<Script> ignored = Mockito.mockConstruction(Script.class,
|
||||
(mock, context) -> Mockito.when(mock.execute()).thenReturn("bitmap add error"))) {
|
||||
Answer answer = wrapper.execute(command, resource);
|
||||
|
||||
Assert.assertFalse(answer.getResult());
|
||||
Assert.assertTrue(answer.getDetails().contains("Failed to add bitmap cp-stopped-2"));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testExecuteRunningVmMissingFromCheckpointCreateTimeReturnsFailure() {
|
||||
Mockito.when(command.isStoppedVM()).thenReturn(false);
|
||||
Mockito.when(command.getVmName()).thenReturn("i-2-3-VM");
|
||||
Mockito.when(command.getToCheckpointId()).thenReturn("cp-running-1");
|
||||
Mockito.when(command.getFromCheckpointId()).thenReturn("cp-running-0");
|
||||
Mockito.when(command.getFromCheckpointCreateTime()).thenReturn(null);
|
||||
Mockito.when(command.getSocket()).thenReturn("sock-1");
|
||||
|
||||
try (MockedConstruction<Script> ignored = Mockito.mockConstruction(Script.class,
|
||||
(mock, context) -> Mockito.when(mock.execute()).thenReturn("not found"))) {
|
||||
Answer answer = wrapper.execute(command, resource);
|
||||
|
||||
Assert.assertFalse(answer.getResult());
|
||||
Assert.assertTrue(answer.getDetails().contains("From checkpoint create time is null"));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testExecuteRunningVmBackupBeginSuccess() {
|
||||
Mockito.when(command.isStoppedVM()).thenReturn(false);
|
||||
Mockito.when(command.getVmName()).thenReturn("i-2-3-VM");
|
||||
Mockito.when(command.getToCheckpointId()).thenReturn("cp-running-2");
|
||||
Mockito.when(command.getFromCheckpointId()).thenReturn(null);
|
||||
Mockito.when(command.getSocket()).thenReturn("sock-2");
|
||||
Mockito.when(command.getDiskPathUuidMap()).thenReturn(Map.of("/path/disk1.qcow2", "vol-1"));
|
||||
Mockito.when(resource.getDiskPathLabelMap("i-2-3-VM")).thenReturn(Map.of("/path/disk1.qcow2", "vda"));
|
||||
|
||||
try (MockedConstruction<Script> ignored = Mockito.mockConstruction(Script.class,
|
||||
(mock, context) -> Mockito.when(mock.execute()).thenReturn(null))) {
|
||||
Answer answer = wrapper.execute(command, resource);
|
||||
|
||||
Assert.assertTrue(answer.getResult());
|
||||
Assert.assertTrue(answer.getDetails().contains("Backup started successfully"));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,95 @@
|
|||
// Licensed to the Apache Software Foundation (ASF) under one
|
||||
// or more contributor license agreements. See the NOTICE file
|
||||
// distributed with this work for additional information
|
||||
// regarding copyright ownership. The ASF licenses this file
|
||||
// to you 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 com.cloud.hypervisor.kvm.resource.wrapper;
|
||||
|
||||
import org.apache.cloudstack.backup.StopBackupCommand;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.libvirt.Connect;
|
||||
import org.libvirt.Domain;
|
||||
import org.mockito.MockedConstruction;
|
||||
import org.mockito.MockedStatic;
|
||||
import org.mockito.Mockito;
|
||||
import org.mockito.junit.MockitoJUnitRunner;
|
||||
|
||||
import com.cloud.agent.api.Answer;
|
||||
import com.cloud.hypervisor.kvm.resource.LibvirtComputingResource;
|
||||
import com.cloud.hypervisor.kvm.resource.LibvirtConnection;
|
||||
import com.cloud.utils.script.Script;
|
||||
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class LibvirtStopBackupCommandWrapperTest {
|
||||
|
||||
private LibvirtStopBackupCommandWrapper wrapper;
|
||||
private StopBackupCommand command;
|
||||
private LibvirtComputingResource resource;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
wrapper = new LibvirtStopBackupCommandWrapper();
|
||||
command = Mockito.mock(StopBackupCommand.class);
|
||||
resource = Mockito.mock(LibvirtComputingResource.class);
|
||||
Mockito.when(command.getVmName()).thenReturn("i-2-3-VM");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testExecuteDomainNotFoundReturnsFailure() throws Exception {
|
||||
Connect connect = Mockito.mock(Connect.class);
|
||||
|
||||
try (MockedStatic<LibvirtConnection> libvirtConnectionMock = Mockito.mockStatic(LibvirtConnection.class)) {
|
||||
libvirtConnectionMock.when(LibvirtConnection::getConnection).thenReturn(connect);
|
||||
Mockito.when(connect.domainLookupByName("i-2-3-VM")).thenReturn(null);
|
||||
|
||||
Answer answer = wrapper.execute(command, resource);
|
||||
|
||||
Assert.assertFalse(answer.getResult());
|
||||
Assert.assertEquals("Domain not found: i-2-3-VM", answer.getDetails());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testExecuteDomainFoundReturnsSuccess() throws Exception {
|
||||
Connect connect = Mockito.mock(Connect.class);
|
||||
Domain domain = Mockito.mock(Domain.class);
|
||||
|
||||
try (MockedConstruction<Script> ignored = Mockito.mockConstruction(Script.class,
|
||||
(mock, context) -> Mockito.when(mock.execute()).thenReturn(null));
|
||||
MockedStatic<LibvirtConnection> libvirtConnectionMock = Mockito.mockStatic(LibvirtConnection.class)) {
|
||||
libvirtConnectionMock.when(LibvirtConnection::getConnection).thenReturn(connect);
|
||||
Mockito.when(connect.domainLookupByName("i-2-3-VM")).thenReturn(domain);
|
||||
|
||||
Answer answer = wrapper.execute(command, resource);
|
||||
|
||||
Assert.assertTrue(answer.getResult());
|
||||
Assert.assertEquals("Backup stopped successfully", answer.getDetails());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testExecuteLibvirtConnectionFailureReturnsFailure() {
|
||||
try (MockedStatic<LibvirtConnection> libvirtConnectionMock = Mockito.mockStatic(LibvirtConnection.class)) {
|
||||
libvirtConnectionMock.when(LibvirtConnection::getConnection).thenThrow(new RuntimeException("libvirt unavailable"));
|
||||
|
||||
Answer answer = wrapper.execute(command, resource);
|
||||
|
||||
Assert.assertFalse(answer.getResult());
|
||||
Assert.assertTrue(answer.getDetails().contains("Error stopping backup: libvirt unavailable"));
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue