blob: d76ff272830cc0b3d7499fe695139e9f28f3cf09 [file] [log] [blame]
/*
* 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.storage.motion;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.MockitoAnnotations.initMocks;
import java.util.HashMap;
import java.util.Map;
import org.apache.cloudstack.engine.subsystem.api.storage.DataObject;
import org.apache.cloudstack.engine.subsystem.api.storage.DataStore;
import org.apache.cloudstack.engine.subsystem.api.storage.PrimaryDataStore;
import org.apache.cloudstack.engine.subsystem.api.storage.StrategyPriority;
import org.apache.cloudstack.engine.subsystem.api.storage.VolumeInfo;
import org.apache.cloudstack.storage.datastore.PrimaryDataStoreImpl;
import org.apache.cloudstack.storage.datastore.db.PrimaryDataStoreDao;
import org.apache.cloudstack.storage.datastore.db.StoragePoolVO;
import org.apache.cloudstack.storage.image.store.ImageStoreImpl;
import org.apache.cloudstack.storage.volume.VolumeObject;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.Spy;
import org.mockito.runners.MockitoJUnitRunner;
import com.cloud.agent.api.MigrateCommand;
import com.cloud.agent.api.to.VirtualMachineTO;
import com.cloud.host.HostVO;
import com.cloud.storage.DataStoreRole;
import com.cloud.storage.ImageStore;
import com.cloud.storage.Storage;
import com.cloud.storage.Volume;
import com.cloud.storage.Storage.StoragePoolType;
import com.cloud.storage.VolumeVO;
@RunWith(MockitoJUnitRunner.class)
public class StorageSystemDataMotionStrategyTest {
@Spy
@InjectMocks
private StorageSystemDataMotionStrategy storageSystemDataMotionStrategy;
@Mock
private VolumeObject volumeObjectSource;
@Mock
private DataObject dataObjectDestination;
@Mock
private PrimaryDataStore primaryDataStoreSourceStore;
@Mock
private ImageStore destinationStore;
@Mock
private PrimaryDataStoreDao primaryDataStoreDao;
@Before
public void setUp() throws Exception {
primaryDataStoreSourceStore = mock(PrimaryDataStoreImpl.class);
destinationStore = mock(ImageStoreImpl.class);
volumeObjectSource = mock(VolumeObject.class);
dataObjectDestination = mock(VolumeObject.class);
initMocks(storageSystemDataMotionStrategy);
}
@Test
public void cantHandleSecondary() {
doReturn(primaryDataStoreSourceStore).when(volumeObjectSource).getDataStore();
doReturn(DataStoreRole.Primary).when(primaryDataStoreSourceStore).getRole();
doReturn(destinationStore).when(dataObjectDestination).getDataStore();
doReturn(DataStoreRole.Image).when((DataStore)destinationStore).getRole();
doReturn(primaryDataStoreSourceStore).when(volumeObjectSource).getDataStore();
doReturn(destinationStore).when(dataObjectDestination).getDataStore();
StoragePoolVO storeVO = new StoragePoolVO();
doReturn(storeVO).when(primaryDataStoreDao).findById(0l);
assertTrue(storageSystemDataMotionStrategy.canHandle(volumeObjectSource, dataObjectDestination) == StrategyPriority.CANT_HANDLE);
}
@Test
public void internalCanHandleTestAllStoragePoolsAreManaged() {
configureAndTestInternalCanHandle(true, true, StrategyPriority.HIGHEST);
}
@Test
public void internalCanHandleTestFirstStoragePoolsIsManaged() {
configureAndTestInternalCanHandle(false, true, StrategyPriority.HIGHEST);
}
@Test
public void internalCanHandleTestSecondStoragePoolsIsManaged() {
configureAndTestInternalCanHandle(true, false, StrategyPriority.HIGHEST);
}
@Test
public void internalCanHandleTestNoStoragePoolsIsManaged() {
configureAndTestInternalCanHandle(false, false, StrategyPriority.CANT_HANDLE);
}
private void configureAndTestInternalCanHandle(boolean sPool0IsManaged, boolean sPool1IsManaged, StrategyPriority expectedStrategyPriority) {
VolumeObject volumeInfo = Mockito.spy(new VolumeObject());
Mockito.doReturn(0l).when(volumeInfo).getPoolId();
DataStore ds = Mockito.spy(new PrimaryDataStoreImpl());
Mockito.doReturn(1l).when(ds).getId();
Map<VolumeInfo, DataStore> volumeMap = new HashMap<>();
volumeMap.put(volumeInfo, ds);
StoragePoolVO storagePool0 = Mockito.spy(new StoragePoolVO());
Mockito.doReturn(sPool0IsManaged).when(storagePool0).isManaged();
StoragePoolVO storagePool1 = Mockito.spy(new StoragePoolVO());
Mockito.doReturn(sPool1IsManaged).when(storagePool1).isManaged();
Mockito.doReturn(storagePool0).when(primaryDataStoreDao).findById(0l);
Mockito.doReturn(storagePool1).when(primaryDataStoreDao).findById(1l);
StrategyPriority strategyPriority = storageSystemDataMotionStrategy.internalCanHandle(volumeMap);
Assert.assertEquals(expectedStrategyPriority, strategyPriority);
}
@Test
public void isStoragePoolTypeOfFileTest() {
StoragePoolVO sourceStoragePool = Mockito.spy(new StoragePoolVO());
StoragePoolType[] storagePoolTypeArray = StoragePoolType.values();
for (int i = 0; i < storagePoolTypeArray.length; i++) {
Mockito.doReturn(storagePoolTypeArray[i]).when(sourceStoragePool).getPoolType();
boolean result = storageSystemDataMotionStrategy.isStoragePoolTypeOfFile(sourceStoragePool);
if (sourceStoragePool.getPoolType() == StoragePoolType.Filesystem) {
Assert.assertTrue(result);
} else {
Assert.assertFalse(result);
}
}
}
@Test
public void generateDestPathTest() {
VolumeObject destVolumeInfo = Mockito.spy(new VolumeObject());
HostVO destHost = new HostVO("guid");
Mockito.doReturn("iScsiName").when(destVolumeInfo).get_iScsiName();
Mockito.doReturn(0l).when(destVolumeInfo).getPoolId();
Mockito.doReturn("expected").when(storageSystemDataMotionStrategy).connectHostToVolume(destHost, 0l, "iScsiName");
String expected = storageSystemDataMotionStrategy.generateDestPath(Mockito.mock(VirtualMachineTO.class), Mockito.mock(VolumeVO.class), destHost,
Mockito.mock(StoragePoolVO.class), destVolumeInfo);
Assert.assertEquals(expected, "expected");
Mockito.verify(storageSystemDataMotionStrategy).connectHostToVolume(destHost, 0l, "iScsiName");
}
@Test
public void configureMigrateDiskInfoTest() {
VolumeObject srcVolumeInfo = Mockito.spy(new VolumeObject());
Mockito.doReturn("volume path").when(srcVolumeInfo).getPath();
MigrateCommand.MigrateDiskInfo migrateDiskInfo = storageSystemDataMotionStrategy.configureMigrateDiskInfo(srcVolumeInfo, "destPath");
Assert.assertEquals(MigrateCommand.MigrateDiskInfo.DiskType.BLOCK, migrateDiskInfo.getDiskType());
Assert.assertEquals(MigrateCommand.MigrateDiskInfo.DriverType.RAW, migrateDiskInfo.getDriverType());
Assert.assertEquals(MigrateCommand.MigrateDiskInfo.Source.DEV, migrateDiskInfo.getSource());
Assert.assertEquals("destPath", migrateDiskInfo.getSourceText());
Assert.assertEquals("volume path", migrateDiskInfo.getSerialNumber());
}
@Test
public void setVolumePathTest() {
VolumeVO volume = new VolumeVO("name", 0l, 0l, 0l, 0l, 0l, "folder", "path", Storage.ProvisioningType.THIN, 0l, Volume.Type.ROOT);
String volumePath = "iScsiName";
volume.set_iScsiName(volumePath);
storageSystemDataMotionStrategy.setVolumePath(volume);
Assert.assertEquals(volumePath, volume.getPath());
}
@Test
public void shouldMigrateVolumeTest() {
StoragePoolVO sourceStoragePool = Mockito.spy(new StoragePoolVO());
HostVO destHost = new HostVO("guid");
StoragePoolVO destStoragePool = new StoragePoolVO();
StoragePoolType[] storagePoolTypes = StoragePoolType.values();
for (int i = 0; i < storagePoolTypes.length; i++) {
Mockito.doReturn(storagePoolTypes[i]).when(sourceStoragePool).getPoolType();
boolean result = storageSystemDataMotionStrategy.shouldMigrateVolume(sourceStoragePool, destHost, destStoragePool);
Assert.assertTrue(result);
}
}
}