diff options
Diffstat (limited to 'container-core/src/test/java')
7 files changed, 62 insertions, 42 deletions
diff --git a/container-core/src/test/java/com/yahoo/container/handler/LogReaderTest.java b/container-core/src/test/java/com/yahoo/container/handler/LogReaderTest.java index e98f8cac276..749033cd1bf 100644 --- a/container-core/src/test/java/com/yahoo/container/handler/LogReaderTest.java +++ b/container-core/src/test/java/com/yahoo/container/handler/LogReaderTest.java @@ -3,8 +3,8 @@ package com.yahoo.container.handler; import com.yahoo.compress.ZstdCompressor; import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.condition.EnabledIf; import org.junit.jupiter.api.io.TempDir; import java.io.ByteArrayOutputStream; @@ -30,7 +30,8 @@ public class LogReaderTest { private static final String logv11 = "3600.2\tnode1.com\t5480\tcontainer\tstdout\tinfo\tfourth\n"; private static final String logv = "90000.1\tnode1.com\t5480\tcontainer\tstdout\tinfo\tlast\n"; - private static final String log100 = "0.2\tnode2.com\t5480\tcontainer\tstdout\tinfo\tsecond\n"; + private static final String log100a = "0.2\tnode2.com\t5480\tcontainer\tstdout\tinfo\tsecond\n"; + private static final String log100b = "0.15\tnode2.com\t5480\tcontainer\tstdout\tinfo\tfirst\n"; private static final String log101 = "0.1\tnode2.com\t5480\tcontainer\tstdout\tinfo\tERROR: Bundle canary-application [71] Unable to get module class path. (java.lang.NullPointerException)\n"; private static final String log110 = "3600.1\tnode1.com\t5480\tcontainer\tstderr\twarning\tthird\n"; private static final String log200 = "86400.1\tnode2.com\t5480\tcontainer\tstderr\twarning\tjava.lang.NullPointerException\\n\\tat org.apache.felix.framework.BundleRevisionImpl.calculateContentPath(BundleRevisionImpl.java:438)\\n\\tat org.apache.felix.framework.BundleRevisionImpl.initializeContentPath(BundleRevisionImpl.java:371)\n"; @@ -41,27 +42,37 @@ public class LogReaderTest { // Log archive paths and file names indicate what hour they contain logs for, with the start of that hour. // Multiple entries may exist for each hour. Files.createDirectories(logDirectory.resolve("1970/01/01")); - Files.write(logDirectory.resolve("1970/01/01/00-0.gz"), compress1(log100)); - Files.write(logDirectory.resolve("1970/01/01/00-1"), log101.getBytes(UTF_8)); + // Files may contain out-of-order entries. + Files.write(logDirectory.resolve("1970/01/01/00-0.gz"), compress1(log100a + log100b)); + Files.writeString(logDirectory.resolve("1970/01/01/00-1"), log101); Files.write(logDirectory.resolve("1970/01/01/01-0.zst"), compress2(log110)); Files.createDirectories(logDirectory.resolve("1970/01/02")); - Files.write(logDirectory.resolve("1970/01/02/00-0"), log200.getBytes(UTF_8)); + Files.writeString(logDirectory.resolve("1970/01/02/00-0"), log200); // Vespa log file names are the second-truncated timestamp of the last entry. // The current log file has no timestamp suffix. - Files.write(logDirectory.resolve("vespa.log-1970-01-01.01-00-00"), logv11.getBytes(UTF_8)); - Files.write(logDirectory.resolve("vespa.log"), logv.getBytes(UTF_8)); + Files.writeString(logDirectory.resolve("vespa.log-1970-01-01.01-00-00"), logv11); + Files.writeString(logDirectory.resolve("vespa.log"), logv); } - @Disabled + private static boolean hasZstdcat() { + try { + return new ProcessBuilder("zstdcat", "--version").start().waitFor() == 0; + } + catch (Exception e) { + return false; + } + } + + @EnabledIf("hasZstdcat") @Test void testThatLogsOutsideRangeAreExcluded() { ByteArrayOutputStream baos = new ByteArrayOutputStream(); LogReader logReader = new LogReader(logDirectory, Pattern.compile(".*")); logReader.writeLogs(baos, Instant.ofEpochMilli(150), Instant.ofEpochMilli(3601050), 100, Optional.empty()); - assertEquals(log100 + logv11 + log110, baos.toString(UTF_8)); + assertEquals(log100b + log100a + logv11 + log110, baos.toString(UTF_8)); } @Test @@ -73,14 +84,14 @@ public class LogReaderTest { assertEquals(log101 + logv11, baos.toString(UTF_8)); } - @Disabled // TODO: zts log line missing on Mac + @EnabledIf("hasZstdcat") @Test void testZippedStreaming() { ByteArrayOutputStream zippedBaos = new ByteArrayOutputStream(); LogReader logReader = new LogReader(logDirectory, Pattern.compile(".*")); logReader.writeLogs(zippedBaos, Instant.EPOCH, Instant.EPOCH.plus(Duration.ofDays(2)), 100, Optional.empty()); - assertEquals(log101 + log100 + logv11 + log110 + log200 + logv, zippedBaos.toString(UTF_8)); + assertEquals(log101 + log100b + log100a + logv11 + log110 + log200 + logv, zippedBaos.toString(UTF_8)); } @Test @@ -89,7 +100,7 @@ public class LogReaderTest { LogReader logReader = new LogReader(logDirectory, Pattern.compile(".*")); logReader.writeLogs(baos, Instant.EPOCH, Instant.EPOCH.plus(Duration.ofDays(2)), 100, Optional.of("node2.com")); - assertEquals(log101 + log100 + log200, baos.toString(UTF_8)); + assertEquals(log101 + log100b + log100a + log200, baos.toString(UTF_8)); } @Test @@ -98,7 +109,7 @@ public class LogReaderTest { LogReader logReader = new LogReader(logDirectory, Pattern.compile(".*")); logReader.writeLogs(baos, Instant.EPOCH, Instant.EPOCH.plus(Duration.ofDays(2)), 2, Optional.of("node2.com")); - assertEquals(log101 + log100, baos.toString(UTF_8)); + assertEquals(log101 + log100b, baos.toString(UTF_8)); } private byte[] compress1(String input) throws IOException { diff --git a/container-core/src/test/java/com/yahoo/jdisc/http/server/jetty/HttpResponseStatisticsCollectorTest.java b/container-core/src/test/java/com/yahoo/jdisc/http/server/jetty/HttpResponseStatisticsCollectorTest.java index 1f65bc4f582..165659389ec 100644 --- a/container-core/src/test/java/com/yahoo/jdisc/http/server/jetty/HttpResponseStatisticsCollectorTest.java +++ b/container-core/src/test/java/com/yahoo/jdisc/http/server/jetty/HttpResponseStatisticsCollectorTest.java @@ -25,6 +25,7 @@ import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.nio.ByteBuffer; import java.util.List; +import java.util.Set; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.equalTo; @@ -38,7 +39,7 @@ public class HttpResponseStatisticsCollectorTest { private Connector connector; private List<String> monitoringPaths = List.of("/status.html"); private List<String> searchPaths = List.of("/search"); - private HttpResponseStatisticsCollector collector = new HttpResponseStatisticsCollector(monitoringPaths, searchPaths); + private HttpResponseStatisticsCollector collector = new HttpResponseStatisticsCollector(monitoringPaths, searchPaths, Set.of()); private int httpResponseCode = 500; @Test diff --git a/container-core/src/test/java/com/yahoo/jdisc/http/server/jetty/HttpServerTest.java b/container-core/src/test/java/com/yahoo/jdisc/http/server/jetty/HttpServerTest.java index 2c5d36bd776..318067ac634 100644 --- a/container-core/src/test/java/com/yahoo/jdisc/http/server/jetty/HttpServerTest.java +++ b/container-core/src/test/java/com/yahoo/jdisc/http/server/jetty/HttpServerTest.java @@ -743,7 +743,7 @@ public class HttpServerTest { } @Test - void requestThatFallbackServerNameCanBeOverridden() throws Exception { + void fallbackServerNameCanBeOverridden() throws Exception { String fallbackHostname = "myhostname"; JettyTestDriver driver = JettyTestDriver.newConfiguredInstance( new UriRequestHandler(), @@ -752,13 +752,29 @@ public class HttpServerTest { .serverName(new ConnectorConfig.ServerName.Builder().fallback(fallbackHostname))); int listenPort = driver.server().getListenPort(); HttpGet req = new HttpGet("http://localhost:" + listenPort + "/"); - req.addHeader("Host", null); + req.setHeader("Host", null); driver.client().execute(req) .expectStatusCode(is(OK)) .expectContent(containsString("http://" + fallbackHostname + ":" + listenPort + "/")); assertTrue(driver.close()); } + @Test + void acceptedServerNamesCanBeRestricted() throws Exception { + String requiredServerName = "myhostname"; + JettyTestDriver driver = JettyTestDriver.newConfiguredInstance( + new EchoRequestHandler(), + new ServerConfig.Builder(), + new ConnectorConfig.Builder() + .serverName(new ConnectorConfig.ServerName.Builder().allowed(requiredServerName))); + int listenPort = driver.server().getListenPort(); + HttpGet req = new HttpGet("http://localhost:" + listenPort + "/"); + req.setHeader("Host", requiredServerName); + driver.client().execute(req).expectStatusCode(is(OK)); + driver.client().get("/").expectStatusCode(is(NOT_FOUND)); + assertTrue(driver.close()); + } + private static JettyTestDriver createSslWithTlsClientAuthenticationEnforcer(Path certificateFile, Path privateKeyFile) { ConnectorConfig.Builder connectorConfig = new ConnectorConfig.Builder() .tlsClientAuthEnforcer( diff --git a/container-core/src/test/java/com/yahoo/metrics/simple/BucketTest.java b/container-core/src/test/java/com/yahoo/metrics/simple/BucketTest.java index 11cc8b86a09..75d1c37c5c1 100644 --- a/container-core/src/test/java/com/yahoo/metrics/simple/BucketTest.java +++ b/container-core/src/test/java/com/yahoo/metrics/simple/BucketTest.java @@ -23,9 +23,10 @@ import com.yahoo.metrics.simple.UntypedMetric.AssumedType; * Functional tests for the value buckets, as implemented in the class Bucket, * and by extension the value store itself, UntypedValue. * - * @author <a href="mailto:steinar@yahoo-inc.com">Steinar Knutsen</a> + * @author Steinar Knutsen */ public class BucketTest { + private Bucket bucket; @BeforeEach @@ -55,23 +56,12 @@ public class BucketTest { for (Entry<Identifier, UntypedMetric> x : bucket.entrySet()) { String metricName = x.getKey().getName(); switch (metricName) { - case "nalle": - ++nalle; - break; - case "nalle_0": - ++nalle0; - break; - case "nalle_1": - ++nalle1; - break; - case "nalle_2": - ++nalle2; - break; - case "nalle_3": - ++nalle3; - break; - default: - throw new IllegalStateException(); + case "nalle" -> ++nalle; + case "nalle_0" -> ++nalle0; + case "nalle_1" -> ++nalle1; + case "nalle_2" -> ++nalle2; + case "nalle_3" -> ++nalle3; + default -> throw new IllegalStateException(); } } assertEquals(4, nalle); diff --git a/container-core/src/test/java/com/yahoo/metrics/simple/CounterTest.java b/container-core/src/test/java/com/yahoo/metrics/simple/CounterTest.java index 45a76078619..074c0c7b2e5 100644 --- a/container-core/src/test/java/com/yahoo/metrics/simple/CounterTest.java +++ b/container-core/src/test/java/com/yahoo/metrics/simple/CounterTest.java @@ -33,7 +33,7 @@ public class CounterTest { } @Test - final void testAdd() throws InterruptedException { + final void testAdd() { final String metricName = "unitTestCounter"; Counter c = receiver.declareCounter(metricName); c.add(); @@ -47,7 +47,7 @@ public class CounterTest { } @Test - final void testAddLong() throws InterruptedException { + final void testAddLong() { final String metricName = "unitTestCounter"; Counter c = receiver.declareCounter(metricName); final long twoToThePowerOfFourtyeight = 65536L * 65536L * 65536L; @@ -62,7 +62,7 @@ public class CounterTest { } @Test - final void testAddPoint() throws InterruptedException { + final void testAddPoint() { final String metricName = "unitTestCounter"; Point p = receiver.pointBuilder().set("x", 2L).set("y", 3.0d).set("z", "5").build(); Counter c = receiver.declareCounter(metricName, p); @@ -77,7 +77,7 @@ public class CounterTest { } @Test - final void testAddLongPoint() throws InterruptedException { + final void testAddLongPoint() { final String metricName = "unitTestCounter"; Point p = receiver.pointBuilder().set("x", 2L).set("y", 3.0d).set("z", "5").build(); Counter c = receiver.declareCounter(metricName, p); diff --git a/container-core/src/test/java/com/yahoo/metrics/simple/MetricsTest.java b/container-core/src/test/java/com/yahoo/metrics/simple/MetricsTest.java index f64998f0be4..dd949627f30 100644 --- a/container-core/src/test/java/com/yahoo/metrics/simple/MetricsTest.java +++ b/container-core/src/test/java/com/yahoo/metrics/simple/MetricsTest.java @@ -17,9 +17,10 @@ import com.yahoo.metrics.simple.jdisc.SimpleMetricConsumer; /** * Functional test for simple metric implementation. * - * @author <a href="mailto:steinar@yahoo-inc.com">Steinar Knutsen</a> + * @author Steinar Knutsen */ public class MetricsTest extends UnitTestSetup { + SimpleMetricConsumer metricApi; @BeforeEach @@ -36,7 +37,7 @@ public class MetricsTest extends UnitTestSetup { @Test final void smokeTest() throws InterruptedException { final String metricName = "testMetric"; - metricApi.set(metricName, Double.valueOf(1.0d), null); + metricApi.set(metricName, 1.0d, null); updater.gotData.await(10, TimeUnit.SECONDS); Bucket s = getUpdatedSnapshot(); Collection<Entry<Point, UntypedMetric>> values = s.getValuesForMetric(metricName); diff --git a/container-core/src/test/java/com/yahoo/metrics/simple/jdisc/SnapshotConverterTest.java b/container-core/src/test/java/com/yahoo/metrics/simple/jdisc/SnapshotConverterTest.java index 7981e5904f3..1d5cf264964 100644 --- a/container-core/src/test/java/com/yahoo/metrics/simple/jdisc/SnapshotConverterTest.java +++ b/container-core/src/test/java/com/yahoo/metrics/simple/jdisc/SnapshotConverterTest.java @@ -20,6 +20,7 @@ import java.util.Map; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.fail; /** * @author bratseth @@ -50,7 +51,7 @@ public class SnapshotConverterTest { for (Map.Entry<MetricDimensions, MetricSet> entry : snapshot) { for (Map.Entry<String, String> dv : entry.getKey()) { - assertTrue(false); + fail(); } int cnt = 0; @@ -67,7 +68,7 @@ public class SnapshotConverterTest { assertEquals(42.25, ((GaugeMetric) mv.getValue()).getLast(), 0.001); assertEquals(1, ((GaugeMetric) mv.getValue()).getCount()); } else { - assertTrue(false); + fail(); } } assertEquals(3, cnt); |