Skip to content

Commit edb3846

Browse files
committed
more tests
Signed-off-by: Abhishek Kumar <[email protected]>
1 parent adee70d commit edb3846

File tree

3 files changed

+160
-19
lines changed

3 files changed

+160
-19
lines changed
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
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.agent.api;
18+
19+
import org.junit.Assert;
20+
import org.junit.Test;
21+
import org.junit.runner.RunWith;
22+
import org.mockito.junit.MockitoJUnitRunner;
23+
24+
@RunWith(MockitoJUnitRunner.class)
25+
public class GetStorageStatsAnswerTest {
26+
27+
@Test
28+
public void testDefaultConstructor() {
29+
GetStorageStatsAnswer answer = new GetStorageStatsAnswer();
30+
31+
Assert.assertEquals(0, answer.getByteUsed());
32+
Assert.assertEquals(0, answer.getCapacityBytes());
33+
Assert.assertNull(answer.getCapacityIops());
34+
Assert.assertNull(answer.getUsedIops());
35+
}
36+
37+
@Test
38+
public void testConstructorWithCapacityAndUsedBytes() {
39+
GetStorageStatsCommand mockCmd = new GetStorageStatsCommand();
40+
long capacityBytes = 1024L;
41+
long usedBytes = 512L;
42+
43+
GetStorageStatsAnswer answer = new GetStorageStatsAnswer(mockCmd, capacityBytes, usedBytes);
44+
45+
Assert.assertEquals(capacityBytes, answer.getCapacityBytes());
46+
Assert.assertEquals(usedBytes, answer.getByteUsed());
47+
Assert.assertNull(answer.getCapacityIops());
48+
Assert.assertNull(answer.getUsedIops());
49+
}
50+
51+
@Test
52+
public void testConstructorWithIops() {
53+
GetStorageStatsCommand mockCmd = new GetStorageStatsCommand();
54+
long capacityBytes = 2048L;
55+
long usedBytes = 1024L;
56+
Long capacityIops = 1000L;
57+
Long usedIops = 500L;
58+
59+
GetStorageStatsAnswer answer = new GetStorageStatsAnswer(mockCmd, capacityBytes, usedBytes, capacityIops, usedIops);
60+
61+
Assert.assertEquals(capacityBytes, answer.getCapacityBytes());
62+
Assert.assertEquals(usedBytes, answer.getByteUsed());
63+
Assert.assertEquals(capacityIops, answer.getCapacityIops());
64+
Assert.assertEquals(usedIops, answer.getUsedIops());
65+
}
66+
67+
@Test
68+
public void testErrorConstructor() {
69+
GetStorageStatsCommand mockCmd = new GetStorageStatsCommand();
70+
String errorDetails = "An error occurred";
71+
72+
GetStorageStatsAnswer answer = new GetStorageStatsAnswer(mockCmd, errorDetails);
73+
74+
Assert.assertFalse(answer.getResult());
75+
Assert.assertEquals(errorDetails, answer.getDetails());
76+
Assert.assertEquals(0, answer.getCapacityBytes());
77+
Assert.assertEquals(0, answer.getByteUsed());
78+
Assert.assertNull(answer.getCapacityIops());
79+
Assert.assertNull(answer.getUsedIops());
80+
}
81+
}

server/src/main/java/com/cloud/storage/StorageManagerImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -553,7 +553,7 @@ public Answer sendToPool(StoragePool pool, Command cmd) throws StorageUnavailabl
553553
return answers[0];
554554
}
555555

556-
private Pair<Long, Long> getStoragePoolIopsStats(PrimaryDataStoreDriver primaryStoreDriver, StoragePool pool) {
556+
protected Pair<Long, Long> getStoragePoolIopsStats(PrimaryDataStoreDriver primaryStoreDriver, StoragePool pool) {
557557
Pair<Long, Long> result = primaryStoreDriver.getStorageIopsStats(pool);
558558
if (result != null) {
559559
return result;

server/src/test/java/com/cloud/storage/StorageManagerImplTest.java

Lines changed: 78 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -22,26 +22,10 @@
2222
import java.util.HashMap;
2323
import java.util.List;
2424
import java.util.Map;
25-
import org.apache.cloudstack.api.command.admin.storage.ChangeStoragePoolScopeCmd;
26-
import com.cloud.agent.api.StoragePoolInfo;
27-
import com.cloud.dc.ClusterVO;
28-
import com.cloud.dc.DataCenter;
29-
import com.cloud.dc.DataCenterVO;
30-
import com.cloud.dc.dao.ClusterDao;
31-
import com.cloud.dc.dao.DataCenterDao;
32-
import com.cloud.exception.ConnectionException;
33-
import com.cloud.exception.InvalidParameterValueException;
34-
import com.cloud.exception.PermissionDeniedException;
35-
import com.cloud.host.Host;
36-
import com.cloud.hypervisor.Hypervisor.HypervisorType;
37-
import com.cloud.storage.dao.VolumeDao;
38-
import com.cloud.user.AccountManagerImpl;
39-
import com.cloud.utils.Pair;
40-
import com.cloud.utils.exception.CloudRuntimeException;
41-
import com.cloud.vm.VMInstanceVO;
42-
import com.cloud.vm.dao.VMInstanceDao;
4325

4426
import org.apache.cloudstack.api.ApiConstants;
27+
import org.apache.cloudstack.api.command.admin.storage.ChangeStoragePoolScopeCmd;
28+
import org.apache.cloudstack.engine.subsystem.api.storage.PrimaryDataStoreDriver;
4529
import org.apache.cloudstack.framework.config.ConfigDepot;
4630
import org.apache.cloudstack.framework.config.ConfigKey;
4731
import org.apache.cloudstack.framework.config.dao.ConfigurationDao;
@@ -65,14 +49,31 @@
6549

6650
import com.cloud.agent.AgentManager;
6751
import com.cloud.agent.api.Command;
52+
import com.cloud.agent.api.StoragePoolInfo;
6853
import com.cloud.capacity.CapacityManager;
54+
import com.cloud.dc.ClusterVO;
55+
import com.cloud.dc.DataCenter;
56+
import com.cloud.dc.DataCenterVO;
6957
import com.cloud.dc.VsphereStoragePolicyVO;
58+
import com.cloud.dc.dao.ClusterDao;
59+
import com.cloud.dc.dao.DataCenterDao;
7060
import com.cloud.dc.dao.VsphereStoragePolicyDao;
7161
import com.cloud.exception.AgentUnavailableException;
62+
import com.cloud.exception.ConnectionException;
63+
import com.cloud.exception.InvalidParameterValueException;
7264
import com.cloud.exception.OperationTimedoutException;
65+
import com.cloud.exception.PermissionDeniedException;
7366
import com.cloud.exception.StorageUnavailableException;
67+
import com.cloud.host.Host;
68+
import com.cloud.hypervisor.Hypervisor.HypervisorType;
7469
import com.cloud.hypervisor.HypervisorGuruManager;
70+
import com.cloud.storage.dao.VolumeDao;
71+
import com.cloud.user.AccountManagerImpl;
72+
import com.cloud.utils.Pair;
73+
import com.cloud.utils.exception.CloudRuntimeException;
7574
import com.cloud.vm.DiskProfile;
75+
import com.cloud.vm.VMInstanceVO;
76+
import com.cloud.vm.dao.VMInstanceDao;
7677

7778
@RunWith(MockitoJUnitRunner.class)
7879
public class StorageManagerImplTest {
@@ -835,4 +836,63 @@ public void testCheckPoolforSpaceForResize4() throws NoSuchFieldException, Illeg
835836
boolean result = storageManagerImpl.checkPoolforSpace(pool, allocatedSizeWithTemplate, totalAskingSize, true);
836837
Assert.assertTrue(result);
837838
}
839+
840+
@Test
841+
public void testGetStoragePoolIopsStats_ReturnsDriverResultWhenNotNull() {
842+
StoragePool pool = Mockito.mock(StoragePool.class);
843+
PrimaryDataStoreDriver primaryStoreDriver = Mockito.mock(PrimaryDataStoreDriver.class);
844+
Pair<Long, Long> expectedResult = new Pair<>(1000L, 500L);
845+
Mockito.when(primaryStoreDriver.getStorageIopsStats(pool)).thenReturn(expectedResult);
846+
847+
Pair<Long, Long> result = storageManagerImpl.getStoragePoolIopsStats(primaryStoreDriver, pool);
848+
849+
Assert.assertSame("Should return the result from primaryStoreDriver.getStorageIopsStats", expectedResult, result);
850+
Mockito.verify(primaryStoreDriver, Mockito.never()).getUsedIops(Mockito.any());
851+
Mockito.verify(pool, Mockito.never()).getCapacityIops();
852+
}
853+
854+
@Test
855+
public void testGetStoragePoolIopsStats_UsedIopsPositive() {
856+
StoragePool pool = Mockito.mock(StoragePool.class);
857+
PrimaryDataStoreDriver primaryStoreDriver = Mockito.mock(PrimaryDataStoreDriver.class);
858+
Mockito.when(primaryStoreDriver.getStorageIopsStats(pool)).thenReturn(null);
859+
Mockito.when(primaryStoreDriver.getUsedIops(pool)).thenReturn(500L);
860+
Mockito.when(pool.getCapacityIops()).thenReturn(1000L);
861+
862+
Pair<Long, Long> result = storageManagerImpl.getStoragePoolIopsStats(primaryStoreDriver, pool);
863+
864+
Assert.assertNotNull(result);
865+
Assert.assertEquals("Capacity IOPS should match pool's capacity IOPS", 1000L, result.first().longValue());
866+
Assert.assertEquals("Used IOPS should match the positive value returned", 500L, result.second().longValue());
867+
}
868+
869+
@Test
870+
public void testGetStoragePoolIopsStats_UsedIopsZero() {
871+
StoragePool pool = Mockito.mock(StoragePool.class);
872+
PrimaryDataStoreDriver primaryStoreDriver = Mockito.mock(PrimaryDataStoreDriver.class);
873+
Mockito.when(primaryStoreDriver.getStorageIopsStats(pool)).thenReturn(null);
874+
Mockito.when(primaryStoreDriver.getUsedIops(pool)).thenReturn(0L);
875+
Mockito.when(pool.getCapacityIops()).thenReturn(1000L);
876+
877+
Pair<Long, Long> result = storageManagerImpl.getStoragePoolIopsStats(primaryStoreDriver, pool);
878+
879+
Assert.assertNotNull(result);
880+
Assert.assertEquals("Capacity IOPS should match pool's capacity IOPS", 1000L, result.first().longValue());
881+
Assert.assertNull("Used IOPS should be null when usedIops <= 0", result.second());
882+
}
883+
884+
@Test
885+
public void testGetStoragePoolIopsStats_UsedIopsNegative() {
886+
StoragePool pool = Mockito.mock(StoragePool.class);
887+
PrimaryDataStoreDriver primaryStoreDriver = Mockito.mock(PrimaryDataStoreDriver.class);
888+
Mockito.when(primaryStoreDriver.getStorageIopsStats(pool)).thenReturn(null);
889+
Mockito.when(primaryStoreDriver.getUsedIops(pool)).thenReturn(-100L);
890+
Mockito.when(pool.getCapacityIops()).thenReturn(1000L);
891+
892+
Pair<Long, Long> result = storageManagerImpl.getStoragePoolIopsStats(primaryStoreDriver, pool);
893+
894+
Assert.assertNotNull(result);
895+
Assert.assertEquals("Capacity IOPS should match pool's capacity IOPS", 1000L, result.first().longValue());
896+
Assert.assertNull("Used IOPS should be null when usedIops <= 0", result.second());
897+
}
838898
}

0 commit comments

Comments
 (0)