Skip to content

Commit

Permalink
adds testcase and polish (#1581)
Browse files Browse the repository at this point in the history
* adds testcase and polish

* adds more assertions
  • Loading branch information
rajadilipkolli authored Dec 19, 2024
1 parent 1c95019 commit 149c35d
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 50 deletions.
17 changes: 0 additions & 17 deletions boot-grafana-lgtm/config/prometheus/prometheus.yml

This file was deleted.

16 changes: 0 additions & 16 deletions boot-grafana-lgtm/docker/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
version: '3.8'
services:

grafanalgtm:
Expand All @@ -11,19 +10,4 @@ services:
- "3000:3000"
- "4317:4317"
- "4318:4318"

prometheus:
image: prom/prometheus:v3.0.1
extra_hosts: ['host.docker.internal:host-gateway']
container_name: prometheus
command:
- --enable-feature=exemplar-storage
- --config.file=/etc/prometheus/prometheus.yml
volumes:
- ../config/prometheus/prometheus.yml:/etc/prometheus/prometheus.yml:ro
ports:
- "9090:9090"
healthcheck:
interval: 5s
retries: 10
test: wget --no-verbose --tries=1 --spider http://localhost:9090/status || exit 1
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
import static io.restassured.RestAssured.when;
import static org.hamcrest.Matchers.empty;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.not;

import com.learning.grafanalgtm.common.ContainerConfig;
import io.restassured.RestAssured;
import io.restassured.authentication.PreemptiveBasicAuthScheme;
import io.restassured.http.ContentType;
Expand All @@ -19,7 +21,7 @@
import org.testcontainers.grafana.LgtmStackContainer;

@SpringBootTest(
classes = {TestGrafanaLGTMApplication.class},
classes = {ContainerConfig.class},
webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
class GrafanaLGTMApplicationTest {
Expand Down Expand Up @@ -58,7 +60,30 @@ void prometheus() {
.contentType(ContentType.JSON)
.body("status", is("success"))
.body("data.resultType", is("vector"))
.body("data.result", empty());
.body("data.result", empty())
.log()
.all();
}

@Test
void queryPrometheus() {
// calling endpoint to load metrics
when().get("/greetings").then().statusCode(HttpStatus.SC_OK);

RestAssured.port = lgtmContainer.getMappedPort(9090);
given().contentType(ContentType.URLENC)
.body("query=up")
.when()
.post("/api/v1/query")
.then()
.statusCode(HttpStatus.SC_OK)
.contentType(ContentType.JSON)
.body("status", is("success"))
.body("data.resultType", is("vector"))
.body("data.result", not(empty()))
.body("data.result[0].value[1]", is("1")) // Verify service is up
.log()
.all();
}

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -1,26 +1,13 @@
package com.learning.grafanalgtm;

import java.time.Duration;
import com.learning.grafanalgtm.common.ContainerConfig;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.boot.testcontainers.service.connection.ServiceConnection;
import org.springframework.context.annotation.Bean;
import org.testcontainers.grafana.LgtmStackContainer;
import org.testcontainers.utility.DockerImageName;

@TestConfiguration(proxyBeanMethods = false)
public class TestGrafanaLGTMApplication {

public static void main(String[] args) {
SpringApplication.from(GrafanaLGTMApplication::main)
.with(TestGrafanaLGTMApplication.class)
.with(ContainerConfig.class)
.run(args);
}

@Bean
@ServiceConnection
LgtmStackContainer lgtmContainer() {
return new LgtmStackContainer(DockerImageName.parse("grafana/otel-lgtm:0.7.8"))
.withStartupTimeout(Duration.ofMinutes(2));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.learning.grafanalgtm.common;

import java.time.Duration;
import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.boot.testcontainers.service.connection.ServiceConnection;
import org.springframework.context.annotation.Bean;
import org.testcontainers.grafana.LgtmStackContainer;
import org.testcontainers.utility.DockerImageName;

@TestConfiguration(proxyBeanMethods = false)
public class ContainerConfig {

@Bean
@ServiceConnection
LgtmStackContainer lgtmContainer() {
return new LgtmStackContainer(DockerImageName.parse("grafana/otel-lgtm:0.8.1"))
.withStartupTimeout(Duration.ofMinutes(2));
}
}

0 comments on commit 149c35d

Please sign in to comment.