Return-Path: X-Original-To: archive-asf-public-internal@cust-asf2.ponee.io Delivered-To: archive-asf-public-internal@cust-asf2.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by cust-asf2.ponee.io (Postfix) with ESMTP id AE1842009F3 for ; Fri, 20 May 2016 14:35:21 +0200 (CEST) Received: by cust-asf.ponee.io (Postfix) id ACDB21609AF; Fri, 20 May 2016 12:35:21 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id CDF63160A0E for ; Fri, 20 May 2016 14:35:20 +0200 (CEST) Received: (qmail 41565 invoked by uid 500); 20 May 2016 12:35:19 -0000 Mailing-List: contact commits-help@cloudstack.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@cloudstack.apache.org Delivered-To: mailing list commits@cloudstack.apache.org Received: (qmail 41537 invoked by uid 99); 20 May 2016 12:35:19 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 20 May 2016 12:35:19 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id C4DB7E0159; Fri, 20 May 2016 12:35:19 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: swill@apache.org To: commits@cloudstack.apache.org Date: Fri, 20 May 2016 12:35:20 -0000 Message-Id: <84b5bdc58d2e423fa7829e30a7b4dc40@git.apache.org> In-Reply-To: <248f5da249d5456e9d4b4009a26f5211@git.apache.org> References: <248f5da249d5456e9d4b4009a26f5211@git.apache.org> X-Mailer: ASF-Git Admin Mailer Subject: [2/3] git commit: updated refs/heads/master to cb55624 archived-at: Fri, 20 May 2016 12:35:21 -0000 CLOUDSTACK-9365 : updateVirtualMachine with userdata should not error when a VM is attached to multiple networks from which one or more doesn't support userdata Project: http://git-wip-us.apache.org/repos/asf/cloudstack/repo Commit: http://git-wip-us.apache.org/repos/asf/cloudstack/commit/71c9c90e Tree: http://git-wip-us.apache.org/repos/asf/cloudstack/tree/71c9c90e Diff: http://git-wip-us.apache.org/repos/asf/cloudstack/diff/71c9c90e Branch: refs/heads/master Commit: 71c9c90e0c81ea9e0f90ab71d3430d62cd8fa02e Parents: dc5b529 Author: Nick Livens Authored: Mon May 2 13:16:14 2016 +0200 Committer: Nick Livens Committed: Wed May 11 07:59:38 2016 +0200 ---------------------------------------------------------------------- server/src/com/cloud/vm/UserVmManagerImpl.java | 38 ++++++++------ server/test/com/cloud/vm/UserVmManagerTest.java | 52 ++++++++++++++++++++ 2 files changed, 76 insertions(+), 14 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/cloudstack/blob/71c9c90e/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 cc007d9..83ee248 100644 --- a/server/src/com/cloud/vm/UserVmManagerImpl.java +++ b/server/src/com/cloud/vm/UserVmManagerImpl.java @@ -2492,24 +2492,34 @@ public class UserVmManagerImpl extends ManagerBase implements UserVmManager, Vir return false; } + boolean userDataApplied = false; for (Nic nic : nics) { - Network network = _networkDao.findById(nic.getNetworkId()); - NicProfile nicProfile = new NicProfile(nic, network, null, null, null, _networkModel.isSecurityGroupSupportedInNetwork(network), _networkModel.getNetworkTag( - template.getHypervisorType(), network)); + userDataApplied |= applyUserData(template.getHypervisorType(), vm, nic); + } + return userDataApplied; + } - VirtualMachineProfile vmProfile = new VirtualMachineProfileImpl(vm); + protected boolean applyUserData(HypervisorType hyperVisorType, UserVm vm, Nic nic) throws ResourceUnavailableException, InsufficientCapacityException { + Network network = _networkDao.findById(nic.getNetworkId()); + NicProfile nicProfile = new NicProfile(nic, network, null, null, null, _networkModel.isSecurityGroupSupportedInNetwork(network), _networkModel.getNetworkTag( + hyperVisorType, network)); + VirtualMachineProfile vmProfile = new VirtualMachineProfileImpl(vm); - UserDataServiceProvider element = _networkModel.getUserDataUpdateProvider(network); - if (element == null) { - throw new CloudRuntimeException("Can't find network element for " + Service.UserData.getName() + " provider needed for UserData update"); - } - boolean result = element.saveUserData(network, nicProfile, vmProfile); - if (!result) { - s_logger.error("Failed to update userdata for vm " + vm + " and nic " + nic); - } + if (_networkModel.areServicesSupportedByNetworkOffering(network.getNetworkOfferingId(), Service.UserData)) { + UserDataServiceProvider element = _networkModel.getUserDataUpdateProvider(network); + if (element == null) { + throw new CloudRuntimeException("Can't find network element for " + Service.UserData.getName() + " provider needed for UserData update"); + } + boolean result = element.saveUserData(network, nicProfile, vmProfile); + if (!result) { + s_logger.error("Failed to update userdata for vm " + vm + " and nic " + nic); + } else { + return true; + } + } else { + s_logger.debug("Not applying userdata for nic id=" + nic.getId() + " in vm id=" + vmProfile.getId() + " because it is not supported in network id=" + network.getId()); } - - return true; + return false; } @Override http://git-wip-us.apache.org/repos/asf/cloudstack/blob/71c9c90e/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 637a309..9df3f68 100644 --- a/server/test/com/cloud/vm/UserVmManagerTest.java +++ b/server/test/com/cloud/vm/UserVmManagerTest.java @@ -17,6 +17,8 @@ package com.cloud.vm; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; import static org.mockito.Matchers.any; import static org.mockito.Matchers.anyBoolean; import static org.mockito.Matchers.anyFloat; @@ -37,9 +39,11 @@ import java.util.ArrayList; import java.util.List; import java.util.UUID; +import com.cloud.network.element.UserDataServiceProvider; import com.cloud.storage.Storage; import com.cloud.user.User; import com.cloud.event.dao.UsageEventDao; +import com.cloud.uservm.UserVm; import org.junit.Before; import org.junit.Test; import org.mockito.Mock; @@ -930,6 +934,54 @@ public class UserVmManagerTest { } @Test + public void testApplyUserDataInNetworkWithoutUserDataSupport() throws Exception { + UserVm userVm = mock(UserVm.class); + when(userVm.getId()).thenReturn(1L); + + when(_nicMock.getNetworkId()).thenReturn(2L); + when(_networkMock.getNetworkOfferingId()).thenReturn(3L); + when(_networkDao.findById(2L)).thenReturn(_networkMock); + + // No userdata support + assertFalse(_userVmMgr.applyUserData(HypervisorType.KVM, userVm, _nicMock)); + } + + @Test(expected = CloudRuntimeException.class) + public void testApplyUserDataInNetworkWithoutElement() throws Exception { + UserVm userVm = mock(UserVm.class); + when(userVm.getId()).thenReturn(1L); + + when(_nicMock.getNetworkId()).thenReturn(2L); + when(_networkMock.getNetworkOfferingId()).thenReturn(3L); + when(_networkDao.findById(2L)).thenReturn(_networkMock); + + UserDataServiceProvider userDataServiceProvider = mock(UserDataServiceProvider.class); + when(userDataServiceProvider.saveUserData(any(Network.class), any(NicProfile.class), any(VirtualMachineProfile.class))).thenReturn(true); + + // Userdata support, but no implementing element + when(_networkModel.areServicesSupportedByNetworkOffering(3L, Service.UserData)).thenReturn(true); + _userVmMgr.applyUserData(HypervisorType.KVM, userVm, _nicMock); + } + + @Test + public void testApplyUserDataSuccessful() throws Exception { + UserVm userVm = mock(UserVm.class); + when(userVm.getId()).thenReturn(1L); + + when(_nicMock.getNetworkId()).thenReturn(2L); + when(_networkMock.getNetworkOfferingId()).thenReturn(3L); + when(_networkDao.findById(2L)).thenReturn(_networkMock); + + UserDataServiceProvider userDataServiceProvider = mock(UserDataServiceProvider.class); + when(userDataServiceProvider.saveUserData(any(Network.class), any(NicProfile.class), any(VirtualMachineProfile.class))).thenReturn(true); + + // Userdata support with implementing element + when(_networkModel.areServicesSupportedByNetworkOffering(3L, Service.UserData)).thenReturn(true); + when(_networkModel.getUserDataUpdateProvider(_networkMock)).thenReturn(userDataServiceProvider); + assertTrue(_userVmMgr.applyUserData(HypervisorType.KVM, userVm, _nicMock)); + } + + @Test public void testPersistDeviceBusInfoWithNullController() { when(_vmMock.getDetail(any(String.class))).thenReturn(null); _userVmMgr.persistDeviceBusInfo(_vmMock, null);