Skip to content

Commit c34a0c5

Browse files
committed
Merge remote-tracking branch 'origin/4.15' into main
Signed-off-by: Rohit Yadav <rohit.yadav@shapeblue.com>
2 parents f176837 + 5ed3246 commit c34a0c5

2 files changed

Lines changed: 43 additions & 2 deletions

File tree

plugins/metrics/src/main/java/org/apache/cloudstack/response/HostMetricsResponse.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,10 @@ public void setCpuAllocated(final String cpuAllocated, final Integer cpuNumber,
138138
}
139139
}
140140

141+
public String getCpuAllocatedGhz() {
142+
return cpuAllocated;
143+
}
144+
141145
public void setMemTotal(final Long memTotal) {
142146
if (memTotal != null) {
143147
this.memTotal = String.format("%.2f GB", memTotal / (1024.0 * 1024.0 * 1024.0));
@@ -182,13 +186,13 @@ public void setCpuUsageDisableThreshold(final String cpuUsed, final Float thresh
182186

183187
public void setCpuAllocatedThreshold(final String cpuAllocated, final Double threshold) {
184188
if (cpuAllocated != null && threshold != null) {
185-
this.cpuAllocatedThresholdExceeded = Double.valueOf(cpuAllocated.replace("%", "")) > (100.0 * threshold );
189+
this.cpuAllocatedThresholdExceeded = parseCPU(cpuAllocated) > (100.0 * threshold );
186190
}
187191
}
188192

189193
public void setCpuAllocatedDisableThreshold(final String cpuAllocated, final Float threshold) {
190194
if (cpuAllocated != null && threshold != null) {
191-
this.cpuAllocatedDisableThresholdExceeded = Double.valueOf(cpuAllocated.replace("%", "")) > (100.0 * threshold);
195+
this.cpuAllocatedDisableThresholdExceeded = parseCPU(cpuAllocated) > (100.0 * threshold);
192196
}
193197
}
194198

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package org.apache.cloudstack.response;
2+
3+
import org.junit.Assert;
4+
import org.junit.Test;
5+
6+
import com.cloud.utils.exception.CloudRuntimeException;
7+
8+
public class HostMetricsResponseTest {
9+
10+
@Test
11+
public void testSetCpuAllocatedWithZeroCpu() {
12+
final HostMetricsResponse hostResponse = new HostMetricsResponse();
13+
hostResponse.setCpuAllocated("50.25%", 0, 1000L);
14+
Assert.assertEquals("0.00 Ghz", hostResponse.getCpuAllocatedGhz());
15+
}
16+
17+
@Test
18+
public void testSetCpuAllocatedWithInfiniteCpuAllocated() {
19+
final HostMetricsResponse hostResponse = new HostMetricsResponse();
20+
hostResponse.setCpuAllocated("∞%", 10, 1000L);
21+
Assert.assertEquals("Infinity Ghz", hostResponse.getCpuAllocatedGhz());
22+
}
23+
24+
@Test(expected = CloudRuntimeException.class)
25+
public void testSetCpuAllocatedWithInvalidCpu() {
26+
final HostMetricsResponse hostResponse = new HostMetricsResponse();
27+
hostResponse.setCpuAllocated("abc", 10, 1000L);
28+
}
29+
30+
@Test
31+
public void testSetCpuAllocatedWithValidCpu() {
32+
final HostMetricsResponse hostResponse = new HostMetricsResponse();
33+
hostResponse.setCpuAllocated("50.25%", 10, 1000L);
34+
Assert.assertEquals("5.03 Ghz", hostResponse.getCpuAllocatedGhz());
35+
}
36+
37+
}

0 commit comments

Comments
 (0)