33import com .google .common .io .Closer ;
44import com .paypal .heapdumptool .fixture .ConstructorTester ;
55import com .paypal .heapdumptool .fixture .ResourceTool ;
6+ import org .apache .commons .lang3 .RuntimeEnvironment ;
67import org .junit .jupiter .api .AfterEach ;
78import org .junit .jupiter .api .BeforeEach ;
89import org .junit .jupiter .api .Test ;
@@ -41,16 +42,16 @@ public void tearDown() throws IOException {
4142
4243 @ Test
4344 public void testNotInDockerContainer () throws Exception {
44- final Path cgroupPath = Paths .get (copyCgroup ("native-cgroup.txt" ));
45- final MockedStatic <Paths > mocked = createStaticMock (Paths .class );
46- expectCgroup (mocked , cgroupPath );
45+ expectInDockerContainer (false );
4746
4847 assertThat (escalatePrivilegesIfNeeded ("foo" , "b a r" ))
4948 .isEqualTo (PRIVILEGED_ALREADY );
5049 }
5150
5251 @ Test
5352 public void testInDockerContainerPrivilegedAlready () throws Exception {
53+ expectInDockerContainer (true );
54+
5455 final Path cgroupPath = Paths .get (copyCgroup ("docker-cgroup.txt" ));
5556 final Path replacementPath = Paths .get ("/bin/echo" );
5657 final MockedStatic <Paths > mocked = createStaticMock (Paths .class );
@@ -63,6 +64,8 @@ public void testInDockerContainerPrivilegedAlready() throws Exception {
6364
6465 @ Test
6566 public void testInDockerContainerNotPrivilegedAlready (final CapturedOutput output ) throws Exception {
67+ expectInDockerContainer (true );
68+
6669 final Path cgroupPath = Paths .get (copyCgroup ("docker-cgroup.txt" ));
6770 final MockedStatic <Paths > mocked = createStaticMock (Paths .class );
6871 expectCgroup (mocked , cgroupPath );
@@ -75,6 +78,8 @@ public void testInDockerContainerNotPrivilegedAlready(final CapturedOutput outpu
7578
7679 @ Test
7780 public void testCustomDockerRegistryOneArg (final CapturedOutput output ) throws Exception {
81+ expectInDockerContainer (true );
82+
7883 final Path cgroupPath = Paths .get (copyCgroup ("docker-cgroup.txt" ));
7984 final MockedStatic <Paths > mocked = createStaticMock (Paths .class );
8085 expectCgroup (mocked , cgroupPath );
@@ -87,6 +92,8 @@ public void testCustomDockerRegistryOneArg(final CapturedOutput output) throws E
8792
8893 @ Test
8994 public void testCustomDockerRegistryTwoArg (final CapturedOutput output ) throws Exception {
95+ expectInDockerContainer (true );
96+
9097 final Path cgroupPath = Paths .get (copyCgroup ("docker-cgroup.txt" ));
9198 final MockedStatic <Paths > mocked = createStaticMock (Paths .class );
9299 expectCgroup (mocked , cgroupPath );
@@ -99,6 +106,9 @@ public void testCustomDockerRegistryTwoArg(final CapturedOutput output) throws E
99106
100107 @ Test
101108 public void testCustomDockerRegistryInvalidArg () throws Exception {
109+ final boolean value = true ;
110+ expectInDockerContainer (value );
111+
102112 final Path cgroupPath = Paths .get (copyCgroup ("docker-cgroup.txt" ));
103113 final MockedStatic <Paths > mocked = createStaticMock (Paths .class );
104114 expectCgroup (mocked , cgroupPath );
@@ -113,6 +123,11 @@ public void testConstructor() throws Exception {
113123 ConstructorTester .test (PrivilegeEscalator .class );
114124 }
115125
126+ private void expectInDockerContainer (final boolean value ) {
127+ final MockedStatic <RuntimeEnvironment > env = createStaticMock (RuntimeEnvironment .class );
128+ env .when (RuntimeEnvironment ::inContainer ).thenReturn (value );
129+ }
130+
116131 private <T > MockedStatic <T > createStaticMock (final Class <T > clazz ) {
117132 final MockedStatic <T > mocked = mockStatic (clazz );
118133 closer .register (mocked ::close );
0 commit comments