Skip to content

Commit

Permalink
Don't run EmptyDirTaskTests in a Docker container (#3792) (#3912)
Browse files Browse the repository at this point in the history
* Don't run EmptyDirTaskTests in a Docker container

Signed-off-by: Daniel Widdis <[email protected]>

* Catch possible exceptions

Signed-off-by: Daniel Widdis <[email protected]>

* Fix newlines with spotless

Signed-off-by: Daniel Widdis <[email protected]>

* Add comments explaining test incompatibilities

Signed-off-by: Daniel Widdis <[email protected]>

* IDE incorrectly removed needed imports

Signed-off-by: Daniel Widdis <[email protected]>

* Manual edit missed a duplicate

Signed-off-by: Daniel Widdis <[email protected]>
(cherry picked from commit c526b0c)

Co-authored-by: Daniel Widdis <[email protected]>
  • Loading branch information
opensearch-trigger-bot[bot] and dbwiddis authored Jul 15, 2022
1 parent 298cc9c commit 6841abd
Showing 1 changed file with 24 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.InvalidPathException;
import java.nio.file.Path;

import com.carrotsearch.randomizedtesting.RandomizedTest;
import org.apache.tools.ant.taskdefs.condition.Os;
Expand Down Expand Up @@ -64,7 +67,11 @@ public void testCreateEmptyDir() throws Exception {
}

public void testCreateEmptyDirNoPermissions() throws Exception {
RandomizedTest.assumeFalse("Functionality is Unix specific", Os.isFamily(Os.FAMILY_WINDOWS));
// Test depends on Posix file permissions
RandomizedTest.assumeFalse("Functionality is Unix-like OS specific", Os.isFamily(Os.FAMILY_WINDOWS));
// Java's Files.setPosixFilePermissions is a NOOP inside a Docker container as
// files are created by default with UID and GID = 0 (root).
RandomizedTest.assumeFalse("Functionality doesn't work in Docker", isRunningInDocker());

Project project = ProjectBuilder.builder().build();
EmptyDirTask emptyDirTask = project.getTasks().create("emptyDirTask", EmptyDirTask.class);
Expand Down Expand Up @@ -92,4 +99,20 @@ private File getNewNonExistingTempFolderFile(Project project) throws IOException
return newEmptyFolder;
}

private static boolean isRunningInDocker() {
// Only reliable existing method but may be removed in future
if (new File("/.dockerenv").exists()) {
return true;
}
try {
// Backup 1: look for 'docker' in one of the paths in /proc/1/cgroup
if (Files.lines(Path.of("/proc/1/cgroup")).anyMatch(line -> line.contains("docker"))) {
return true;
}
// Backup 2: look for 'docker' in overlay fs
return Files.lines(Path.of("/proc/1/mounts")).anyMatch(line -> line.startsWith("overlay") && line.contains("docker"));
} catch (InvalidPathException | IOException e) {
return false;
}
}
}

0 comments on commit 6841abd

Please sign in to comment.