Skip to content

Commit 1a2ec6b

Browse files
committed
chore: go back to string in env vars
1 parent 953518e commit 1a2ec6b

File tree

4 files changed

+44
-16
lines changed

4 files changed

+44
-16
lines changed

modules/ollama/examples_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ func ExampleRun_withLocal() {
178178
ctx := context.Background()
179179

180180
// localOllama {
181-
ollamaContainer, err := tcollama.Run(ctx, "ollama/ollama:0.3.13", tcollama.WithUseLocal(map[string]string{"OLLAMA_DEBUG": "true"}))
181+
ollamaContainer, err := tcollama.Run(ctx, "ollama/ollama:0.3.13", tcollama.WithUseLocal("OLLAMA_DEBUG=true"))
182182
defer func() {
183183
if err := testcontainers.TerminateContainer(ollamaContainer); err != nil {
184184
log.Printf("failed to terminate container: %s", err)

modules/ollama/local_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ func TestRun_local(t *testing.T) {
2929
ollamaContainer, err := ollama.Run(
3030
ctx,
3131
"ollama/ollama:0.1.25",
32-
ollama.WithUseLocal(map[string]string{"FOO": "BAR"}),
32+
ollama.WithUseLocal("FOO=BAR"),
3333
)
3434
testcontainers.CleanupContainer(t, ollamaContainer)
3535
require.NoError(t, err)
@@ -266,7 +266,7 @@ func TestRun_localWithCustomLogFile(t *testing.T) {
266266

267267
ctx := context.Background()
268268

269-
ollamaContainer, err := ollama.Run(ctx, "ollama/ollama:0.1.25", ollama.WithUseLocal(map[string]string{"FOO": "BAR"}))
269+
ollamaContainer, err := ollama.Run(ctx, "ollama/ollama:0.1.25", ollama.WithUseLocal("FOO=BAR"))
270270
require.NoError(t, err)
271271
testcontainers.CleanupContainer(t, ollamaContainer)
272272

@@ -285,7 +285,7 @@ func TestRun_localWithCustomHost(t *testing.T) {
285285

286286
ctx := context.Background()
287287

288-
ollamaContainer, err := ollama.Run(ctx, "ollama/ollama:0.1.25", ollama.WithUseLocal(nil))
288+
ollamaContainer, err := ollama.Run(ctx, "ollama/ollama:0.1.25", ollama.WithUseLocal())
289289
require.NoError(t, err)
290290
testcontainers.CleanupContainer(t, ollamaContainer)
291291

modules/ollama/options.go

+14-6
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package ollama
22

33
import (
44
"context"
5+
"fmt"
6+
"strings"
57

68
"github.com/docker/docker/api/types/container"
79

@@ -42,23 +44,29 @@ var _ testcontainers.ContainerCustomizer = (*UseLocal)(nil)
4244

4345
// UseLocal will use the local Ollama instance instead of pulling the Docker image.
4446
type UseLocal struct {
45-
env map[string]string
47+
env []string
4648
}
4749

4850
// WithUseLocal the module will use the local Ollama instance instead of pulling the Docker image.
4951
// Pass the environment variables you need to set for the Ollama binary to be used,
5052
// in the format of "KEY=VALUE". KeyValue pairs with the wrong format will cause an error.
51-
func WithUseLocal(keyVal map[string]string) UseLocal {
52-
return UseLocal{env: keyVal}
53+
func WithUseLocal(values ...string) UseLocal {
54+
return UseLocal{env: values}
5355
}
5456

5557
// Customize implements the ContainerCustomizer interface, taking the key value pairs
5658
// and setting them as environment variables for the Ollama binary.
5759
// In the case of an invalid key value pair, an error is returned.
5860
func (u UseLocal) Customize(req *testcontainers.GenericContainerRequest) error {
59-
if len(u.env) == 0 {
60-
return nil
61+
env := make(map[string]string)
62+
for _, kv := range u.env {
63+
parts := strings.SplitN(kv, "=", 2)
64+
if len(parts) != 2 {
65+
return fmt.Errorf("invalid environment variable: %s", kv)
66+
}
67+
68+
env[parts[0]] = parts[1]
6169
}
6270

63-
return testcontainers.WithEnv(u.env)(req)
71+
return testcontainers.WithEnv(env)(req)
6472
}

modules/ollama/options_test.go

+26-6
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,38 @@ import (
1212
func TestWithUseLocal(t *testing.T) {
1313
req := testcontainers.GenericContainerRequest{}
1414

15-
t.Run("empty", func(t *testing.T) {
16-
opt := ollama.WithUseLocal(nil)
15+
t.Run("keyVal/valid", func(t *testing.T) {
16+
opt := ollama.WithUseLocal("OLLAMA_MODELS=/path/to/models")
1717
err := opt.Customize(&req)
1818
require.NoError(t, err)
19-
require.Empty(t, req.Env)
19+
require.Equal(t, "/path/to/models", req.Env["OLLAMA_MODELS"])
20+
})
21+
22+
t.Run("keyVal/invalid", func(t *testing.T) {
23+
opt := ollama.WithUseLocal("OLLAMA_MODELS")
24+
err := opt.Customize(&req)
25+
require.Error(t, err)
26+
})
27+
28+
t.Run("keyVal/valid/multiple", func(t *testing.T) {
29+
opt := ollama.WithUseLocal("OLLAMA_MODELS=/path/to/models", "OLLAMA_HOST=localhost")
30+
err := opt.Customize(&req)
31+
require.NoError(t, err)
32+
require.Equal(t, "/path/to/models", req.Env["OLLAMA_MODELS"])
33+
require.Equal(t, "localhost", req.Env["OLLAMA_HOST"])
2034
})
2135

22-
t.Run("valid", func(t *testing.T) {
23-
opt := ollama.WithUseLocal(map[string]string{"OLLAMA_MODELS": "/path/to/models", "OLLAMA_HOST": "localhost:1234"})
36+
t.Run("keyVal/valid/multiple-equals", func(t *testing.T) {
37+
opt := ollama.WithUseLocal("OLLAMA_MODELS=/path/to/models", "OLLAMA_HOST=localhost=127.0.0.1")
2438
err := opt.Customize(&req)
2539
require.NoError(t, err)
2640
require.Equal(t, "/path/to/models", req.Env["OLLAMA_MODELS"])
27-
require.Equal(t, "localhost:1234", req.Env["OLLAMA_HOST"])
41+
require.Equal(t, "localhost=127.0.0.1", req.Env["OLLAMA_HOST"])
42+
})
43+
44+
t.Run("keyVal/invalid/multiple", func(t *testing.T) {
45+
opt := ollama.WithUseLocal("OLLAMA_MODELS=/path/to/models", "OLLAMA_HOST")
46+
err := opt.Customize(&req)
47+
require.Error(t, err)
2848
})
2949
}

0 commit comments

Comments
 (0)