mirror of
https://github.com/tiennm99/tjahzi.git
synced 2026-10-14 18:19:20 +00:00
This time also cleaning up tests on logback side.
This commit is contained in:
1 parent
35bd48c62c
commit
dec21db1c2
14 files changed
+298
-613
No files matched your search
+3
-18
@@ -1,27 +1,19 @@
|
||||
package pl.tkowalcz.tjahzi.log4j2;
|
||||
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.core.LoggerContext;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static pl.tkowalcz.tjahzi.log4j2.infra.IntegrationTest.loadConfig;
|
||||
|
||||
class BufferSizeConfigurationTest {
|
||||
|
||||
@Test
|
||||
void shouldUseDefaultWhenLogBufferSizeIsNotSetViaConfiguration() throws URISyntaxException {
|
||||
// Given
|
||||
URI uri = getClass()
|
||||
.getClassLoader()
|
||||
.getResource("appender-test-with-buffer-size-megabytes-unset.xml")
|
||||
.toURI();
|
||||
|
||||
// When
|
||||
LoggerContext context = (LoggerContext) LogManager.getContext(false);
|
||||
context.setConfigLocation(uri);
|
||||
LoggerContext context = loadConfig("appender-test-with-buffer-size-megabytes-unset.xml");
|
||||
|
||||
// Then
|
||||
LokiAppender loki = context.getConfiguration().getAppender("Loki");
|
||||
@@ -31,15 +23,8 @@ class BufferSizeConfigurationTest {
|
||||
|
||||
@Test
|
||||
void shouldSetLogBufferSizeViaConfiguration() throws URISyntaxException {
|
||||
// Given
|
||||
URI uri = getClass()
|
||||
.getClassLoader()
|
||||
.getResource("appender-test-with-buffer-size-megabytes-set-to-64.xml")
|
||||
.toURI();
|
||||
|
||||
// When
|
||||
LoggerContext context = (LoggerContext) LogManager.getContext(false);
|
||||
context.setConfigLocation(uri);
|
||||
LoggerContext context = loadConfig("appender-test-with-buffer-size-megabytes-set-to-64.xml");
|
||||
|
||||
// Then
|
||||
LokiAppender loki = context.getConfiguration().getAppender("Loki");
|
||||
|
||||
@@ -5,6 +5,7 @@ import org.apache.logging.log4j.Logger;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import pl.tkowalcz.tjahzi.log4j2.infra.IntegrationTest;
|
||||
|
||||
import java.io.File;
|
||||
import java.net.URI;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Paths;
|
||||
@@ -28,7 +29,7 @@ class LokiAppenderReloadTest extends IntegrationTest {
|
||||
.getResource("appender-test-reload-after.xml")
|
||||
.toURI();
|
||||
|
||||
URI uri = new URI("file://" + Paths.get(uriBefore).toFile().getParent() + "/appender-test-reload.xml");
|
||||
URI uri = new File(Paths.get(uriBefore).toFile().getParent(), "appender-test-reload.xml").toURI();
|
||||
Files.copy(Paths.get(uriBefore), Paths.get(uri), StandardCopyOption.REPLACE_EXISTING);
|
||||
|
||||
loadConfig(uri);
|
||||
|
||||
@@ -1,44 +1,24 @@
|
||||
package pl.tkowalcz.tjahzi.logback;
|
||||
|
||||
import ch.qos.logback.classic.LoggerContext;
|
||||
import ch.qos.logback.classic.joran.JoranConfigurator;
|
||||
import com.google.monitoring.runtime.instrumentation.AllocationRecorder;
|
||||
import org.junit.jupiter.api.Disabled;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
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 org.testcontainers.shaded.com.google.common.util.concurrent.Uninterruptibles;
|
||||
import pl.tkowalcz.tjahzi.logback.infra.IntegrationTest;
|
||||
|
||||
import java.net.URI;
|
||||
import java.util.Comparator;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.atomic.AtomicLong;
|
||||
|
||||
@Testcontainers
|
||||
public class AllocationTest {
|
||||
|
||||
@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);
|
||||
public class AllocationTest extends IntegrationTest {
|
||||
|
||||
@Test
|
||||
@Disabled
|
||||
void shouldSendData() throws Exception {
|
||||
void shouldSendData() {
|
||||
AtomicLong totalAllocatedMemory = new AtomicLong();
|
||||
Map<String, AtomicLong> allocatedMemory = new HashMap<>();
|
||||
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
|
||||
@@ -53,21 +33,7 @@ public class AllocationTest {
|
||||
}));
|
||||
|
||||
// Given
|
||||
System.setProperty("loki.host", loki.getHost());
|
||||
System.setProperty("loki.port", loki.getFirstMappedPort().toString());
|
||||
|
||||
URI uri = getClass()
|
||||
.getClassLoader()
|
||||
.getResource("basic-appender-test-configuration.xml")
|
||||
.toURI();
|
||||
|
||||
LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory();
|
||||
|
||||
JoranConfigurator configurator = new JoranConfigurator();
|
||||
configurator.setContext(context);
|
||||
|
||||
context.reset();
|
||||
configurator.doConfigure(uri.toURL());
|
||||
LoggerContext context = loadConfig("basic-appender-test-configuration.xml");
|
||||
|
||||
String logLine = "Cupcake ipsum dolor sit amet cake wafer. " +
|
||||
"Souffle jelly beans biscuit topping. " +
|
||||
@@ -99,7 +65,7 @@ public class AllocationTest {
|
||||
return;
|
||||
}
|
||||
|
||||
if(desc.contains("LoggingEvent")) {
|
||||
if (desc.contains("LoggingEvent")) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
+4
-31
@@ -1,33 +1,18 @@
|
||||
package pl.tkowalcz.tjahzi.logback;
|
||||
|
||||
import ch.qos.logback.classic.LoggerContext;
|
||||
import ch.qos.logback.classic.joran.JoranConfigurator;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import pl.tkowalcz.tjahzi.logback.util.TestUtil;
|
||||
|
||||
import java.net.URI;
|
||||
import pl.tkowalcz.tjahzi.logback.infra.TestUtil;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static pl.tkowalcz.tjahzi.logback.infra.IntegrationTest.loadConfig;
|
||||
|
||||
class BufferSizeConfigurationTest {
|
||||
|
||||
@Test
|
||||
void shouldUseDefaultWhenLogBufferSizeIsNotSetViaConfiguration() throws Exception {
|
||||
// Given
|
||||
URI uri = getClass()
|
||||
.getClassLoader()
|
||||
.getResource("appender-test-with-buffer-size-megabytes-unset.xml")
|
||||
.toURI();
|
||||
|
||||
// When
|
||||
LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory();
|
||||
|
||||
JoranConfigurator configurator = new JoranConfigurator();
|
||||
configurator.setContext(context);
|
||||
|
||||
context.reset();
|
||||
configurator.doConfigure(uri.toURL());
|
||||
LoggerContext context = loadConfig("appender-test-with-buffer-size-megabytes-unset.xml");
|
||||
|
||||
// Then
|
||||
LokiAppender loki = TestUtil.getLokiAppender(context);
|
||||
@@ -36,20 +21,8 @@ class BufferSizeConfigurationTest {
|
||||
|
||||
@Test
|
||||
void shouldSetLogBufferSizeViaConfiguration() throws Exception {
|
||||
// Given
|
||||
URI uri = getClass()
|
||||
.getClassLoader()
|
||||
.getResource("appender-test-with-buffer-size-megabytes-set-to-64.xml")
|
||||
.toURI();
|
||||
|
||||
// When
|
||||
LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory();
|
||||
|
||||
JoranConfigurator configurator = new JoranConfigurator();
|
||||
configurator.setContext(context);
|
||||
|
||||
context.reset();
|
||||
configurator.doConfigure(uri.toURL());
|
||||
LoggerContext context = loadConfig("appender-test-with-buffer-size-megabytes-set-to-64.xml");
|
||||
|
||||
// Then
|
||||
LokiAppender loki = TestUtil.getLokiAppender(context);
|
||||
|
||||
+21
-110
@@ -2,139 +2,50 @@ package pl.tkowalcz.tjahzi.logback;
|
||||
|
||||
import ch.qos.logback.classic.Logger;
|
||||
import ch.qos.logback.classic.LoggerContext;
|
||||
import ch.qos.logback.classic.joran.JoranConfigurator;
|
||||
import io.restassured.RestAssured;
|
||||
import io.restassured.http.ContentType;
|
||||
import io.restassured.parsing.Parser;
|
||||
import org.awaitility.Awaitility;
|
||||
import org.awaitility.Durations;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.slf4j.LoggerFactory;
|
||||
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.logback.infra.IntegrationTest;
|
||||
|
||||
import java.net.URI;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.hamcrest.CoreMatchers.equalTo;
|
||||
import static org.hamcrest.CoreMatchers.nullValue;
|
||||
import static org.hamcrest.Matchers.greaterThanOrEqualTo;
|
||||
import static pl.tkowalcz.tjahzi.logback.infra.LokiAssert.assertThat;
|
||||
|
||||
@Testcontainers
|
||||
class LogLevelLabelConfigurationTest {
|
||||
|
||||
@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 LogLevelLabelConfigurationTest extends IntegrationTest {
|
||||
|
||||
@Test
|
||||
void shouldWorkWithNoLogLevelConfigured() throws Exception {
|
||||
void shouldWorkWithNoLogLevelConfigured() {
|
||||
// Given
|
||||
System.setProperty("loki.host", loki.getHost());
|
||||
System.setProperty("loki.port", loki.getFirstMappedPort().toString());
|
||||
|
||||
URI uri = getClass()
|
||||
.getClassLoader()
|
||||
.getResource("appender-test-with-log-label-unset.xml")
|
||||
.toURI();
|
||||
|
||||
LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory();
|
||||
|
||||
JoranConfigurator configurator = new JoranConfigurator();
|
||||
configurator.setContext(context);
|
||||
|
||||
context.reset();
|
||||
configurator.doConfigure(uri.toURL());
|
||||
LoggerContext context = loadConfig("appender-test-with-log-label-unset.xml");
|
||||
|
||||
// When
|
||||
Logger logger = context.getLogger(LogLevelLabelConfigurationTest.class);
|
||||
logger.info("Test");
|
||||
|
||||
// Then
|
||||
RestAssured.port = loki.getFirstMappedPort();
|
||||
RestAssured.baseURI = "http://" + loki.getHost();
|
||||
RestAssured.registerParser("text/plain", Parser.JSON);
|
||||
|
||||
Awaitility
|
||||
.await()
|
||||
.atMost(Durations.TEN_SECONDS)
|
||||
.pollInterval(Durations.ONE_SECOND)
|
||||
.ignoreExceptions()
|
||||
.untilAsserted(() -> {
|
||||
given()
|
||||
.contentType(ContentType.URLENC)
|
||||
.urlEncodingEnabled(false)
|
||||
.formParam("query=%7Btest%3D%22shouldWorkWithNoLogLevelConfigured%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.log_level", nullValue());
|
||||
});
|
||||
assertThat(loki)
|
||||
.withFormParam("query=%7Btest%3D%22shouldWorkWithNoLogLevelConfigured%22%7D")
|
||||
.returns(response -> response
|
||||
.body("status", equalTo("success"))
|
||||
.body("data.result.size()", equalTo(1))
|
||||
.body("data.result[0].stream.log_level", nullValue())
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldSendLogLevelAsConfigured() throws Exception {
|
||||
void shouldSendLogLevelAsConfigured() {
|
||||
// Given
|
||||
System.setProperty("loki.host", loki.getHost());
|
||||
System.setProperty("loki.port", loki.getFirstMappedPort().toString());
|
||||
|
||||
URI uri = getClass()
|
||||
.getClassLoader()
|
||||
.getResource("appender-test-with-log-label-set.xml")
|
||||
.toURI();
|
||||
|
||||
LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory();
|
||||
|
||||
JoranConfigurator configurator = new JoranConfigurator();
|
||||
configurator.setContext(context);
|
||||
|
||||
context.reset();
|
||||
configurator.doConfigure(uri.toURL());
|
||||
LoggerContext context = loadConfig("appender-test-with-log-label-set.xml");
|
||||
|
||||
// When
|
||||
Logger logger = context.getLogger(LogLevelLabelConfigurationTest.class);
|
||||
logger.info("Test");
|
||||
|
||||
// Then
|
||||
RestAssured.port = loki.getFirstMappedPort();
|
||||
RestAssured.baseURI = "http://" + loki.getHost();
|
||||
RestAssured.registerParser("text/plain", Parser.JSON);
|
||||
|
||||
Awaitility
|
||||
.await()
|
||||
.atMost(Durations.TEN_SECONDS)
|
||||
.pollInterval(Durations.ONE_SECOND)
|
||||
.ignoreExceptions()
|
||||
.untilAsserted(() -> {
|
||||
given()
|
||||
.contentType(ContentType.URLENC)
|
||||
.urlEncodingEnabled(false)
|
||||
.formParam("query=%7Btest%3D%22shouldSendLogLevelAsConfigured%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.log_level", equalTo("INFO"));
|
||||
});
|
||||
assertThat(loki)
|
||||
.withFormParam("query=%7Btest%3D%22shouldSendLogLevelAsConfigured%22%7D")
|
||||
.returns(response -> response
|
||||
.body("status", equalTo("success"))
|
||||
.body("data.result.size()", equalTo(1))
|
||||
.body("data.result[0].stream.log_level", equalTo("INFO"))
|
||||
);
|
||||
}
|
||||
}
|
||||
+30
-87
@@ -2,63 +2,25 @@ package pl.tkowalcz.tjahzi.logback;
|
||||
|
||||
import ch.qos.logback.classic.Logger;
|
||||
import ch.qos.logback.classic.LoggerContext;
|
||||
import ch.qos.logback.classic.joran.JoranConfigurator;
|
||||
import io.restassured.RestAssured;
|
||||
import io.restassured.http.ContentType;
|
||||
import io.restassured.parsing.Parser;
|
||||
import org.awaitility.Awaitility;
|
||||
import org.awaitility.Durations;
|
||||
import org.hamcrest.BaseMatcher;
|
||||
import org.hamcrest.Description;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.slf4j.LoggerFactory;
|
||||
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.logback.infra.IntegrationTest;
|
||||
|
||||
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 pl.tkowalcz.tjahzi.logback.infra.LokiAssert.assertThat;
|
||||
|
||||
@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();
|
||||
|
||||
LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory();
|
||||
|
||||
JoranConfigurator configurator = new JoranConfigurator();
|
||||
configurator.setContext(context);
|
||||
|
||||
context.reset();
|
||||
configurator.doConfigure(uri.toURL());
|
||||
LoggerContext context = loadConfig("appender-test-large-batches.xml");
|
||||
Logger logger = context.getLogger(LokiAppenderLargeBatchesTest.class);
|
||||
|
||||
String expectedLogLine = "Cupcake ipsum dolor sit amet cake wafer. " +
|
||||
"Souffle jelly beans biscuit topping. " +
|
||||
@@ -71,7 +33,6 @@ class LokiAppenderLargeBatchesTest {
|
||||
"Souffle cake muffin liquorice tart souffle pie sesame snaps.";
|
||||
|
||||
long expectedTimestamp = System.currentTimeMillis();
|
||||
Logger logger = context.getLogger(LokiAppenderLargeBatchesTest.class);
|
||||
|
||||
// When
|
||||
for (int i = 0; i < 1000; i++) {
|
||||
@@ -79,52 +40,34 @@ class LokiAppenderLargeBatchesTest {
|
||||
}
|
||||
|
||||
// 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);
|
||||
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;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void describeTo(Description description) {
|
||||
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) {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
))
|
||||
);
|
||||
}
|
||||
}
|
||||
+4
-20
@@ -2,41 +2,25 @@ package pl.tkowalcz.tjahzi.logback;
|
||||
|
||||
import ch.qos.logback.classic.Logger;
|
||||
import ch.qos.logback.classic.LoggerContext;
|
||||
import ch.qos.logback.classic.joran.JoranConfigurator;
|
||||
import com.codahale.metrics.Counter;
|
||||
import com.codahale.metrics.MetricRegistry;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.testcontainers.junit.jupiter.Testcontainers;
|
||||
import pl.tkowalcz.tjahzi.logback.util.TestUtil;
|
||||
import pl.tkowalcz.tjahzi.logback.infra.IntegrationTest;
|
||||
import pl.tkowalcz.tjahzi.logback.infra.TestUtil;
|
||||
import pl.tkowalcz.tjahzi.stats.DropwizardMonitoringModule;
|
||||
|
||||
import java.net.URI;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.awaitility.Awaitility.await;
|
||||
|
||||
@Testcontainers
|
||||
class LokiAppenderMonitoringTest {
|
||||
|
||||
@Test
|
||||
void shouldInjectMonitoringAndUseIt() throws Exception {
|
||||
void shouldInjectMonitoringAndUseIt() {
|
||||
// Given
|
||||
System.setProperty("loki.host", "somewhere");
|
||||
System.setProperty("loki.port", "42");
|
||||
|
||||
URI uri = getClass()
|
||||
.getClassLoader()
|
||||
.getResource("basic-appender-test-configuration.xml")
|
||||
.toURI();
|
||||
|
||||
LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory();
|
||||
|
||||
JoranConfigurator configurator = new JoranConfigurator();
|
||||
configurator.setContext(context);
|
||||
|
||||
context.reset();
|
||||
configurator.doConfigure(uri.toURL());
|
||||
LoggerContext context = IntegrationTest.loadConfig("basic-appender-test-configuration.xml");
|
||||
|
||||
LokiAppender loki = TestUtil.getLokiAppender(context);
|
||||
|
||||
|
||||
+7
-60
@@ -2,19 +2,8 @@ package pl.tkowalcz.tjahzi.logback;
|
||||
|
||||
import ch.qos.logback.classic.Logger;
|
||||
import ch.qos.logback.classic.LoggerContext;
|
||||
import ch.qos.logback.classic.joran.JoranConfigurator;
|
||||
import io.restassured.RestAssured;
|
||||
import io.restassured.http.ContentType;
|
||||
import io.restassured.parsing.Parser;
|
||||
import org.awaitility.Awaitility;
|
||||
import org.awaitility.Durations;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.slf4j.LoggerFactory;
|
||||
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.logback.infra.IntegrationTest;
|
||||
|
||||
import java.io.File;
|
||||
import java.net.URI;
|
||||
@@ -22,30 +11,14 @@ import java.nio.file.Files;
|
||||
import java.nio.file.Paths;
|
||||
import java.nio.file.StandardCopyOption;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.hamcrest.CoreMatchers.*;
|
||||
import static pl.tkowalcz.tjahzi.logback.infra.LokiAssert.assertThat;
|
||||
|
||||
@Testcontainers
|
||||
class LokiAppenderReloadTest {
|
||||
|
||||
@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 LokiAppenderReloadTest extends IntegrationTest {
|
||||
|
||||
@Test
|
||||
void shouldNotLooseDataWhenConfigChanges() throws Exception {
|
||||
// Given
|
||||
System.setProperty("loki.host", loki.getHost());
|
||||
System.setProperty("loki.port", loki.getFirstMappedPort().toString());
|
||||
|
||||
URI uriBefore = getClass()
|
||||
.getClassLoader()
|
||||
.getResource("appender-test-reload-before.xml")
|
||||
@@ -59,44 +32,18 @@ class LokiAppenderReloadTest {
|
||||
URI uri = new File(Paths.get(uriBefore).toFile().getParent(), "appender-test-reload.xml").toURI();
|
||||
Files.copy(Paths.get(uriBefore), Paths.get(uri), StandardCopyOption.REPLACE_EXISTING);
|
||||
|
||||
LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory();
|
||||
|
||||
JoranConfigurator configurator = new JoranConfigurator();
|
||||
configurator.setContext(context);
|
||||
|
||||
context.reset();
|
||||
configurator.doConfigure(uri.toURL());
|
||||
LoggerContext context = loadConfig(uri);
|
||||
Logger logger = context.getLogger(LokiAppenderReloadTest.class);
|
||||
|
||||
String expectedLogLine = "Test";
|
||||
|
||||
long expectedTimestamp = System.currentTimeMillis();
|
||||
Logger logger = context.getLogger(LokiAppenderReloadTest.class);
|
||||
|
||||
// When
|
||||
logger.info(expectedLogLine);
|
||||
Files.copy(Paths.get(uriAfter), Paths.get(uri), StandardCopyOption.REPLACE_EXISTING);
|
||||
|
||||
// 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"))
|
||||
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(1))
|
||||
|
||||
+17
-75
@@ -2,102 +2,44 @@ package pl.tkowalcz.tjahzi.logback;
|
||||
|
||||
import ch.qos.logback.classic.Logger;
|
||||
import ch.qos.logback.classic.LoggerContext;
|
||||
import ch.qos.logback.classic.joran.JoranConfigurator;
|
||||
import io.restassured.RestAssured;
|
||||
import io.restassured.http.ContentType;
|
||||
import io.restassured.parsing.Parser;
|
||||
import org.awaitility.Awaitility;
|
||||
import org.awaitility.Durations;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.slf4j.LoggerFactory;
|
||||
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.logback.infra.IntegrationTest;
|
||||
|
||||
import java.net.URI;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.hamcrest.CoreMatchers.*;
|
||||
import static pl.tkowalcz.tjahzi.logback.infra.LokiAssert.assertThat;
|
||||
|
||||
@Testcontainers
|
||||
class LokiAppenderShutdownTest {
|
||||
|
||||
@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 LokiAppenderShutdownTest extends IntegrationTest {
|
||||
|
||||
@Test
|
||||
void shouldNotLooseDataWhenShuttingDown() throws Exception {
|
||||
void shouldNotLooseDataWhenShuttingDown() {
|
||||
// Given
|
||||
System.setProperty("loki.host", loki.getHost());
|
||||
System.setProperty("loki.port", loki.getFirstMappedPort().toString());
|
||||
|
||||
URI uri = getClass()
|
||||
.getClassLoader()
|
||||
.getResource("appender-test-shutdown.xml")
|
||||
.toURI();
|
||||
|
||||
LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory();
|
||||
|
||||
JoranConfigurator configurator = new JoranConfigurator();
|
||||
configurator.setContext(context);
|
||||
|
||||
context.reset();
|
||||
configurator.doConfigure(uri.toURL());
|
||||
LoggerContext context = loadConfig("appender-test-shutdown.xml");
|
||||
Logger logger = context.getLogger(LokiAppenderShutdownTest.class);
|
||||
|
||||
String expectedLogLine = "Test";
|
||||
|
||||
long expectedTimestamp = System.currentTimeMillis();
|
||||
Logger logger = context.getLogger(LokiAppenderShutdownTest.class);
|
||||
|
||||
// When
|
||||
logger.info(expectedLogLine);
|
||||
((LoggerContext) LoggerFactory.getILoggerFactory()).stop();
|
||||
|
||||
// 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(1))
|
||||
.body(
|
||||
"data.result.values",
|
||||
hasItems(
|
||||
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(1))
|
||||
.body(
|
||||
"data.result.values",
|
||||
hasItems(
|
||||
hasItems(
|
||||
containsString("INFO p.t.t.l.LokiAppenderShutdownTest - Test")
|
||||
hasItems(
|
||||
containsString("INFO p.t.t.l.LokiAppenderShutdownTest - Test")
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1,118 +1,41 @@
|
||||
package pl.tkowalcz.tjahzi.logback;
|
||||
|
||||
import ch.qos.logback.classic.LoggerContext;
|
||||
import ch.qos.logback.classic.joran.JoranConfigurator;
|
||||
import io.restassured.RestAssured;
|
||||
import io.restassured.http.ContentType;
|
||||
import io.restassured.parsing.Parser;
|
||||
import org.awaitility.Awaitility;
|
||||
import org.awaitility.Durations;
|
||||
import org.hamcrest.BaseMatcher;
|
||||
import org.hamcrest.Description;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
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.logback.infra.IntegrationTest;
|
||||
|
||||
import java.net.URI;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import static org.hamcrest.CoreMatchers.*;
|
||||
import static pl.tkowalcz.tjahzi.logback.infra.LokiAssert.assertThat;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.hamcrest.CoreMatchers.equalTo;
|
||||
import static org.hamcrest.CoreMatchers.hasItems;
|
||||
|
||||
@Testcontainers
|
||||
class LokiAppenderTest {
|
||||
|
||||
@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 LokiAppenderTest extends IntegrationTest {
|
||||
|
||||
@Test
|
||||
void shouldSendData() throws Exception {
|
||||
void shouldSendData() {
|
||||
// Given
|
||||
System.setProperty("loki.host", loki.getHost());
|
||||
System.setProperty("loki.port", loki.getFirstMappedPort().toString());
|
||||
LoggerContext context = loadConfig("basic-appender-test-configuration.xml");
|
||||
Logger logger = context.getLogger(LokiAppenderTest.class);
|
||||
|
||||
URI uri = getClass()
|
||||
.getClassLoader()
|
||||
.getResource("basic-appender-test-configuration.xml")
|
||||
.toURI();
|
||||
|
||||
LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory();
|
||||
|
||||
JoranConfigurator configurator = new JoranConfigurator();
|
||||
configurator.setContext(context);
|
||||
|
||||
context.reset();
|
||||
configurator.doConfigure(uri.toURL());
|
||||
|
||||
String expectedLogLine = "Test";
|
||||
long expectedTimestamp = System.currentTimeMillis();
|
||||
String expectedLogLine = "Hello World";
|
||||
|
||||
// When
|
||||
Logger logger = context.getLogger(LokiAppenderTest.class);
|
||||
logger.info(expectedLogLine);
|
||||
|
||||
// Then
|
||||
RestAssured.port = loki.getFirstMappedPort();
|
||||
RestAssured.baseURI = "http://" + loki.getHost();
|
||||
RestAssured.registerParser("text/plain", Parser.JSON);
|
||||
|
||||
Awaitility
|
||||
.await()
|
||||
.atMost(Durations.TEN_SECONDS)
|
||||
.pollInterval(Durations.ONE_SECOND)
|
||||
.ignoreExceptions()
|
||||
.untilAsserted(() -> {
|
||||
given()
|
||||
.contentType(ContentType.URLENC)
|
||||
.urlEncodingEnabled(false)
|
||||
.formParam("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", 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) {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
));
|
||||
});
|
||||
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.values",
|
||||
hasItems(
|
||||
hasItems(
|
||||
hasItems(
|
||||
containsString("p.t.tjahzi.logback.LokiAppenderTest - " + expectedLogLine)
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
+13
-71
@@ -2,61 +2,23 @@ package pl.tkowalcz.tjahzi.logback;
|
||||
|
||||
import ch.qos.logback.classic.Logger;
|
||||
import ch.qos.logback.classic.LoggerContext;
|
||||
import ch.qos.logback.classic.joran.JoranConfigurator;
|
||||
import io.restassured.RestAssured;
|
||||
import io.restassured.http.ContentType;
|
||||
import io.restassured.parsing.Parser;
|
||||
import org.awaitility.Awaitility;
|
||||
import org.awaitility.Durations;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.slf4j.MDC;
|
||||
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.logback.infra.IntegrationTest;
|
||||
|
||||
import java.net.URI;
|
||||
import java.util.UUID;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.hamcrest.CoreMatchers.equalTo;
|
||||
import static org.hamcrest.Matchers.greaterThanOrEqualTo;
|
||||
import static pl.tkowalcz.tjahzi.logback.infra.LokiAssert.assertThat;
|
||||
|
||||
@Testcontainers
|
||||
class MdcLogLabelConfigurationTest {
|
||||
|
||||
@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 MdcLogLabelConfigurationTest extends IntegrationTest {
|
||||
|
||||
@Test
|
||||
void shouldSendMdcLogLabelAsConfigured() throws Exception {
|
||||
void shouldSendMdcLogLabelAsConfigured() {
|
||||
// Given
|
||||
System.setProperty("loki.host", loki.getHost());
|
||||
System.setProperty("loki.port", loki.getFirstMappedPort().toString());
|
||||
|
||||
URI uri = getClass()
|
||||
.getClassLoader()
|
||||
.getResource("appender-test-with-md-log-label-set.xml")
|
||||
.toURI();
|
||||
|
||||
LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory();
|
||||
|
||||
JoranConfigurator configurator = new JoranConfigurator();
|
||||
configurator.setContext(context);
|
||||
|
||||
context.reset();
|
||||
configurator.doConfigure(uri.toURL());
|
||||
LoggerContext context = loadConfig("appender-test-with-md-log-label-set.xml");
|
||||
Logger logger = context.getLogger(LokiAppenderLargeBatchesTest.class);
|
||||
|
||||
// When
|
||||
String traceIdValue = UUID.randomUUID().toString();
|
||||
@@ -65,35 +27,15 @@ class MdcLogLabelConfigurationTest {
|
||||
MDC.put("trace_id", traceIdValue);
|
||||
MDC.put("span_id", spanIdValue);
|
||||
|
||||
Logger logger = context.getLogger(LokiAppenderLargeBatchesTest.class);
|
||||
logger.info("Test");
|
||||
|
||||
// Then
|
||||
RestAssured.port = loki.getFirstMappedPort();
|
||||
RestAssured.baseURI = "http://" + loki.getHost();
|
||||
RestAssured.registerParser("text/plain", Parser.JSON);
|
||||
|
||||
Awaitility
|
||||
.await()
|
||||
.atMost(Durations.TEN_SECONDS)
|
||||
.pollInterval(Durations.ONE_SECOND)
|
||||
.ignoreExceptions()
|
||||
.untilAsserted(() -> {
|
||||
given()
|
||||
.contentType(ContentType.URLENC)
|
||||
.urlEncodingEnabled(false)
|
||||
.formParam("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()", greaterThanOrEqualTo(1))
|
||||
.body("data.result[0].stream.server", equalTo("127.0.0.1"))
|
||||
.body("data.result[0].stream.trace_id", equalTo(traceIdValue))
|
||||
.body("data.result[0].stream.span_id", equalTo(spanIdValue));
|
||||
});
|
||||
assertThat(loki)
|
||||
.returns(response -> response
|
||||
.body("data.result.size()", greaterThanOrEqualTo(1))
|
||||
.body("data.result[0].stream.server", equalTo("127.0.0.1"))
|
||||
.body("data.result[0].stream.trace_id", equalTo(traceIdValue))
|
||||
.body("data.result[0].stream.span_id", equalTo(spanIdValue))
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
package pl.tkowalcz.tjahzi.logback.infra;
|
||||
|
||||
import ch.qos.logback.classic.LoggerContext;
|
||||
import ch.qos.logback.classic.joran.JoranConfigurator;
|
||||
import ch.qos.logback.core.joran.spi.JoranException;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.slf4j.LoggerFactory;
|
||||
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 java.net.MalformedURLException;
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
|
||||
@Testcontainers
|
||||
public class IntegrationTest {
|
||||
|
||||
@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);
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
System.setProperty("loki.host", loki.getHost());
|
||||
System.setProperty("loki.port", loki.getFirstMappedPort().toString());
|
||||
}
|
||||
|
||||
public static LoggerContext loadConfig(String fileName) {
|
||||
try {
|
||||
URI uri = IntegrationTest.class
|
||||
.getClassLoader()
|
||||
.getResource(fileName)
|
||||
.toURI();
|
||||
|
||||
return loadConfig(uri);
|
||||
} catch (URISyntaxException e) {
|
||||
Assertions.fail(e);
|
||||
}
|
||||
|
||||
throw new IllegalStateException();
|
||||
}
|
||||
|
||||
public static LoggerContext loadConfig(URI uri) {
|
||||
LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory();
|
||||
|
||||
try {
|
||||
JoranConfigurator configurator = new JoranConfigurator();
|
||||
configurator.setContext(context);
|
||||
|
||||
context.reset();
|
||||
configurator.doConfigure(uri.toURL());
|
||||
} catch (MalformedURLException | JoranException e) {
|
||||
Assertions.fail(e);
|
||||
}
|
||||
|
||||
return context;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,97 @@
|
||||
package pl.tkowalcz.tjahzi.logback.infra;
|
||||
|
||||
import io.restassured.RestAssured;
|
||||
import io.restassured.http.ContentType;
|
||||
import io.restassured.parsing.Parser;
|
||||
import io.restassured.response.ValidatableResponse;
|
||||
import io.restassured.specification.RequestSpecification;
|
||||
import org.awaitility.Awaitility;
|
||||
import org.awaitility.Durations;
|
||||
import org.testcontainers.containers.GenericContainer;
|
||||
|
||||
import java.util.function.Consumer;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.hamcrest.CoreMatchers.equalTo;
|
||||
|
||||
@SuppressWarnings("rawtypes")
|
||||
public class LokiAssert {
|
||||
|
||||
private String url = "/loki/api/v1/query_range";
|
||||
private String param = "query=%7Bserver%3D%22127.0.0.1%22%7D";
|
||||
|
||||
private String username;
|
||||
private String password;
|
||||
|
||||
public LokiAssert() {
|
||||
RestAssured.reset();
|
||||
RestAssured.registerParser("text/plain", Parser.JSON);
|
||||
}
|
||||
|
||||
public LokiAssert(GenericContainer loki) {
|
||||
RestAssured.port = loki.getFirstMappedPort();
|
||||
RestAssured.baseURI = "http://" + loki.getHost();
|
||||
RestAssured.registerParser("text/plain", Parser.JSON);
|
||||
}
|
||||
|
||||
public static LokiAssert assertThat() {
|
||||
return new LokiAssert();
|
||||
}
|
||||
|
||||
public LokiAssert withFormParam(String param) {
|
||||
this.param = param;
|
||||
return this;
|
||||
}
|
||||
|
||||
public static LokiAssert assertThat(GenericContainer loki) {
|
||||
return new LokiAssert(loki);
|
||||
}
|
||||
|
||||
public void returns(Consumer<ValidatableResponse> restOfAssertionsGoHere) {
|
||||
Awaitility
|
||||
.await()
|
||||
.atMost(Durations.ONE_MINUTE)
|
||||
.pollInterval(Durations.ONE_SECOND)
|
||||
.ignoreExceptions()
|
||||
.untilAsserted(() -> {
|
||||
RequestSpecification requestSpecification = given()
|
||||
.contentType(ContentType.URLENC)
|
||||
.urlEncodingEnabled(false)
|
||||
.formParam(param);
|
||||
|
||||
if (username != null || password != null) {
|
||||
requestSpecification = requestSpecification
|
||||
.auth()
|
||||
.preemptive()
|
||||
.basic(
|
||||
username,
|
||||
password
|
||||
);
|
||||
}
|
||||
|
||||
ValidatableResponse validatableResponse = requestSpecification
|
||||
.when()
|
||||
.get(url)
|
||||
.then()
|
||||
.log()
|
||||
.all()
|
||||
.statusCode(200)
|
||||
.body("status", equalTo("success"));
|
||||
|
||||
restOfAssertionsGoHere.accept(validatableResponse);
|
||||
});
|
||||
}
|
||||
|
||||
public LokiAssert withCredentials(String username, String password) {
|
||||
this.username = username;
|
||||
this.password = password;
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
public LokiAssert calling(String url) {
|
||||
this.url = url;
|
||||
|
||||
return this;
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package pl.tkowalcz.tjahzi.logback.util;
|
||||
package pl.tkowalcz.tjahzi.logback.infra;
|
||||
|
||||
import ch.qos.logback.classic.LoggerContext;
|
||||
import pl.tkowalcz.tjahzi.logback.LokiAppender;
|
||||
Reference in new issue
Block a user