Skip to content

Commit

Permalink
Add logs around resolving a container runtime (#787)
Browse files Browse the repository at this point in the history
  • Loading branch information
cristianrgreco authored Jun 28, 2024
1 parent e5b4848 commit 0f26b4a
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 15 deletions.
8 changes: 2 additions & 6 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,10 @@ jobs:
strategy:
matrix:
node-version: [ 18.x, 20.x ]
container-runtime: [ docker, docker-rootless, podman ]
container-runtime: [ docker, podman ]
include:
- container-runtime: docker
runner: ubuntu-22.04
- container-runtime: docker-rootless
runner: ubuntu-22.04
- container-runtime: podman
runner: ubuntu-22.04
uses: ./.github/workflows/test-template.yml
Expand All @@ -81,12 +79,10 @@ jobs:
strategy:
matrix:
node-version: [ 18.x, 20.x ]
container-runtime: [ docker, docker-rootless, podman ]
container-runtime: [ docker, podman ]
include:
- container-runtime: docker
runner: ubuntu-22.04
- container-runtime: docker-rootless
runner: ubuntu-22.04
- container-runtime: podman
runner: ubuntu-22.04
module: ${{ fromJson(needs.list-modules.outputs.modules) }}
Expand Down
32 changes: 23 additions & 9 deletions packages/testcontainers/src/container-runtime/clients/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,15 @@ export async function getContainerRuntimeClient(): Promise<ContainerRuntimeClien

for (const strategy of strategies) {
try {
log.debug(`Testing container runtime strategy "${strategy.getName()}"...`);
log.debug(`Checking container runtime strategy "${strategy.getName()}"...`);
const client = await initStrategy(strategy);
if (client) {
log.debug(`Container runtime strategy "${strategy.getName()}" works`);
containerRuntimeClient = client;
return client;
}
} catch {
log.debug(`Container runtime strategy "${strategy.getName()}" does not work`);
} catch (err) {
log.debug(`Container runtime strategy "${strategy.getName()}" does not work: ${err}`);
}
}
throw new Error("Could not find a working container runtime strategy");
Expand All @@ -65,6 +65,7 @@ async function initStrategy(strategy: ContainerRuntimeClientStrategy): Promise<C
const result = await strategy.getResult();

if (!result) {
log.debug(`Container runtime strategy "${strategy.getName()}" is not applicable`);
return undefined;
}

Expand All @@ -74,30 +75,42 @@ async function initStrategy(strategy: ContainerRuntimeClientStrategy): Promise<C
};
const dockerode = new Dockerode(dockerodeOptions);

log.trace("Fetching Docker info...");
const dockerodeInfo = await dockerode.info();

const indexServerAddress =
!isDefined(dockerodeInfo.IndexServerAddress) || isEmptyString(dockerodeInfo.IndexServerAddress)
? "https://index.docker.io/v1/"
: dockerodeInfo.IndexServerAddress;

log.trace("Fetching remote container runtime socket path...");
const remoteContainerRuntimeSocketPath = getRemoteContainerRuntimeSocketPath(result, dockerodeInfo.OperatingSystem);

log.trace("Resolving host...");
const host = await resolveHost(dockerode, result, indexServerAddress);

log.trace("Fetching Compose info...");
const composeClient = await getComposeClient(result.composeEnvironment);
const containerClient = result.uri.includes("podman.sock")
? new PodmanContainerClient(dockerode)
: new DockerContainerClient(dockerode);
const imageClient = new DockerImageClient(dockerode, indexServerAddress);
const networkClient = new DockerNetworkClient(dockerode);

const nodeInfo: NodeInfo = {
version: process.version,
architecture: process.arch,
platform: process.platform,
};

log.trace("Looking up host IPs...");
const hostIps = await lookupHostIps(host);

log.trace("Initialising clients...");
const containerClient = result.uri.includes("podman.sock")
? new PodmanContainerClient(dockerode)
: new DockerContainerClient(dockerode);
const imageClient = new DockerImageClient(dockerode, indexServerAddress);
const networkClient = new DockerNetworkClient(dockerode);

const containerRuntimeInfo: ContainerRuntimeInfo = {
host,
hostIps: await lookupHostIps(host),
hostIps,
remoteSocketPath: remoteContainerRuntimeSocketPath,
indexServerAddress: indexServerAddress,
serverVersion: dockerodeInfo.ServerVersion,
Expand All @@ -113,5 +126,6 @@ async function initStrategy(strategy: ContainerRuntimeClientStrategy): Promise<C

const info: Info = { node: nodeInfo, containerRuntime: containerRuntimeInfo, compose: composeInfo };

log.trace(`Container runtime info:\n${JSON.stringify(info, null, 2)}`);
return new ContainerRuntimeClient(info, composeClient, containerClient, imageClient, networkClient);
}

0 comments on commit 0f26b4a

Please sign in to comment.