Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test product tests launcher #17875

Merged
merged 5 commits into from
Jun 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import io.trino.tests.product.launcher.util.ConsoleTable;
wendigo marked this conversation as resolved.
Show resolved Hide resolved
import org.testcontainers.utility.MountableFile;
import picocli.CommandLine;
import picocli.CommandLine.ExitCode;
import picocli.CommandLine.Mixin;
import picocli.CommandLine.Option;

Expand Down Expand Up @@ -190,7 +191,7 @@ public Integer call()

log.info("Environment '%s' file mounts:\n%s", environmentUpOptions.environment, mountsTable.render());

return 1;
return ExitCode.OK;
}

private String simplifyPath(String path)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@
import picocli.CommandLine.ExitCode;
import picocli.CommandLine.Option;

import java.io.FileDescriptor;
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
Expand Down Expand Up @@ -78,7 +76,7 @@ public Execution(EnvironmentFactory factory, EnvironmentConfigFactory configFact
this.configFactory = requireNonNull(configFactory, "configFactory is null");

try {
this.out = new PrintStream(new FileOutputStream(FileDescriptor.out), true, Charset.defaultCharset().name());
this.out = new PrintStream(System.out, true, Charset.defaultCharset().name());
}
catch (UnsupportedEncodingException e) {
throw new IllegalStateException("Could not create print stream", e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,18 +59,18 @@ public class Launcher
public static void main(String[] args)
{
Launcher launcher = new Launcher();
run(launcher, new LauncherBundle(), args);
System.exit(execute(launcher, new LauncherBundle(), args));
}

public static void run(Launcher launcher, ResourceBundle bundle, String[] args)
public static int execute(Launcher launcher, ResourceBundle bundle, String[] args)
{
IFactory factory = createFactory(launcher.getExtensions());
System.exit(new CommandLine(launcher, factory)
return new CommandLine(launcher, factory)
.setCaseInsensitiveEnumValuesAllowed(true)
.registerConverter(Duration.class, Duration::valueOf)
.registerConverter(Path.class, Paths::get)
.setResourceBundle(bundle)
.execute(args));
.execute(args);
}

private static IFactory createFactory(Extensions extensions)
Expand Down Expand Up @@ -157,7 +157,7 @@ public String[] getVersion()
}
}

private static class LauncherBundle
static class LauncherBundle
extends ListResourceBundle
{
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,6 @@
import picocli.CommandLine.Mixin;

import java.io.File;
import java.io.FileDescriptor;
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
Expand Down Expand Up @@ -232,7 +230,7 @@ public Execution(
this.outputBuilder = describeOptions.format.outputBuilderFactory.get();

try {
this.out = new PrintStream(new FileOutputStream(FileDescriptor.out), true, Charset.defaultCharset().name());
this.out = new PrintStream(System.out, true, Charset.defaultCharset().name());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is causing to actually write to stderr because of https://github.com/airlift/airlift/blob/master/log-manager/src/main/java/io/airlift/log/Logging.java#L139

This commit should be reverted. Logging to stderr here makes this fail to read any output: https://github.com/trinodb/trino/blob/master/.github/bin/build-pt-matrix-from-impacted-connectors.py#L108
and in result we're always running all PTs in PRs

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}
catch (UnsupportedEncodingException e) {
throw new IllegalStateException("Could not create print stream", e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@
import picocli.CommandLine.ExitCode;
import picocli.CommandLine.Option;

import java.io.FileDescriptor;
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
Expand Down Expand Up @@ -83,7 +81,7 @@ public Execution(SuiteFactory suiteFactory, EnvironmentConfigFactory configFacto
this.suiteFactory = requireNonNull(suiteFactory, "suiteFactory is null");

try {
this.out = new PrintStream(new FileOutputStream(FileDescriptor.out), true, Charset.defaultCharset().name());
this.out = new PrintStream(System.out, true, Charset.defaultCharset().name());
}
catch (UnsupportedEncodingException e) {
throw new IllegalStateException("Could not create print stream", e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,7 @@
import org.testcontainers.utility.MountableFile;

import java.io.File;
import java.io.FileDescriptor;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.io.UncheckedIOException;
Expand Down Expand Up @@ -635,7 +633,7 @@ private static Consumer<OutputFrame> printContainerLogs(DockerContainer containe
{
try {
// write directly to System.out, bypassing logging & io.airlift.log.Logging#rewireStdStreams
PrintStream out = new PrintStream(new FileOutputStream(FileDescriptor.out), true, Charset.defaultCharset().name());
PrintStream out = new PrintStream(System.out, true, Charset.defaultCharset().name());
return new PrintingLogConsumer(out, format("%-20s| ", container.getLogicalName()));
}
catch (UnsupportedEncodingException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
*/
package io.trino.tests.product.launcher.env;

import jakarta.annotation.Nullable;
import picocli.CommandLine;
import picocli.CommandLine.Model.CommandSpec;
import picocli.CommandLine.Spec;
Expand Down Expand Up @@ -60,6 +61,7 @@ public final class EnvironmentOptions
public String jdkProvider = BUILT_IN_NAME;

@Option(names = "--jdk-tmp-download-path", paramLabel = "<jdk-tmp-download-path>", defaultValue = "${env:PTL_TMP_DOWNLOAD_PATH:-${sys:java.io.tmpdir}/ptl-tmp-download}", description = "Path to use to download JDK distributions " + DEFAULT_VALUE)
@Nullable
public Path jdkDownloadPath;

@Option(names = "--bind", description = "Bind exposed container ports to host ports, possible values: " + BIND_ON_HOST + ", " + DO_NOT_BIND + ", [port base number] " + DEFAULT_VALUE, defaultValue = BIND_ON_HOST, arity = "0..1", fallbackValue = BIND_ON_HOST)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import java.util.function.Consumer;
import java.util.function.Function;

import static com.google.common.base.MoreObjects.firstNonNull;
import static com.google.common.base.Strings.isNullOrEmpty;
import static com.google.common.base.Verify.verify;
import static io.trino.testing.containers.TestContainers.getDockerArchitectureInfo;
Expand All @@ -53,7 +54,12 @@ public abstract class TarDownloadingJdkProvider

public TarDownloadingJdkProvider(EnvironmentOptions environmentOptions)
{
this.downloadPath = requireNonNull(environmentOptions.jdkDownloadPath, "environmentOptions.jdkDownloadPath is null");
try {
this.downloadPath = firstNonNull(environmentOptions.jdkDownloadPath, Files.createTempDirectory("ptl-temp-path"));
}
catch (IOException e) {
throw new UncheckedIOException(e);
}
}

protected abstract String getDownloadUri(DockerArchitecture architecture);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.trino.tests.product.launcher.cli;

import org.testng.annotations.Test;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.nio.file.Files;

import static io.trino.tests.product.launcher.cli.Launcher.execute;
import static java.nio.charset.StandardCharsets.UTF_8;
import static java.util.Objects.requireNonNull;
import static org.assertj.core.api.Assertions.assertThat;

@Test(singleThreaded = true)
public class TestInvocations
{
@Test
public void testEnvironmentList()
{
InvocationResult invocationResult = invokeLauncher("env", "list");

assertThat(invocationResult.exitCode()).isEqualTo(0);
assertThat(invocationResult.output())
.contains("Available environments:")
.contains("multinode");
}

@Test
public void testSuiteList()
{
InvocationResult invocationResult = invokeLauncher("suite", "list");

assertThat(invocationResult.exitCode()).isEqualTo(0);
assertThat(invocationResult.output())
.contains("Available suites:")
.contains("suite-1");
}

@Test
public void testDescribeEnvironment()
throws IOException
{
InvocationResult invocationResult = invokeLauncher(
"env", "describe",
"--server-package", Files.createTempFile("server", ".tar.gz").toString(),
// This is known to work for both arm and x86
"--environment", "multinode-postgresql");

assertThat(invocationResult.exitCode()).isEqualTo(0);
assertThat(invocationResult.output())
.contains("Environment 'multinode-postgresql' file mounts:");
}

@Test
public void testDescribeSuite()
throws IOException
{
InvocationResult invocationResult = invokeLauncher(
"suite", "describe",
"--server-package", Files.createTempFile("server", ".tar.gz").toString(),
"--suite", "suite-1");

assertThat(invocationResult.exitCode()).isEqualTo(0);
assertThat(invocationResult.output())
.contains("Suite 'suite-1' with configuration 'config-default' consists of following test runs");
}

@Test
public void testEnvUpDown()
throws IOException
{
InvocationResult upResult = invokeLauncher(
"env", "up",
"--server-package", Files.createTempFile("server", ".tar.gz").toString(),
"--without-trino",
"--background",
"--bind",
"off",
// This is known to work for both arm and x86
"--environment", "multinode-postgresql");

assertThat(upResult.exitCode()).isEqualTo(0);
InvocationResult downResult = invokeLauncher("env", "down");
assertThat(downResult.exitCode()).isEqualTo(0);
}

public static InvocationResult invokeLauncher(String... args)
{
Launcher launcher = new Launcher();
ByteArrayOutputStream out = new ByteArrayOutputStream();

try (CaptureOutput ignored = new CaptureOutput(out)) {
int exitCode = execute(launcher, new Launcher.LauncherBundle(), args);
return new InvocationResult(exitCode, out.toString(UTF_8));
}
}

@SuppressWarnings("UnusedVariable")
private record InvocationResult(int exitCode, String output) {}

private static class CaptureOutput
implements AutoCloseable
{
private final PrintStream originalOut;
private final PrintStream originalErr;

public CaptureOutput(ByteArrayOutputStream out)
{
PrintStream stream = new PrintStream(requireNonNull(out, "out is null"));
this.originalOut = System.out;
this.originalErr = System.err;
System.setOut(stream);
System.setErr(stream);
}

@Override
public void close()
{
System.setOut(originalOut);
System.setErr(originalErr);
}
}
}