-
-
Notifications
You must be signed in to change notification settings - Fork 206
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e5e772b
commit ab46673
Showing
10 changed files
with
182 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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,11 @@ | ||
import type { Config } from "jest"; | ||
import * as path from "path"; | ||
|
||
const config: Config = { | ||
preset: "ts-jest", | ||
moduleNameMapper: { | ||
"^testcontainers$": path.resolve(__dirname, "../../testcontainers/src"), | ||
}, | ||
}; | ||
|
||
export default config; |
This file contains 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,34 @@ | ||
{ | ||
"name": "@testcontainers/ollama", | ||
"version": "10.7.2", | ||
"license": "MIT", | ||
"keywords": [ | ||
"ollama", | ||
"testing", | ||
"docker", | ||
"testcontainers" | ||
], | ||
"description": "Ollama module for Testcontainers", | ||
"homepage": "https://github.com/testcontainers/testcontainers-node#readme", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/testcontainers/testcontainers-node" | ||
}, | ||
"bugs": { | ||
"url": "https://github.com/testcontainers/testcontainers-node/issues" | ||
}, | ||
"main": "build/index.js", | ||
"files": [ | ||
"build" | ||
], | ||
"publishConfig": { | ||
"access": "public" | ||
}, | ||
"scripts": { | ||
"prepack": "shx cp ../../../README.md . && shx cp ../../../LICENSE .", | ||
"build": "tsc --project tsconfig.build.json" | ||
}, | ||
"dependencies": { | ||
"testcontainers": "^10.7.2" | ||
} | ||
} |
This file contains 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 @@ | ||
export { OllamaContainer, StartedOllamaContainer } from "./ollama-container"; |
This file contains 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,35 @@ | ||
import { OllamaContainer } from "./ollama-container"; | ||
|
||
describe("OllamaContainer", () => { | ||
jest.setTimeout(180_000); | ||
|
||
it("should run ollama with default config", async () => { | ||
const container = await new OllamaContainer("ollama/ollama:0.1.44").start(); | ||
const response = await fetch(`${container.getEndpoint()}/api/version`); | ||
expect(response.status).toEqual(200); | ||
const body = await response.json(); | ||
expect(body.version).toEqual("0.1.44"); | ||
await container.stop(); | ||
}); | ||
|
||
it("download model and commit to image", async () => { | ||
const container = await new OllamaContainer("ollama/ollama:0.1.44").start(); | ||
const execResult = await container.exec(["ollama", "pull", "all-minilm"]); | ||
console.log(execResult.output); | ||
const response = await fetch(`${container.getEndpoint()}/api/tags`); | ||
expect(response.status).toEqual(200); | ||
const body = await response.json(); | ||
expect(body.models[0].name).toContain("all-minilm"); | ||
|
||
const newImageName: string = "tc-ollama-allminilm-" + (Math.random() + 1).toString(36).substring(4).toLowerCase(); | ||
await container.commitToImage(newImageName); | ||
|
||
const newContainer = await new OllamaContainer(newImageName).start(); | ||
const response2 = await fetch(`${newContainer.getEndpoint()}/api/tags`); | ||
expect(response2.status).toEqual(200); | ||
const body2 = await response2.json(); | ||
expect(body2.models[0].name).toContain("all-minilm"); | ||
await container.stop(); | ||
await newContainer.stop(); | ||
}); | ||
}); |
This file contains 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,54 @@ | ||
import { | ||
AbstractStartedContainer, | ||
GenericContainer, | ||
getContainerRuntimeClient, | ||
StartedTestContainer, | ||
Wait, | ||
} from "testcontainers"; | ||
|
||
export const OLLAMA_PORT = 11434; | ||
|
||
export class OllamaContainer extends GenericContainer { | ||
constructor(image = "ollama/ollama") { | ||
super(image); | ||
this.withExposedPorts(OLLAMA_PORT) | ||
.withWaitStrategy(Wait.forLogMessage("Listening on ")) | ||
.withStartupTimeout(120_000); | ||
} | ||
|
||
public override async start(): Promise<StartedOllamaContainer> { | ||
const containerRuntimeClient = await getContainerRuntimeClient(); | ||
const runtimes = containerRuntimeClient.info.containerRuntime.runtimes; | ||
if (runtimes.includes("nvidia")) { | ||
this.hostConfig.DeviceRequests = [ | ||
{ | ||
Driver: "nvidia", | ||
Count: -1, | ||
Capabilities: [["gpu"]], | ||
}, | ||
]; | ||
} | ||
|
||
return new StartedOllamaContainer(await super.start()); | ||
} | ||
} | ||
|
||
export class StartedOllamaContainer extends AbstractStartedContainer { | ||
constructor(startedTestContainer: StartedTestContainer) { | ||
super(startedTestContainer); | ||
} | ||
|
||
public getPort(): number { | ||
return this.startedTestContainer.getMappedPort(OLLAMA_PORT); | ||
} | ||
|
||
public getEndpoint(): string { | ||
return `http://${this.getHost()}:${this.getPort().toString()}`; | ||
} | ||
|
||
public async commitToImage(imageName: string): Promise<NodeJS.ReadableStream> { | ||
const client = await getContainerRuntimeClient(); | ||
const dockerode = client.container.dockerode; | ||
return dockerode.getContainer(this.getId()).commit({ repo: imageName }); | ||
} | ||
} |
This file contains 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,13 @@ | ||
{ | ||
"extends": "./tsconfig.json", | ||
"exclude": [ | ||
"build", | ||
"jest.config.ts", | ||
"src/**/*.test.ts" | ||
], | ||
"references": [ | ||
{ | ||
"path": "../../testcontainers" | ||
} | ||
] | ||
} |
This file contains 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,21 @@ | ||
{ | ||
"extends": "../../../tsconfig.base.json", | ||
"compilerOptions": { | ||
"rootDir": "src", | ||
"outDir": "build", | ||
"paths": { | ||
"testcontainers": [ | ||
"../../testcontainers/src" | ||
] | ||
} | ||
}, | ||
"exclude": [ | ||
"build", | ||
"jest.config.ts" | ||
], | ||
"references": [ | ||
{ | ||
"path": "../../testcontainers" | ||
} | ||
] | ||
} |
This file contains 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 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