From fbecd53e0a4cd537ca3678b2e6c25319664a8c2a Mon Sep 17 00:00:00 2001 From: Daman Arora Date: Wed, 25 Nov 2026 12:03:18 -0500 Subject: [PATCH] fix flasharray template delete leaving its copy on the array --- .../driver/AdaptiveDataStoreDriverImpl.java | 23 ++- ...AdaptiveDataStoreDriverImplDeleteTest.java | 160 ++++++++++++++++++ 2 files changed, 175 insertions(+), 8 deletions(-) create mode 100644 plugins/storage/volume/adaptive/src/test/java/org/apache/cloudstack/storage/datastore/driver/AdaptiveDataStoreDriverImplDeleteTest.java diff --git a/plugins/storage/volume/adaptive/src/main/java/org/apache/cloudstack/storage/datastore/driver/AdaptiveDataStoreDriverImpl.java b/plugins/storage/volume/adaptive/src/main/java/org/apache/cloudstack/storage/datastore/driver/AdaptiveDataStoreDriverImpl.java index 2ccd2bab6cdd..46ff14179241 100644 --- a/plugins/storage/volume/adaptive/src/main/java/org/apache/cloudstack/storage/datastore/driver/AdaptiveDataStoreDriverImpl.java +++ b/plugins/storage/volume/adaptive/src/main/java/org/apache/cloudstack/storage/datastore/driver/AdaptiveDataStoreDriverImpl.java @@ -906,17 +906,24 @@ String generatePathInfo(ProviderVolume volume, Map connIdMap) { ProviderAdapterContext newManagedVolumeContext(DataObject obj) { ProviderAdapterContext ctx = new ProviderAdapterContext(); + // Include removed rows: the record may already be marked removed when its copy on the array is cleaned up if (obj instanceof VolumeInfo) { - VolumeVO vol = _volumeDao.findById(obj.getId()); - ctx.setAccountId(vol.getAccountId()); - ctx.setDomainId(vol.getDomainId()); + VolumeVO vol = _volumeDao.findByIdIncludingRemoved(obj.getId()); + if (vol != null) { + ctx.setAccountId(vol.getAccountId()); + ctx.setDomainId(vol.getDomainId()); + } } else if (obj instanceof SnapshotInfo) { - SnapshotVO snap = _snapshotDao.findById(obj.getId()); - ctx.setAccountId(snap.getAccountId()); - ctx.setDomainId(snap.getDomainId()); + SnapshotVO snap = _snapshotDao.findByIdIncludingRemoved(obj.getId()); + if (snap != null) { + ctx.setAccountId(snap.getAccountId()); + ctx.setDomainId(snap.getDomainId()); + } } else if (obj instanceof TemplateInfo) { - VMTemplateVO template = _vmTemplateDao.findById(obj.getId()); - ctx.setAccountId(template.getAccountId()); + VMTemplateVO template = _vmTemplateDao.findByIdIncludingRemoved(obj.getId()); + if (template != null) { + ctx.setAccountId(template.getAccountId()); + } // templates don't have a domain ID so always set to 0 ctx.setDomainId(0L); } diff --git a/plugins/storage/volume/adaptive/src/test/java/org/apache/cloudstack/storage/datastore/driver/AdaptiveDataStoreDriverImplDeleteTest.java b/plugins/storage/volume/adaptive/src/test/java/org/apache/cloudstack/storage/datastore/driver/AdaptiveDataStoreDriverImplDeleteTest.java new file mode 100644 index 000000000000..33c3954970ad --- /dev/null +++ b/plugins/storage/volume/adaptive/src/test/java/org/apache/cloudstack/storage/datastore/driver/AdaptiveDataStoreDriverImplDeleteTest.java @@ -0,0 +1,160 @@ +// 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.datastore.driver; + +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyLong; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import java.util.HashMap; + +import org.apache.cloudstack.engine.subsystem.api.storage.DataStore; +import org.apache.cloudstack.engine.subsystem.api.storage.SnapshotInfo; +import org.apache.cloudstack.engine.subsystem.api.storage.TemplateInfo; +import org.apache.cloudstack.engine.subsystem.api.storage.VolumeInfo; +import org.apache.cloudstack.framework.async.AsyncCompletionCallback; +import org.apache.cloudstack.storage.command.CommandResult; +import org.apache.cloudstack.storage.datastore.adapter.ProviderAdapter; +import org.apache.cloudstack.storage.datastore.adapter.ProviderAdapterContext; +import org.apache.cloudstack.storage.datastore.adapter.ProviderAdapterDataObject; +import org.apache.cloudstack.storage.datastore.db.PrimaryDataStoreDao; +import org.apache.cloudstack.storage.datastore.db.StoragePoolVO; +import org.apache.cloudstack.storage.datastore.provider.AdaptivePrimaryDatastoreAdapterFactoryMap; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.ArgumentCaptor; +import org.mockito.junit.MockitoJUnitRunner; + +import com.cloud.storage.SnapshotVO; +import com.cloud.storage.VMTemplateVO; +import com.cloud.storage.VolumeVO; +import com.cloud.storage.dao.SnapshotDao; +import com.cloud.storage.dao.VMTemplateDao; +import com.cloud.storage.dao.VolumeDao; +import com.cloud.user.dao.AccountDao; +import com.cloud.domain.dao.DomainDao; + +@RunWith(MockitoJUnitRunner.Silent.class) +public class AdaptiveDataStoreDriverImplDeleteTest { + + private static final long OBJECT_ID = 206L; + private static final long ACCOUNT_ID = 2L; + + private AdaptiveDataStoreDriverImpl driver; + private VMTemplateDao templateDao; + private VolumeDao volumeDao; + private SnapshotDao snapshotDao; + + @Before + public void setUp() { + driver = spy(new AdaptiveDataStoreDriverImpl(mock(AdaptivePrimaryDatastoreAdapterFactoryMap.class))); + templateDao = mock(VMTemplateDao.class); + volumeDao = mock(VolumeDao.class); + snapshotDao = mock(SnapshotDao.class); + driver._vmTemplateDao = templateDao; + driver._volumeDao = volumeDao; + driver._snapshotDao = snapshotDao; + driver._accountDao = mock(AccountDao.class); + driver._domainDao = mock(DomainDao.class); + } + + private TemplateInfo removedTemplate() { + TemplateInfo template = mock(TemplateInfo.class); + when(template.getId()).thenReturn(OBJECT_ID); + VMTemplateVO templateVO = mock(VMTemplateVO.class); + when(templateVO.getAccountId()).thenReturn(ACCOUNT_ID); + when(templateDao.findById(OBJECT_ID)).thenReturn(null); + when(templateDao.findByIdIncludingRemoved(OBJECT_ID)).thenReturn(templateVO); + return template; + } + + @Test + public void contextForRemovedTemplateKeepsItsAccount() { + ProviderAdapterContext ctx = driver.newManagedVolumeContext(removedTemplate()); + + Assert.assertEquals(Long.valueOf(ACCOUNT_ID), ctx.getAccountId()); + Assert.assertEquals(Long.valueOf(0L), ctx.getDomainId()); + } + + @Test + public void contextForMissingRecordsDoesNotCrash() { + TemplateInfo template = mock(TemplateInfo.class); + when(template.getId()).thenReturn(OBJECT_ID); + VolumeInfo volume = mock(VolumeInfo.class); + when(volume.getId()).thenReturn(OBJECT_ID); + SnapshotInfo snapshot = mock(SnapshotInfo.class); + when(snapshot.getId()).thenReturn(OBJECT_ID); + + Assert.assertNull(driver.newManagedVolumeContext(template).getAccountId()); + Assert.assertNull(driver.newManagedVolumeContext(volume).getAccountId()); + Assert.assertNull(driver.newManagedVolumeContext(snapshot).getAccountId()); + } + + @Test + public void contextForRemovedVolumeAndSnapshotKeepsTheirAccount() { + VolumeInfo volume = mock(VolumeInfo.class); + when(volume.getId()).thenReturn(OBJECT_ID); + VolumeVO volumeVO = mock(VolumeVO.class); + when(volumeVO.getAccountId()).thenReturn(ACCOUNT_ID); + when(volumeDao.findByIdIncludingRemoved(OBJECT_ID)).thenReturn(volumeVO); + + SnapshotInfo snapshot = mock(SnapshotInfo.class); + when(snapshot.getId()).thenReturn(OBJECT_ID); + SnapshotVO snapshotVO = mock(SnapshotVO.class); + when(snapshotVO.getAccountId()).thenReturn(ACCOUNT_ID); + when(snapshotDao.findByIdIncludingRemoved(OBJECT_ID)).thenReturn(snapshotVO); + + Assert.assertEquals(Long.valueOf(ACCOUNT_ID), driver.newManagedVolumeContext(volume).getAccountId()); + Assert.assertEquals(Long.valueOf(ACCOUNT_ID), driver.newManagedVolumeContext(snapshot).getAccountId()); + } + + @Test + @SuppressWarnings("unchecked") + public void deleteOfRemovedTemplateReachesTheArray() { + TemplateInfo template = removedTemplate(); + DataStore store = mock(DataStore.class); + when(store.getId()).thenReturn(5L); + + PrimaryDataStoreDao poolDao = mock(PrimaryDataStoreDao.class); + driver._storagePoolDao = poolDao; + StoragePoolVO pool = mock(StoragePoolVO.class); + when(pool.getId()).thenReturn(5L); + when(poolDao.findById(5L)).thenReturn(pool); + when(poolDao.getDetails(anyLong())).thenReturn(new HashMap<>()); + + ProviderAdapter api = mock(ProviderAdapter.class); + doReturn(api).when(driver).getAPI(eq(pool), any()); + ProviderAdapterDataObject inData = new ProviderAdapterDataObject(); + inData.setExternalName("pure-fc-neg::tpl-5-0-2-206"); + doReturn(inData).when(driver).newManagedDataObject(template, pool); + + AsyncCompletionCallback callback = mock(AsyncCompletionCallback.class); + driver.deleteAsync(store, template, callback); + + verify(api).delete(any(), eq(inData)); + ArgumentCaptor captor = ArgumentCaptor.forClass(CommandResult.class); + verify(callback).complete(captor.capture()); + Assert.assertTrue(captor.getValue().isSuccess()); + } +}