|
| 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 org.apache.cloudstack.engine.orchestration; |
| 18 | + |
| 19 | +import java.util.ArrayList; |
| 20 | + |
| 21 | +import org.apache.commons.lang3.ObjectUtils; |
| 22 | +import org.junit.Assert; |
| 23 | +import org.junit.Before; |
| 24 | +import org.junit.Test; |
| 25 | +import org.junit.runner.RunWith; |
| 26 | +import org.mockito.InjectMocks; |
| 27 | +import org.mockito.Mock; |
| 28 | +import org.mockito.Mockito; |
| 29 | +import org.mockito.Spy; |
| 30 | +import org.mockito.junit.MockitoJUnitRunner; |
| 31 | +import org.mockito.stubbing.Answer; |
| 32 | + |
| 33 | +import com.cloud.configuration.Resource; |
| 34 | +import com.cloud.storage.VolumeVO; |
| 35 | +import com.cloud.user.ResourceLimitService; |
| 36 | + |
| 37 | +@RunWith(MockitoJUnitRunner.class) |
| 38 | +public class VolumeOrchestratorTest { |
| 39 | + |
| 40 | + @Mock |
| 41 | + protected ResourceLimitService resourceLimitMgr; |
| 42 | + |
| 43 | + @Spy |
| 44 | + @InjectMocks |
| 45 | + private VolumeOrchestrator volumeOrchestrator = new VolumeOrchestrator(); |
| 46 | + |
| 47 | + private static final Long DEFAULT_ACCOUNT_PS_RESOURCE_COUNT = 100L; |
| 48 | + private Long accountPSResourceCount; |
| 49 | + |
| 50 | + @Before |
| 51 | + public void setUp() throws Exception { |
| 52 | + accountPSResourceCount = DEFAULT_ACCOUNT_PS_RESOURCE_COUNT; |
| 53 | + Mockito.when(resourceLimitMgr.recalculateResourceCount(Mockito.anyLong(), Mockito.anyLong(), Mockito.anyInt())).thenReturn(new ArrayList<>()); |
| 54 | + Mockito.doAnswer((Answer<Void>) invocation -> { |
| 55 | + Resource.ResourceType type = (Resource.ResourceType)invocation.getArguments()[1]; |
| 56 | + Long increment = (Long)invocation.getArguments()[3]; |
| 57 | + if (Resource.ResourceType.primary_storage.equals(type)) { |
| 58 | + accountPSResourceCount += increment; |
| 59 | + } |
| 60 | + return null; |
| 61 | + }).when(resourceLimitMgr).incrementResourceCount(Mockito.anyLong(), Mockito.any(Resource.ResourceType.class), Mockito.anyBoolean(), Mockito.anyLong()); |
| 62 | + Mockito.doAnswer((Answer<Void>) invocation -> { |
| 63 | + Resource.ResourceType type = (Resource.ResourceType)invocation.getArguments()[1]; |
| 64 | + Long decrement = (Long)invocation.getArguments()[3]; |
| 65 | + if (Resource.ResourceType.primary_storage.equals(type)) { |
| 66 | + accountPSResourceCount -= decrement; |
| 67 | + } |
| 68 | + return null; |
| 69 | + }).when(resourceLimitMgr).decrementResourceCount(Mockito.anyLong(), Mockito.any(Resource.ResourceType.class), Mockito.anyBoolean(), Mockito.anyLong()); |
| 70 | + } |
| 71 | + |
| 72 | + private void runCheckAndUpdateVolumeAccountResourceCountTest(Long originalSize, Long newSize) { |
| 73 | + VolumeVO v1 = Mockito.mock(VolumeVO.class); |
| 74 | + Mockito.when(v1.getSize()).thenReturn(originalSize); |
| 75 | + VolumeVO v2 = Mockito.mock(VolumeVO.class); |
| 76 | + Mockito.when(v2.getSize()).thenReturn(newSize); |
| 77 | + volumeOrchestrator.checkAndUpdateVolumeAccountResourceCount(v1, v2); |
| 78 | + Long expected = ObjectUtils.anyNull(originalSize, newSize) ? |
| 79 | + DEFAULT_ACCOUNT_PS_RESOURCE_COUNT : DEFAULT_ACCOUNT_PS_RESOURCE_COUNT + (newSize - originalSize); |
| 80 | + Assert.assertEquals(expected, accountPSResourceCount); |
| 81 | + } |
| 82 | + |
| 83 | + @Test |
| 84 | + public void testCheckAndUpdateVolumeAccountResourceCountSameSize() { |
| 85 | + runCheckAndUpdateVolumeAccountResourceCountTest(10L, 10L); |
| 86 | + } |
| 87 | + |
| 88 | + @Test |
| 89 | + public void testCheckAndUpdateVolumeAccountResourceCountEitherSizeNull() { |
| 90 | + runCheckAndUpdateVolumeAccountResourceCountTest(null, 10L); |
| 91 | + runCheckAndUpdateVolumeAccountResourceCountTest(10L, null); |
| 92 | + } |
| 93 | + |
| 94 | + @Test |
| 95 | + public void testCheckAndUpdateVolumeAccountResourceCountMoreSize() { |
| 96 | + runCheckAndUpdateVolumeAccountResourceCountTest(10L, 20L); |
| 97 | + } |
| 98 | + |
| 99 | + @Test |
| 100 | + public void testCheckAndUpdateVolumeAccountResourceCountLessSize() { |
| 101 | + runCheckAndUpdateVolumeAccountResourceCountTest(20L, 10L); |
| 102 | + } |
| 103 | +} |
0 commit comments