1717
1818package com .cloud .vm ;
1919
20+ import static org .mockito .Matchers .any ;
2021import static org .mockito .Matchers .anyBoolean ;
2122import static org .mockito .Matchers .anyFloat ;
2223import static org .mockito .Matchers .anyInt ;
2324import static org .mockito .Matchers .anyLong ;
25+ import static org .mockito .Matchers .anyString ;
2426import static org .mockito .Matchers .eq ;
2527import static org .mockito .Mockito .doNothing ;
2628import static org .mockito .Mockito .doReturn ;
29+ import static org .mockito .Mockito .doThrow ;
2730import static org .mockito .Mockito .when ;
2831
2932import java .lang .reflect .Field ;
3033import java .util .List ;
34+ import java .util .UUID ;
3135
36+ import org .apache .cloudstack .acl .ControlledEntity ;
37+ import org .apache .cloudstack .acl .SecurityChecker .AccessType ;
3238import org .apache .cloudstack .api .ServerApiException ;
39+ import org .apache .cloudstack .api .command .admin .vm .AssignVMCmd ;
3340import org .apache .cloudstack .api .command .user .vm .RestoreVMCmd ;
3441import org .apache .cloudstack .api .command .user .vm .ScaleVMCmd ;
3542import org .junit .Before ;
4451import com .cloud .exception .ConcurrentOperationException ;
4552import com .cloud .exception .InsufficientCapacityException ;
4653import com .cloud .exception .InvalidParameterValueException ;
54+ import com .cloud .exception .PermissionDeniedException ;
4755import com .cloud .exception .ResourceAllocationException ;
4856import com .cloud .exception .ResourceUnavailableException ;
4957import com .cloud .hypervisor .Hypervisor ;
58+ import com .cloud .hypervisor .Hypervisor .HypervisorType ;
5059import com .cloud .offering .ServiceOffering ;
5160import com .cloud .service .ServiceOfferingVO ;
5261import com .cloud .storage .VMTemplateVO ;
5766import com .cloud .storage .dao .VolumeDao ;
5867import com .cloud .user .Account ;
5968import com .cloud .user .AccountManager ;
69+ import com .cloud .user .AccountService ;
6070import com .cloud .user .AccountVO ;
6171import com .cloud .user .UserContext ;
6272import com .cloud .user .UserVO ;
@@ -73,6 +83,7 @@ public class UserVmManagerTest {
7383 @ Mock VolumeManager _storageMgr ;
7484 @ Mock Account _account ;
7585 @ Mock AccountManager _accountMgr ;
86+ @ Mock AccountService _accountService ;
7687 @ Mock ConfigurationManager _configMgr ;
7788 @ Mock CapacityManager _capacityMgr ;
7889 @ Mock AccountDao _accountDao ;
@@ -91,6 +102,7 @@ public class UserVmManagerTest {
91102 @ Mock VMTemplateVO _templateMock ;
92103 @ Mock VolumeVO _volumeMock ;
93104 @ Mock List <VolumeVO > _rootVols ;
105+ @ Mock Account _accountMock2 ;
94106 @ Before
95107 public void setup (){
96108 MockitoAnnotations .initMocks (this );
@@ -102,6 +114,7 @@ public void setup(){
102114 _userVmMgr ._itMgr = _itMgr ;
103115 _userVmMgr .volumeMgr = _storageMgr ;
104116 _userVmMgr ._accountDao = _accountDao ;
117+ _userVmMgr ._accountService = _accountService ;
105118 _userVmMgr ._userDao = _userDao ;
106119 _userVmMgr ._accountMgr = _accountMgr ;
107120 _userVmMgr ._configMgr = _configMgr ;
@@ -370,6 +383,74 @@ private ServiceOfferingVO getSvcoffering(int ramSize){
370383 return serviceOffering ;
371384 }
372385
373-
386+ // Test Move VM b/w accounts where caller is not ROOT/Domain admin
387+ @ Test (expected =InvalidParameterValueException .class )
388+ public void testMoveVmToUser1 () throws Exception {
389+ AssignVMCmd cmd = new AssignVMCmd ();
390+ Class <?> _class = cmd .getClass ();
391+
392+ Field virtualmachineIdField = _class .getDeclaredField ("virtualMachineId" );
393+ virtualmachineIdField .setAccessible (true );
394+ virtualmachineIdField .set (cmd , 1L );
395+
396+ Field accountNameField = _class .getDeclaredField ("accountName" );
397+ accountNameField .setAccessible (true );
398+ accountNameField .set (cmd , "account" );
399+
400+ Field domainIdField = _class .getDeclaredField ("domainId" );
401+ domainIdField .setAccessible (true );
402+ domainIdField .set (cmd , 1L );
403+
404+ // caller is of type 0
405+ Account caller = (Account ) new AccountVO ("testaccount" , 1 , "networkdomain" , (short ) 0 ,
406+ UUID .randomUUID ().toString ());
407+ UserContext .registerContext (1 , caller , null , true );
408+
409+ _userVmMgr .moveVMToUser (cmd );
410+ }
411+
412+
413+ // Test Move VM b/w accounts where caller doesn't have access to the old or new account
414+ @ Test (expected =PermissionDeniedException .class )
415+ public void testMoveVmToUser2 () throws Exception {
416+ AssignVMCmd cmd = new AssignVMCmd ();
417+ Class <?> _class = cmd .getClass ();
418+
419+ Field virtualmachineIdField = _class .getDeclaredField ("virtualMachineId" );
420+ virtualmachineIdField .setAccessible (true );
421+ virtualmachineIdField .set (cmd , 1L );
422+
423+ Field accountNameField = _class .getDeclaredField ("accountName" );
424+ accountNameField .setAccessible (true );
425+ accountNameField .set (cmd , "account" );
426+
427+ Field domainIdField = _class .getDeclaredField ("domainId" );
428+ domainIdField .setAccessible (true );
429+ domainIdField .set (cmd , 1L );
430+
431+ // caller is of type 0
432+ Account caller = (Account ) new AccountVO ("testaccount" , 1 , "networkdomain" , (short ) 1 ,
433+ UUID .randomUUID ().toString ());
434+ UserContext .registerContext (1 , caller , null , true );
435+
436+ Account oldAccount = (Account ) new AccountVO ("testaccount" , 1 , "networkdomain" , (short ) 0 ,
437+ UUID .randomUUID ().toString ());
438+ Account newAccount = (Account ) new AccountVO ("testaccount" , 1 , "networkdomain" , (short ) 1 ,
439+ UUID .randomUUID ().toString ());
440+
441+ UserVmVO vm = new UserVmVO (10L , "test" , "test" , 1L , HypervisorType .Any , 1L , false , false , 1L , 1L ,
442+ 5L , "test" , "test" , 1L );
443+ vm .setState (VirtualMachine .State .Stopped );
444+ when (_vmDao .findById (anyLong ())).thenReturn (vm );
445+
446+ when (_accountService .getActiveAccountById (anyLong ())).thenReturn (oldAccount );
447+
448+ when (_accountService .getActiveAccountByName (anyString (), anyLong ())).thenReturn (newAccount );
449+
450+ doThrow (new PermissionDeniedException ("Access check failed" )).when (_accountMgr ).checkAccess (any (Account .class ), any (AccessType .class ),
451+ any (Boolean .class ), any (ControlledEntity .class ));
452+
453+ _userVmMgr .moveVMToUser (cmd );
454+ }
374455
375456}
0 commit comments