From d7edd27cdd84e1cc3972975cdd88fcc4b7b7c4dc Mon Sep 17 00:00:00 2001 From: Pavlo Tkach <3469726+ptkach@users.noreply.github.com> Date: Wed, 10 Jan 2024 14:32:34 -0500 Subject: [PATCH] Add support for Ubuntu20 on kokoro (#2279) --- .../tools/DumpGoldenSchemaCommand.java | 21 ++------------- .../tools/params/PathParameterTest.java | 26 ++++++++++++++----- 2 files changed, 22 insertions(+), 25 deletions(-) diff --git a/core/src/nonprod/java/google/registry/tools/DumpGoldenSchemaCommand.java b/core/src/nonprod/java/google/registry/tools/DumpGoldenSchemaCommand.java index d9aa1ddb9..cd7df1c5d 100644 --- a/core/src/nonprod/java/google/registry/tools/DumpGoldenSchemaCommand.java +++ b/core/src/nonprod/java/google/registry/tools/DumpGoldenSchemaCommand.java @@ -16,11 +16,9 @@ package google.registry.tools; import com.beust.jcommander.Parameter; import com.beust.jcommander.Parameters; -import java.io.FileOutputStream; import java.io.IOException; import java.nio.file.Path; import org.flywaydb.core.Flyway; -import org.testcontainers.containers.BindMode; import org.testcontainers.containers.Container; /** @@ -36,9 +34,6 @@ public class DumpGoldenSchemaCommand extends PostgresqlCommand { // The mount point in the container. private static final String CONTAINER_MOUNT_POINT = "/tmp/pg_dump.out"; - // Temporary workaround to fix permission issues on certain Linux distro (e. g. Arch Linux). - private static final String CONTAINER_MOUNT_POINT_TMP = "/tmp/pg_dump.tmp"; - @Parameter( names = {"--output", "-o"}, description = "Output file", @@ -64,19 +59,7 @@ public class DumpGoldenSchemaCommand extends PostgresqlCommand { if (result.getExitCode() != 0) { throw new RuntimeException(result.toString()); } - result = - postgresContainer.execInContainer("cp", CONTAINER_MOUNT_POINT_TMP, CONTAINER_MOUNT_POINT); - if (result.getExitCode() != 0) { - throw new RuntimeException(result.toString()); - } - } - - @Override - protected void onContainerCreate() throws IOException { - // open the output file for write so we can mount it. - new FileOutputStream(output.toFile()).close(); - postgresContainer.withFileSystemBind( - output.toString(), CONTAINER_MOUNT_POINT, BindMode.READ_WRITE); + postgresContainer.copyFileFromContainer(CONTAINER_MOUNT_POINT, output.toString()); } private static String[] getSchemaDumpCommand(String username, String dbName) { @@ -87,7 +70,7 @@ public class DumpGoldenSchemaCommand extends PostgresqlCommand { "-U", username, "-f", - CONTAINER_MOUNT_POINT_TMP, + CONTAINER_MOUNT_POINT, "--schema-only", "--no-owner", "--no-privileges", diff --git a/core/src/test/java/google/registry/tools/params/PathParameterTest.java b/core/src/test/java/google/registry/tools/params/PathParameterTest.java index 6208edcc6..641474067 100644 --- a/core/src/test/java/google/registry/tools/params/PathParameterTest.java +++ b/core/src/test/java/google/registry/tools/params/PathParameterTest.java @@ -38,6 +38,8 @@ class PathParameterTest { private final PathParameter vanilla = new PathParameter(); + private static final Boolean isRoot = "root".equals(System.getProperty("user.name")); + @Test void testConvert_etcPasswd_returnsPath() { assertThat((Object) vanilla.convert("/etc/passwd")).isEqualTo(Paths.get("/etc/passwd")); @@ -105,9 +107,15 @@ class PathParameterTest { void testInputFileValidate_unreadableFile_throws() throws Exception { Path file = Files.createFile(tmpDir.resolve("tmpfile.txt")); Files.setPosixFilePermissions(file, PosixFilePermissions.fromString("-w-------")); - ParameterException thrown = - assertThrows(ParameterException.class, () -> inputFile.validate("input", file.toString())); - assertThat(thrown).hasMessageThat().contains("not readable"); + // This test doesn't take into account the fact that root user has access to read/write the + // file even if posix permissions disallow it. + // For the environmnent that run strictly under root, we will ignore the test results. + if (!isRoot) { + ParameterException thrown = + assertThrows( + ParameterException.class, () -> inputFile.validate("input", file.toString())); + assertThat(thrown).hasMessageThat().contains("not readable"); + } } // =========================== Test OutputFile Validate ======================================== @@ -142,9 +150,15 @@ class PathParameterTest { void testOutputFileValidate_notWritable_throws() throws Exception { Path file = Files.createFile(tmpDir.resolve("newFile.dat")); Files.setPosixFilePermissions(file, PosixFilePermissions.fromString("r--------")); - ParameterException thrown = - assertThrows(ParameterException.class, () -> outputFile.validate("input", file.toString())); - assertThat(thrown).hasMessageThat().contains("not writable"); + // This test doesn't take into account the fact that root user has access to read/write the + // file even if posix permissions disallow it. + // For the environmnent that run strictly under root, we will ignore the test results. + if (!isRoot) { + ParameterException thrown = + assertThrows( + ParameterException.class, () -> outputFile.validate("input", file.toString())); + assertThat(thrown).hasMessageThat().contains("not writable"); + } } @Test