Skip to content

Commit 38db77b

Browse files
committed
Add unit tests
1 parent 3b5c201 commit 38db77b

File tree

2 files changed

+91
-9
lines changed

2 files changed

+91
-9
lines changed

api/src/main/java/org/apache/cloudstack/cluster/ClusterDrsAlgorithm.java

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ default Double getImbalancePostMigration(ServiceOffering serviceOffering, Virtua
117117
private Pair<Long, Map<Long, Ternary<Long, Long, Long>>> getHostMetricsMapAndType(Long clusterId,
118118
ServiceOffering serviceOffering, Map<Long, Ternary<Long, Long, Long>> hostCpuMap,
119119
Map<Long, Ternary<Long, Long, Long>> hostMemoryMap) throws ConfigurationException {
120-
String metric = ClusterDrsMetric.valueIn(clusterId);
120+
String metric = getClusterDrsMetric(clusterId);
121121
Pair<Long, Map<Long, Ternary<Long, Long, Long>>> pair;
122122
switch (metric) {
123123
case "cpu":
@@ -155,22 +155,26 @@ private Double getImbalance(List<Double> metricList) {
155155
return clusterStandardDeviation / clusterMeanMetric;
156156
}
157157

158-
private Double getMetricValue(Long clusterId, double used, double free, double total, Float skipThreshold) {
159-
boolean useRatio = ClusterDrsMetricUseRatio.valueIn(clusterId);
160-
switch (ClusterDrsMetricType.valueIn(clusterId)) {
158+
default String getClusterDrsMetric(long clusterId) {
159+
return ClusterDrsMetric.valueIn(clusterId);
160+
}
161+
162+
default Double getMetricValue(long clusterId, long used, long free, long total, Float skipThreshold) {
163+
boolean useRatio = getDrsMetricUseRatio(clusterId);
164+
switch (getDrsMetricType(clusterId)) {
161165
case "free":
162166
if (skipThreshold != null && free < skipThreshold * total) return null;
163167
if (useRatio) {
164-
return free / total;
168+
return (double) free / total;
165169
} else {
166-
return free;
170+
return (double) free;
167171
}
168172
case "used":
169173
if (skipThreshold != null && used > skipThreshold * total) return null;
170174
if (useRatio) {
171-
return used / total;
175+
return (double) used / total;
172176
} else {
173-
return used;
177+
return (double) used;
174178
}
175179
}
176180
return null;
@@ -206,6 +210,14 @@ default Double getClusterStandardDeviation(List<Double> metricList, Double mean)
206210
}
207211
}
208212

213+
default boolean getDrsMetricUseRatio(long clusterId) {
214+
return ClusterDrsMetricUseRatio.valueIn(clusterId);
215+
}
216+
217+
default String getDrsMetricType(long clusterId) {
218+
return ClusterDrsMetricType.valueIn(clusterId);
219+
}
220+
209221
/**
210222
* The cluster imbalance is defined as the percentage deviation from the mean
211223
* for a configured metric of the cluster. The standard deviation is used as a
@@ -218,7 +230,7 @@ default Double getClusterStandardDeviation(List<Double> metricList, Double mean)
218230
*/
219231
default Double getClusterImbalance(Long clusterId, List<Ternary<Long, Long, Long>> cpuList,
220232
List<Ternary<Long, Long, Long>> memoryList, Float skipThreshold) throws ConfigurationException {
221-
String metric = ClusterDrsMetric.valueIn(clusterId);
233+
String metric = getClusterDrsMetric(clusterId);
222234
List<Double> list;
223235
switch (metric) {
224236
case "cpu":
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package org.apache.cloudstack.cluster;
2+
3+
import com.cloud.utils.Ternary;
4+
import junit.framework.TestCase;
5+
import org.junit.Test;
6+
import org.junit.runner.RunWith;
7+
import org.mockito.Spy;
8+
import org.mockito.junit.MockitoJUnitRunner;
9+
10+
import java.util.List;
11+
12+
import static org.mockito.Mockito.doReturn;
13+
14+
@RunWith(MockitoJUnitRunner.class)
15+
public class ClusterDrsAlgorithmTest extends TestCase {
16+
17+
@Spy
18+
ClusterDrsAlgorithm drsAlgorithm;
19+
20+
@Test
21+
public void testGetMetricValue() {
22+
List<Ternary<Boolean, String, Double>> testData = List.of(
23+
new Ternary<>(true, "free", 0.4),
24+
new Ternary<>(false, "free", 40.0),
25+
new Ternary<>(true, "used", 0.3),
26+
new Ternary<>(false, "used", 30.0)
27+
);
28+
29+
long used = 30;
30+
long free = 40;
31+
long total = 100;
32+
33+
for (Ternary<Boolean, String, Double> data : testData) {
34+
boolean useRatio = data.first();
35+
String metricType = data.second();
36+
double expectedValue = data.third();
37+
38+
doReturn(useRatio).when(drsAlgorithm).getDrsMetricUseRatio(1L);
39+
doReturn(metricType).when(drsAlgorithm).getDrsMetricType(1L);
40+
41+
assertEquals(expectedValue, drsAlgorithm.getMetricValue(1, used, free, total, null));
42+
}
43+
}
44+
45+
@Test
46+
public void testGetMetricValueWithSkipThreshold() {
47+
List<Ternary<Boolean, String, Double>> testData = List.of(
48+
new Ternary<>(true, "free", 0.15),
49+
new Ternary<>(false, "free", 15.0),
50+
new Ternary<>(true, "used", null),
51+
new Ternary<>(false, "used", null)
52+
);
53+
54+
long used = 80;
55+
long free = 15;
56+
long total = 100;
57+
58+
for (Ternary<Boolean, String, Double> data : testData) {
59+
boolean useRatio = data.first();
60+
String metricType = data.second();
61+
Double expectedValue = data.third();
62+
float skipThreshold = metricType.equals("free") ? 0.1f : 0.7f;
63+
64+
doReturn(useRatio).when(drsAlgorithm).getDrsMetricUseRatio(1L);
65+
doReturn(metricType).when(drsAlgorithm).getDrsMetricType(1L);
66+
67+
assertEquals(expectedValue, drsAlgorithm.getMetricValue(1L, used, free, total, skipThreshold));
68+
}
69+
}
70+
}

0 commit comments

Comments
 (0)