View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      https://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.commons.lang3;
19  
20  import static org.junit.jupiter.api.Assertions.assertEquals;
21  
22  import java.io.IOException;
23  import java.nio.charset.Charset;
24  import java.nio.file.Files;
25  import java.nio.file.Path;
26  import java.util.UUID;
27  
28  import org.junit.jupiter.api.io.TempDir;
29  import org.junit.jupiter.params.ParameterizedTest;
30  import org.junit.jupiter.params.provider.Arguments;
31  import org.junit.jupiter.params.provider.MethodSource;
32  
33  /**
34   * Tests {@link RuntimeEnvironment}.
35   */
36  class RuntimeEnvironmentTest {
37  
38      private static final String simpleEnviron = "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin\u0000" +
39              "HOSTNAME=d62718b69f37\u0000TERM=xterm\u0000HOME=/root\u0000";
40  
41      private static final String podmanEnviron = "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin\u0000" +
42              "HOSTNAME=d62718b69f37\u0000TERM=xterm\u0000container=podman\u0000HOME=/root\u0000";
43  
44      private static final String emptyContainer = "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin\u0000" +
45              "HOSTNAME=d62718b69f37\u0000TERM=xterm\u0000container=\u0000HOME=/root\u0000";
46  
47      private static Arguments[] testIsContainer() {
48          return new Arguments[]{
49                  Arguments.of("in docker no file", simpleEnviron, null, false),
50                  Arguments.of("in docker with file", simpleEnviron, ".dockerenv", true),
51                  Arguments.of("in podman no file", podmanEnviron, "run/.containerenv", true),
52                  Arguments.of("in podman with file", simpleEnviron, "run/.containerenv", true),
53                  Arguments.of("in podman empty env var no file", emptyContainer, null, false),
54                  Arguments.of("in podman empty env var with file", emptyContainer, "run/.containerenv", false),
55                  Arguments.of("not in container", simpleEnviron, null, false),
56                  Arguments.of("pid1 error no file", null, null, false),
57                  Arguments.of("pid1 error docker file", null, ".dockerenv", true),
58                  Arguments.of("pid1 error podman file", null, ".dockerenv", true),
59          };
60      }
61  
62      @TempDir
63      private Path tempDir;
64  
65      private boolean doTestInContainer(final String environ, final String fileToCreate) throws IOException {
66          final Path testDir = tempDir.resolve(UUID.randomUUID().toString());
67          final Path pid1EnvironFile = testDir.resolve("proc/1/environ");
68          Files.createDirectories(pid1EnvironFile.getParent());
69          if (fileToCreate != null) {
70              final Path file = testDir.resolve(fileToCreate);
71              Files.createDirectories(file.getParent());
72              Files.createFile(file);
73          }
74          if (environ != null) {
75              Files.write(pid1EnvironFile, environ.getBytes(Charset.defaultCharset()));
76          }
77          return RuntimeEnvironment.inContainer(testDir.toString());
78      }
79  
80      @ParameterizedTest
81      @MethodSource
82      void testIsContainer(final String label, final String environ, final String fileToCreate, final boolean expected) throws IOException {
83          assertEquals(expected, doTestInContainer(environ, fileToCreate), label);
84      }
85  }