mirror of
https://github.com/apache/cloudstack.git
synced 2025-12-16 18:43:26 +01:00
CLOUDSTACK-9051: add unit tests for UpdateVmNicIp
This commit is contained in:
parent
b79d338f29
commit
c01c73e44d
@ -0,0 +1,91 @@
|
||||
// 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 org.apache.cloudstack.api.command.test;
|
||||
|
||||
import junit.framework.Assert;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.mockito.Mockito;
|
||||
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
import org.apache.cloudstack.api.ResponseGenerator;
|
||||
import org.apache.cloudstack.api.ServerApiException;
|
||||
import org.apache.cloudstack.api.ResponseObject.ResponseView;
|
||||
import org.apache.cloudstack.api.command.user.vm.UpdateVmNicIpCmd;
|
||||
import org.apache.cloudstack.api.response.UserVmResponse;
|
||||
|
||||
import com.cloud.exception.ConcurrentOperationException;
|
||||
import com.cloud.exception.InsufficientCapacityException;
|
||||
import com.cloud.exception.ResourceAllocationException;
|
||||
import com.cloud.exception.ResourceUnavailableException;
|
||||
import com.cloud.uservm.UserVm;
|
||||
import com.cloud.vm.UserVmService;
|
||||
|
||||
public class UpdateVmNicIpTest extends TestCase {
|
||||
|
||||
private UpdateVmNicIpCmd updateVmNicIpCmd;
|
||||
private ResponseGenerator responseGenerator;
|
||||
|
||||
@Override
|
||||
@Before
|
||||
public void setUp() {
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSuccess() throws ResourceAllocationException, ResourceUnavailableException, ConcurrentOperationException, InsufficientCapacityException {
|
||||
|
||||
UserVmService userVmService = Mockito.mock(UserVmService.class);
|
||||
updateVmNicIpCmd = Mockito.mock(UpdateVmNicIpCmd.class);
|
||||
UserVm userVm = Mockito.mock(UserVm.class);
|
||||
|
||||
Mockito.when(userVmService.updateNicIpForVirtualMachine(Mockito.any(UpdateVmNicIpCmd.class))).thenReturn(userVm);
|
||||
|
||||
updateVmNicIpCmd._userVmService = userVmService;
|
||||
responseGenerator = Mockito.mock(ResponseGenerator.class);
|
||||
|
||||
List<UserVmResponse> list = new LinkedList<UserVmResponse>();
|
||||
UserVmResponse userVmResponse = Mockito.mock(UserVmResponse.class);
|
||||
list.add(userVmResponse);
|
||||
Mockito.when(responseGenerator.createUserVmResponse(ResponseView.Restricted, "virtualmachine", userVm)).thenReturn(list);
|
||||
|
||||
updateVmNicIpCmd._responseGenerator = responseGenerator;
|
||||
updateVmNicIpCmd.execute();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFailure() throws ResourceAllocationException, ResourceUnavailableException, ConcurrentOperationException, InsufficientCapacityException {
|
||||
UserVmService userVmService = Mockito.mock(UserVmService.class);
|
||||
updateVmNicIpCmd = Mockito.mock(UpdateVmNicIpCmd.class);
|
||||
|
||||
Mockito.when(userVmService.updateNicIpForVirtualMachine(Mockito.any(UpdateVmNicIpCmd.class))).thenReturn(null);
|
||||
|
||||
updateVmNicIpCmd._userVmService = userVmService;
|
||||
|
||||
updateVmNicIpCmd._responseGenerator = responseGenerator;
|
||||
try {
|
||||
updateVmNicIpCmd.execute();
|
||||
} catch (ServerApiException exception) {
|
||||
Assert.assertEquals("Failed to update ip address on vm NIC. Refer to server logs for details.", exception.getDescription());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@ -1433,9 +1433,6 @@ public class UserVmManagerImpl extends ManagerBase implements UserVmManager, Vir
|
||||
if (vm == null) {
|
||||
throw new InvalidParameterValueException("There is no vm with the nic");
|
||||
}
|
||||
if (vm.getState() != State.Stopped) {
|
||||
throw new InvalidParameterValueException("The vm is not Stopped, please stop it before update Vm nic Ip");
|
||||
}
|
||||
|
||||
if (!_networkModel.listNetworkOfferingServices(nicVO.getNetworkId()).isEmpty() && vm.getState() != State.Stopped) {
|
||||
InvalidParameterValueException ex = new InvalidParameterValueException(
|
||||
@ -1482,8 +1479,8 @@ public class UserVmManagerImpl extends ManagerBase implements UserVmManager, Vir
|
||||
Transaction.execute(new TransactionCallbackNoReturn() {
|
||||
@Override
|
||||
public void doInTransactionWithoutResult(TransactionStatus status) {
|
||||
_ipAddrMgr.markIpAsUnavailable(ip.getId());
|
||||
_ipAddressDao.unassignIpAddress(ip.getId());
|
||||
_ipAddrMgr.markIpAsUnavailable(ip.getId());
|
||||
_ipAddressDao.unassignIpAddress(ip.getId());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@ -33,6 +33,7 @@ import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
@ -42,6 +43,7 @@ import com.cloud.event.dao.UsageEventDao;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.Mockito;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
import org.mockito.Spy;
|
||||
|
||||
@ -51,6 +53,7 @@ import org.apache.cloudstack.api.ServerApiException;
|
||||
import org.apache.cloudstack.api.command.admin.vm.AssignVMCmd;
|
||||
import org.apache.cloudstack.api.command.user.vm.RestoreVMCmd;
|
||||
import org.apache.cloudstack.api.command.user.vm.ScaleVMCmd;
|
||||
import org.apache.cloudstack.api.command.user.vm.UpdateVmNicIpCmd;
|
||||
import org.apache.cloudstack.context.CallContext;
|
||||
import org.apache.cloudstack.engine.orchestration.service.VolumeOrchestrationService;
|
||||
import org.apache.cloudstack.framework.config.dao.ConfigurationDao;
|
||||
@ -60,6 +63,9 @@ import org.apache.cloudstack.storage.datastore.db.TemplateDataStoreVO;
|
||||
|
||||
import com.cloud.capacity.CapacityManager;
|
||||
import com.cloud.configuration.ConfigurationManager;
|
||||
import com.cloud.dc.DataCenterVO;
|
||||
import com.cloud.dc.DataCenter.NetworkType;
|
||||
import com.cloud.dc.dao.DataCenterDao;
|
||||
import com.cloud.exception.ConcurrentOperationException;
|
||||
import com.cloud.exception.InsufficientCapacityException;
|
||||
import com.cloud.exception.InvalidParameterValueException;
|
||||
@ -68,6 +74,13 @@ import com.cloud.exception.ResourceAllocationException;
|
||||
import com.cloud.exception.ResourceUnavailableException;
|
||||
import com.cloud.hypervisor.Hypervisor;
|
||||
import com.cloud.hypervisor.Hypervisor.HypervisorType;
|
||||
import com.cloud.network.IpAddressManager;
|
||||
import com.cloud.network.Network.GuestType;
|
||||
import com.cloud.network.Network.Service;
|
||||
import com.cloud.network.NetworkModel;
|
||||
import com.cloud.network.dao.IPAddressDao;
|
||||
import com.cloud.network.dao.NetworkDao;
|
||||
import com.cloud.network.dao.NetworkVO;
|
||||
import com.cloud.offering.ServiceOffering;
|
||||
import com.cloud.service.ServiceOfferingVO;
|
||||
import com.cloud.service.dao.ServiceOfferingDao;
|
||||
@ -87,6 +100,8 @@ import com.cloud.user.dao.AccountDao;
|
||||
import com.cloud.user.dao.UserDao;
|
||||
import com.cloud.utils.db.EntityManager;
|
||||
import com.cloud.utils.exception.CloudRuntimeException;
|
||||
import com.cloud.vm.VirtualMachine.State;
|
||||
import com.cloud.vm.dao.NicDao;
|
||||
import com.cloud.vm.dao.UserVmDao;
|
||||
import com.cloud.vm.dao.VMInstanceDao;
|
||||
import com.cloud.vm.snapshot.VMSnapshotVO;
|
||||
@ -161,6 +176,26 @@ public class UserVmManagerTest {
|
||||
UsageEventDao _usageEventDao;
|
||||
@Mock
|
||||
VMSnapshotDao _vmSnapshotDao;
|
||||
@Mock
|
||||
UpdateVmNicIpCmd _updateVmNicIpCmd;
|
||||
@Mock
|
||||
NicDao _nicDao;
|
||||
@Mock
|
||||
NicVO _nicMock;
|
||||
@Mock
|
||||
NetworkModel _networkModel;
|
||||
@Mock
|
||||
NetworkDao _networkDao;
|
||||
@Mock
|
||||
NetworkVO _networkMock;
|
||||
@Mock
|
||||
DataCenterDao _dcDao;
|
||||
@Mock
|
||||
DataCenterVO _dcMock;
|
||||
@Mock
|
||||
IpAddressManager _ipAddrMgr;
|
||||
@Mock
|
||||
IPAddressDao _ipAddressDao;
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
@ -186,6 +221,12 @@ public class UserVmManagerTest {
|
||||
_userVmMgr._entityMgr = _entityMgr;
|
||||
_userVmMgr._storagePoolDao = _storagePoolDao;
|
||||
_userVmMgr._vmSnapshotDao = _vmSnapshotDao;
|
||||
_userVmMgr._nicDao = _nicDao;
|
||||
_userVmMgr._networkModel = _networkModel;
|
||||
_userVmMgr._networkDao = _networkDao;
|
||||
_userVmMgr._dcDao = _dcDao;
|
||||
_userVmMgr._ipAddrMgr = _ipAddrMgr;
|
||||
_userVmMgr._ipAddressDao = _ipAddressDao;
|
||||
|
||||
doReturn(3L).when(_account).getId();
|
||||
doReturn(8L).when(_vmMock).getAccountId();
|
||||
@ -648,4 +689,208 @@ public class UserVmManagerTest {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUpdateVmNicIpSuccess1() throws Exception {
|
||||
UpdateVmNicIpCmd cmd = new UpdateVmNicIpCmd();
|
||||
Class<?> _class = cmd.getClass();
|
||||
|
||||
Field virtualmachineIdField = _class.getDeclaredField("nicId");
|
||||
virtualmachineIdField.setAccessible(true);
|
||||
virtualmachineIdField.set(cmd, 1L);
|
||||
|
||||
Field accountNameField = _class.getDeclaredField("ipAddr");
|
||||
accountNameField.setAccessible(true);
|
||||
accountNameField.set(cmd, "10.10.10.10");
|
||||
|
||||
NicVO nic = new NicVO("nic", 1L, 2L, VirtualMachine.Type.User);
|
||||
when(_nicDao.findById(anyLong())).thenReturn(nic);
|
||||
when(_vmDao.findById(anyLong())).thenReturn(_vmMock);
|
||||
|
||||
List<Service> services = new ArrayList<Service>();
|
||||
services.add(Service.Dhcp);
|
||||
when(_networkModel.listNetworkOfferingServices(anyLong())).thenReturn(services);
|
||||
when(_vmMock.getState()).thenReturn(State.Stopped);
|
||||
doNothing().when(_accountMgr).checkAccess(_account, null, true, _vmMock);
|
||||
when(_accountDao.findByIdIncludingRemoved(anyLong())).thenReturn(_accountMock);
|
||||
|
||||
when(_networkDao.findById(anyLong())).thenReturn(_networkMock);
|
||||
when(_networkMock.getDataCenterId()).thenReturn(3L);
|
||||
when(_networkMock.getGuestType()).thenReturn(GuestType.Isolated);
|
||||
when(_dcDao.findById(anyLong())).thenReturn(_dcMock);
|
||||
when(_dcMock.getNetworkType()).thenReturn(NetworkType.Advanced);
|
||||
|
||||
when(_ipAddrMgr.allocateGuestIP(Mockito.eq(_networkMock), anyString())).thenReturn("10.10.10.10");
|
||||
when(_nicDao.persist(any(NicVO.class))).thenReturn(nic);
|
||||
|
||||
Account caller = new AccountVO("testaccount", 1, "networkdomain", (short)0, UUID.randomUUID().toString());
|
||||
UserVO user = new UserVO(1, "testuser", "password", "firstname", "lastName", "email", "timezone", UUID.randomUUID().toString(), User.Source.UNKNOWN);
|
||||
CallContext.register(user, caller);
|
||||
try {
|
||||
_userVmMgr.updateNicIpForVirtualMachine(cmd);
|
||||
} finally {
|
||||
CallContext.unregister();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUpdateVmNicIpSuccess2() throws Exception {
|
||||
UpdateVmNicIpCmd cmd = new UpdateVmNicIpCmd();
|
||||
Class<?> _class = cmd.getClass();
|
||||
|
||||
Field virtualmachineIdField = _class.getDeclaredField("nicId");
|
||||
virtualmachineIdField.setAccessible(true);
|
||||
virtualmachineIdField.set(cmd, 1L);
|
||||
|
||||
Field accountNameField = _class.getDeclaredField("ipAddr");
|
||||
accountNameField.setAccessible(true);
|
||||
accountNameField.set(cmd, "10.10.10.10");
|
||||
|
||||
NicVO nic = new NicVO("nic", 1L, 2L, VirtualMachine.Type.User);
|
||||
when(_nicDao.findById(anyLong())).thenReturn(nic);
|
||||
when(_vmDao.findById(anyLong())).thenReturn(_vmMock);
|
||||
|
||||
List<Service> services = new ArrayList<Service>();
|
||||
when(_networkModel.listNetworkOfferingServices(anyLong())).thenReturn(services);
|
||||
when(_vmMock.getState()).thenReturn(State.Running);
|
||||
doNothing().when(_accountMgr).checkAccess(_account, null, true, _vmMock);
|
||||
when(_accountDao.findByIdIncludingRemoved(anyLong())).thenReturn(_accountMock);
|
||||
|
||||
when(_networkDao.findById(anyLong())).thenReturn(_networkMock);
|
||||
when(_networkMock.getDataCenterId()).thenReturn(3L);
|
||||
when(_networkMock.getGuestType()).thenReturn(GuestType.Shared);
|
||||
when(_dcDao.findById(anyLong())).thenReturn(_dcMock);
|
||||
when(_dcMock.getNetworkType()).thenReturn(NetworkType.Advanced);
|
||||
|
||||
when(_ipAddrMgr.allocatePublicIpForGuestNic(Mockito.eq(_networkMock), anyLong(), Mockito.eq(_accountMock), anyString())).thenReturn("10.10.10.10");
|
||||
when(_ipAddressDao.findByIpAndSourceNetworkId(anyLong(), anyString())).thenReturn(null);
|
||||
when(_nicDao.persist(any(NicVO.class))).thenReturn(nic);
|
||||
|
||||
Account caller = new AccountVO("testaccount", 1, "networkdomain", (short)0, UUID.randomUUID().toString());
|
||||
UserVO user = new UserVO(1, "testuser", "password", "firstname", "lastName", "email", "timezone", UUID.randomUUID().toString(), User.Source.UNKNOWN);
|
||||
CallContext.register(user, caller);
|
||||
try {
|
||||
_userVmMgr.updateNicIpForVirtualMachine(cmd);
|
||||
} finally {
|
||||
CallContext.unregister();
|
||||
}
|
||||
}
|
||||
|
||||
// vm is running in network with dhcp support
|
||||
@Test(expected = InvalidParameterValueException.class)
|
||||
public void testUpdateVmNicIpFailure1() throws Exception {
|
||||
UpdateVmNicIpCmd cmd = new UpdateVmNicIpCmd();
|
||||
Class<?> _class = cmd.getClass();
|
||||
|
||||
Field virtualmachineIdField = _class.getDeclaredField("nicId");
|
||||
virtualmachineIdField.setAccessible(true);
|
||||
virtualmachineIdField.set(cmd, 1L);
|
||||
|
||||
Field accountNameField = _class.getDeclaredField("ipAddr");
|
||||
accountNameField.setAccessible(true);
|
||||
accountNameField.set(cmd, "10.10.10.10");
|
||||
|
||||
NicVO nic = new NicVO("nic", 1L, 2L, VirtualMachine.Type.User);
|
||||
when(_nicDao.findById(anyLong())).thenReturn(nic);
|
||||
when(_vmDao.findById(anyLong())).thenReturn(_vmMock);
|
||||
|
||||
List<Service> services = new ArrayList<Service>();
|
||||
services.add(Service.Dhcp);
|
||||
when(_networkModel.listNetworkOfferingServices(anyLong())).thenReturn(services);
|
||||
when(_vmMock.getState()).thenReturn(State.Running);
|
||||
|
||||
Account caller = new AccountVO("testaccount", 1, "networkdomain", (short)0, UUID.randomUUID().toString());
|
||||
UserVO user = new UserVO(1, "testuser", "password", "firstname", "lastName", "email", "timezone", UUID.randomUUID().toString(), User.Source.UNKNOWN);
|
||||
CallContext.register(user, caller);
|
||||
try {
|
||||
_userVmMgr.updateNicIpForVirtualMachine(cmd);
|
||||
} finally {
|
||||
CallContext.unregister();
|
||||
}
|
||||
}
|
||||
|
||||
// vm is stopped in isolated network in advanced zone
|
||||
@Test(expected = InvalidParameterValueException.class)
|
||||
public void testUpdateVmNicIpFailure2() throws Exception {
|
||||
UpdateVmNicIpCmd cmd = new UpdateVmNicIpCmd();
|
||||
Class<?> _class = cmd.getClass();
|
||||
|
||||
Field virtualmachineIdField = _class.getDeclaredField("nicId");
|
||||
virtualmachineIdField.setAccessible(true);
|
||||
virtualmachineIdField.set(cmd, 1L);
|
||||
|
||||
Field accountNameField = _class.getDeclaredField("ipAddr");
|
||||
accountNameField.setAccessible(true);
|
||||
accountNameField.set(cmd, "10.10.10.10");
|
||||
|
||||
NicVO nic = new NicVO("nic", 1L, 2L, VirtualMachine.Type.User);
|
||||
when(_nicDao.findById(anyLong())).thenReturn(nic);
|
||||
when(_vmDao.findById(anyLong())).thenReturn(_vmMock);
|
||||
|
||||
List<Service> services = new ArrayList<Service>();
|
||||
services.add(Service.Dhcp);
|
||||
when(_networkModel.listNetworkOfferingServices(anyLong())).thenReturn(services);
|
||||
when(_vmMock.getState()).thenReturn(State.Stopped);
|
||||
doNothing().when(_accountMgr).checkAccess(_account, null, true, _vmMock);
|
||||
when(_accountDao.findByIdIncludingRemoved(anyLong())).thenReturn(_accountMock);
|
||||
|
||||
when(_networkDao.findById(anyLong())).thenReturn(_networkMock);
|
||||
when(_networkMock.getDataCenterId()).thenReturn(3L);
|
||||
when(_networkMock.getGuestType()).thenReturn(GuestType.Isolated);
|
||||
when(_dcDao.findById(anyLong())).thenReturn(_dcMock);
|
||||
when(_dcMock.getNetworkType()).thenReturn(NetworkType.Advanced);
|
||||
|
||||
when(_ipAddrMgr.allocateGuestIP(Mockito.eq(_networkMock), anyString())).thenReturn(null);
|
||||
|
||||
Account caller = new AccountVO("testaccount", 1, "networkdomain", (short)0, UUID.randomUUID().toString());
|
||||
UserVO user = new UserVO(1, "testuser", "password", "firstname", "lastName", "email", "timezone", UUID.randomUUID().toString(), User.Source.UNKNOWN);
|
||||
CallContext.register(user, caller);
|
||||
try {
|
||||
_userVmMgr.updateNicIpForVirtualMachine(cmd);
|
||||
} finally {
|
||||
CallContext.unregister();
|
||||
}
|
||||
}
|
||||
|
||||
// vm is stopped in shared network in advanced zone
|
||||
@Test(expected = InvalidParameterValueException.class)
|
||||
public void testUpdateVmNicIpFailure3() throws Exception {
|
||||
UpdateVmNicIpCmd cmd = new UpdateVmNicIpCmd();
|
||||
Class<?> _class = cmd.getClass();
|
||||
|
||||
Field virtualmachineIdField = _class.getDeclaredField("nicId");
|
||||
virtualmachineIdField.setAccessible(true);
|
||||
virtualmachineIdField.set(cmd, 1L);
|
||||
|
||||
Field accountNameField = _class.getDeclaredField("ipAddr");
|
||||
accountNameField.setAccessible(true);
|
||||
accountNameField.set(cmd, "10.10.10.10");
|
||||
|
||||
NicVO nic = new NicVO("nic", 1L, 2L, VirtualMachine.Type.User);
|
||||
when(_nicDao.findById(anyLong())).thenReturn(nic);
|
||||
when(_vmDao.findById(anyLong())).thenReturn(_vmMock);
|
||||
|
||||
List<Service> services = new ArrayList<Service>();
|
||||
services.add(Service.Dhcp);
|
||||
when(_networkModel.listNetworkOfferingServices(anyLong())).thenReturn(services);
|
||||
when(_vmMock.getState()).thenReturn(State.Stopped);
|
||||
doNothing().when(_accountMgr).checkAccess(_account, null, true, _vmMock);
|
||||
when(_accountDao.findByIdIncludingRemoved(anyLong())).thenReturn(_accountMock);
|
||||
|
||||
when(_networkDao.findById(anyLong())).thenReturn(_networkMock);
|
||||
when(_networkMock.getDataCenterId()).thenReturn(3L);
|
||||
when(_networkMock.getGuestType()).thenReturn(GuestType.Shared);
|
||||
when(_dcDao.findById(anyLong())).thenReturn(_dcMock);
|
||||
when(_dcMock.getNetworkType()).thenReturn(NetworkType.Advanced);
|
||||
|
||||
when(_ipAddrMgr.allocatePublicIpForGuestNic(Mockito.eq(_networkMock), anyLong(), Mockito.eq(_accountMock), anyString())).thenReturn(null);
|
||||
|
||||
Account caller = new AccountVO("testaccount", 1, "networkdomain", (short)0, UUID.randomUUID().toString());
|
||||
UserVO user = new UserVO(1, "testuser", "password", "firstname", "lastName", "email", "timezone", UUID.randomUUID().toString(), User.Source.UNKNOWN);
|
||||
CallContext.register(user, caller);
|
||||
try {
|
||||
_userVmMgr.updateNicIpForVirtualMachine(cmd);
|
||||
} finally {
|
||||
CallContext.unregister();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user