Skip to content

Commit

Permalink
Fix RAT report complaint
Browse files Browse the repository at this point in the history
  • Loading branch information
maxxedev committed Dec 1, 2024
1 parent 6bac146 commit a409fd6
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 13 deletions.
28 changes: 15 additions & 13 deletions src/test/java/org/apache/commons/lang3/RuntimeEnvironmentTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@
import org.junit.jupiter.api.io.TempDir;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.UUID;
Expand All @@ -38,6 +37,12 @@
*/
public class RuntimeEnvironmentTest {

private static final String simpleEnviron = "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin\u0000" +
"HOSTNAME=d62718b69f37\u0000TERM=xterm\u0000HOME=/root\u0000";

private static final String podmanEnviron = "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin\u0000" +
"HOSTNAME=d62718b69f37\u0000TERM=xterm\u0000container=podman\u0000HOME=/root\u0000";

@TempDir
private Path tempDir;

Expand All @@ -50,19 +55,19 @@ public static void tearDown() {
public DynamicTest[] testIsContainerDocker() {
return new DynamicTest[]{
dynamicTest("in docker no file", () ->
assertFalse(doTestInContainer("RuntimeEnvironmentTest.docker.txt", null))),
assertFalse(doTestInContainer(simpleEnviron, null))),

dynamicTest("in docker with file", () ->
assertTrue(doTestInContainer("RuntimeEnvironmentTest.docker.txt", ".dockerenv"))),
assertTrue(doTestInContainer(simpleEnviron, ".dockerenv"))),

dynamicTest("in podman no file", () ->
assertTrue(doTestInContainer("RuntimeEnvironmentTest.podman.txt", null))),
assertTrue(doTestInContainer(podmanEnviron, null))),

dynamicTest("in podman with file", () ->
assertTrue(doTestInContainer("RuntimeEnvironmentTest.none.txt", "run/.containerenv"))),
assertTrue(doTestInContainer(simpleEnviron, "run/.containerenv"))),

dynamicTest("not in container", () ->
assertFalse(doTestInContainer("RuntimeEnvironmentTest.none.txt", null))),
assertFalse(doTestInContainer(simpleEnviron, null))),

dynamicTest("pid1 error no file", () ->
assertFalse(doTestInContainer(null, null))),
Expand All @@ -75,7 +80,7 @@ public DynamicTest[] testIsContainerDocker() {
};
}

private boolean doTestInContainer(String envFile, String fileToCreate) throws IOException {
private boolean doTestInContainer(String environ, String fileToCreate) throws IOException {
Path testDir = tempDir.resolve(UUID.randomUUID().toString());
RuntimeEnvironment.rootDir = testDir + "/";
Path pid1EnvironFile = testDir.resolve("proc/1/environ");
Expand All @@ -87,11 +92,8 @@ private boolean doTestInContainer(String envFile, String fileToCreate) throws IO
Files.createFile(file);
}

if (envFile != null) {
URL resource = RuntimeEnvironmentTest.class.getResource(envFile);
try (InputStream in = resource.openStream()) {
Files.copy(in, pid1EnvironFile);
}
if (environ != null) {
Files.write(pid1EnvironFile, environ.getBytes(StandardCharsets.UTF_8));
}

return RuntimeEnvironment.inContainer();
Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.

0 comments on commit a409fd6

Please sign in to comment.