Reformatting, changing imports etc.

This commit is contained in:
tomasz.kowalczewski committed 2021-08-06 23:05:28 +02:00
1 parent 49b99bac70
commit 35bd48c62c
11 files changed
+35 -68

No files matched your search

@@ -5,39 +5,20 @@ import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.junit.jupiter.api.Disabled;
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 org.testcontainers.shaded.com.google.common.util.concurrent.Uninterruptibles;
import pl.tkowalcz.tjahzi.log4j2.infra.IntegrationTest;
import java.net.URI;
import java.net.URISyntaxException;
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 URISyntaxException {
void shouldSendData() {
AtomicLong totalAllocatedMemory = new AtomicLong();
Map<String, AtomicLong> allocatedMemory = new HashMap<>();
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
@@ -52,16 +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();
((org.apache.logging.log4j.core.LoggerContext) LogManager.getContext(false))
.setConfigLocation(uri);
loadConfig("basic-appender-test-configuration.xml");
String logLine = "Cupcake ipsum dolor sit amet cake wafer. " +
"Souffle jelly beans biscuit topping. " +
@@ -4,15 +4,15 @@ import org.apache.commons.lang3.RandomStringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.junit.jupiter.api.Test;
import pl.tkowalcz.tjahzi.log4j2.infra.LokiAssert;
import static org.hamcrest.CoreMatchers.*;
import static pl.tkowalcz.tjahzi.log4j2.infra.IntegrationTest.loadConfig;
import static pl.tkowalcz.tjahzi.log4j2.infra.LokiAssert.assertThat;
class GrafanaCloudAppenderTest {
@Test
void shouldSendData() throws Exception {
void shouldSendData() {
// Given
System.setProperty("loki.host", "logs-prod-us-central1.grafana.net");
System.setProperty("loki.port", "443");
@@ -26,7 +26,7 @@ class GrafanaCloudAppenderTest {
logger.info(expectedLogLine);
// Then
LokiAssert.assertThat()
assertThat()
.calling("https://logs-prod-us-central1.grafana.net/loki/api/v1/query_range")
.withCredentials(
System.getenv("grafana_username"),
@@ -8,27 +8,19 @@ import org.apache.logging.log4j.core.LoggerContext;
import org.junit.jupiter.api.Test;
import pl.tkowalcz.tjahzi.stats.DropwizardMonitoringModule;
import java.net.URI;
import java.net.URISyntaxException;
import static org.assertj.core.api.Assertions.assertThat;
import static org.awaitility.Awaitility.await;
import static pl.tkowalcz.tjahzi.log4j2.infra.IntegrationTest.loadConfig;
class LokiAppenderMonitoringTest {
@Test
void shouldInjectMonitoringAndUseIt() throws URISyntaxException {
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) LogManager.getContext(false);
context.setConfigLocation(uri);
LoggerContext context = loadConfig("basic-appender-test-configuration.xml");
LokiAppender loki = context.getConfiguration().getAppender("Loki");
MetricRegistry metricRegistry = new MetricRegistry();
@@ -4,7 +4,6 @@ import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.junit.jupiter.api.Test;
import pl.tkowalcz.tjahzi.log4j2.infra.IntegrationTest;
import pl.tkowalcz.tjahzi.log4j2.infra.LokiAssert;
import java.net.URI;
import java.nio.file.Files;
@@ -12,6 +11,7 @@ import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import static org.hamcrest.CoreMatchers.*;
import static pl.tkowalcz.tjahzi.log4j2.infra.LokiAssert.assertThat;
class LokiAppenderReloadTest extends IntegrationTest {
@@ -41,7 +41,7 @@ class LokiAppenderReloadTest extends IntegrationTest {
Files.copy(Paths.get(uriAfter), Paths.get(uri), StandardCopyOption.REPLACE_EXISTING);
// Then
LokiAssert.assertThat(loki)
assertThat(loki)
.returns(response -> response
.body("data.result.size()", equalTo(1))
.body("data.result[0].stream.server", equalTo("127.0.0.1"))
@@ -4,9 +4,9 @@ import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.junit.jupiter.api.Test;
import pl.tkowalcz.tjahzi.log4j2.infra.IntegrationTest;
import pl.tkowalcz.tjahzi.log4j2.infra.LokiAssert;
import static org.hamcrest.CoreMatchers.*;
import static pl.tkowalcz.tjahzi.log4j2.infra.LokiAssert.assertThat;
class LokiAppenderShutdownTest extends IntegrationTest {
@@ -23,7 +23,7 @@ class LokiAppenderShutdownTest extends IntegrationTest {
LogManager.shutdown();
// Then
LokiAssert.assertThat(loki)
assertThat(loki)
.returns(response ->
response
.body("data.result.size()", equalTo(1))
@@ -4,9 +4,9 @@ import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.junit.jupiter.api.Test;
import pl.tkowalcz.tjahzi.log4j2.infra.IntegrationTest;
import pl.tkowalcz.tjahzi.log4j2.infra.LokiAssert;
import static org.hamcrest.CoreMatchers.*;
import static pl.tkowalcz.tjahzi.log4j2.infra.LokiAssert.assertThat;
class LokiAppenderTest extends IntegrationTest {
@@ -22,7 +22,7 @@ class LokiAppenderTest extends IntegrationTest {
logger.info(expectedLogLine);
// Then
LokiAssert.assertThat(loki)
assertThat(loki)
.returns(response -> response
.body("data.result.size()", equalTo(1))
.body("data.result[0].stream.server", equalTo("127.0.0.1"))
@@ -11,10 +11,10 @@ import org.apache.logging.log4j.core.config.builder.api.ConfigurationBuilderFact
import org.apache.logging.log4j.core.config.builder.impl.BuiltConfiguration;
import org.junit.jupiter.api.Test;
import pl.tkowalcz.tjahzi.log4j2.infra.IntegrationTest;
import pl.tkowalcz.tjahzi.log4j2.infra.LokiAssert;
import static org.hamcrest.CoreMatchers.*;
import static org.hamcrest.Matchers.greaterThan;
import static pl.tkowalcz.tjahzi.log4j2.infra.LokiAssert.assertThat;
public class ProgrammaticConfigurationTest extends IntegrationTest {
@@ -29,7 +29,7 @@ public class ProgrammaticConfigurationTest extends IntegrationTest {
// Then
logger.warn("Here I come!");
LokiAssert.assertThat(loki)
assertThat(loki)
.returns(response ->
response
.body("data.result.size()", equalTo(1))
@@ -1,6 +1,7 @@
package pl.tkowalcz.tjahzi.log4j2.infra;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.core.LoggerContext;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.testcontainers.containers.BindMode;
@@ -34,21 +35,25 @@ public class IntegrationTest {
System.setProperty("loki.port", loki.getFirstMappedPort().toString());
}
public static void loadConfig(String fileName) {
public static LoggerContext loadConfig(String fileName) {
try {
URI uri = IntegrationTest.class
.getClassLoader()
.getResource(fileName)
.toURI();
loadConfig(uri);
return loadConfig(uri);
} catch (URISyntaxException e) {
Assertions.fail(e);
}
return null;
}
public static void loadConfig(URI uri) {
((org.apache.logging.log4j.core.LoggerContext) LogManager.getContext(false))
.setConfigLocation(uri);
public static LoggerContext loadConfig(URI uri) {
LoggerContext context = (LoggerContext) LogManager.getContext(false);
context.setConfigLocation(uri);
return context;
}
}
@@ -5,12 +5,12 @@ import org.apache.logging.log4j.Logger;
import org.junit.jupiter.api.Test;
import org.slf4j.MDC;
import pl.tkowalcz.tjahzi.log4j2.infra.IntegrationTest;
import pl.tkowalcz.tjahzi.log4j2.infra.LokiAssert;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.hasItems;
import static org.hamcrest.core.Every.everyItem;
import static pl.tkowalcz.tjahzi.log4j2.infra.LokiAssert.assertThat;
class LabelsContextSubstitutionTest extends IntegrationTest {
@@ -34,7 +34,7 @@ class LabelsContextSubstitutionTest extends IntegrationTest {
logger.info("Test3");
// Then
LokiAssert.assertThat(loki)
assertThat(loki)
.returns(response -> response
.body("data.result.size()", equalTo(3))
.body("data.result.stream.server", everyItem(equalTo("127.0.0.1")))
@@ -5,12 +5,12 @@ import org.apache.logging.log4j.Logger;
import org.junit.jupiter.api.Test;
import org.slf4j.MDC;
import pl.tkowalcz.tjahzi.log4j2.infra.IntegrationTest;
import pl.tkowalcz.tjahzi.log4j2.infra.LokiAssert;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.hasItems;
import static org.hamcrest.core.Every.everyItem;
import static pl.tkowalcz.tjahzi.log4j2.infra.LokiAssert.assertThat;
class Log4jPatternsInLabelsTest extends IntegrationTest {
@@ -29,7 +29,7 @@ class Log4jPatternsInLabelsTest extends IntegrationTest {
logger.info("Test2");
// Then
LokiAssert.assertThat(loki)
assertThat(loki)
.returns(response -> response
.body("data.result.size()", equalTo(2))
.body("data.result.stream.server", everyItem(equalTo("127.0.0.1")))
@@ -4,12 +4,10 @@ import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.junit.jupiter.api.Test;
import pl.tkowalcz.tjahzi.log4j2.infra.IntegrationTest;
import pl.tkowalcz.tjahzi.log4j2.infra.LokiAssert;
import java.net.URISyntaxException;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.nullValue;
import static pl.tkowalcz.tjahzi.log4j2.infra.LokiAssert.assertThat;
class LogLevelLabelConfigurationTest extends IntegrationTest {
@@ -23,7 +21,7 @@ class LogLevelLabelConfigurationTest extends IntegrationTest {
logger.info("Test");
// Then
LokiAssert.assertThat(loki)
assertThat(loki)
.withFormParam("query=%7Btest%3D%22shouldWorkWithNoLogLevelConfigured%22%7D")
.returns(response -> response
.body("data.result.size()", equalTo(1))
@@ -41,7 +39,7 @@ class LogLevelLabelConfigurationTest extends IntegrationTest {
logger.info("Test");
// Then
LokiAssert.assertThat(loki)
assertThat(loki)
.withFormParam("query=%7Btest%3D%22shouldSendLogLevelAsConfigured%22%7D")
.returns(response ->
response