Skip to content

Commit ed981d4

Browse files
author
Viet Nguyen
committed
New test for openshift integration
1 parent 225cfe9 commit ed981d4

File tree

9 files changed

+337
-8
lines changed

9 files changed

+337
-8
lines changed

pom.xml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
<hawkular.inventory.json.helper.version>0.3.2.Final</hawkular.inventory.json.helper.version>
5252
<resteasy.version>3.0.8.Final</resteasy.version>
5353
<excluded.tests>nothing-to-exclude</excluded.tests>
54+
<excluded.groups>none</excluded.groups>
5455
</properties>
5556

5657
<dependencies>
@@ -161,7 +162,7 @@
161162
<profile>
162163
<id>ci</id>
163164
<properties>
164-
<excluded.tests></excluded.tests>
165+
<excluded.groups>openshift,known-failure</excluded.groups>
165166
</properties>
166167
</profile>
167168
<profile>
@@ -177,8 +178,10 @@
177178
<groupId>org.apache.maven.plugins</groupId>
178179
<artifactId>maven-surefire-plugin</artifactId>
179180
<configuration>
181+
<groups></groups>
182+
<excludedGroups>${excluded.groups}</excludedGroups>
180183
<excludes>
181-
<exclude>${excluded.tests}</exclude>
184+
<excluded.tests>${excluded.tests}</excluded.tests>
182185
</excludes>
183186
</configuration>
184187
</plugin>

src/main/java/org/hawkular/client/metrics/MetricsClient.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@ public interface MetricsClient {
3939
*/
4040
boolean createTenant(Tenant tenant);
4141

42+
/**
43+
* Find metric definitions
44+
*/
45+
List<MetricDefinition> findMetricDefinitions(String tenantId, String type, String tags);
46+
4247
/**
4348
* Create a Gauge metric definition
4449
*/
@@ -59,6 +64,11 @@ public interface MetricsClient {
5964
*/
6065
List<GaugeDataPoint> getGaugeData(String tenantId, String metricId);
6166

67+
/**
68+
* Retrieve Gauge metric data with range
69+
*/
70+
List<GaugeDataPoint> getGaugeData(String tenantId, String metricId, long startTime, long Endtime);
71+
6272
// /**
6373
// * Retrieve most recent numeric metric data. See implementation for default time range.
6474
// */

src/main/java/org/hawkular/client/metrics/MetricsClientImpl.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,15 @@ public List<GaugeDataPoint> getGaugeData(String tenantId, String metricId) {
9393
return restApi().getGaugeData(tenantId, metricId);
9494
}
9595

96+
@Override
97+
public List<GaugeDataPoint> getGaugeData(String tenantId, String metricId,
98+
long startTime, long endTime) {
99+
logger.debug("getGaugeData(): tenant={}, metricId={}, startTime={}, endTime={}",
100+
tenantId, metricId, startTime, endTime);
101+
return restApi().getGaugeData(tenantId, metricId, startTime, endTime);
102+
}
103+
104+
96105
@Override
97106
public void createAvailabilityMetric(String tenantId,
98107
MetricDefinition metricDefinition) {
@@ -144,6 +153,12 @@ public List<CounterDataPoint> getCounterData(String tenantId, String metricId) {
144153
return restApi().getCounterData(tenantId, metricId);
145154
}
146155

156+
@Override
157+
public List<MetricDefinition> findMetricDefinitions(String tenantId, String type, String tags) {
158+
logger.debug("metrics(); tenantId={}, type={}, tags={}", tenantId, type, tags);
159+
return restApi().metrics(tenantId, type, tags);
160+
}
161+
147162

148163
// @Override
149164
// public List<NumericData> getNumericMetricData(String tenantId,

src/main/java/org/hawkular/client/metrics/MetricsRestApi.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,12 @@ public interface MetricsRestApi {
4949
@Path ("/tenants")
5050
Response createTenant(TenantParam tenant);
5151

52+
@GET
53+
@Path("metrics")
54+
List<MetricDefinition> metrics(@HeaderParam ("Hawkular-Tenant") String tenantId,
55+
@QueryParam("type") String type,
56+
@QueryParam("tags") String tags);
57+
5258
@POST
5359
@Path ("/gauges")
5460
void createGaugeMetric(@HeaderParam ("Hawkular-Tenant") String tenantId,

src/test/java/org/hawkular/client/test/metrics/CounterMetricTest.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@
2626
import org.testng.Reporter;
2727
import org.testng.annotations.Test;
2828

29-
import com.google.common.collect.Lists;
30-
3129
public class CounterMetricTest extends BaseTest {
3230

3331
private final MetricDefinition expectedDefinition = CounterDataGenerator.genDef();
@@ -62,6 +60,6 @@ public void getData() throws Exception {
6260
List<CounterDataPoint> actual =
6361
client().metrics().getCounterData(expectedDefinition.getTenantId(), expectedDefinition.getId());
6462
Reporter.log("Got: " + actual.toString(), true);
65-
Assert.assertEquals(Lists.reverse(actual), expectedData);
63+
Assert.assertEquals(actual, expectedData);
6664
}
6765
}

src/test/java/org/hawkular/client/test/metrics/GaugeMetricTest.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,7 @@
2626
import org.testng.Reporter;
2727
import org.testng.annotations.Test;
2828

29-
import com.google.common.collect.Lists;
30-
29+
@Test(groups={"known-failure"})
3130
public class GaugeMetricTest extends BaseTest {
3231

3332
private final MetricDefinition expectedMetric = GaugeDataGenerator.genDef();
@@ -67,7 +66,7 @@ public void addData() {
6766
public void getData() throws Exception {
6867
List<?> actual = client().metrics().getGaugeData(expectedMetric.getTenantId(), expectedMetric.getId());
6968
Reporter.log("Got: " + actual.toString(), true);
70-
Assert.assertEquals(Lists.reverse(actual), expectedData1);
69+
Assert.assertEquals(actual, expectedData1);
7170
}
7271

7372
}
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/*
2+
* Copyright 2015 Red Hat, Inc. and/or its affiliates
3+
* and other contributors as indicated by the @author tags.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* 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, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.hawkular.client.test.metrics.openshift;
18+
19+
import java.time.Duration;
20+
import java.time.Instant;
21+
import java.util.Arrays;
22+
import java.util.List;
23+
import java.util.Map;
24+
import java.util.stream.Collectors;
25+
26+
import org.apache.commons.lang3.ArrayUtils;
27+
import org.apache.commons.math3.stat.descriptive.DescriptiveStatistics;
28+
import org.hawkular.client.metrics.model.GaugeDataPoint;
29+
import org.testng.Assert;
30+
import org.testng.Reporter;
31+
import org.testng.annotations.Test;
32+
33+
/**
34+
*Find value=0 datapoints
35+
* @author vnguyen
36+
*/
37+
@Test(groups={"openshift"})
38+
public class CollectionRateDetailTest extends OpenshiftBaseTest {
39+
40+
public CollectionRateDetailTest() throws Exception {
41+
super();
42+
}
43+
44+
@Test
45+
public void findZeroValuesTest() throws Exception {
46+
String project = "default";
47+
String container = "hawkular-metrics";
48+
String testID = "namespace: " + project + ", container: " + container;
49+
50+
String metricID = super.getMetricID(project, container, METRIC_SUFFIX.CPU_USAGE);
51+
52+
long now = Instant.now().toEpochMilli();
53+
long start = now - Duration.ofHours(36).toMillis();
54+
long dur = start + Duration.ofHours(36).toMillis();
55+
56+
Reporter.log("Fetching large data set... may take a couple minutes", true);
57+
List<GaugeDataPoint> rawData = client().metrics().getGaugeData(TENANT_ID, metricID, start, dur);
58+
59+
Assert.assertNotNull(rawData, testID);
60+
Reporter.log("raw datapoints: " + rawData.size(), true);
61+
62+
List<Long> zeroList = findZeroValues(rawData);
63+
64+
Assert.assertTrue(zeroList == null || zeroList.size() == 0, testID);
65+
66+
Duration timeBucket = Duration.ofHours(1);
67+
68+
Map<Long, Integer> hist = OpenshiftBaseTest.makeHistogram(rawData, timeBucket);
69+
70+
Double[] result = hist.entrySet().stream()
71+
.map(x -> new Double(x.getValue()))
72+
.toArray(size -> new Double[size]);
73+
74+
double[] d = ArrayUtils.toPrimitive(result);
75+
76+
// drop the first and last as they are usually outliers
77+
DescriptiveStatistics stats = new DescriptiveStatistics(Arrays.copyOfRange(d,1, d.length-1));
78+
79+
Reporter.log(hist.toString(), true);
80+
Reporter.log("size: " + stats.getN(), true);
81+
Reporter.log("min/max: " + stats.getMin() + "/" + stats.getMax(), true);
82+
Reporter.log("mean: " + stats.getMean(), true);
83+
Reporter.log("variance: " + stats.getVariance(), true);
84+
Reporter.log("stddev: " + stats.getStandardDeviation(), true);
85+
}
86+
87+
private List<Long> findZeroValues(List<GaugeDataPoint> rawData) {
88+
List<Long> zeroList = rawData
89+
.stream()
90+
.filter(p -> !(p.getValue() != 0))
91+
.map(p -> p.getTimestamp())
92+
.collect(Collectors.toList());
93+
return zeroList;
94+
}
95+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
* Copyright 2015 Red Hat, Inc. and/or its affiliates
3+
* and other contributors as indicated by the @author tags.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* 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, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.hawkular.client.test.metrics.openshift;
18+
19+
import java.time.Duration;
20+
import java.util.List;
21+
import java.util.Map;
22+
import java.util.TreeMap;
23+
24+
import org.hawkular.client.metrics.model.GaugeDataPoint;
25+
import org.hawkular.client.metrics.model.MetricDefinition;
26+
import org.hawkular.client.test.BaseTest;
27+
import org.testng.Assert;
28+
import org.testng.annotations.Test;
29+
30+
@Test(groups={"openshift"})
31+
public class OpenshiftBaseTest extends BaseTest {
32+
public enum METRIC_SUFFIX {
33+
CPU_USAGE("cpu/usage"), MEM_USAGE("mem/usage");
34+
private String value;
35+
private METRIC_SUFFIX(String value) {
36+
this.value = value;
37+
}
38+
public String hawkularValue() {
39+
return value;
40+
}
41+
};
42+
43+
public static final String TENANT_ID="heapster";
44+
45+
public OpenshiftBaseTest() throws Exception {
46+
super();
47+
}
48+
49+
public String getMetricID(String podNamespace, String containerName, METRIC_SUFFIX metricSuffix) {
50+
List<MetricDefinition> defs = client().metrics().findMetricDefinitions(TENANT_ID,
51+
"gauge",
52+
"container_name:" + containerName + ",pod_namespace:" + podNamespace);
53+
Assert.assertNotNull(defs, "namespace: " + podNamespace + ", container: " + containerName);
54+
Assert.assertTrue(defs.size() > 1);
55+
String podId = defs.get(0).getTags().get("pod_id");
56+
return containerName + "/" + podId + "/" + metricSuffix.hawkularValue();
57+
}
58+
59+
public static Map<Long, Integer> makeHistogram(List<GaugeDataPoint> rawData, Duration timeBucket) {
60+
long t = timeBucket.toMillis();
61+
Map<Long, Integer> histogram = new TreeMap<Long, Integer>();
62+
for(GaugeDataPoint dp : rawData) {
63+
long key = dp.getTimestamp() / t;
64+
key = key * t;
65+
int c = histogram.getOrDefault(key, 0);
66+
histogram.put(key, c+1);
67+
}
68+
69+
return histogram;
70+
}
71+
72+
}

0 commit comments

Comments
 (0)