cloudstack-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ko...@apache.org
Subject [1/2] git commit: updated refs/heads/master to be8c6fe
Date Mon, 21 Apr 2014 08:26:22 GMT
Repository: cloudstack
Updated Branches:
  refs/heads/master bcc640d13 -> be8c6fe62


test for AccountManagerImpl

- new test for disableUser
- unused code removed
- A redundant if branch removed - all branches are doing the same

Signed-off-by: Laszlo Hornyak <laszlo.hornyak@gmail.com>


Project: http://git-wip-us.apache.org/repos/asf/cloudstack/repo
Commit: http://git-wip-us.apache.org/repos/asf/cloudstack/commit/be8c6fe6
Tree: http://git-wip-us.apache.org/repos/asf/cloudstack/tree/be8c6fe6
Diff: http://git-wip-us.apache.org/repos/asf/cloudstack/diff/be8c6fe6

Branch: refs/heads/master
Commit: be8c6fe626ae933326224a96d3d9b01161871603
Parents: 54cfc2c
Author: Laszlo Hornyak <laszlo.hornyak@gmail.com>
Authored: Sun Apr 20 23:54:16 2014 +0200
Committer: Laszlo Hornyak <laszlo.hornyak@gmail.com>
Committed: Mon Apr 21 10:25:16 2014 +0200

----------------------------------------------------------------------
 .../src/com/cloud/user/AccountManagerImpl.java  |  31 +--
 .../com/cloud/user/AccountManagerImplTest.java  | 260 +++++++++++++++++++
 2 files changed, 272 insertions(+), 19 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cloudstack/blob/be8c6fe6/server/src/com/cloud/user/AccountManagerImpl.java
----------------------------------------------------------------------
diff --git a/server/src/com/cloud/user/AccountManagerImpl.java b/server/src/com/cloud/user/AccountManagerImpl.java
index 32ff44a..c6131b9 100755
--- a/server/src/com/cloud/user/AccountManagerImpl.java
+++ b/server/src/com/cloud/user/AccountManagerImpl.java
@@ -150,9 +150,7 @@ import com.cloud.vm.ReservationContextImpl;
 import com.cloud.vm.UserVmManager;
 import com.cloud.vm.UserVmVO;
 import com.cloud.vm.VMInstanceVO;
-import com.cloud.vm.VirtualMachine.Type;
 import com.cloud.vm.VirtualMachineManager;
-import com.cloud.vm.dao.DomainRouterDao;
 import com.cloud.vm.dao.InstanceGroupDao;
 import com.cloud.vm.dao.UserVmDao;
 import com.cloud.vm.dao.VMInstanceDao;
@@ -228,8 +226,6 @@ public class AccountManagerImpl extends ManagerBase implements AccountManager,
M
     @Inject
     private VpcManager _vpcMgr;
     @Inject
-    private DomainRouterDao _routerDao;
-    @Inject
     Site2SiteVpnManager _vpnMgr;
     @Inject
     private AutoScaleManager _autoscaleMgr;
@@ -455,9 +451,9 @@ public class AccountManagerImpl extends ManagerBase implements AccountManager,
M
     public void checkAccess(Account caller, AccessType accessType, String apiName, ControlledEntity...
entities) throws PermissionDeniedException {
         boolean granted = false;
         // construct entities identification string
-        StringBuffer entityBuf = new StringBuffer("{");
+        StringBuilder entityBuf = new StringBuilder("{");
         for (ControlledEntity ent : entities) {
-            entityBuf.append(ent.toString());
+            entityBuf.append(ent);
         }
         entityBuf.append("}");
         String entityStr = entityBuf.toString();
@@ -732,8 +728,10 @@ public class AccountManagerImpl extends ManagerBase implements AccountManager,
M
                 for (IpAddress ip : ipsToRelease) {
                     s_logger.debug("Releasing ip " + ip + " as a part of account id=" + accountId
+ " cleanup");
                     if (!_ipAddrMgr.disassociatePublicIpAddress(ip.getId(), callerUserId,
caller)) {
-                    s_logger.warn("Failed to release ip address " + ip + " as a part of account
id=" + accountId + " clenaup");
-                    accountCleanupNeeded = true;
+                        s_logger.warn("Failed to release ip address " + ip
+                                + " as a part of account id=" + accountId
+                                + " clenaup");
+                        accountCleanupNeeded = true;
                     }
                 }
             }
@@ -777,7 +775,7 @@ public class AccountManagerImpl extends ManagerBase implements AccountManager,
M
                 if (ip.isPortable()) {
                 s_logger.debug("Releasing portable ip " + ip + " as a part of account id="
+ accountId + " cleanup");
                 _ipAddrMgr.releasePortableIpAddress(ip.getId());
-            }
+                }
             }
 
             // release dedication if any
@@ -861,15 +859,11 @@ public class AccountManagerImpl extends ManagerBase implements AccountManager,
M
         for (VMInstanceVO vm : vms) {
             try {
                 try {
-                    if (vm.getType() == Type.User) {
-                        _itMgr.advanceStop(vm.getUuid(), false);
-                    } else if (vm.getType() == Type.DomainRouter) {
-                        _itMgr.advanceStop(vm.getUuid(), false);
-                    } else {
-                        _itMgr.advanceStop(vm.getUuid(), false);
-                    }
+                    _itMgr.advanceStop(vm.getUuid(), false);
                 } catch (OperationTimedoutException ote) {
-                    s_logger.warn("Operation for stopping vm timed out, unable to stop vm
" + vm.getHostName(), ote);
+                    s_logger.warn(
+                            "Operation for stopping vm timed out, unable to stop vm "
+                                    + vm.getHostName(), ote);
                     success = false;
                 }
             } catch (AgentUnavailableException aue) {
@@ -1298,8 +1292,7 @@ public class AccountManagerImpl extends ManagerBase implements AccountManager,
M
     @Override
     @ActionEvent(eventType = EventTypes.EVENT_ACCOUNT_DELETE, eventDescription = "deleting
account", async = true)
     // This method deletes the account
-        public
-        boolean deleteUserAccount(long accountId) {
+    public boolean deleteUserAccount(long accountId) {
 
         CallContext ctx = CallContext.current();
         long callerUserId = ctx.getCallingUserId();

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/be8c6fe6/server/test/com/cloud/user/AccountManagerImplTest.java
----------------------------------------------------------------------
diff --git a/server/test/com/cloud/user/AccountManagerImplTest.java b/server/test/com/cloud/user/AccountManagerImplTest.java
new file mode 100644
index 0000000..e0b7b4b
--- /dev/null
+++ b/server/test/com/cloud/user/AccountManagerImplTest.java
@@ -0,0 +1,260 @@
+// 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.user;
+
+import java.lang.reflect.Field;
+import java.util.Arrays;
+
+import javax.inject.Inject;
+
+import org.apache.cloudstack.acl.ControlledEntity;
+import org.apache.cloudstack.acl.SecurityChecker;
+import org.apache.cloudstack.acl.SecurityChecker.AccessType;
+import org.apache.cloudstack.affinity.dao.AffinityGroupDao;
+import org.apache.cloudstack.context.CallContext;
+import org.apache.cloudstack.engine.orchestration.service.NetworkOrchestrationService;
+import org.apache.cloudstack.framework.config.dao.ConfigurationDao;
+import org.apache.cloudstack.framework.messagebus.MessageBus;
+import org.apache.cloudstack.region.gslb.GlobalLoadBalancerRuleDao;
+import org.junit.After;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.Mockito;
+import org.mockito.runners.MockitoJUnitRunner;
+
+import com.cloud.configuration.ConfigurationManager;
+import com.cloud.configuration.dao.ResourceCountDao;
+import com.cloud.configuration.dao.ResourceLimitDao;
+import com.cloud.dc.dao.DataCenterDao;
+import com.cloud.dc.dao.DataCenterVnetDao;
+import com.cloud.dc.dao.DedicatedResourceDao;
+import com.cloud.domain.dao.DomainDao;
+import com.cloud.exception.ConcurrentOperationException;
+import com.cloud.exception.ResourceUnavailableException;
+import com.cloud.network.as.AutoScaleManager;
+import com.cloud.network.dao.AccountGuestVlanMapDao;
+import com.cloud.network.dao.IPAddressDao;
+import com.cloud.network.dao.NetworkDao;
+import com.cloud.network.dao.RemoteAccessVpnDao;
+import com.cloud.network.dao.VpnUserDao;
+import com.cloud.network.security.SecurityGroupManager;
+import com.cloud.network.security.dao.SecurityGroupDao;
+import com.cloud.network.vpc.VpcManager;
+import com.cloud.network.vpn.RemoteAccessVpnService;
+import com.cloud.network.vpn.Site2SiteVpnManager;
+import com.cloud.projects.ProjectManager;
+import com.cloud.projects.dao.ProjectAccountDao;
+import com.cloud.projects.dao.ProjectDao;
+import com.cloud.storage.VolumeApiService;
+import com.cloud.storage.dao.SnapshotDao;
+import com.cloud.storage.dao.VMTemplateDao;
+import com.cloud.storage.dao.VolumeDao;
+import com.cloud.storage.snapshot.SnapshotManager;
+import com.cloud.template.TemplateManager;
+import com.cloud.user.Account.State;
+import com.cloud.user.dao.AccountDao;
+import com.cloud.user.dao.UserAccountDao;
+import com.cloud.user.dao.UserDao;
+import com.cloud.vm.UserVmManager;
+import com.cloud.vm.VMInstanceVO;
+import com.cloud.vm.VirtualMachineManager;
+import com.cloud.vm.dao.DomainRouterDao;
+import com.cloud.vm.dao.InstanceGroupDao;
+import com.cloud.vm.dao.UserVmDao;
+import com.cloud.vm.dao.VMInstanceDao;
+
+@RunWith(MockitoJUnitRunner.class)
+public class AccountManagerImplTest {
+    @Mock
+    AccountDao _accountDao;
+    @Mock
+    ConfigurationDao _configDao;
+    @Mock
+    ResourceCountDao _resourceCountDao;
+    @Mock
+    UserDao _userDao;
+    @Mock
+    InstanceGroupDao _vmGroupDao;
+    @Mock
+    UserAccountDao _userAccountDao;
+    @Mock
+    VolumeDao _volumeDao;
+    @Mock
+    UserVmDao _userVmDao;
+    @Mock
+    VMTemplateDao _templateDao;
+    @Mock
+    NetworkDao _networkDao;
+    @Mock
+    SecurityGroupDao _securityGroupDao;
+    @Mock
+    VMInstanceDao _vmDao;
+    @Mock
+    protected SnapshotDao _snapshotDao;
+    @Mock
+    protected VMTemplateDao _vmTemplateDao;
+    @Mock
+    SecurityGroupManager _networkGroupMgr;
+    @Mock
+    NetworkOrchestrationService _networkMgr;
+    @Mock
+    SnapshotManager _snapMgr;
+    @Mock
+    UserVmManager _vmMgr;
+    @Mock
+    TemplateManager _tmpltMgr;
+    @Mock
+    ConfigurationManager _configMgr;
+    @Mock
+    VirtualMachineManager _itMgr;
+    @Mock
+    RemoteAccessVpnDao _remoteAccessVpnDao;
+    @Mock
+    RemoteAccessVpnService _remoteAccessVpnMgr;
+    @Mock
+    VpnUserDao _vpnUser;
+    @Mock
+    DataCenterDao _dcDao;
+    @Mock
+    DomainManager _domainMgr;
+    @Mock
+    ProjectManager _projectMgr;
+    @Mock
+    ProjectDao _projectDao;
+    @Mock
+    AccountDetailsDao _accountDetailsDao;
+    @Mock
+    DomainDao _domainDao;
+    @Mock
+    ProjectAccountDao _projectAccountDao;
+    @Mock
+    IPAddressDao _ipAddressDao;
+    @Mock
+    VpcManager _vpcMgr;
+    @Mock
+    DomainRouterDao _routerDao;
+    @Mock
+    Site2SiteVpnManager _vpnMgr;
+    @Mock
+    AutoScaleManager _autoscaleMgr;
+    @Mock
+    VolumeApiService volumeService;
+    @Mock
+    AffinityGroupDao _affinityGroupDao;
+    @Mock
+    AccountGuestVlanMapDao _accountGuestVlanMapDao;
+    @Mock
+    DataCenterVnetDao _dataCenterVnetDao;
+    @Mock
+    ResourceLimitService _resourceLimitMgr;
+    @Mock
+    ResourceLimitDao _resourceLimitDao;
+    @Mock
+    DedicatedResourceDao _dedicatedDao;
+    @Mock
+    GlobalLoadBalancerRuleDao _gslbRuleDao;
+    @Mock
+    MessageBus _messageBus;
+
+    @Mock
+    User callingUser;
+    @Mock
+    Account callingAccount;
+
+    AccountManagerImpl accountManager;
+
+    @Mock
+    SecurityChecker securityChecker;
+
+    @Before
+    public void setup() throws NoSuchFieldException, SecurityException,
+            IllegalArgumentException, IllegalAccessException {
+        accountManager = new AccountManagerImpl();
+        for (Field field : AccountManagerImpl.class.getDeclaredFields()) {
+            if (field.getAnnotation(Inject.class) != null) {
+                field.setAccessible(true);
+                try {
+                    Field mockField = this.getClass().getDeclaredField(
+                            field.getName());
+                    field.set(accountManager, mockField.get(this));
+                } catch (Exception e) {
+                    // ignore missing fields
+                }
+            }
+        }
+        accountManager.setSecurityCheckers(Arrays.asList(securityChecker));
+        CallContext.register(callingUser, callingAccount);
+    }
+
+    @After
+    public void cleanup() {
+        CallContext.unregister();
+    }
+
+    @Test
+    public void disableAccountNotexisting()
+            throws ConcurrentOperationException, ResourceUnavailableException {
+        Mockito.when(_accountDao.findById(42l)).thenReturn(null);
+        Assert.assertTrue(accountManager.disableAccount(42));
+    }
+
+    @Test
+    public void disableAccountDisabled() throws ConcurrentOperationException,
+            ResourceUnavailableException {
+        AccountVO disabledAccount = new AccountVO();
+        disabledAccount.setState(State.disabled);
+        Mockito.when(_accountDao.findById(42l)).thenReturn(disabledAccount);
+        Assert.assertTrue(accountManager.disableAccount(42));
+    }
+
+    @Test
+    public void disableAccount() throws ConcurrentOperationException,
+            ResourceUnavailableException {
+        AccountVO account = new AccountVO();
+        account.setState(State.enabled);
+        Mockito.when(_accountDao.findById(42l)).thenReturn(account);
+        Mockito.when(_accountDao.createForUpdate()).thenReturn(new AccountVO());
+        Mockito.when(
+                _accountDao.update(Mockito.eq(42l),
+                        Mockito.any(AccountVO.class))).thenReturn(true);
+        Mockito.when(_vmDao.listByAccountId(42l)).thenReturn(
+                Arrays.asList(Mockito.mock(VMInstanceVO.class)));
+        Assert.assertTrue(accountManager.disableAccount(42));
+        Mockito.verify(_accountDao, Mockito.atLeastOnce()).update(
+                Mockito.eq(42l), Mockito.any(AccountVO.class));
+    }
+
+    @Test
+    public void deleteAccount() {
+        AccountVO account = new AccountVO();
+        account.setId(42l);
+        Mockito.when(_accountDao.findById(42l)).thenReturn(account);
+        Mockito.when(
+                securityChecker.checkAccess(Mockito.any(Account.class),
+                        Mockito.any(AccessType.class), Mockito.anyString(),
+                        Mockito.any(ControlledEntity[].class)))
+                .thenReturn(true);
+        Mockito.when(_accountDao.remove(42l)).thenReturn(true);
+        Mockito.when(_configMgr.releaseAccountSpecificVirtualRanges(42l)).thenReturn(true);
+        Assert.assertTrue(accountManager.deleteUserAccount(42));
+        //assert that this was a clean delete
+        Mockito.verify(_accountDao, Mockito.never()).markForCleanup(Mockito.eq(42l));
+    }
+}


Mime
View raw message