winterhazel commented on code in PR #13053: URL: https://github.com/apache/cloudstack/pull/13053#discussion_r3174027981
########## plugins/storage/volume/ontap/src/test/java/org/apache/cloudstack/storage/vmsnapshot/OntapVMSnapshotStrategyTest.java: ########## @@ -0,0 +1,933 @@ +/* + * 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.vmsnapshot; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.doThrow; +import static org.mockito.Mockito.lenient; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import java.lang.reflect.Field; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.apache.cloudstack.engine.subsystem.api.storage.StrategyPriority; +import org.apache.cloudstack.engine.subsystem.api.storage.VMSnapshotOptions; +import org.apache.cloudstack.engine.subsystem.api.storage.VolumeDataFactory; +import org.apache.cloudstack.engine.subsystem.api.storage.VolumeInfo; +import org.apache.cloudstack.storage.datastore.db.PrimaryDataStoreDao; +import org.apache.cloudstack.storage.datastore.db.StoragePoolDetailsDao; +import org.apache.cloudstack.storage.datastore.db.StoragePoolVO; +import org.apache.cloudstack.storage.to.VolumeObjectTO; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.Mock; +import org.mockito.Spy; +import org.mockito.junit.jupiter.MockitoExtension; +import org.mockito.junit.jupiter.MockitoSettings; +import org.mockito.quality.Strictness; +import org.apache.cloudstack.storage.utils.OntapStorageConstants; +import com.cloud.agent.AgentManager; +import com.cloud.agent.api.FreezeThawVMAnswer; +import com.cloud.agent.api.FreezeThawVMCommand; +import com.cloud.agent.api.VMSnapshotTO; +import com.cloud.exception.AgentUnavailableException; +import com.cloud.exception.OperationTimedoutException; +import com.cloud.hypervisor.Hypervisor; +import com.cloud.storage.GuestOSVO; +import com.cloud.storage.VolumeDetailVO; +import com.cloud.storage.VolumeVO; +import com.cloud.storage.dao.GuestOSDao; +import com.cloud.storage.dao.VolumeDao; +import com.cloud.storage.dao.VolumeDetailsDao; +import com.cloud.utils.exception.CloudRuntimeException; +import com.cloud.utils.fsm.NoTransitionException; +import com.cloud.vm.UserVmVO; +import com.cloud.vm.VirtualMachine; +import com.cloud.vm.dao.UserVmDao; +import com.cloud.vm.snapshot.VMSnapshot; +import com.cloud.vm.snapshot.VMSnapshotDetailsVO; +import com.cloud.vm.snapshot.VMSnapshotVO; +import com.cloud.vm.snapshot.dao.VMSnapshotDao; +import com.cloud.vm.snapshot.dao.VMSnapshotDetailsDao; + +/** + * Unit tests for {@link OntapVMSnapshotStrategy}. + * + * <p>Tests cover: + * <ul> + * <li>canHandle(VMSnapshot) — various conditions for Allocated and non-Allocated states</li> + * <li>canHandle(Long vmId, Long rootPoolId, boolean snapshotMemory) — allocation-phase checks</li> + * <li>takeVMSnapshot — state transition failure scenarios</li> + * <li>Freeze/thaw behavior (freeze success/failure, thaw success/failure, agent errors)</li> + * <li>Quiesce behavior (honors user input; freeze/thaw only when quiesce=true)</li> + * </ul> + */ +@ExtendWith(MockitoExtension.class) +@MockitoSettings(strictness = Strictness.LENIENT) +class OntapVMSnapshotStrategyTest { + + private static final long VM_ID = 100L; + private static final long HOST_ID = 10L; + private static final long SNAPSHOT_ID = 200L; + private static final long VOLUME_ID_1 = 301L; + private static final long VOLUME_ID_2 = 302L; + private static final long POOL_ID_1 = 401L; + private static final long POOL_ID_2 = 402L; + private static final long GUEST_OS_ID = 50L; + private static final String VM_INSTANCE_NAME = "i-2-100-VM"; + private static final String VM_UUID = "vm-uuid-123"; + + @Spy + private OntapVMSnapshotStrategy strategy; + + @Mock + private UserVmDao userVmDao; + @Mock + private VolumeDao volumeDao; + @Mock + private PrimaryDataStoreDao storagePool; + @Mock + private StoragePoolDetailsDao storagePoolDetailsDao; + @Mock + private VMSnapshotDetailsDao vmSnapshotDetailsDao; + @Mock + private VMSnapshotHelper vmSnapshotHelper; + @Mock + private VMSnapshotDao vmSnapshotDao; + @Mock + private AgentManager agentMgr; + @Mock + private GuestOSDao guestOSDao; + @Mock + private VolumeDataFactory volumeDataFactory; + @Mock + private VolumeDetailsDao volumeDetailsDao; + + @BeforeEach + void setUp() throws Exception { + // Inject mocks into the inherited fields via reflection + // DefaultVMSnapshotStrategy fields + setField(strategy, DefaultVMSnapshotStrategy.class, "vmSnapshotHelper", vmSnapshotHelper); + setField(strategy, DefaultVMSnapshotStrategy.class, "guestOSDao", guestOSDao); + setField(strategy, DefaultVMSnapshotStrategy.class, "userVmDao", userVmDao); + setField(strategy, DefaultVMSnapshotStrategy.class, "vmSnapshotDao", vmSnapshotDao); + setField(strategy, DefaultVMSnapshotStrategy.class, "agentMgr", agentMgr); + setField(strategy, DefaultVMSnapshotStrategy.class, "volumeDao", volumeDao); + + // StorageVMSnapshotStrategy fields + setField(strategy, StorageVMSnapshotStrategy.class, "storagePool", storagePool); + setField(strategy, StorageVMSnapshotStrategy.class, "vmSnapshotDetailsDao", vmSnapshotDetailsDao); + setField(strategy, StorageVMSnapshotStrategy.class, "volumeDataFactory", volumeDataFactory); + + // OntapVMSnapshotStrategy fields + setField(strategy, OntapVMSnapshotStrategy.class, "storagePoolDetailsDao", storagePoolDetailsDao); + setField(strategy, OntapVMSnapshotStrategy.class, "volumeDetailsDao", volumeDetailsDao); Review Comment: Please use `@InjectMocks` instead of reflection -- 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]
