Skip to content
This repository has been archived by the owner on Nov 9, 2017. It is now read-only.

Commit

Permalink
Making pinger and end-to-end test compatible with the new inventory.
Browse files Browse the repository at this point in the history
  • Loading branch information
jkremser committed Apr 13, 2015
1 parent e40245e commit 601d935
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 62 deletions.
5 changes: 3 additions & 2 deletions kettle/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,12 @@

<!-- The versions of the components that go into our integration distribution -->
<version.org.hawkular.nest>1.0.0-SNAPSHOT</version.org.hawkular.nest>
<version.org.hawkular.inventory>1.0.0-SNAPSHOT</version.org.hawkular.inventory>
<version.org.hawkular.inventory>0.0.1-SNAPSHOT</version.org.hawkular.inventory>
<version.org.hawkular.alerts>1.0.0-SNAPSHOT</version.org.hawkular.alerts>
<version.org.hawkular.metrics>0.3.2-SNAPSHOT</version.org.hawkular.metrics>
<version.org.hawkular.console>1.0.0-SNAPSHOT</version.org.hawkular.console>
<version.org.hawkular.accounts>1.0.0-SNAPSHOT</version.org.hawkular.accounts>
<version.org.hawkular.pinger>1.0.0-SNAPSHOT</version.org.hawkular.pinger>
<version.org.keycloak>1.1.0.Final</version.org.keycloak>
</properties>

Expand All @@ -65,7 +66,7 @@
<dependency>
<groupId>org.hawkular</groupId>
<artifactId>hawkular-pinger</artifactId>
<version>${version.org.hawkular.inventory}</version>
<version>${version.org.hawkular.pinger}</version>
<type>war</type>
</dependency>
<dependency>
Expand Down
2 changes: 1 addition & 1 deletion modules/end-to-end-test/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
<dependency>
<groupId>org.hawkular.inventory</groupId>
<artifactId>inventory-api</artifactId>
<version>1.0.0-SNAPSHOT</version>
<version>0.0.1-SNAPSHOT</version>
</dependency>

<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,64 +17,85 @@

package org.hawkular.integration.test

import org.hawkular.inventory.api.MetricDefinition
import org.hawkular.inventory.api.MetricUnit
import org.hawkular.inventory.api.Resource
import org.hawkular.inventory.api.ResourceType
import org.hawkular.inventory.api.model.Environment
import org.hawkular.inventory.api.model.Metric
import org.hawkular.inventory.api.model.MetricType
import org.hawkular.inventory.api.model.Resource
import org.hawkular.inventory.api.model.ResourceType
import org.hawkular.inventory.api.model.Tenant

import org.junit.Test

import static junit.framework.Assert.assertEquals

class Scenario1 extends AbstractTestBase {

def tenantId = "i-test"
def environmentId = "test"
def hawk_id = "hawkular_web"

@Test
public void testScenario() throws Exception {


// 1) Add the resource to be monitored

def hawk_web = new Resource();

hawk_web.id = hawk_id;
hawk_web.type = ResourceType.URL
hawk_web.addParameter("url","http://hawkular.github.io")

def response = client.post(path: "inventory/$tenantId/resources", body : hawk_web)

assertEquals(200, response.status)

// 2) Add the "ping" status + time metrics
def statusCode = new MetricDefinition("status.code");
statusCode.description = "Status code returned from ping"
def statusTime = new MetricDefinition("status.time",MetricUnit.MILLI_SECOND);
statusTime.description = "Time to ping the target in ms"

response = client.post(path: "inventory/$tenantId/resources/$hawk_id/metrics",
body: [ statusCode, statusTime])

def good = response.status == 200 || response.status == 304
assert good

// 3 inform pinger is internal

// 4 simulate ping + response - metrics for ~ the last 30 minutes
// 1) create tenant in inventory
def tenant = new Tenant(tenantId)
def response = client.post(path: "inventory/tenants", body : tenant)
assertResponseOk(response.status)

// 2) create environment in inventory
def environment = new Environment(environmentId)
response = client.post(path: "inventory/$tenantId/environments", body : environment)
assertResponseOk(response.status)

// 3) create resource type in inventory
def res_type = new ResourceType(tenantId, "URL", "1.0")
response = client.post(path: "inventory/$tenantId/resourceTypes", body : res_type)
assertResponseOk(response.status)

// 4) create resource in inventory
def hawk_web = new Resource(tenantId, environmentId, hawk_id, res_type)
hawk_web.getProperties().put("url", "http://hawkular.org")
response = client.post(path: "inventory/$tenantId/$environmentId/resources", body : hawk_web)
assertResponseOk(response.status)

// 5) create metric types ("ping" status + time metrics)
def status_code_type = new MetricType(tenantId, "status.code.type")
status_code_type.getProperties().put("description", "Status code returned from ping")
def status_time_type = new MetricType(tenantId, "status.time.type", MetricUnit.MILLI_SECOND)
status_time_type.getProperties().put("description", "Time to ping the target in ms")
response = client.post(path: "inventory/$tenantId/metricTypes", body: status_code_type)
assertResponseOk(response.status)
response = client.post(path: "inventory/$tenantId/metricTypes", body: status_time_type)
assertResponseOk(response.status)

// 6) create metrics
def status_code = new Metric(tenantId, environmentId, "status.code", status_code_type)
def status_time = new Metric(tenantId, environmentId, "status.time", status_time_type)
response = client.post(path: "inventory/$tenantId/$environmentId/metrics", body: status_code)
assertResponseOk(response.status)
response = client.post(path: "inventory/$tenantId/$environmentId/metrics", body: status_time)
assertResponseOk(response.status)

// 7) assign metrics to the resource
response = client.post(path: "inventory/$tenantId/$environmentId/resources/$hawk_id/metrics",
body: ["status.code", "status.time"]
)
assertResponseOk(response.status)

// 8 informing pinger is internal (bus msg)

// 9 simulate ping + response - metrics for ~ the last 30 minutes
for (int i = -30 ; i <-3 ; i++ ) {
postMetricValue(hawk_id, statusTime, 100 + i, i)
postMetricValue(hawk_id, statusCode, 200, i)
postMetricValue(hawk_id, status_time.id, 100 + i, i)
postMetricValue(hawk_id, status_code.id, 200, i)
}

postMetricValue(hawk_id, statusCode, 500, -2)
postMetricValue(hawk_id, statusCode, 404, -1)
postMetricValue(hawk_id, statusCode, 200, 0)
postMetricValue(hawk_id, statusTime, 42, 0)

postMetricValue(hawk_id, status_code.id, 500, -2)
postMetricValue(hawk_id, status_code.id, 404, -1)
postMetricValue(hawk_id, status_code.id, 200, 0)
postMetricValue(hawk_id, status_time.id, 42, 0)

// 5 was simulated in step 4 as well

// 6 Get values for a chart - last 4h data
// 10 Get values for a chart - last 4h data

def end = System.currentTimeMillis()
def start = end - 4 *3600 * 1000 // 4h earlier
Expand All @@ -83,21 +104,26 @@ class Scenario1 extends AbstractTestBase {

println(response.data)

// 7 define an alert
// 11 define an alert

// response = client.post(path: "alerts/triggers/")

}

private void postMetricValue(String resourceId, MetricDefinition metric, int value, int timeSkewMinutes = 0) {
private void assertResponseOk(int responseCode) {
assertTrue("Response code should be 2xx or 304", (responseCode.status >= 200 && responseCode.status < 300) ||
responseCode.status == 304)
}

private void postMetricValue(String resourceId, String metricName, int value, int timeSkewMinutes = 0) {
def response
def now = System.currentTimeMillis()
def tmp = "$resourceId.$metric.name"
def tmp = "$resourceId.$metricName"

long time = now + (timeSkewMinutes * 60 * 1000)

response = client.post(path: "/hawkular-metrics/$tenantId/metrics/numeric/$tmp/data",
body: [[timestamp: time, value: value]])
assertEquals(200, response.status)
assertResponseOk(response.status)
}
}
2 changes: 1 addition & 1 deletion modules/pinger/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
<dependency>
<groupId>org.hawkular.inventory</groupId>
<artifactId>inventory-api</artifactId>
<version>1.0.0-SNAPSHOT</version>
<version>0.0.1-SNAPSHOT</version>
</dependency>

<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@
import com.google.gson.GsonBuilder;
import org.hawkular.bus.common.SimpleBasicMessage;
import org.hawkular.bus.common.consumer.BasicMessageListener;
import org.hawkular.inventory.api.Resource;
import org.hawkular.inventory.api.ResourceType;
import org.hawkular.inventory.api.model.Resource;

import javax.ejb.ActivationConfigProperty;
import javax.ejb.EJB;
Expand Down Expand Up @@ -64,10 +63,10 @@ public void onBasicMessage(SimpleBasicMessage message) {
Gson gson = new GsonBuilder().create();

Resource resource = gson.fromJson(payload, Resource.class);
if (!resource.getType().equals(ResourceType.URL)) {
if (!"URL".equals(resource.getType().getId())) {
return;
}
String url = resource.getParameters().get("url");
String url = (String) resource.getProperties().get("url");
PingDestination destination = new PingDestination(resource.getId(), url);

if ("resource_added".equals(code)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,10 @@ public class PingManager {

@PostConstruct
public void startUp() {

Client client = ClientBuilder.newClient();
WebTarget target = client.target("http://localhost:8080/hawkular/inventory/" + tenantId + "/resources");
target.queryParam("type", "URL");
// TODO: inventory does not have to be co-located
WebTarget target = client.target("http://localhost:8080/hawkular/inventory/" + tenantId
+"/resourceTypes/URL/resources");
Response response = target.request().get();

if (response.getStatus() == 200) {
Expand All @@ -77,17 +77,15 @@ public void startUp() {

for (Object o : list) {
if (o instanceof Map) {

Map<String, Object> m = (Map) o;
String id = (String) m.get("id");
String type = (String) m.get("type");
Map<String, String> params = (Map<String, String>) m.get("parameters");
Map<String, Object> type = (Map<String, Object>) m.get("type");
Map<String, String> params = (Map<String, String>) m.get("properties");
String url = params.get("url");
destinations.add(new PingDestination(id, url));
}
}
}
else {
} else {
Log.LOG.wNoInventoryFound(response.getStatus(), response.getStatusInfo().getReasonPhrase());
}
}
Expand Down

0 comments on commit 601d935

Please sign in to comment.