|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | +package com.cloud.storage.snapshot; |
| 18 | + |
| 19 | +import static org.mockito.Mockito.when; |
| 20 | + |
| 21 | +import org.apache.cloudstack.context.CallContext; |
| 22 | +import org.apache.cloudstack.engine.subsystem.api.storage.DataStore; |
| 23 | +import org.apache.cloudstack.engine.subsystem.api.storage.DataStoreManager; |
| 24 | +import org.apache.cloudstack.engine.subsystem.api.storage.SnapshotInfo; |
| 25 | +import org.apache.cloudstack.engine.subsystem.api.storage.SnapshotResult; |
| 26 | +import org.apache.cloudstack.engine.subsystem.api.storage.SnapshotService; |
| 27 | +import org.apache.cloudstack.engine.subsystem.api.storage.SnapshotStrategy; |
| 28 | +import org.apache.cloudstack.framework.async.AsyncCallFuture; |
| 29 | +import org.junit.Test; |
| 30 | +import org.junit.runner.RunWith; |
| 31 | +import org.mockito.InjectMocks; |
| 32 | +import org.mockito.Mock; |
| 33 | +import org.mockito.MockedStatic; |
| 34 | +import org.mockito.Mockito; |
| 35 | +import org.mockito.junit.MockitoJUnitRunner; |
| 36 | + |
| 37 | +import com.cloud.configuration.Resource.ResourceType; |
| 38 | +import com.cloud.storage.DataStoreRole; |
| 39 | +import com.cloud.storage.dao.SnapshotZoneDao; |
| 40 | +import com.cloud.user.Account; |
| 41 | +import com.cloud.user.ResourceLimitService; |
| 42 | + |
| 43 | +@RunWith(MockitoJUnitRunner.Silent.class) |
| 44 | +public class SnapshotManagerImplCopyResourceCountTest { |
| 45 | + |
| 46 | + @Mock |
| 47 | + DataStoreManager dataStoreMgr; |
| 48 | + @Mock |
| 49 | + SnapshotService snapshotSrv; |
| 50 | + @Mock |
| 51 | + SnapshotZoneDao snapshotZoneDao; |
| 52 | + @Mock |
| 53 | + ResourceLimitService _resourceLimitMgr; |
| 54 | + |
| 55 | + @InjectMocks |
| 56 | + SnapshotManagerImpl snapshotManager = new SnapshotManagerImpl(); |
| 57 | + |
| 58 | + @Test |
| 59 | + @SuppressWarnings("unchecked") |
| 60 | + public void copySnapshotOnPoolChargesPrimaryStorageToTheSnapshotOwnerAccount() throws Exception { |
| 61 | + long ownerAccountId = 42L; |
| 62 | + long snapshotSize = 1024L; |
| 63 | + Long poolId = 5L; |
| 64 | + |
| 65 | + SnapshotInfo snapshot = Mockito.mock(SnapshotInfo.class); |
| 66 | + SnapshotInfo snapshotOnStore = Mockito.mock(SnapshotInfo.class); |
| 67 | + SnapshotStrategy strategy = Mockito.mock(SnapshotStrategy.class); |
| 68 | + DataStore store = Mockito.mock(DataStore.class); |
| 69 | + AsyncCallFuture<SnapshotResult> future = Mockito.mock(AsyncCallFuture.class); |
| 70 | + SnapshotResult result = Mockito.mock(SnapshotResult.class); |
| 71 | + |
| 72 | + when(snapshot.getId()).thenReturn(100L); |
| 73 | + when(snapshot.getSize()).thenReturn(snapshotSize); |
| 74 | + when(snapshot.getAccountId()).thenReturn(ownerAccountId); |
| 75 | + when(dataStoreMgr.getDataStore(poolId, DataStoreRole.Primary)).thenReturn(store); |
| 76 | + when(store.create(snapshot)).thenReturn(snapshotOnStore); |
| 77 | + when(snapshotOnStore.getDataCenterId()).thenReturn(2L); |
| 78 | + when(snapshotSrv.copySnapshot(snapshot, snapshotOnStore, strategy)).thenReturn(future); |
| 79 | + when(future.get()).thenReturn(result); |
| 80 | + when(result.isFailed()).thenReturn(false); |
| 81 | + |
| 82 | + try (MockedStatic<CallContext> ctxMock = Mockito.mockStatic(CallContext.class)) { |
| 83 | + CallContext ctx = Mockito.mock(CallContext.class); |
| 84 | + ctxMock.when(CallContext::current).thenReturn(ctx); |
| 85 | + // Use SYSTEM ids so the usage-event branch is skipped; the point of the |
| 86 | + // test is which account the primary_storage count is charged to. |
| 87 | + when(ctx.getCallingUserId()).thenReturn(Account.ACCOUNT_ID_SYSTEM); |
| 88 | + when(ctx.getCallingAccountId()).thenReturn(Account.ACCOUNT_ID_SYSTEM); |
| 89 | + |
| 90 | + snapshotManager.copySnapshotOnPool(snapshot, strategy, poolId); |
| 91 | + } |
| 92 | + |
| 93 | + // Must charge the snapshot OWNER's account, not the calling user id. |
| 94 | + // Before the fix this was called with CallContext.getCallingUserId(). |
| 95 | + Mockito.verify(_resourceLimitMgr).incrementResourceCount( |
| 96 | + Mockito.eq(ownerAccountId), Mockito.eq(ResourceType.primary_storage), Mockito.eq(snapshotSize)); |
| 97 | + } |
| 98 | +} |
0 commit comments