Skip to content

Commit

Permalink
Refactored tests to remove repeated setup and assert code.
Browse files Browse the repository at this point in the history
  • Loading branch information
tkowalcz committed Aug 3, 2021
1 parent 51f28da commit 49b99ba
Show file tree
Hide file tree
Showing 13 changed files with 335 additions and 734 deletions.
1 change: 0 additions & 1 deletion log4j2-appender/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@
<scope>test</scope>
</dependency>


<dependency>
<groupId>com.google.code.java-allocation-instrumenter</groupId>
<artifactId>java-allocation-instrumenter</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,13 @@
package pl.tkowalcz.tjahzi.log4j2;

import io.restassured.RestAssured;
import io.restassured.http.ContentType;
import io.restassured.parsing.Parser;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.awaitility.Awaitility;
import org.awaitility.Durations;
import org.hamcrest.BaseMatcher;
import org.hamcrest.Description;
import org.junit.jupiter.api.Test;
import pl.tkowalcz.tjahzi.log4j2.infra.LokiAssert;

import java.net.URI;
import java.util.List;
import java.util.concurrent.TimeUnit;

import static io.restassured.RestAssured.given;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.hasItems;
import static org.hamcrest.CoreMatchers.*;
import static pl.tkowalcz.tjahzi.log4j2.infra.IntegrationTest.loadConfig;

class GrafanaCloudAppenderTest {

Expand All @@ -28,74 +17,34 @@ void shouldSendData() throws Exception {
System.setProperty("loki.host", "logs-prod-us-central1.grafana.net");
System.setProperty("loki.port", "443");

URI uri = getClass()
.getClassLoader()
.getResource("grafana-cloud-appender-test-configuration.xml")
.toURI();

((org.apache.logging.log4j.core.LoggerContext) LogManager.getContext(false))
.setConfigLocation(uri);
loadConfig("grafana-cloud-appender-test-configuration.xml");
Logger logger = LogManager.getLogger(GrafanaCloudAppenderTest.class);

String expectedLogLine = RandomStringUtils.randomAlphabetic(42);
long expectedTimestamp = System.currentTimeMillis();

// When
Logger logger = LogManager.getLogger(GrafanaCloudAppenderTest.class);
logger.info(expectedLogLine);

// Then
RestAssured.reset();
RestAssured.registerParser("text/plain", Parser.JSON);

Awaitility
.await()
.atMost(Durations.ONE_MINUTE)
.pollInterval(Durations.ONE_SECOND)
.ignoreExceptions()
.untilAsserted(() -> {
given()
.contentType(ContentType.URLENC)
.urlEncodingEnabled(false)
.formParam("query=%7Bserver%3D%22127.0.0.1%22%7D")
.auth()
.preemptive()
.basic(
System.getenv("grafana_username"),
System.getenv("grafana_password")
)
.when()
.get("https://logs-prod-us-central1.grafana.net/loki/api/v1/query_range")
.then()
.log()
.all()
.statusCode(200)
.body("status", equalTo("success"))
.body("data.result.size()", equalTo(1))
.body("data.result[0].stream.server", equalTo("127.0.0.1"))
.body("data.result[0].values", hasItems(
new BaseMatcher<>() {
@Override
public boolean matches(Object o) {
List<Object> list = (List<Object>) o;
if (list.size() != 2) {
return false;
}

long actualTimestamp = Long.parseLong(list.get(0).toString());
String actualLogLine = list.get(1).toString();

return actualLogLine.contains(expectedLogLine)
&& (expectedTimestamp - actualTimestamp) < TimeUnit.MINUTES.toMillis(1);
}

@Override
public void describeTo(Description description) {

}
}

)
);
});
LokiAssert.assertThat()
.calling("https://logs-prod-us-central1.grafana.net/loki/api/v1/query_range")
.withCredentials(
System.getenv("grafana_username"),
System.getenv("grafana_password")
)
.returns(response -> response
.body("data.result.size()", equalTo(1))
.body("data.result[0].stream.server", equalTo("127.0.0.1"))
.body(
"data.result.values",
hasItems(
hasItems(
hasItems(
containsString(expectedLogLine)
)
)
)
)
);
}
}
Original file line number Diff line number Diff line change
@@ -1,85 +1,33 @@
package pl.tkowalcz.tjahzi.log4j2;

import io.restassured.RestAssured;
import io.restassured.http.ContentType;
import io.restassured.parsing.Parser;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.awaitility.Awaitility;
import org.awaitility.Durations;
import org.junit.jupiter.api.Test;
import org.testcontainers.containers.BindMode;
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.containers.wait.strategy.Wait;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;
import pl.tkowalcz.tjahzi.log4j2.infra.IntegrationTest;
import pl.tkowalcz.tjahzi.log4j2.infra.LokiAssert;

import java.net.URI;

import static io.restassured.RestAssured.given;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.hasItems;

@Testcontainers
class LogLineFragmentationTest {

@Container
public GenericContainer loki = new GenericContainer("grafana/loki:latest")
.withCommand("-config.file=/etc/loki-config.yaml")
.withClasspathResourceMapping("loki-config.yaml",
"/etc/loki-config.yaml",
BindMode.READ_ONLY
)
.waitingFor(
Wait.forHttp("/ready")
.forPort(3100)
)
.withExposedPorts(3100);
class LogLineFragmentationTest extends IntegrationTest {

@Test
void shouldFragmentMessageOverLimit() throws Exception {
void shouldFragmentMessageOverLimit() {
// Given
System.setProperty("loki.host", loki.getHost());
System.setProperty("loki.port", loki.getFirstMappedPort().toString());

URI uri = getClass()
.getClassLoader()
.getResource("appender-test-large-log-line-fragmentation.xml")
.toURI();

((org.apache.logging.log4j.core.LoggerContext) LogManager.getContext(false))
.setConfigLocation(uri);
loadConfig("appender-test-large-log-line-fragmentation.xml");
Logger logger = LogManager.getLogger(LogLineFragmentationTest.class);

String unfragmentedLogLine = RandomStringUtils.randomAlphanumeric(100 * 1024 - 1);
String fragmentedLogLine = RandomStringUtils.randomAlphanumeric(110 * 1024);

long expectedTimestamp = System.currentTimeMillis();
Logger logger = LogManager.getLogger(LogLineFragmentationTest.class);

// When
logger.info(unfragmentedLogLine);
logger.info(fragmentedLogLine);

// Then
RestAssured.port = loki.getFirstMappedPort();
RestAssured.baseURI = "http://" + loki.getHost();
RestAssured.registerParser("text/plain", Parser.JSON);

Awaitility
.await()
.atMost(Durations.ONE_MINUTE)
.pollInterval(Durations.ONE_SECOND)
.ignoreExceptions()
.untilAsserted(() -> given()
.contentType(ContentType.URLENC)
.urlEncodingEnabled(false)
.formParam("&start=" + expectedTimestamp + "&limit=1000&query=%7Bserver%3D%22127.0.0.1%22%7D")
.when()
.get("/loki/api/v1/query_range")
.then()
.statusCode(200)
.body("status", equalTo("success"))
LokiAssert.assertThat(loki)
.returns(response -> response
.body("data.result.size()", equalTo(1))
.body("data.result[0].stream.server", equalTo("127.0.0.1"))
.body("data.result[0].values.size()", equalTo(3))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,57 +1,26 @@
package pl.tkowalcz.tjahzi.log4j2;

import io.restassured.RestAssured;
import io.restassured.http.ContentType;
import io.restassured.parsing.Parser;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.awaitility.Awaitility;
import org.awaitility.Durations;
import org.hamcrest.BaseMatcher;
import org.hamcrest.Description;
import org.junit.jupiter.api.Test;
import org.testcontainers.containers.BindMode;
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.containers.wait.strategy.Wait;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;
import pl.tkowalcz.tjahzi.log4j2.infra.IntegrationTest;
import pl.tkowalcz.tjahzi.log4j2.infra.LokiAssert;

import java.net.URI;
import java.util.List;
import java.util.concurrent.TimeUnit;

import static io.restassured.RestAssured.given;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.hasItems;

@Testcontainers
class LokiAppenderLargeBatchesTest {

@Container
public GenericContainer loki = new GenericContainer("grafana/loki:latest")
.withCommand("-config.file=/etc/loki-config.yaml")
.withClasspathResourceMapping("loki-config.yaml",
"/etc/loki-config.yaml",
BindMode.READ_ONLY)
.waitingFor(
Wait.forHttp("/ready")
.forPort(3100)
)
.withExposedPorts(3100);
class LokiAppenderLargeBatchesTest extends IntegrationTest {

@Test
void shouldSendData() throws Exception {
void shouldSendData() {
// Given
System.setProperty("loki.host", loki.getHost());
System.setProperty("loki.port", loki.getFirstMappedPort().toString());

URI uri = getClass()
.getClassLoader()
.getResource("appender-test-large-batches.xml")
.toURI();

((org.apache.logging.log4j.core.LoggerContext) LogManager.getContext(false))
.setConfigLocation(uri);
loadConfig("appender-test-large-batches.xml");
Logger logger = LogManager.getLogger(LokiAppenderLargeBatchesTest.class);

String expectedLogLine = "Cupcake ipsum dolor sit amet cake wafer. " +
"Souffle jelly beans biscuit topping. " +
Expand All @@ -64,60 +33,42 @@ void shouldSendData() throws Exception {
"Souffle cake muffin liquorice tart souffle pie sesame snaps.";

long expectedTimestamp = System.currentTimeMillis();
Logger logger = LogManager.getLogger(LokiAppenderLargeBatchesTest.class);

// When
for (int i = 0; i < 1000; i++) {
logger.info(i + " " + expectedLogLine);
}

// Then
RestAssured.port = loki.getFirstMappedPort();
RestAssured.baseURI = "http://" + loki.getHost();
RestAssured.registerParser("text/plain", Parser.JSON);

Awaitility
.await()
.atMost(Durations.ONE_MINUTE)
.pollInterval(Durations.ONE_SECOND)
.ignoreExceptions()
.untilAsserted(() -> {
given()
.contentType(ContentType.URLENC)
.urlEncodingEnabled(false)
.formParam("&start=" + expectedTimestamp + "&limit=1000&query=%7Bserver%3D%22127.0.0.1%22%7D")
.when()
.get("/loki/api/v1/query_range")
.then()
.log()
.all()
.statusCode(200)
.body("status", equalTo("success"))
.body("data.result.size()", equalTo(1))
.body("data.result[0].stream.server", equalTo("127.0.0.1"))
.body("data.result[0].values.size()", equalTo(1000))
.body("data.result[0].values", hasItems(new BaseMatcher<>() {
@Override
public boolean matches(Object o) {
List<Object> list = (List<Object>) o;
if (list.size() != 2) {
return false;
}

long actualTimestamp = Long.parseLong(list.get(0).toString());
String actualLogLine = list.get(1).toString();

return actualLogLine.contains(expectedLogLine)
&& (expectedTimestamp - actualTimestamp) < TimeUnit.MINUTES.toMillis(1);
}

@Override
public void describeTo(Description description) {

}
}

));
});
LokiAssert.assertThat(loki)
.withFormParam("&start=" + expectedTimestamp + "&limit=1000&query=%7Bserver%3D%22127.0.0.1%22%7D")
.returns(response -> response
.body("data.result.size()", equalTo(1))
.body("data.result[0].stream.server", equalTo("127.0.0.1"))
.body("data.result[0].values.size()", equalTo(1000))
.body(
"data.result[0].values",
hasItems(new BaseMatcher<>() {
@Override
public boolean matches(Object o) {
List<Object> list = (List<Object>) o;
if (list.size() != 2) {
return false;
}

long actualTimestamp = Long.parseLong(list.get(0).toString());
String actualLogLine = list.get(1).toString();

return actualLogLine.contains(expectedLogLine)
&& (expectedTimestamp - actualTimestamp) < TimeUnit.MINUTES.toMillis(1);
}

@Override
public void describeTo(Description description) {

}
}
))
);
}
}
Loading

0 comments on commit 49b99ba

Please sign in to comment.