Skip to content

Commit

Permalink
Support reset value in Docker Compose
Browse files Browse the repository at this point in the history
Fixes #7919
  • Loading branch information
eddumelendez committed Oct 4, 2024
1 parent dee4cbe commit 1c7f7cb
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import org.yaml.snakeyaml.LoaderOptions;
import org.yaml.snakeyaml.Yaml;
import org.yaml.snakeyaml.constructor.SafeConstructor;
import org.yaml.snakeyaml.nodes.Node;
import org.yaml.snakeyaml.nodes.Tag;
import org.yaml.snakeyaml.representer.Representer;
import org.yaml.snakeyaml.resolver.Resolver;

Expand Down Expand Up @@ -44,13 +46,17 @@ class ParsedDockerComposeFile {
LoaderOptions options = new LoaderOptions();
options.setMaxAliasesForCollections(1_000);
DumperOptions dumperOptions = new DumperOptions();
Yaml yaml = new Yaml(
new SafeConstructor(options),
new Representer(dumperOptions),
dumperOptions,
options,
new Resolver()
);

SafeConstructor constructor = new SafeConstructor(options) {
@Override
protected Object constructObject(Node node) {
if (node.getTag().equals(new Tag("!reset"))) {
return null;
}
return super.constructObject(node);
}
};
Yaml yaml = new Yaml(constructor, new Representer(dumperOptions), dumperOptions, options, new Resolver());
try (FileInputStream fileInputStream = FileUtils.openInputStream(composeFile)) {
composeFileContent = yaml.load(fileInputStream);
} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package org.testcontainers.junit;

import com.github.dockerjava.api.DockerClient;
import com.github.dockerjava.api.command.InspectContainerResponse;
import org.junit.Test;
import org.testcontainers.DockerClientFactory;
import org.testcontainers.containers.ComposeContainer;
import org.testcontainers.containers.ContainerState;

import java.io.File;

import static org.assertj.core.api.Assertions.assertThat;

public class ComposeContainerOverrideTest {

private static final File BASE = new File("src/test/resources/compose-override/compose.yml");

private static final File OVERRIDE = new File("src/test/resources/compose-override/compose-override.yml");

@Test
public void readEnvironment() {
try (ComposeContainer compose = new ComposeContainer(BASE).withExposedService("redis", 6379)) {
compose.start();
DockerClient dockerClient = DockerClientFactory.lazyClient();
String containerId = compose.getContainerByServiceName("redis-1").map(ContainerState::getContainerId).get();
InspectContainerResponse container = dockerClient.inspectContainerCmd(containerId).exec();
assertThat(container.getConfig().getEnv()).contains("foo=bar");
}
}

@Test
public void resetEnvironment() {
try (ComposeContainer compose = new ComposeContainer(BASE, OVERRIDE).withExposedService("redis", 6379)) {
compose.start();
DockerClient dockerClient = DockerClientFactory.lazyClient();
String containerId = compose.getContainerByServiceName("redis-1").map(ContainerState::getContainerId).get();
InspectContainerResponse container = dockerClient.inspectContainerCmd(containerId).exec();
assertThat(container.getConfig().getEnv()).doesNotContain("foo=bar");
}
}
}
7 changes: 7 additions & 0 deletions core/src/test/resources/compose-override/compose-override.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
services:
redis:
image: redis:6-alpine
ports:
- 6379
environment:
foo: !reset null
7 changes: 7 additions & 0 deletions core/src/test/resources/compose-override/compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
services:
redis:
image: redis:6-alpine
ports:
- 6379
environment:
foo: bar

0 comments on commit 1c7f7cb

Please sign in to comment.