Skip to content

Commit 48b9bdb

Browse files
committed
Add unit tests
1 parent 3b5c201 commit 48b9bdb

File tree

2 files changed

+110
-9
lines changed

2 files changed

+110
-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: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.apache.cloudstack.cluster;
21+
22+
import com.cloud.utils.Ternary;
23+
import junit.framework.TestCase;
24+
import org.junit.Test;
25+
import org.junit.runner.RunWith;
26+
import org.mockito.Spy;
27+
import org.mockito.junit.MockitoJUnitRunner;
28+
29+
import java.util.List;
30+
31+
import static org.mockito.Mockito.doReturn;
32+
33+
@RunWith(MockitoJUnitRunner.class)
34+
public class ClusterDrsAlgorithmTest extends TestCase {
35+
36+
@Spy
37+
ClusterDrsAlgorithm drsAlgorithm;
38+
39+
@Test
40+
public void testGetMetricValue() {
41+
List<Ternary<Boolean, String, Double>> testData = List.of(
42+
new Ternary<>(true, "free", 0.4),
43+
new Ternary<>(false, "free", 40.0),
44+
new Ternary<>(true, "used", 0.3),
45+
new Ternary<>(false, "used", 30.0)
46+
);
47+
48+
long used = 30;
49+
long free = 40;
50+
long total = 100;
51+
52+
for (Ternary<Boolean, String, Double> data : testData) {
53+
boolean useRatio = data.first();
54+
String metricType = data.second();
55+
double expectedValue = data.third();
56+
57+
doReturn(useRatio).when(drsAlgorithm).getDrsMetricUseRatio(1L);
58+
doReturn(metricType).when(drsAlgorithm).getDrsMetricType(1L);
59+
60+
assertEquals(expectedValue, drsAlgorithm.getMetricValue(1, used, free, total, null));
61+
}
62+
}
63+
64+
@Test
65+
public void testGetMetricValueWithSkipThreshold() {
66+
List<Ternary<Boolean, String, Double>> testData = List.of(
67+
new Ternary<>(true, "free", 0.15),
68+
new Ternary<>(false, "free", 15.0),
69+
new Ternary<>(true, "used", null),
70+
new Ternary<>(false, "used", null)
71+
);
72+
73+
long used = 80;
74+
long free = 15;
75+
long total = 100;
76+
77+
for (Ternary<Boolean, String, Double> data : testData) {
78+
boolean useRatio = data.first();
79+
String metricType = data.second();
80+
Double expectedValue = data.third();
81+
float skipThreshold = metricType.equals("free") ? 0.1f : 0.7f;
82+
83+
doReturn(useRatio).when(drsAlgorithm).getDrsMetricUseRatio(1L);
84+
doReturn(metricType).when(drsAlgorithm).getDrsMetricType(1L);
85+
86+
assertEquals(expectedValue, drsAlgorithm.getMetricValue(1L, used, free, total, skipThreshold));
87+
}
88+
}
89+
}

0 commit comments

Comments
 (0)