forked from testcontainers/testcontainers-node
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgeneric-container-commit.test.ts
More file actions
91 lines (74 loc) · 3.4 KB
/
generic-container-commit.test.ts
File metadata and controls
91 lines (74 loc) · 3.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import { expect } from "vitest";
import { RandomUuid } from "../common";
import { getContainerRuntimeClient } from "../container-runtime";
import { getReaper } from "../reaper/reaper";
import { LABEL_TESTCONTAINERS_SESSION_ID } from "../utils/labels";
import { deleteImageByName, getImageInfo } from "../utils/test-helper";
import { GenericContainer } from "./generic-container";
describe("GenericContainer commit", { timeout: 180_000 }, () => {
const imageName = "cristianrgreco/testcontainer";
const imageVersion = "1.1.14";
it("should commit container changes to a new image", async () => {
const testContent = "test content";
const newImageTag = `test-commit-${new RandomUuid().nextUuid()}`;
const testAuthor = "test-author";
const testComment = "test-comment";
// Start original container and make a change
const container = await new GenericContainer(`${imageName}:${imageVersion}`).withExposedPorts(8080).start();
// Make a change to the container
await container.exec(["sh", "-c", `echo '${testContent}' > /test-file.txt`]);
// Commit the changes to a new image
const imageId = await container.commit({
repo: imageName,
tag: newImageTag,
author: testAuthor,
comment: testComment,
});
// Verify image metadata is set
const imageInfo = await getImageInfo(imageId);
expect(imageInfo.Author).toBe(testAuthor);
expect(imageInfo.Comment).toBe(testComment);
// Start a new container from the committed image
const newContainer = await new GenericContainer(imageId).withExposedPorts(8080).start();
// Verify the changes exist in the new container
const result = await newContainer.exec(["cat", "/test-file.txt"]);
expect(result.output.trim()).toBe(testContent);
// Cleanup
await container.stop();
await newContainer.stop();
});
it("should add session ID label when deleteOnExit is true", async () => {
const newImageTag = `test-commit-${new RandomUuid().nextUuid()}`;
const container = await new GenericContainer(`${imageName}:${imageVersion}`).withExposedPorts(8080).start();
// Commit with deleteOnExit true (default)
const imageId = await container.commit({
repo: imageName,
tag: newImageTag,
});
// Verify session ID label is present
const imageInfo = await getImageInfo(imageId);
const client = await getContainerRuntimeClient();
const reaper = await getReaper(client);
expect(imageInfo.Config.Labels[LABEL_TESTCONTAINERS_SESSION_ID]).toBe(reaper.sessionId);
await container.stop();
});
it("should not add session ID label when deleteOnExit is false", async () => {
const newImageTag = `test-commit-${new RandomUuid().nextUuid()}`;
const container = await new GenericContainer(`${imageName}:${imageVersion}`).withExposedPorts(8080).start();
// Commit with deleteOnExit false
const imageId = await container.commit({
repo: imageName,
tag: newImageTag,
changes: ["LABEL test=test", "ENV test=test"],
deleteOnExit: false,
});
const imageInfo = await getImageInfo(imageId);
// Verify session ID label is not present
expect(imageInfo.Config.Labels[LABEL_TESTCONTAINERS_SESSION_ID]).toBeFalsy();
// Verify other changes are present
expect(imageInfo.Config.Labels.test).toBe("test");
expect(imageInfo.Config.Env).toContain("test=test");
await container.stop();
await deleteImageByName(imageId);
});
});