You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cloudstack.apache.org by da...@apache.org on 2015/12/06 19:38:14 UTC
[2/4] git commit: updated refs/heads/master to bbe891b
CLOUDSTACK-9051: add unit tests for UpdateVmNicIp
Project: http://git-wip-us.apache.org/repos/asf/cloudstack/repo
Commit: http://git-wip-us.apache.org/repos/asf/cloudstack/commit/c01c73e4
Tree: http://git-wip-us.apache.org/repos/asf/cloudstack/tree/c01c73e4
Diff: http://git-wip-us.apache.org/repos/asf/cloudstack/diff/c01c73e4
Branch: refs/heads/master
Commit: c01c73e44dd98db317b4e2ec5f5ece4fbfc8c26d
Parents: b79d338
Author: Wei Zhou <w....@tech.leaseweb.com>
Authored: Thu Nov 19 11:00:39 2015 +0100
Committer: Wei Zhou <w....@tech.leaseweb.com>
Committed: Mon Nov 30 09:20:26 2015 +0100
----------------------------------------------------------------------
.../api/command/test/UpdateVmNicIpTest.java | 91 +++++++
server/src/com/cloud/vm/UserVmManagerImpl.java | 7 +-
server/test/com/cloud/vm/UserVmManagerTest.java | 245 +++++++++++++++++++
3 files changed, 338 insertions(+), 5 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/cloudstack/blob/c01c73e4/api/test/org/apache/cloudstack/api/command/test/UpdateVmNicIpTest.java
----------------------------------------------------------------------
diff --git a/api/test/org/apache/cloudstack/api/command/test/UpdateVmNicIpTest.java b/api/test/org/apache/cloudstack/api/command/test/UpdateVmNicIpTest.java
new file mode 100644
index 0000000..9a42aa1
--- /dev/null
+++ b/api/test/org/apache/cloudstack/api/command/test/UpdateVmNicIpTest.java
@@ -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());
+ }
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/cloudstack/blob/c01c73e4/server/src/com/cloud/vm/UserVmManagerImpl.java
----------------------------------------------------------------------
diff --git a/server/src/com/cloud/vm/UserVmManagerImpl.java b/server/src/com/cloud/vm/UserVmManagerImpl.java
index 02c58b1..27c0328 100644
--- a/server/src/com/cloud/vm/UserVmManagerImpl.java
+++ b/server/src/com/cloud/vm/UserVmManagerImpl.java
@@ -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());
}
});
}
http://git-wip-us.apache.org/repos/asf/cloudstack/blob/c01c73e4/server/test/com/cloud/vm/UserVmManagerTest.java
----------------------------------------------------------------------
diff --git a/server/test/com/cloud/vm/UserVmManagerTest.java b/server/test/com/cloud/vm/UserVmManagerTest.java
index 17054c2..a94b765 100644
--- a/server/test/com/cloud/vm/UserVmManagerTest.java
+++ b/server/test/com/cloud/vm/UserVmManagerTest.java
@@ -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();
+ }
+ }
}