8000 test: Container tests stabilized (#13) · homecentr/docker-cadvisor@82b08cb · GitHub
[go: up one dir, main page]

Skip to content
This repository was archived by the owner on Feb 16, 2023. It is now read-only.

Commit 82b08cb

Browse files
authored
test: Container tests stabilized (#13)
1 parent cf524da commit 82b08cb

12 files changed

+162
-116
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ jobs:
3030
run: docker build . -t ${{ env.IMAGE_NAME }}:${{ steps.vars.outputs.docker_tag }}
3131

3232
- name: Test Docker image
33-
run: cd tests && gradle test --info -Dimage_tag=${{ env.IMAGE_NAME }}:${{ steps.vars.outputs.docker_tag }}
33+
run: cd tests && gradle test --info -Ddocker_image_tag=${{ env.IMAGE_NAME }}:${{ steps.vars.outputs.docker_tag }}
3434

3535
- name: Scan with Phonito Security
3636
uses: phonito/phonito-scanner-action@master

.github/workflows/ci_cd.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ jobs:
4040
4141
- name: Test Docker image
4242
if: env.RELEASE_VERSION != ''
43-
run: cd tests && gradle test -Dimage_tag=${{ env.IMAGE_NAME }}:${{ env.RELEASE_VERSION }}
43+
run: cd tests && gradle test -Ddocker_image_tag=${{ env.IMAGE_NAME }}:${{ env.RELEASE_VERSION }}
4444

4545
- name: Scan with Phonito Security
4646
if: env.RELEASE_VERSION != ''

tests/.idea/dictionaries/lholota.xml

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/.idea/jarRepositories.xml

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/build.gradle

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,22 @@ sourceCompatibility = 1.8
99

1010
repositories {
1111
mavenCentral()
12+
maven {
13+
url "https://dl.bintray.com/homecentr/maven"
14+
}
1215
}
1316

1417
dependencies {
1518
testImplementation group: 'junit', name: 'junit', version: '4.13'
16-
testImplementation "org.testcontainers:testcontainers:1.14.1"
19+
testImplementation 'org.testcontainers:testcontainers:1.14.1'
20+
testImplementation 'io.homecentr:testcontainers-extensions:1.3.2'
1721
testImplementation group: 'org.slf4j', name: 'slf4j-api', version: '1.7.30'
1822
testImplementation group: 'org.slf4j', name: 'slf4j-simple', version: '1.7.30'
1923
}
2024

2125
test {
22-
systemProperty 'image_tag', System.getProperty('image_tag')
26+
systemProperty 'docker_image_tag', System.getProperty('docker_image_tag')
27+
2328
afterTest { desc, result ->
2429
logger.quiet "Executing test ${desc.name} [${desc.className}] with result: ${result.resultType}"
2530
}
Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,48 @@
1+
import helpers.CadvisorImageTagResolver;
2+
import helpers.MachineIdFile;
3+
import io.homecentr.testcontainers.containers.GenericContainerEx;
4+
import io.homecentr.testcontainers.containers.wait.strategy.WaitEx;
5+
import io.homecentr.testcontainers.images.PullPolicyEx;
16
import org.junit.AfterClass;
27
import org.junit.BeforeClass;
8+
import org.slf4j.Logger;
9+
import org.slf4j.LoggerFactory;
10+
import org.testcontainers.containers.output.Slf4jLogConsumer;
311

4-
import java.util.HashMap;
12+
import java.io.IOException;
513

614
public class CadvisorContainerRunningAsNonRootShould extends CadvisorContainerTests {
7-
private static ContainerController _controller;
15+
private static final Logger _logger = LoggerFactory.getLogger(CadvisorContainerRunningAsNonRootShould.class);
16+
17+
private static GenericContainerEx _container;
18+
private static MachineIdFile _machineIdFile;
819

920
@BeforeClass
10-
public static void start() {
11-
_controller = new ContainerController();
12-
_controller.startContainer(new HashMap<>());
21+
public static void start() throws IOException {
22+
_machineIdFile = MachineIdFile.create();
23+
24+
_container = new GenericContainerEx<>(new CadvisorImageTagResolver())
25+
.withImagePullPolicy(PullPolicyEx.never())
26+
.withPrivilegedMode(true)
27+
.withFileSystemBind("//var/run/docker.sock", "/var/run/docker.sock")
28+
29+
30+
// .withFileSystemBind("//dev/kmsg", "/dev/kmsg")
31+
.withFileSystemBind(_machineIdFile.getAbsolutePath(), "/etc/machine-id")
32+
.waitingFor(WaitEx.forHttp("/healthz").forPort(8080));
33+
34+
_container.start();
35+
_container.followOutput(new Slf4jLogConsumer(_logger));
1336
}
1437

1538
@AfterClass
1639
public static void cleanUp() {
17-
_controller.stopContainerIfExists();
40+
_container.close();
41+
_machineIdFile.close();
1842
}
1943

2044
@Override
21-
protected ContainerController getController() {
22-
return _controller;
45+
protected GenericContainerEx getContainer() {
46+
return _container;
2347
}
2448
}
Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,47 @@
1+
import helpers.CadvisorImageTagResolver;
2+
import helpers.MachineIdFile;
3+
import io.homecentr.testcontainers.containers.GenericContainerEx;
4+
import io.homecentr.testcontainers.containers.wait.strategy.WaitEx;
5+
import io.homecentr.testcontainers.images.PullPolicyEx;
16
import org.junit.AfterClass;
27
import org.junit.BeforeClass;
8+
import org.slf4j.Logger;
9+
import org.slf4j.LoggerFactory;
10+
import org.testcontainers.containers.output.Slf4jLogConsumer;
311

4-
import java.util.HashMap;
12+
import java.io.IOException;
513

614
public class CadvisorContainerRunningAsRootShould extends CadvisorContainerTests {
15+
private static final Logger _logger = LoggerFactory.getLogger(CadvisorContainerRunningAsNonRootShould.class);
716

8-
private static ContainerController _controller;
17+
private static GenericContainerEx _container;
18+
private static MachineIdFile _machineIdFile;
919

1020
@BeforeClass
11-
public static void start() {
12-
HashMap<String, String> envVars = new HashMap<>();
13-
envVars.put("PUID", "0");
14-
envVars.put("PGID", "0");
21+
public static void start() throws IOException {
22+
_machineIdFile = MachineIdFile.create();
1523

16-
_controller = new ContainerController();
17-
_controller.startContainer(envVars);
24+
_container = new GenericContainerEx<>(new CadvisorImageTagResolver())
25+
.withImagePullPolicy(PullPolicyEx.never())
26+
.withPrivilegedMode(true)
27+
.withFileSystemBind("//var/run/docker.sock", "/var/run/docker.sock")
28+
.withFileSystemBind(_machineIdFile.getAbsolutePath(), "/etc/machine-id")
29+
.withEnv("PUID", "0")
30+
.withEnv("PGID", "0")
31+
.waitingFor(WaitEx.forHttp("/healthz").forPort(8080));
32+
33+
_container.start();
34+
_container.followOutput(new Slf4jLogConsumer(_logger));
1835
}
1936

2037
@AfterClass
2138
public static void cleanUp() {
22-
_controller.stopContainerIfExists();
39+
_container.close();
40+
_machineIdFile.close();
2341
}
2442

2543
@Override
26-
protected ContainerController getController() {
27-
return _controller;
44+
protected GenericContainerEx getContainer() {
45+
return _container;
2846
}
2947
}
Lines changed: 9 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,33 @@
1+
import io.homecentr.testcontainers.containers.GenericContainerEx;
2+
import io.homecentr.testcontainers.containers.HttpResponse;
13
import org.junit.Test;
24

35
import java.io.IOException;
4-
import java.net.HttpURLConnection;
5-
import java.net.URL;
66

7+
import static helpers.ContainerLogsUtils.hasErrorOutputExceptKnownErrors;
78
import static org.junit.Assert.assertEquals;
89
import static org.junit.Assert.assertFalse;
910

1011
public abstract class CadvisorContainerTests {
1112

12-
protected abstract ContainerController getController();
13+
protected abstract GenericContainerEx getContainer();
1314

1415
@Test
1516
public void notProduceErrorOutput() {
16-
assertFalse(getController().hasErrorOutput());
17+
assertFalse(hasErrorOutputExceptKnownErrors(getContainer()));
1718
}
1819

1920
@Test
2021
public void listenOnWebUiPort() throws IOException {
21-
URL root = new URL(String.format("http://%s:%d",
22-
getController().getContainer().getContainerIpAddress(),
23-
getController().getContainer().getMappedPort(8080)));
22+
HttpResponse response = getContainer().makeHttpRequest(8080, "/");
2423

25-
HttpURLConnection connection = (HttpURLConnection)root.openConnection();
26-
connection.connect();
27-
28-
assertEquals(200, connection.getResponseCode());
24+
assertEquals(200, response.getResponseCode());
2925
}
3026

3127
@Test
3228
public void returnMetrics() throws IOException {
33-
URL root = new URL(String.format("http://%s:%d/metrics",
34-
getController().getContainer().getContainerIpAddress(),
35-
getController().getContainer().getMappedPort(8080)));
36-
37-
HttpURLConnection connection = (HttpURLConnection)root.openConnection();
38-
connection.connect();
39-
40-
assertEquals(200, connection.getResponseCode());
41-
}
42-
43-
@Test
44-
public void returnSuccessOnHealthCheckEndpoint() throws IOException {
45-
URL root = new URL(String.format("http://%s:%d/healthz",
46-
getController().getContainer().getContainerIpAddress(),
47-
getController().getContainer().getMappedPort(8080)));
48-
49-
HttpURLConnection connection = (HttpURLConnection)root.openConnection();
50-
connection.connect();
29+
HttpResponse response = getContainer().makeHttpRequest(8080, "/metrics");
5130

52-
assertEquals(200, connection.getResponseCode());
31+
assertEquals(200, response.getResponseCode());
5332
}
5433
}

tests/src/test/java/ContainerController.java

Lines changed: 0 additions & 63 deletions
This file was deleted.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package helpers;
2+
3+
import io.homecentr.testcontainers.images.EnvironmentImageTagResolver;
4+
5+
public class CadvisorImageTagResolver extends EnvironmentImageTagResolver {
6+
public CadvisorImageTagResolver() {
7+
super("homecentr/cadvisor:local");
8+
}
9+
}

0 commit comments

Comments
 (0)
0