-
-
Notifications
You must be signed in to change notification settings - Fork 251
Add support for container commit #923
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
51ee497
feat: add support for container.commit() [884]
smcroskey 74585dc
lint fixes
smcroskey f8c67aa
update method signature to return image ID
smcroskey 097578a
add labels (use labels from source image)
smcroskey 55b19aa
expose deleteOnExit & changes options
smcroskey 8f112a8
address PR feedback
smcroskey 5a4b78e
export CommitOptions
smcroskey File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 2 additions & 1 deletion
3
packages/testcontainers/src/container-runtime/clients/image/image-client.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,9 @@ | ||
| import { ImageBuildOptions } from "dockerode"; | ||
| import { ImageBuildOptions, ImageInspectInfo } from "dockerode"; | ||
| import { ImageName } from "../../image-name"; | ||
|
|
||
| export interface ImageClient { | ||
| build(context: string, opts: ImageBuildOptions): Promise<void>; | ||
| pull(imageName: ImageName, opts?: { force: boolean; platform: string | undefined }): Promise<void>; | ||
| inspect(imageName: ImageName): Promise<ImageInspectInfo>; | ||
| exists(imageName: ImageName): Promise<boolean>; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 91 additions & 0 deletions
91
packages/testcontainers/src/generic-container/generic-container-commit.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,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); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.