Skip to content

Commit 3697410

Browse files
mdelapenyaclaude
andauthored
chore(ollama): use Run function (#3420)
🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-authored-by: Claude <[email protected]>
1 parent a1e747c commit 3697410

File tree

2 files changed

+37
-21
lines changed

2 files changed

+37
-21
lines changed

modules/ollama/local.go

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,21 @@ type localProcess struct {
9090
binary string
9191
}
9292

93-
// runLocal returns an OllamaContainer that uses the local Ollama binary instead of using a Docker container.
94-
func (c *localProcess) run(ctx context.Context, req testcontainers.GenericContainerRequest) (*OllamaContainer, error) {
93+
// run returns an OllamaContainer that uses the local Ollama binary instead of using a Docker container.
94+
func (c *localProcess) run(ctx context.Context, img string, opts []testcontainers.ContainerCustomizer) (*OllamaContainer, error) {
95+
req := testcontainers.GenericContainerRequest{
96+
ContainerRequest: testcontainers.ContainerRequest{
97+
Image: img,
98+
},
99+
Started: true,
100+
}
101+
102+
for _, opt := range opts {
103+
if err := opt.Customize(&req); err != nil {
104+
return nil, fmt.Errorf("customize: %w", err)
105+
}
106+
}
107+
95108
if err := c.validateRequest(req); err != nil {
96109
return nil, fmt.Errorf("validate request: %w", err)
97110
}
@@ -714,7 +727,11 @@ func (c *localProcess) Customize(req *testcontainers.GenericContainerRequest) er
714727
if req.WaitingFor == nil {
715728
req.WaitingFor = logStrategy
716729
} else {
717-
req.WaitingFor = wait.ForAll(req.WaitingFor, logStrategy)
730+
if w, ok := req.WaitingFor.(*wait.MultiStrategy); ok && len(w.Strategies) == 0 {
731+
w.Strategies = append(w.Strategies, logStrategy)
732+
} else {
733+
req.WaitingFor = logStrategy
734+
}
718735
}
719736

720737
// Setup the environment variables using a random port by default

modules/ollama/ollama.go

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -74,41 +74,40 @@ func RunContainer(ctx context.Context, opts ...testcontainers.ContainerCustomize
7474

7575
// Run creates an instance of the Ollama container type
7676
func Run(ctx context.Context, img string, opts ...testcontainers.ContainerCustomizer) (*OllamaContainer, error) {
77-
req := testcontainers.GenericContainerRequest{
78-
ContainerRequest: testcontainers.ContainerRequest{
79-
Image: img,
80-
ExposedPorts: []string{"11434/tcp"},
81-
WaitingFor: wait.ForListeningPort("11434/tcp").WithStartupTimeout(60 * time.Second),
82-
},
83-
Started: true,
77+
moduleOpts := []testcontainers.ContainerCustomizer{
78+
testcontainers.WithExposedPorts("11434/tcp"),
79+
testcontainers.WithWaitStrategy(wait.ForListeningPort("11434/tcp").WithStartupTimeout(60 * time.Second)),
8480
}
8581

86-
// Always request a GPU if the host supports it.
87-
opts = append(opts, withGpu())
88-
82+
// Check if we need to use the local process
8983
var local *localProcess
9084
for _, opt := range opts {
91-
if err := opt.Customize(&req); err != nil {
92-
return nil, fmt.Errorf("customize: %w", err)
93-
}
9485
if l, ok := opt.(*localProcess); ok {
9586
local = l
87+
break
9688
}
9789
}
9890

91+
// Only request a GPU if NOT using local process and the host supports it.
92+
if local == nil {
93+
opts = append(opts, withGpu())
94+
}
95+
96+
moduleOpts = append(moduleOpts, opts...)
97+
9998
// Now we have processed all the options, we can check if we need to use the local process.
10099
if local != nil {
101-
return local.run(ctx, req)
100+
return local.run(ctx, img, moduleOpts)
102101
}
103102

104-
container, err := testcontainers.GenericContainer(ctx, req)
103+
ctr, err := testcontainers.Run(ctx, img, moduleOpts...)
105104
var c *OllamaContainer
106-
if container != nil {
107-
c = &OllamaContainer{Container: container}
105+
if ctr != nil {
106+
c = &OllamaContainer{Container: ctr}
108107
}
109108

110109
if err != nil {
111-
return c, fmt.Errorf("generic container: %w", err)
110+
return c, fmt.Errorf("run ollama: %w", err)
112111
}
113112

114113
return c, nil

0 commit comments

Comments
 (0)