Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/features/containers.md
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,8 @@ container.copyContentToContainer([{
container.copyArchiveToContainer(nodeReadable, "/some/nested/remotedir");
```

When copying files, symbolic links in `source` are followed and the linked file content is copied into the container.

An optional `mode` can be specified in octal for setting file permissions:

```js
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { PullPolicy } from "../utils/pull-policy";
import {
checkContainerIsHealthy,
checkContainerIsHealthyUdp,
createTempSymlinkedFile,
getDockerEventStream,
getRunningContainerNames,
waitForDockerEvent,
Expand Down Expand Up @@ -364,6 +365,23 @@ describe("GenericContainer", { timeout: 180_000 }, () => {
expect((await container.exec(["cat", target])).output).toEqual(expect.stringContaining("hello world"));
});

it("should follow symlink when copying file to container", async () => {
if (process.platform === "win32") {
return;
}

const content = `hello world ${new RandomUuid().nextUuid()}`;
const target = "/tmp/test.txt";
await using symlinkedFile = await createTempSymlinkedFile(content);
await using container = await new GenericContainer("cristianrgreco/testcontainer:1.1.14")
.withCopyFilesToContainer([{ source: symlinkedFile.symlink, target }])
.withExposedPorts(8080)
.start();

expect((await container.exec(["cat", target])).output).toEqual(expect.stringContaining(content));
expect((await container.exec(["sh", "-c", `[ -L ${target} ]`])).exitCode).toBe(1);
});

it("should copy file to container with permissions", async () => {
const source = path.resolve(fixtures, "docker", "test.txt");
const target = "/tmp/test.txt";
Expand All @@ -389,6 +407,24 @@ describe("GenericContainer", { timeout: 180_000 }, () => {
expect((await container.exec(["cat", target])).output).toEqual(expect.stringContaining("hello world"));
});

it("should follow symlink when copying file to started container", async () => {
if (process.platform === "win32") {
return;
}

const content = `hello world ${new RandomUuid().nextUuid()}`;
const target = "/tmp/test.txt";
await using symlinkedFile = await createTempSymlinkedFile(content);
await using container = await new GenericContainer("cristianrgreco/testcontainer:1.1.14")
.withExposedPorts(8080)
.start();

await container.copyFilesToContainer([{ source: symlinkedFile.symlink, target }]);

expect((await container.exec(["cat", target])).output).toEqual(expect.stringContaining(content));
expect((await container.exec(["sh", "-c", `[ -L ${target} ]`])).exitCode).toBe(1);
});

it("should copy directory to container", async () => {
const source = path.resolve(fixtures, "docker");
const target = "/tmp";
Expand Down
15 changes: 11 additions & 4 deletions packages/testcontainers/src/generic-container/generic-container.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import archiver from "archiver";
import AsyncLock from "async-lock";
import { Container, ContainerCreateOptions, HostConfig } from "dockerode";
import { promises as fs } from "fs";
import { Readable } from "stream";
import { containerLog, hash, log, toNanos } from "../common";
import { ContainerRuntimeClient, getContainerRuntimeClient, ImageName } from "../container-runtime";
Expand Down Expand Up @@ -179,7 +180,7 @@ export class GenericContainer implements TestContainer {
}

if (this.filesToCopy.length > 0 || this.directoriesToCopy.length > 0 || this.contentsToCopy.length > 0) {
const archive = this.createArchiveToCopyToContainer();
const archive = await this.createArchiveToCopyToContainer();
archive.finalize();
await client.container.putArchive(container, archive, "/");
}
Expand Down Expand Up @@ -255,11 +256,17 @@ export class GenericContainer implements TestContainer {
}
}

private createArchiveToCopyToContainer(): archiver.Archiver {
private async createArchiveToCopyToContainer(): Promise<archiver.Archiver> {
const tar = archiver("tar");
const filesToCopyWithStats = await Promise.all(
this.filesToCopy.map(async (fileToCopy) => ({
...fileToCopy,
stats: await fs.stat(fileToCopy.source),
}))
);

for (const { source, target, mode } of this.filesToCopy) {
tar.file(source, { name: target, mode });
for (const { source, target, mode, stats } of filesToCopyWithStats) {
tar.file(source, { name: target, mode, stats });
}
for (const { source, target, mode } of this.directoriesToCopy) {
tar.directory(source, target, { mode });
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import archiver from "archiver";
import AsyncLock from "async-lock";
import Dockerode, { ContainerInspectInfo } from "dockerode";
import { promises as fs } from "fs";
import { Readable } from "stream";
import { containerLog, log } from "../common";
import { ContainerRuntimeClient, getContainerRuntimeClient } from "../container-runtime";
Expand Down Expand Up @@ -183,7 +184,13 @@ export class StartedGenericContainer implements StartedTestContainer {
log.debug(`Copying files to container...`, { containerId: this.container.id });
const client = await getContainerRuntimeClient();
const tar = archiver("tar");
filesToCopy.forEach(({ source, target }) => tar.file(source, { name: target }));
const filesToCopyWithStats = await Promise.all(
filesToCopy.map(async (fileToCopy) => ({
...fileToCopy,
stats: await fs.stat(fileToCopy.source),
}))
);
filesToCopyWithStats.forEach(({ source, target, mode, stats }) => tar.file(source, { name: target, mode, stats }));
tar.finalize();
await client.container.putArchive(this.container, tar, "/");
log.debug(`Copied files to container`, { containerId: this.container.id });
Expand Down
20 changes: 19 additions & 1 deletion packages/testcontainers/src/utils/test-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { GetEventsOptions, ImageInspectInfo } from "dockerode";
import { createServer, Server } from "http";
import { createSocket } from "node:dgram";
import fs from "node:fs";
import { EOL } from "node:os";
import { EOL, tmpdir } from "node:os";
import path from "node:path";
import { Readable } from "stream";
import { Agent, request } from "undici";
Expand Down Expand Up @@ -202,3 +202,21 @@ export async function createTestServer(port: number): Promise<Server> {
await new Promise<void>((resolve) => server.listen(port, resolve));
return server;
}

export const createTempSymlinkedFile = async (
content: string
): Promise<{ source: string; symlink: string } & AsyncDisposable> => {
const directory = await fs.promises.mkdtemp(path.join(tmpdir(), "testcontainers-"));
const source = path.join(directory, "source.txt");
const symlink = path.join(directory, "symlink.txt");
await fs.promises.writeFile(source, content);
await fs.promises.symlink(source, symlink);

return {
source,
symlink,
[Symbol.asyncDispose]: async () => {
await fs.promises.rm(directory, { recursive: true, force: true });
},
};
};
Loading