2626import com .cloud .org .Grouping ;
2727import com .cloud .storage .DataStoreRole ;
2828import com .cloud .storage .Snapshot ;
29+ import com .cloud .storage .Storage .StoragePoolType ;
2930import com .cloud .storage .SnapshotPolicyVO ;
3031import com .cloud .storage .SnapshotVO ;
3132import com .cloud .storage .VolumeVO ;
5556import org .apache .cloudstack .engine .subsystem .api .storage .SnapshotResult ;
5657import org .apache .cloudstack .engine .subsystem .api .storage .SnapshotService ;
5758import org .apache .cloudstack .framework .async .AsyncCallFuture ;
59+ import org .apache .cloudstack .storage .datastore .db .PrimaryDataStoreDao ;
5860import org .apache .cloudstack .storage .datastore .db .SnapshotDataStoreDao ;
5961import org .apache .cloudstack .storage .datastore .db .SnapshotDataStoreVO ;
62+ import org .apache .cloudstack .storage .datastore .db .StoragePoolVO ;
6063
6164import org .junit .Assert ;
6265import org .junit .After ;
@@ -105,6 +108,8 @@ public class SnapshotManagerImplTest {
105108 SnapshotScheduler snapshotScheduler ;
106109 @ Mock
107110 TaggedResourceService taggedResourceService ;
111+ @ Mock
112+ PrimaryDataStoreDao primaryDataStoreDao ;
108113 @ InjectMocks
109114 SnapshotManagerImpl snapshotManager = new SnapshotManagerImpl ();
110115
@@ -115,6 +120,7 @@ public void setUp() {
115120 snapshotManager ._accountMgr = accountManager ;
116121 snapshotManager ._snapSchedMgr = snapshotScheduler ;
117122 snapshotManager .taggedResourceService = taggedResourceService ;
123+ snapshotManager ._storagePoolDao = primaryDataStoreDao ;
118124 }
119125
120126 @ After
@@ -610,4 +616,57 @@ public void testDeleteSnapshotPoliciesManualPolicyId() {
610616
611617 snapshotManager .deleteSnapshotPolicies (cmd );
612618 }
619+
620+ @ Test
621+ public void testRemoveClvmPrimarySnapshotStoreRefIfNeeded_ClvmPool () {
622+ SnapshotInfo snapshot = Mockito .mock (SnapshotInfo .class );
623+ DataStore dataStore = Mockito .mock (DataStore .class );
624+ StoragePoolVO pool = Mockito .mock (StoragePoolVO .class );
625+
626+ Mockito .when (snapshot .getDataStore ()).thenReturn (dataStore );
627+ Mockito .when (snapshot .getId ()).thenReturn (1L );
628+ Mockito .when (dataStore .getId ()).thenReturn (2L );
629+ Mockito .when (dataStore .getRole ()).thenReturn (DataStoreRole .Primary );
630+ Mockito .when (primaryDataStoreDao .findById (2L )).thenReturn (pool );
631+ Mockito .when (pool .getPoolType ()).thenReturn (StoragePoolType .CLVM );
632+
633+ snapshotManager .removeClvmPrimarySnapshotStoreRefIfNeeded (snapshot );
634+
635+ Mockito .verify (snapshotStoreDao ).removeBySnapshotStore (1L , 2L , DataStoreRole .Primary );
636+ }
637+
638+ @ Test
639+ public void testRemoveClvmPrimarySnapshotStoreRefIfNeeded_ClvmNgPool () {
640+ SnapshotInfo snapshot = Mockito .mock (SnapshotInfo .class );
641+ DataStore dataStore = Mockito .mock (DataStore .class );
642+ StoragePoolVO pool = Mockito .mock (StoragePoolVO .class );
643+
644+ Mockito .when (snapshot .getDataStore ()).thenReturn (dataStore );
645+ Mockito .when (snapshot .getId ()).thenReturn (3L );
646+ Mockito .when (dataStore .getId ()).thenReturn (4L );
647+ Mockito .when (dataStore .getRole ()).thenReturn (DataStoreRole .Primary );
648+ Mockito .when (primaryDataStoreDao .findById (4L )).thenReturn (pool );
649+ Mockito .when (pool .getPoolType ()).thenReturn (StoragePoolType .CLVM_NG );
650+
651+ snapshotManager .removeClvmPrimarySnapshotStoreRefIfNeeded (snapshot );
652+
653+ Mockito .verify (snapshotStoreDao ).removeBySnapshotStore (3L , 4L , DataStoreRole .Primary );
654+ }
655+
656+ @ Test
657+ public void testRemoveClvmPrimarySnapshotStoreRefIfNeeded_NonClvmPool () {
658+ SnapshotInfo snapshot = Mockito .mock (SnapshotInfo .class );
659+ DataStore dataStore = Mockito .mock (DataStore .class );
660+ StoragePoolVO pool = Mockito .mock (StoragePoolVO .class );
661+
662+ Mockito .when (snapshot .getDataStore ()).thenReturn (dataStore );
663+ Mockito .when (dataStore .getId ()).thenReturn (5L );
664+ Mockito .when (primaryDataStoreDao .findById (5L )).thenReturn (pool );
665+ Mockito .when (pool .getPoolType ()).thenReturn (StoragePoolType .NetworkFilesystem );
666+
667+ snapshotManager .removeClvmPrimarySnapshotStoreRefIfNeeded (snapshot );
668+
669+ Mockito .verify (snapshotStoreDao , Mockito .never ()).removeBySnapshotStore (
670+ Mockito .anyLong (), Mockito .anyLong (), Mockito .any ());
671+ }
613672}
0 commit comments