Search in sources :

Example 1 with StoragePoolVO

use of com.cloud.storage.datastore.db.StoragePoolVO in project cosmic by MissionCriticalCloud.

the class VolumeApiServiceImplTest method setup.

@Before
public void setup() throws Exception {
    MockitoAnnotations.initMocks(this);
    _svc._volsDao = _volumeDao;
    _svc._accountMgr = _accountMgr;
    _svc._userVmDao = _userVmDao;
    _svc._storagePoolDao = _storagePoolDao;
    _svc._vmSnapshotDao = _vmSnapshotDao;
    _svc._vmInstanceDao = _vmInstanceDao;
    _svc._jobMgr = _jobMgr;
    _svc.volFactory = _volFactory;
    _svc.volService = volService;
    _svc._dcDao = _dcDao;
    _svc._resourceLimitMgr = _resourceLimitMgr;
    _svc._accountDao = _accountDao;
    _svc._hostDao = _hostDao;
    // mock caller context
    final AccountVO account = new AccountVO("admin", 1L, "networkDomain", Account.ACCOUNT_TYPE_NORMAL, "uuid");
    final AccountVO account2 = new AccountVO("Account2", 2L, "networkDomain", Account.ACCOUNT_TYPE_NORMAL, "uuid");
    final UserVO user = new UserVO(1, "testuser", "password", "firstname", "lastName", "email", "timezone", UUID.randomUUID().toString(), User.Source.UNKNOWN);
    CallContext.register(user, account);
    // mock async context
    final AsyncJobExecutionContext context = new AsyncJobExecutionContext();
    AsyncJobExecutionContext.init(_svc._jobMgr, _joinMapDao);
    final AsyncJobVO job = new AsyncJobVO();
    context.setJob(job);
    AsyncJobExecutionContext.setCurrentExecutionContext(context);
    final TransactionLegacy txn = TransactionLegacy.open("runVolumeDaoImplTest");
    try {
        // volume of running vm id=1
        final VolumeVO volumeOfRunningVm = new VolumeVO("root", 1L, 1L, 1L, 1L, 1L, "root", "root", Storage.ProvisioningType.THIN, 1, null, null, "root", Volume.Type.ROOT);
        when(_svc._volsDao.findById(1L)).thenReturn(volumeOfRunningVm);
        final UserVmVO runningVm = new UserVmVO(1L, "vm", "vm", 1, HypervisorType.XenServer, 1L, false, false, 1L, 1L, 1, 1L, null, "vm", null);
        runningVm.setState(State.Running);
        runningVm.setDataCenterId(1L);
        when(_svc._userVmDao.findById(1L)).thenReturn(runningVm);
        // volume of stopped vm id=2
        final VolumeVO volumeOfStoppedVm = new VolumeVO("root", 1L, 1L, 1L, 1L, 2L, "root", "root", Storage.ProvisioningType.THIN, 1, null, null, "root", Volume.Type.ROOT);
        volumeOfStoppedVm.setPoolId(1L);
        when(_svc._volsDao.findById(2L)).thenReturn(volumeOfStoppedVm);
        final UserVmVO stoppedVm = new UserVmVO(2L, "vm", "vm", 1, HypervisorType.XenServer, 1L, false, false, 1L, 1L, 1, 1L, null, "vm", null);
        stoppedVm.setState(State.Stopped);
        stoppedVm.setDataCenterId(1L);
        when(_svc._userVmDao.findById(2L)).thenReturn(stoppedVm);
        final StoragePoolVO unmanagedPool = new StoragePoolVO();
        when(_svc._storagePoolDao.findById(1L)).thenReturn(unmanagedPool);
        // volume of managed pool id=4
        final StoragePoolVO managedPool = new StoragePoolVO();
        managedPool.setManaged(true);
        when(_svc._storagePoolDao.findById(2L)).thenReturn(managedPool);
        final VolumeVO managedPoolVolume = new VolumeVO("root", 1L, 1L, 1L, 1L, 2L, "root", "root", Storage.ProvisioningType.THIN, 1, null, null, "root", Volume.Type.ROOT);
        managedPoolVolume.setPoolId(2L);
        when(_svc._volsDao.findById(4L)).thenReturn(managedPoolVolume);
        // non-root non-datadisk volume
        final VolumeInfo volumeWithIncorrectVolumeType = Mockito.mock(VolumeInfo.class);
        when(volumeWithIncorrectVolumeType.getId()).thenReturn(5L);
        when(volumeWithIncorrectVolumeType.getVolumeType()).thenReturn(Volume.Type.ISO);
        when(_svc.volFactory.getVolume(5L)).thenReturn(volumeWithIncorrectVolumeType);
        // correct root volume
        final VolumeInfo correctRootVolume = Mockito.mock(VolumeInfo.class);
        when(correctRootVolume.getId()).thenReturn(6L);
        when(correctRootVolume.getDataCenterId()).thenReturn(1L);
        when(correctRootVolume.getVolumeType()).thenReturn(Volume.Type.ROOT);
        when(correctRootVolume.getInstanceId()).thenReturn(null);
        when(_svc.volFactory.getVolume(6L)).thenReturn(correctRootVolume);
        final VolumeVO correctRootVolumeVO = new VolumeVO("root", 1L, 1L, 1L, 1L, 2L, "root", "root", Storage.ProvisioningType.THIN, 1, null, null, "root", Volume.Type.ROOT);
        when(_svc._volsDao.findById(6L)).thenReturn(correctRootVolumeVO);
        // managed root volume
        final VolumeInfo managedVolume = Mockito.mock(VolumeInfo.class);
        when(managedVolume.getId()).thenReturn(7L);
        when(managedVolume.getDataCenterId()).thenReturn(1L);
        when(managedVolume.getVolumeType()).thenReturn(Volume.Type.ROOT);
        when(managedVolume.getInstanceId()).thenReturn(null);
        when(managedVolume.getPoolId()).thenReturn(2L);
        when(_svc.volFactory.getVolume(7L)).thenReturn(managedVolume);
        final VolumeVO managedVolume1 = new VolumeVO("root", 1L, 1L, 1L, 1L, 2L, "root", "root", Storage.ProvisioningType.THIN, 1, null, null, "root", Volume.Type.ROOT);
        managedVolume1.setPoolId(2L);
        managedVolume1.setDataCenterId(1L);
        when(_svc._volsDao.findById(7L)).thenReturn(managedVolume1);
        // vm having root volume
        final UserVmVO vmHavingRootVolume = new UserVmVO(4L, "vm", "vm", 1, HypervisorType.XenServer, 1L, false, false, 1L, 1L, 1, 1L, null, "vm", null);
        vmHavingRootVolume.setState(State.Stopped);
        vmHavingRootVolume.setDataCenterId(1L);
        when(_svc._userVmDao.findById(4L)).thenReturn(vmHavingRootVolume);
        final List<VolumeVO> vols = new ArrayList<>();
        vols.add(new VolumeVO());
        when(_svc._volsDao.findByInstanceAndDeviceId(4L, 0L)).thenReturn(vols);
        // volume in uploaded state
        final VolumeInfo uploadedVolume = Mockito.mock(VolumeInfo.class);
        when(uploadedVolume.getId()).thenReturn(8L);
        when(uploadedVolume.getDataCenterId()).thenReturn(1L);
        when(uploadedVolume.getVolumeType()).thenReturn(Volume.Type.ROOT);
        when(uploadedVolume.getInstanceId()).thenReturn(null);
        when(uploadedVolume.getPoolId()).thenReturn(1L);
        when(uploadedVolume.getState()).thenReturn(Volume.State.Uploaded);
        when(_svc.volFactory.getVolume(8L)).thenReturn(uploadedVolume);
        final VolumeVO upVolume = new VolumeVO("root", 1L, 1L, 1L, 1L, 2L, "root", "root", Storage.ProvisioningType.THIN, 1, null, null, "root", Volume.Type.ROOT);
        upVolume.setPoolId(1L);
        upVolume.setDataCenterId(1L);
        upVolume.setState(Volume.State.Uploaded);
        when(_svc._volsDao.findById(8L)).thenReturn(upVolume);
        // helper dao methods mock
        when(_svc._vmSnapshotDao.findByVm(any(Long.class))).thenReturn(new ArrayList<>());
        when(_svc._vmInstanceDao.findById(any(Long.class))).thenReturn(stoppedVm);
    } finally {
        txn.close("runVolumeDaoImplTest");
    }
    // helper methods mock
    doNothing().when(_svc._accountMgr).checkAccess(any(Account.class), any(AccessType.class), any(Boolean.class), any(ControlledEntity.class));
    doNothing().when(_svc._jobMgr).updateAsyncJobAttachment(any(Long.class), any(String.class), any(Long.class));
    when(_svc._jobMgr.submitAsyncJob(any(AsyncJobVO.class), any(String.class), any(Long.class))).thenReturn(1L);
}
Also used : Account(com.cloud.user.Account) UserVmVO(com.cloud.vm.UserVmVO) AsyncJobExecutionContext(com.cloud.framework.jobs.AsyncJobExecutionContext) ArrayList(java.util.ArrayList) VolumeInfo(com.cloud.engine.subsystem.api.storage.VolumeInfo) AccountVO(com.cloud.user.AccountVO) AsyncJobVO(com.cloud.framework.jobs.impl.AsyncJobVO) TransactionLegacy(com.cloud.utils.db.TransactionLegacy) UserVO(com.cloud.user.UserVO) ControlledEntity(com.cloud.acl.ControlledEntity) StoragePoolVO(com.cloud.storage.datastore.db.StoragePoolVO) Matchers.anyLong(org.mockito.Matchers.anyLong) AccessType(com.cloud.acl.SecurityChecker.AccessType) Before(org.junit.Before)

Example 2 with StoragePoolVO

use of com.cloud.storage.datastore.db.StoragePoolVO in project cosmic by MissionCriticalCloud.

the class StoragePoolMonitorTest method setUp.

@Before
public void setUp() throws Exception {
    storageManager = Mockito.mock(StorageManagerImpl.class);
    poolDao = Mockito.mock(PrimaryDataStoreDao.class);
    storagePoolMonitor = new StoragePoolMonitor(storageManager, poolDao);
    host = new HostVO("some-uuid");
    pool = new StoragePoolVO();
    pool.setScope(ScopeType.CLUSTER);
    pool.setStatus(StoragePoolStatus.Up);
    pool.setId(123L);
    cmd = new StartupRoutingCommand();
    cmd.setHypervisorType(Hypervisor.HypervisorType.KVM);
}
Also used : StoragePoolVO(com.cloud.storage.datastore.db.StoragePoolVO) StartupRoutingCommand(com.cloud.agent.api.StartupRoutingCommand) StorageManagerImpl(com.cloud.storage.StorageManagerImpl) HostVO(com.cloud.host.HostVO) PrimaryDataStoreDao(com.cloud.storage.datastore.db.PrimaryDataStoreDao) Before(org.junit.Before)

Example 3 with StoragePoolVO

use of com.cloud.storage.datastore.db.StoragePoolVO in project cosmic by MissionCriticalCloud.

the class UserVmManagerTest method testRestoreVMF5.

// Test restoreVM on providing new ISO Id, when VM(deployed using ISO) is in running state
@Test
public void testRestoreVMF5() throws ResourceUnavailableException, InsufficientCapacityException, ServerApiException, ConcurrentOperationException, ResourceAllocationException {
    doReturn(VirtualMachine.State.Running).when(_vmMock).getState();
    when(_vmDao.findById(anyLong())).thenReturn(_vmMock);
    when(_volsDao.findByInstanceAndType(314L, Volume.Type.ROOT)).thenReturn(_rootVols);
    doReturn(false).when(_rootVols).isEmpty();
    when(_rootVols.get(eq(0))).thenReturn(_volumeMock);
    doReturn(null).when(_volumeMock).getTemplateId();
    doReturn(3L).when(_vmMock).getIsoId();
    doReturn(ImageFormat.ISO).when(_templateMock).getFormat();
    when(_templateDao.findById(anyLong())).thenReturn(_templateMock);
    doNothing().when(_accountMgr).checkAccess(_account, null, true, _templateMock);
    when(_storageMgr.allocateDuplicateVolume(_volumeMock, null)).thenReturn(_volumeMock);
    doNothing().when(_vmMock).setIsoId(14L);
    when(_templateMock.getGuestOSId()).thenReturn(5L);
    doNothing().when(_vmMock).setGuestOSId(anyLong());
    doNothing().when(_vmMock).setTemplateId(3L);
    when(_vmDao.update(314L, _vmMock)).thenReturn(true);
    when(_storageMgr.allocateDuplicateVolume(_volumeMock, null)).thenReturn(_volumeMock);
    doNothing().when(_volsDao).attachVolume(anyLong(), anyLong(), anyLong());
    when(_volumeMock.getId()).thenReturn(3L);
    doNothing().when(_volsDao).detachVolume(anyLong());
    final List<VMSnapshotVO> mockList = mock(List.class);
    when(_vmSnapshotDao.findByVm(anyLong())).thenReturn(mockList);
    when(mockList.size()).thenReturn(0);
    when(_templateMock.getUuid()).thenReturn("b1a3626e-72e0-4697-8c7c-a110940cc55d");
    final Account account = new AccountVO("testaccount", 1L, "networkdomain", (short) 0, "uuid");
    final UserVO user = new UserVO(1, "testuser", "password", "firstname", "lastName", "email", "timezone", UUID.randomUUID().toString(), User.Source.UNKNOWN);
    final StoragePoolVO storagePool = new StoragePoolVO();
    storagePool.setManaged(false);
    when(_storagePoolDao.findById(anyLong())).thenReturn(storagePool);
    CallContext.register(user, account);
    try {
        _userVmMgr.restoreVMInternal(_account, _vmMock, 14L);
    } finally {
        CallContext.unregister();
    }
    verify(_vmMock, times(1)).setIsoId(14L);
}
Also used : VMSnapshotVO(com.cloud.vm.snapshot.VMSnapshotVO) Account(com.cloud.user.Account) UserVO(com.cloud.user.UserVO) StoragePoolVO(com.cloud.storage.datastore.db.StoragePoolVO) AccountVO(com.cloud.user.AccountVO) Test(org.junit.Test)

Example 4 with StoragePoolVO

use of com.cloud.storage.datastore.db.StoragePoolVO in project cosmic by MissionCriticalCloud.

the class UserVmManagerTest method testRestoreVMF4.

// Test restoreVM on providing new template Id, when VM is in running state
@Test
public void testRestoreVMF4() throws ResourceUnavailableException, InsufficientCapacityException, ServerApiException, ConcurrentOperationException, ResourceAllocationException {
    doReturn(VirtualMachine.State.Running).when(_vmMock).getState();
    when(_vmDao.findById(anyLong())).thenReturn(_vmMock);
    when(_volsDao.findByInstanceAndType(314L, Volume.Type.ROOT)).thenReturn(_rootVols);
    doReturn(false).when(_rootVols).isEmpty();
    when(_rootVols.get(eq(0))).thenReturn(_volumeMock);
    doReturn(3L).when(_volumeMock).getTemplateId();
    doReturn(ImageFormat.VHD).when(_templateMock).getFormat();
    when(_templateDao.findById(anyLong())).thenReturn(_templateMock);
    doNothing().when(_accountMgr).checkAccess(_account, null, true, _templateMock);
    when(_storageMgr.allocateDuplicateVolume(_volumeMock, 14L)).thenReturn(_volumeMock);
    when(_templateMock.getGuestOSId()).thenReturn(5L);
    doNothing().when(_vmMock).setGuestOSId(anyLong());
    doNothing().when(_vmMock).setTemplateId(3L);
    when(_vmDao.update(314L, _vmMock)).thenReturn(true);
    when(_storageMgr.allocateDuplicateVolume(_volumeMock, null)).thenReturn(_volumeMock);
    doNothing().when(_volsDao).attachVolume(anyLong(), anyLong(), anyLong());
    when(_volumeMock.getId()).thenReturn(3L);
    doNothing().when(_volsDao).detachVolume(anyLong());
    final List<VMSnapshotVO> mockList = mock(List.class);
    when(_vmSnapshotDao.findByVm(anyLong())).thenReturn(mockList);
    when(mockList.size()).thenReturn(0);
    when(_templateMock.getUuid()).thenReturn("b1a3626e-72e0-4697-8c7c-a110940cc55d");
    final Account account = new AccountVO("testaccount", 1L, "networkdomain", (short) 0, "uuid");
    final UserVO user = new UserVO(1, "testuser", "password", "firstname", "lastName", "email", "timezone", UUID.randomUUID().toString(), User.Source.UNKNOWN);
    final StoragePoolVO storagePool = new StoragePoolVO();
    storagePool.setManaged(false);
    when(_storagePoolDao.findById(anyLong())).thenReturn(storagePool);
    CallContext.register(user, account);
    try {
        _userVmMgr.restoreVMInternal(_account, _vmMock, 14L);
    } finally {
        CallContext.unregister();
    }
}
Also used : VMSnapshotVO(com.cloud.vm.snapshot.VMSnapshotVO) Account(com.cloud.user.Account) UserVO(com.cloud.user.UserVO) StoragePoolVO(com.cloud.storage.datastore.db.StoragePoolVO) AccountVO(com.cloud.user.AccountVO) Test(org.junit.Test)

Example 5 with StoragePoolVO

use of com.cloud.storage.datastore.db.StoragePoolVO in project cosmic by MissionCriticalCloud.

the class UserVmManagerTest method testRestoreVMF3.

// Test restoreVM when VM is in running state
@Test
public void testRestoreVMF3() throws ResourceUnavailableException, InsufficientCapacityException, ServerApiException, ConcurrentOperationException, ResourceAllocationException {
    doReturn(VirtualMachine.State.Running).when(_vmMock).getState();
    when(_vmDao.findById(anyLong())).thenReturn(_vmMock);
    when(_volsDao.findByInstanceAndType(314L, Volume.Type.ROOT)).thenReturn(_rootVols);
    doReturn(false).when(_rootVols).isEmpty();
    when(_rootVols.get(eq(0))).thenReturn(_volumeMock);
    doReturn(3L).when(_volumeMock).getTemplateId();
    when(_templateDao.findById(anyLong())).thenReturn(_templateMock);
    when(_storageMgr.allocateDuplicateVolume(_volumeMock, null)).thenReturn(_volumeMock);
    doNothing().when(_volsDao).attachVolume(anyLong(), anyLong(), anyLong());
    when(_volumeMock.getId()).thenReturn(3L);
    doNothing().when(_volsDao).detachVolume(anyLong());
    when(_templateMock.getUuid()).thenReturn("e0552266-7060-11e2-bbaa-d55f5db67735");
    final Account account = new AccountVO("testaccount", 1L, "networkdomain", (short) 0, "uuid");
    final UserVO user = new UserVO(1, "testuser", "password", "firstname", "lastName", "email", "timezone", UUID.randomUUID().toString(), User.Source.UNKNOWN);
    final StoragePoolVO storagePool = new StoragePoolVO();
    storagePool.setManaged(false);
    when(_storagePoolDao.findById(anyLong())).thenReturn(storagePool);
    CallContext.register(user, account);
    try {
        _userVmMgr.restoreVMInternal(_account, _vmMock, null);
    } finally {
        CallContext.unregister();
    }
}
Also used : Account(com.cloud.user.Account) UserVO(com.cloud.user.UserVO) StoragePoolVO(com.cloud.storage.datastore.db.StoragePoolVO) AccountVO(com.cloud.user.AccountVO) Test(org.junit.Test)

Aggregations

StoragePoolVO (com.cloud.storage.datastore.db.StoragePoolVO)86 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)29 InvalidParameterValueException (com.cloud.utils.exception.InvalidParameterValueException)22 Test (org.junit.Test)18 HostVO (com.cloud.host.HostVO)15 VolumeVO (com.cloud.storage.VolumeVO)15 ArrayList (java.util.ArrayList)15 Answer (com.cloud.agent.api.Answer)14 Account (com.cloud.user.Account)13 DataStore (com.cloud.engine.subsystem.api.storage.DataStore)12 StorageUnavailableException (com.cloud.exception.StorageUnavailableException)11 StoragePool (com.cloud.storage.StoragePool)10 AttachAnswer (com.cloud.storage.command.AttachAnswer)10 VolumeInfo (com.cloud.engine.subsystem.api.storage.VolumeInfo)9 VMInstanceVO (com.cloud.vm.VMInstanceVO)9 RebootAnswer (com.cloud.agent.api.RebootAnswer)8 PrimaryDataStore (com.cloud.engine.subsystem.api.storage.PrimaryDataStore)8 ConcurrentOperationException (com.cloud.exception.ConcurrentOperationException)8 PermissionDeniedException (com.cloud.exception.PermissionDeniedException)8 VMTemplateStoragePoolVO (com.cloud.storage.VMTemplateStoragePoolVO)8