Skip to content
This repository has been archived by the owner on Jun 27, 2024. It is now read-only.

Commit

Permalink
fix(bug): Connector process not executed as passed PUID/PGID (#67)
Browse files Browse the repository at this point in the history
  • Loading branch information
lholota committed Aug 12, 2021
1 parent 320a0e6 commit 59021b2
Show file tree
Hide file tree
Showing 9 changed files with 86 additions and 105 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ jobs:
run: docker build . -t ${{ env.IMAGE_NAME }}:${{ steps.vars.outputs.docker_tag }}

- name: Test Docker image
run: cd tests && sudo gradle test --info -Dimage_tag=${{ env.IMAGE_NAME }}:${{ steps.vars.outputs.docker_tag }}
run: cd tests && sudo --preserve-env gradle test --info -Ddocker_image_tag=${{ env.IMAGE_NAME }}:${{ steps.vars.outputs.docker_tag }}
env: # To allow downloading packages
GITHUB_USERNAME: ${{ github.actor }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
2 changes: 1 addition & 1 deletion .github/workflows/ci_cd.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ jobs:
- name: Test Docker image
if: env.RELEASE_VERSION != ''
run: cd tests && sudo gradle test -Dimage_tag=${{ env.IMAGE_NAME }}:${{ env.RELEASE_VERSION }}
run: cd tests && sudo --preserve-env gradle test -Ddocker_image_tag=${{ env.IMAGE_NAME }}:${{ env.RELEASE_VERSION }}
env: # To allow downloading packages
GITHUB_USERNAME: ${{ github.actor }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
Expand Down
4 changes: 1 addition & 3 deletions fs/etc/services.d/connector/run
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#!/usr/bin/with-contenv sh

s6-setuidgid "$PUID:$PGID"

exec /swarm-local-network-connector
exec runas /swarm-local-network-connector
5 changes: 5 additions & 0 deletions tests/.idea/jarRepositories.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 11 additions & 1 deletion tests/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,27 @@ sourceCompatibility = 1.8

repositories {
mavenCentral()
maven {
name = "GitHubPackages"
url = uri("https://maven.pkg.github.com/homecentr/lib-testcontainers-extensions")
credentials {
username = System.getenv("GITHUB_USERNAME")
password = System.getenv("GITHUB_TOKEN")
}
}
}

dependencies {
testImplementation group: 'junit', name: 'junit', version: '4.13.2'
testImplementation group: 'org.testcontainers', name: 'testcontainers', version: '1.16.0'
testImplementation group: 'io.homecentr', name: 'testcontainers-extensions', version: '1.6.0'
testImplementation group: 'org.slf4j', name: 'slf4j-api', version: '1.7.32'
testImplementation group: 'org.slf4j', name: 'slf4j-simple', version: '1.7.32'
}

test {
systemProperty 'image_tag', System.getProperty('image_tag')
systemProperty 'docker_image_tag', System.getProperty('docker_image_tag')

afterTest { desc, result ->
logger.quiet "Executing test ${desc.name} [${desc.className}] with result: ${result.resultType}"
}
Expand Down
123 changes: 56 additions & 67 deletions tests/src/test/java/ConnectorContainerShould.java
Original file line number Diff line number Diff line change
@@ -1,114 +1,103 @@
import PullPolicies.NeverPullImagePolicy;
import helpers.DockerImageTagResolver;
import io.homecentr.testcontainers.containers.GenericContainerEx;
import io.homecentr.testcontainers.images.NeverPullImagePolicy;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.containers.output.Slf4jLogConsumer;
import org.testcontainers.containers.wait.strategy.Wait;

import java.time.Duration;
import java.util.concurrent.TimeoutException;

import static io.homecentr.testcontainers.WaitLoop.waitFor;
import static org.junit.Assert.assertEquals;

public class ConnectorContainerShould extends ContainerTestBase {

private static final Logger logger = LoggerFactory.getLogger(ContainerTestBase.class);

@Test
public void connectContainersCreatedBeforeStartOfConnector() throws InterruptedException, TimeoutException {

GenericContainer targetContainer = new GenericContainer<>("nginx")
.withLabel("io.homecentr.local-networks", "[{ \"Network\": \""+ getNetworkName() +"\" }]");
private GenericContainer _connectorContainer;
private GenericContainer _targetContainer;

GenericContainer connectorContainer = createConnectorContainer();

try {
targetContainer.start();
@Before
public void before() {
_connectorContainer = new GenericContainerEx<>(new DockerImageTagResolver())
.withFileSystemBind("//var/run/docker.sock", "/var/run/docker.sock")
.withImagePullPolicy(new NeverPullImagePolicy())
.withEnv("LOG_LEVEL", "debug")
.withEnv("PUID", "0")
.withEnv("PGID", "0")
.waitingFor(Wait.forLogMessage(".*Started, waiting for signal.*", 1));
}

connectorContainer.start();
connectorContainer.followOutput(new Slf4jLogConsumer(logger));
@After
public void after() {
_connectorContainer.close();

waitUntilContainerConnectedToNetwork(targetContainer, 5000);
}
finally {
targetContainer.close();
connectorContainer.close();
if(_targetContainer != null) {
_targetContainer.close();
}
}

@Test
public void connectContainerCreatedAfterStartOfConnector() throws InterruptedException, TimeoutException {
GenericContainer targetContainer = new GenericContainer<>("nginx")
public void connectContainersCreatedBeforeStartOfConnector() throws Exception {
_targetContainer = new GenericContainer<>("nginx")
.withLabel("io.homecentr.local-networks", "[{ \"Network\": \""+ getNetworkName() +"\" }]");

GenericContainer connectorContainer = createConnectorContainer();
_targetContainer.start();

try {
connectorContainer.start();
targetContainer.start();
_connectorContainer.start();
_connectorContainer.followOutput(new Slf4jLogConsumer(logger));

connectorContainer.followOutput(new Slf4jLogConsumer(logger));
waitFor(Duration.ofSeconds(5), () -> isConnectedToNetwork(_targetContainer));
}

waitUntilContainerConnectedToNetwork(targetContainer, 5000);
}
finally {
targetContainer.close();
connectorContainer.close();
}
@Test
public void connectContainerCreatedAfterStartOfConnector() throws Exception {
_targetContainer = new GenericContainer<>("nginx")
.withLabel("io.homecentr.local-networks", "[{ \"Network\": \""+ getNetworkName() +"\" }]");

_connectorContainer.start();
_connectorContainer.followOutput(new Slf4jLogConsumer(logger));

_targetContainer.start();

waitFor(Duration.ofSeconds(5), () -> isConnectedToNetwork(_targetContainer));
}

@Test
public void connectContainerWithExplicitConfig() throws Exception {
GenericContainer targetContainer = new GenericContainer<>("nginx")
_targetContainer = new GenericContainer<>("nginx")
.withLabel("io.homecentr.local-networks", "[{ \"Network\": \""+ getNetworkName() +"\", \"Config\": { \"IPAMConfig\": { \"IPV4Address\": \"192.168.99.100\" } } }]");

GenericContainer connectorContainer = createConnectorContainer();

try {
connectorContainer.start();
targetContainer.start();
_connectorContainer.start();
_connectorContainer.followOutput(new Slf4jLogConsumer(logger));

connectorContainer.followOutput(new Slf4jLogConsumer(logger));
_targetContainer.start();

waitUntilContainerConnectedToNetwork(targetContainer, 5000);
waitFor(Duration.ofSeconds(5), () -> isConnectedToNetwork(_targetContainer));

assertEquals("192.168.99.100", getContainerIpAddress(targetContainer));
}
finally {
targetContainer.close();
connectorContainer.close();
}
assertEquals("192.168.99.100", getContainerIpAddress(_targetContainer));
}

@Test
public void ignoreContainerWithInvalidLabel() throws Exception {
GenericContainer targetContainer = new GenericContainer<>("nginx")
_targetContainer = new GenericContainer<>("nginx")
.withLabel("io.homecentr.local-networks", "[{ \"Network\": \""+ getNetworkName() +"\" }]");

GenericContainer connectorContainer = createConnectorContainer();

try {
connectorContainer.start();
targetContainer.start();
_connectorContainer.start();
_connectorContainer.followOutput(new Slf4jLogConsumer(logger));

connectorContainer.followOutput(new Slf4jLogConsumer(logger));
_targetContainer.start();

try {
waitUntilContainerConnectedToNetwork(targetContainer, 5000);
}
catch (TimeoutException ex) { }
}
finally {
targetContainer.close();
connectorContainer.close();
try {
// Wait to make sure the container is not connected to the network
waitFor(Duration.ofSeconds(5), () -> isConnectedToNetwork(_targetContainer));
}
}

private GenericContainer createConnectorContainer() {
return new GenericContainer<>(getConnectorImageTag())
.withFileSystemBind("//var/run/docker.sock", "/var/run/docker.sock")
.withImagePullPolicy(new NeverPullImagePolicy())
.withEnv("LOG_LEVEL", "debug")
.waitingFor(Wait.forLogMessage(".*Started, waiting for signal.*", 1));
catch (TimeoutException ex) { }
}
}
22 changes: 1 addition & 21 deletions tests/src/test/java/ContainerTestBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
import org.testcontainers.DockerClientFactory;
import org.testcontainers.containers.GenericContainer;

import java.util.concurrent.TimeoutException;

public abstract class ContainerTestBase {
private static final String networkName = "target-network";

Expand All @@ -27,24 +25,6 @@ public static void cleanUp() {
deleteNetwork(_networkId);
}

protected String getConnectorImageTag() {
return System.getProperty("image_tag");
}

protected void waitUntilContainerConnectedToNetwork(GenericContainer container, Integer timeout) throws InterruptedException, TimeoutException {
long startTime = System.currentTimeMillis();

while (!isConnectedToNetwork(container)) {
long remaining = System.currentTimeMillis() - startTime - timeout;

if (remaining < 0) {
throw new TimeoutException("The container did not get connected to the network within the timeout.");
}

Thread.sleep(500);
}
}

protected String getContainerIpAddress(GenericContainer container) throws Exception {
InspectContainerResponse response = _client.inspectContainerCmd(container.getContainerId()).exec();

Expand All @@ -57,7 +37,7 @@ protected String getContainerIpAddress(GenericContainer container) throws Except
throw new Exception("The container is not connected to the network " + _networkId);
}

private boolean isConnectedToNetwork(GenericContainer container) {
protected boolean isConnectedToNetwork(GenericContainer container) {
InspectContainerResponse response = _client.inspectContainerCmd(container.getContainerId()).exec();

for (ContainerNetwork network : response.getNetworkSettings().getNetworks().values()) {
Expand Down
11 changes: 0 additions & 11 deletions tests/src/test/java/PullPolicies/NeverPullImagePolicy.java

This file was deleted.

10 changes: 10 additions & 0 deletions tests/src/test/java/helpers/DockerImageTagResolver.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package helpers;

import io.homecentr.testcontainers.images.EnvironmentImageTagResolver;
import io.homecentr.testcontainers.images.SystemWrapperImpl;

public class DockerImageTagResolver extends EnvironmentImageTagResolver {
public DockerImageTagResolver() {
super(new SystemWrapperImpl(), "homecentr/swarm-local-network-connector:local");
}
}

0 comments on commit 59021b2

Please sign in to comment.