slavkap commented on code in PR #14389:
URL: https://github.com/apache/cloudstack/pull/14389#discussion_r4247534601
##########
plugins/storage/volume/storpool/src/test/java/org/apache/cloudstack/storage/datastore/driver/StorPoolPrimaryDataStoreDriverTest.java:
##########
@@ -214,6 +236,345 @@ public void testCopyVolumeNotAttachedToVmPassed() {
Assert.assertNull(response.getError());
}
+ @Test
+ public void testListVolumesForImportReturnsGlobalIdVolume() {
+ when(StorPoolUtil.getSpConnection(importPool.getUuid(),
importPool.getId(), detailsDao, storagePool)).thenReturn(conn);
+ StorPoolUtil.SpApiResponse resp =
volumesListResponse(volumeJson("~t.t.t", null, false, 1073741824L, 524288000L));
+ when(StorPoolUtil.volumesListResponse(conn)).thenReturn(resp);
+
+ List<VolumeOnStorageTO> volumes =
storPoolPrimaryDataStoreDriver.listVolumesForImport(importPool, null, null,
false);
+
+ Assert.assertEquals(1, volumes.size());
+ VolumeOnStorageTO volume = volumes.get(0);
+ Assert.assertEquals("t.t.t", volume.getName());
+ Assert.assertEquals("/dev/storpool-byid/t.t.t", volume.getPath());
+ Assert.assertEquals("/dev/storpool-byid/t.t.t", volume.getFullPath());
+ Assert.assertEquals(Storage.ImageFormat.QCOW2.toString(),
volume.getFormat());
+ Assert.assertEquals(524288000L, volume.getSize());
+ Assert.assertEquals(1073741824L, volume.getVirtualSize());
+ }
+
+ @Test
+ public void testListVolumesForImportSkipsVolumeTaggedCs() {
+ when(StorPoolUtil.getSpConnection(importPool.getUuid(),
importPool.getId(), detailsDao, storagePool)).thenReturn(conn);
+ JsonObject tagged = volumeJsonObject("~t.t.t", 1073741824L,
1073741824L);
+ JsonObject tags = new JsonObject();
+ tags.addProperty("cs", "volume");
+ tagged.add("tags", tags);
+ StorPoolUtil.SpApiResponse resp = volumesListResponse(tagged);
+ when(StorPoolUtil.volumesListResponse(conn)).thenReturn(resp);
+
+ List<VolumeOnStorageTO> volumes =
storPoolPrimaryDataStoreDriver.listVolumesForImport(importPool, null, null,
false);
+
+ Assert.assertTrue(volumes.isEmpty());
+ }
+
+ @Test
+ public void testListVolumesForImportSkipsDeletedVolume() {
+ when(StorPoolUtil.getSpConnection(importPool.getUuid(),
importPool.getId(), detailsDao, storagePool)).thenReturn(conn);
+ StorPoolUtil.SpApiResponse resp =
volumesListResponse(volumeJson("~t.t.t", null, true, 1073741824L, 1073741824L));
+ when(StorPoolUtil.volumesListResponse(conn)).thenReturn(resp);
+
+ List<VolumeOnStorageTO> volumes =
storPoolPrimaryDataStoreDriver.listVolumesForImport(importPool, null, null,
false);
+
+ Assert.assertTrue(volumes.isEmpty());
+ }
+
+ @Test
+ public void testListVolumesForImportSkipsNonGlobalIdAndCloneNames() {
+ when(StorPoolUtil.getSpConnection(importPool.getUuid(),
importPool.getId(), detailsDao, storagePool)).thenReturn(conn);
+ JsonArray data = new JsonArray();
+ data.add(volumeJsonObject("friendlyname", 1073741824L, 1073741824L));
+ data.add(volumeJsonObject("*s.s.s", 1073741824L, 1073741824L));
+ data.add(volumeJsonObject("[email protected]", 1073741824L, 1073741824L));
+ StorPoolUtil.SpApiResponse resp = volumesListResponse(data);
+ when(StorPoolUtil.volumesListResponse(conn)).thenReturn(resp);
+
+ List<VolumeOnStorageTO> volumes =
storPoolPrimaryDataStoreDriver.listVolumesForImport(importPool, null, null,
false);
+
+ Assert.assertTrue(volumes.isEmpty());
+ }
+
+ @Test
+ public void testListVolumesForImportFiltersByKeyword() {
+ when(StorPoolUtil.getSpConnection(importPool.getUuid(),
importPool.getId(), detailsDao, storagePool)).thenReturn(conn);
+ JsonArray data = new JsonArray();
+ data.add(volumeJsonObject("~t.t.t", 1073741824L, 1073741824L));
+ data.add(volumeJsonObject("~s.s.s", 1073741824L, 1073741824L));
+ StorPoolUtil.SpApiResponse resp = volumesListResponse(data);
+ when(StorPoolUtil.volumesListResponse(conn)).thenReturn(resp);
+
+ List<VolumeOnStorageTO> volumes =
storPoolPrimaryDataStoreDriver.listVolumesForImport(importPool, null, "s.s.s",
false);
+
+ Assert.assertEquals(1, volumes.size());
+ Assert.assertEquals("s.s.s", volumes.get(0).getName());
+ }
+
+ @Test
+ public void testListVolumesForImportApiErrorReturnsEmptyList() {
+ when(StorPoolUtil.getSpConnection(importPool.getUuid(),
importPool.getId(), detailsDao, storagePool)).thenReturn(conn);
+ StorPoolUtil.SpApiResponse resp = new StorPoolUtil.SpApiResponse();
+ setResponseError(resp);
+ when(StorPoolUtil.volumesListResponse(conn)).thenReturn(resp);
+
+ List<VolumeOnStorageTO> volumes =
storPoolPrimaryDataStoreDriver.listVolumesForImport(importPool, null, null,
false);
+
+ Assert.assertNotNull(volumes);
+ Assert.assertTrue(volumes.isEmpty());
+ }
+
+ @Test
+ public void testListVolumesForImportWithPathReturnsVolume() {
+ when(StorPoolUtil.getSpConnection(importPool.getUuid(),
importPool.getId(), detailsDao, storagePool)).thenReturn(conn);
+ StorPoolUtil.SpApiResponse resp =
volumesListResponse(volumeJson("~t.t.t", null, false, 1073741824L, 524288000L));
+ when(StorPoolUtil.volumeDescribeResponse("~t.t.t",
conn)).thenReturn(resp);
+
+ List<VolumeOnStorageTO> volumes =
storPoolPrimaryDataStoreDriver.listVolumesForImport(importPool,
"/dev/storpool-byid/t.t.t", null, false);
+
+ Assert.assertEquals(1, volumes.size());
+ VolumeOnStorageTO volume = volumes.get(0);
+ Assert.assertEquals("t.t.t", volume.getName());
+ Assert.assertEquals("/dev/storpool-byid/t.t.t", volume.getPath());
+ Assert.assertEquals("/dev/storpool-byid/t.t.t", volume.getFullPath());
+ Assert.assertEquals(Storage.ImageFormat.QCOW2.toString(),
volume.getFormat());
+ Assert.assertEquals(524288000L, volume.getSize());
+ Assert.assertEquals(1073741824L, volume.getVirtualSize());
+ }
+
+ @Test
+ public void testListVolumesForImportWithPathExcludesVolumeTaggedCs() {
+ when(StorPoolUtil.getSpConnection(importPool.getUuid(),
importPool.getId(), detailsDao, storagePool)).thenReturn(conn);
+ JsonObject tagged = volumeJsonObject("~t.t.t", 1073741824L,
1073741824L);
+ JsonObject tags = new JsonObject();
+ tags.addProperty("cs", "volume");
+ tagged.add("tags", tags);
+ StorPoolUtil.SpApiResponse resp = volumesListResponse(tagged);
+ when(StorPoolUtil.volumeDescribeResponse("~t.t.t",
conn)).thenReturn(resp);
+
+ List<VolumeOnStorageTO> volumes =
storPoolPrimaryDataStoreDriver.listVolumesForImport(importPool,
"/dev/storpool-byid/t.t.t", null, false);
+
+ Assert.assertTrue(volumes.isEmpty());
+ }
+
+ @Test
+ public void
testListVolumesForImportWithPathForUnmanageReturnsVolumeEvenIfTaggedCs() {
+ when(StorPoolUtil.getSpConnection(importPool.getUuid(),
importPool.getId(), detailsDao, storagePool)).thenReturn(conn);
+ JsonObject tagged = volumeJsonObject("~t.t.t", 1073741824L,
1073741824L);
+ JsonObject tags = new JsonObject();
+ tags.addProperty("cs", "volume");
+ tagged.add("tags", tags);
+ StorPoolUtil.SpApiResponse resp = volumesListResponse(tagged);
+ when(StorPoolUtil.volumeDescribeResponse("~t.t.t",
conn)).thenReturn(resp);
+
+ List<VolumeOnStorageTO> volumes =
storPoolPrimaryDataStoreDriver.listVolumesForImport(importPool,
"/dev/storpool-byid/t.t.t", null, true);
+
+ Assert.assertEquals(1, volumes.size());
+ Assert.assertEquals("t.t.t", volumes.get(0).getName());
+ }
+
+ @Test
+ public void
testListVolumesForImportWithPathForUnmanageSkipsNonGlobalIdName() {
+ when(StorPoolUtil.getSpConnection(importPool.getUuid(),
importPool.getId(), detailsDao, storagePool)).thenReturn(conn);
+ StorPoolUtil.SpApiResponse resp =
volumesListResponse(volumeJsonObject("[email protected]", 1073741824L, 1073741824L));
+ when(StorPoolUtil.volumeDescribeResponse("~t.t.t",
conn)).thenReturn(resp);
+
+ List<VolumeOnStorageTO> volumes =
storPoolPrimaryDataStoreDriver.listVolumesForImport(importPool,
"/dev/storpool-byid/t.t.t", null, true);
+
+ Assert.assertTrue(volumes.isEmpty());
+ }
+
+ @Test
+ public void testListVolumesForImportWithPathSkipsDeletedVolume() {
+ when(StorPoolUtil.getSpConnection(importPool.getUuid(),
importPool.getId(), detailsDao, storagePool)).thenReturn(conn);
+ StorPoolUtil.SpApiResponse resp =
volumesListResponse(volumeJson("~t.t.t", null, true, 1073741824L, 1073741824L));
+ when(StorPoolUtil.volumeDescribeResponse("~t.t.t",
conn)).thenReturn(resp);
+
+ List<VolumeOnStorageTO> volumes =
storPoolPrimaryDataStoreDriver.listVolumesForImport(importPool,
"/dev/storpool-byid/t.t.t", null, false);
+
+ Assert.assertTrue(volumes.isEmpty());
+ }
+
+ @Test
+ public void testListVolumesForImportWithPathNotFoundReturnsEmptyList() {
+ when(StorPoolUtil.getSpConnection(importPool.getUuid(),
importPool.getId(), detailsDao, storagePool)).thenReturn(conn);
+ StorPoolUtil.SpApiResponse resp = volumesListResponse(new JsonArray());
+ when(StorPoolUtil.volumeDescribeResponse("~t.t.t",
conn)).thenReturn(resp);
+
+ List<VolumeOnStorageTO> volumes =
storPoolPrimaryDataStoreDriver.listVolumesForImport(importPool,
"/dev/storpool-byid/t.t.t", null, false);
+
+ Assert.assertNotNull(volumes);
+ Assert.assertTrue(volumes.isEmpty());
+ }
+
+ @Test
+ public void testListVolumesForImportWithPathApiErrorReturnsEmptyList() {
+ when(StorPoolUtil.getSpConnection(importPool.getUuid(),
importPool.getId(), detailsDao, storagePool)).thenReturn(conn);
+ StorPoolUtil.SpApiResponse resp = new StorPoolUtil.SpApiResponse();
+ setResponseError(resp);
+ when(StorPoolUtil.volumeDescribeResponse("~t.t.t",
conn)).thenReturn(resp);
+
+ List<VolumeOnStorageTO> volumes =
storPoolPrimaryDataStoreDriver.listVolumesForImport(importPool,
"/dev/storpool-byid/t.t.t", null, false);
+
+ Assert.assertNotNull(volumes);
+ Assert.assertTrue(volumes.isEmpty());
+ }
+
+ @Test
+ public void testCreateAsyncSetsVolumeFormatToQcow2() {
+ DataStore dataStore = mock(DataStore.class);
+
when(dataStore.getUuid()).thenReturn("SP_API_HTTP=1.1.1.1:81;SP_AUTH_TOKEN=token;SP_TEMPLATE=template_name");
+ when(dataStore.getId()).thenReturn(1L);
+
+ VolumeInfo vinfo = mock(VolumeInfo.class);
+ when(vinfo.getType()).thenReturn(DataObjectType.VOLUME);
+ when(vinfo.getUuid()).thenReturn(UUID.randomUUID().toString());
+ when(vinfo.getId()).thenReturn(101L);
+ when(vinfo.getSize()).thenReturn(1073741824L);
+ when(vinfo.getPassphraseId()).thenReturn(null);
+ when(vinfo.getInstanceId()).thenReturn(null);
+ when(vinfo.getDiskOfferingId()).thenReturn(null);
+
+ when(StorPoolUtil.getSpConnection(dataStore.getUuid(),
dataStore.getId(), detailsDao, storagePool)).thenReturn(conn);
+ StorPoolUtil.SpApiResponse resp = new StorPoolUtil.SpApiResponse();
+ when(StorPoolUtil.volumeCreate(any(StorPoolVolumeDef.class),
eq(conn))).thenReturn(resp);
+ when(StorPoolUtil.getNameFromResponse(resp,
false)).thenReturn("t.t.t");
+
+ VolumeVO volumeVO = mock(VolumeVO.class);
+ when(volumeVO.getId()).thenReturn(101L);
+ when(volumeDao.findById(101L)).thenReturn(volumeVO);
+
+ StoragePoolVO storagePoolVO = mock(StoragePoolVO.class);
+ when(storagePool.findById(1L)).thenReturn(storagePoolVO);
+
+ @SuppressWarnings("unchecked")
+ AsyncCompletionCallback<CreateCmdResult> createCallback =
mock(AsyncCompletionCallback.class);
+
+ storPoolPrimaryDataStoreDriver.createAsync(dataStore, vinfo,
createCallback);
+
+ verify(volumeVO).setFormat(Storage.ImageFormat.QCOW2);
+ verify(volumeDao).update(eq(101L), eq(volumeVO));
+ }
+
+ @Test
+ public void testVolumeImportedTagsVolumeWithCsAndUuid() {
+ when(StorPoolUtil.getSpConnection(importPool.getUuid(),
importPool.getId(), detailsDao, storagePool)).thenReturn(conn);
+ String volumeUuid = UUID.randomUUID().toString();
+ VolumeVO volume = mock(VolumeVO.class);
+ when(volume.getPath()).thenReturn("/dev/storpool-byid/t.t.t");
+ when(volume.getUuid()).thenReturn(volumeUuid);
+ when(volume.getDiskOfferingId()).thenReturn(5L);
+ when(volume.getInstanceId()).thenReturn(null);
+ when(diskOfferingDetailsDao.findDetail(5L,
StorPoolUtil.SP_TIER)).thenReturn(null);
+ StorPoolUtil.SpApiResponse resp = new StorPoolUtil.SpApiResponse();
+ when(StorPoolUtil.volumeUpdate(any(StorPoolVolumeDef.class),
eq(conn))).thenReturn(resp);
+
+ storPoolPrimaryDataStoreDriver.volumeManagementChanged(importPool,
volume, true);
+
+ ArgumentCaptor<StorPoolVolumeDef> captor =
ArgumentCaptor.forClass(StorPoolVolumeDef.class);
+ utilities.verify(() -> StorPoolUtil.volumeUpdate(captor.capture(),
eq(conn)), times(1));
+ StorPoolVolumeDef updated = captor.getValue();
+ Assert.assertEquals("~t.t.t", updated.getName());
+ Assert.assertEquals(volumeUuid, updated.getTags().get("uuid"));
+ Assert.assertEquals("volume", updated.getTags().get("cs"));
+ Assert.assertNull(updated.getTags().get("qc"));
+ Assert.assertNull(updated.getTags().get("cvm"));
+ }
+
+ @Test
+ public void testVolumeImportedSetsCvmTagWhenAttachedToVm() {
+ when(StorPoolUtil.getSpConnection(importPool.getUuid(),
importPool.getId(), detailsDao, storagePool)).thenReturn(conn);
+ VolumeVO volume = mock(VolumeVO.class);
+ when(volume.getPath()).thenReturn("/dev/storpool-byid/t.t.t");
+ when(volume.getUuid()).thenReturn(UUID.randomUUID().toString());
+ when(volume.getInstanceId()).thenReturn(7L);
+ VMInstanceVO vm = mock(VMInstanceVO.class);
+ String vmUuid = UUID.randomUUID().toString();
+ when(vm.getUuid()).thenReturn(vmUuid);
+ when(vmInstanceDao.findById(7L)).thenReturn(vm);
+ StorPoolUtil.SpApiResponse resp = new StorPoolUtil.SpApiResponse();
+ when(StorPoolUtil.volumeUpdate(any(StorPoolVolumeDef.class),
eq(conn))).thenReturn(resp);
+
+ storPoolPrimaryDataStoreDriver.volumeManagementChanged(importPool,
volume, true);
+
+ ArgumentCaptor<StorPoolVolumeDef> captor =
ArgumentCaptor.forClass(StorPoolVolumeDef.class);
+ utilities.verify(() -> StorPoolUtil.volumeUpdate(captor.capture(),
eq(conn)), times(1));
+ Assert.assertEquals(vmUuid, captor.getValue().getTags().get("cvm"));
+ }
+
+ @Test
+ public void testVolumeImportedIncludesQosTierFromDiskOffering() {
+ when(StorPoolUtil.getSpConnection(importPool.getUuid(),
importPool.getId(), detailsDao, storagePool)).thenReturn(conn);
+ VolumeVO volume = mock(VolumeVO.class);
+ when(volume.getPath()).thenReturn("/dev/storpool-byid/t.t.t");
+ when(volume.getUuid()).thenReturn(UUID.randomUUID().toString());
+ when(volume.getDiskOfferingId()).thenReturn(5L);
+ when(volume.getInstanceId()).thenReturn(null);
+ DiskOfferingDetailVO tierDetail = mock(DiskOfferingDetailVO.class);
+ when(tierDetail.getValue()).thenReturn("ssd-tier");
+ when(diskOfferingDetailsDao.findDetail(5L,
StorPoolUtil.SP_TIER)).thenReturn(tierDetail);
+ StorPoolUtil.SpApiResponse resp = new StorPoolUtil.SpApiResponse();
+ when(StorPoolUtil.volumeUpdate(any(StorPoolVolumeDef.class),
eq(conn))).thenReturn(resp);
+
+ storPoolPrimaryDataStoreDriver.volumeManagementChanged(importPool,
volume, true);
+
+ ArgumentCaptor<StorPoolVolumeDef> captor =
ArgumentCaptor.forClass(StorPoolVolumeDef.class);
+ utilities.verify(() -> StorPoolUtil.volumeUpdate(captor.capture(),
eq(conn)), times(1));
+ Assert.assertEquals("ssd-tier", captor.getValue().getTags().get("qc"));
+ }
+
+ @Test
+ public void testVolumeUnmanagedClearsCsUuidAndQosTags() {
+ when(StorPoolUtil.getSpConnection(importPool.getUuid(),
importPool.getId(), detailsDao, storagePool)).thenReturn(conn);
+ VolumeVO volume = mock(VolumeVO.class);
+ when(volume.getPath()).thenReturn("/dev/storpool-byid/t.t.t");
+ StorPoolUtil.SpApiResponse resp = new StorPoolUtil.SpApiResponse();
+ when(StorPoolUtil.volumeUpdate(any(StorPoolVolumeDef.class),
eq(conn))).thenReturn(resp);
+
+ storPoolPrimaryDataStoreDriver.volumeManagementChanged(importPool,
volume, false);
+
+ ArgumentCaptor<StorPoolVolumeDef> captor =
ArgumentCaptor.forClass(StorPoolVolumeDef.class);
+ utilities.verify(() -> StorPoolUtil.volumeUpdate(captor.capture(),
eq(conn)), times(1));
+ StorPoolVolumeDef updated = captor.getValue();
+ Assert.assertEquals("~t.t.t", updated.getName());
+ Assert.assertEquals("", updated.getTags().get("cs"));
Review Comment:
No new unit test needed here — given the clarification on #1, a unit test
asserting `cs==""` is treated as "unmanaged" would be testing the wrong thing
(it assumes the JSON we send is literally what a later read-back returns, which
isn't how StorPool's tag-removal semantics work).
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]