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
5 changes: 5 additions & 0 deletions docs/features/containers.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,10 @@ const container = await new GenericContainer("alpine")
content: "hello world",
target: "/remote/file2.txt"
}])
.withCopyArchivesToContainer([{
tar: nodeReadable,
target: "/some/nested/remotedir"
}])
.start();
```

Expand All @@ -153,6 +157,7 @@ container.copyContentToContainer([{
content: "hello world",
target: "/remote/file2.txt"
}])
container.copyArchiveToContainer(nodeReadable, "/some/nested/remotedir");
```

An optional `mode` can be specified in octal for setting file permissions:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ export class AbstractStartedContainer implements StartedTestContainer {
return this.startedTestContainer.copyContentToContainer(contentsToCopy);
}

public copyArchiveToContainer(tar: Readable, target = "/"): Promise<void> {
return this.startedTestContainer.copyArchiveToContainer(tar, target);
}

public copyArchiveFromContainer(path: string): Promise<NodeJS.ReadableStream> {
return this.startedTestContainer.copyArchiveFromContainer(path);
}
Expand Down
11 changes: 11 additions & 0 deletions packages/testcontainers/src/generic-container/generic-container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { PortForwarderInstance, SSHD_IMAGE } from "../port-forwarder/port-forwar
import { getReaper, REAPER_IMAGE } from "../reaper/reaper";
import { StartedTestContainer, TestContainer } from "../test-container";
import {
ArchiveToCopy,
BindMount,
ContentToCopy,
DirectoryToCopy,
Expand Down Expand Up @@ -57,6 +58,7 @@ export class GenericContainer implements TestContainer {
protected filesToCopy: FileToCopy[] = [];
protected directoriesToCopy: DirectoryToCopy[] = [];
protected contentsToCopy: ContentToCopy[] = [];
protected archivesToCopy: ArchiveToCopy[] = [];
protected healthCheck?: HealthCheck;

constructor(image: string) {
Expand Down Expand Up @@ -180,6 +182,10 @@ export class GenericContainer implements TestContainer {
await client.container.putArchive(container, archive, "/");
}

for (const archive of this.archivesToCopy) {
await client.container.putArchive(container, archive.tar, archive.target);
}

log.info(`Starting container for image "${this.createOpts.Image}"...`, { containerId: container.id });
if (this.containerCreated) {
await this.containerCreated(container.id);
Expand Down Expand Up @@ -458,6 +464,11 @@ export class GenericContainer implements TestContainer {
return this;
}

public withCopyArchivesToContainer(archivesToCopy: ArchiveToCopy[]): this {
this.archivesToCopy = [...this.archivesToCopy, ...archivesToCopy];
return this;
}

public withWorkingDir(workingDir: string): this {
this.createOpts.WorkingDir = workingDir;
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,13 @@ export class StartedGenericContainer implements StartedTestContainer {
log.debug(`Copied content to container`, { containerId: this.container.id });
}

public async copyArchiveToContainer(tar: Readable, target = "/"): Promise<void> {
log.debug(`Copying archive to container...`, { containerId: this.container.id });
const client = await getContainerRuntimeClient();
await client.container.putArchive(this.container, tar, target);
log.debug(`Copied archive to container`, { containerId: this.container.id });
}

public async copyArchiveFromContainer(path: string): Promise<NodeJS.ReadableStream> {
log.debug(`Copying archive "${path}" from container...`, { containerId: this.container.id });
const client = await getContainerRuntimeClient();
Expand Down
3 changes: 3 additions & 0 deletions packages/testcontainers/src/test-container.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Readable } from "stream";
import { StartedNetwork } from "./network/network";
import {
ArchiveToCopy,
BindMount,
CommitOptions,
ContentToCopy,
Expand Down Expand Up @@ -44,6 +45,7 @@ export interface TestContainer {
withCopyFilesToContainer(filesToCopy: FileToCopy[]): this;
withCopyDirectoriesToContainer(directoriesToCopy: DirectoryToCopy[]): this;
withCopyContentToContainer(contentsToCopy: ContentToCopy[]): this;
withCopyArchivesToContainer(archivesToCopy: ArchiveToCopy[]): this;

withWorkingDir(workingDir: string): this;
withResourcesQuota(resourcesQuota: ResourcesQuota): this;
Expand Down Expand Up @@ -77,6 +79,7 @@ export interface StartedTestContainer {
getNetworkId(networkName: string): string;
getIpAddress(networkName: string): string;
copyArchiveFromContainer(path: string): Promise<NodeJS.ReadableStream>;
copyArchiveToContainer(tar: Readable, target?: string): Promise<void>;
copyDirectoriesToContainer(directoriesToCopy: DirectoryToCopy[]): Promise<void>;
copyFilesToContainer(filesToCopy: FileToCopy[]): Promise<void>;
copyContentToContainer(contentsToCopy: ContentToCopy[]): Promise<void>;
Expand Down
5 changes: 5 additions & 0 deletions packages/testcontainers/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ export type ContentToCopy = {
mode?: number;
};

export type ArchiveToCopy = {
tar: Readable;
target: string;
};

export type TmpFs = { [dir in string]: string };

export type Ulimits = { [name: string]: { hard: number | undefined; soft: number | undefined } };
Expand Down