Skip to content

Commit

Permalink
test(go): retrieve port info for test container
Browse files Browse the repository at this point in the history
  • Loading branch information
cabljac committed Jul 31, 2024
1 parent bd176a8 commit 775d7e4
Showing 1 changed file with 24 additions and 2 deletions.
26 changes: 24 additions & 2 deletions go/plugins/ollama/ollama_live_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,18 @@ package ollama_test

import (
"context"
"fmt"
"log"
"testing"

"github.com/docker/docker/client"
"github.com/docker/go-connections/nat"
"github.com/firebase/genkit/go/ai"
ollamaPlugin "github.com/firebase/genkit/go/plugins/ollama"
ollamaTestContainers "github.com/testcontainers/testcontainers-go/modules/ollama"
)

func TestWithOllama(t *testing.T) {
func TestWithOllamaContainer(t *testing.T) {
ctx := context.Background()

// Start the Ollama container
Expand All @@ -38,15 +41,34 @@ func TestWithOllama(t *testing.T) {
}
}()

// Get the container information
cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
if err != nil {
t.Fatalf("failed to create docker client: %s", err)
}
containerID := ollamaContainer.GetContainerID()
containerInfo, err := cli.ContainerInspect(ctx, containerID)
if err != nil {
t.Fatalf("failed to inspect container: %s", err)
}

// Retrieve the host port for the container's port 11434
portBindings := containerInfo.NetworkSettings.Ports[nat.Port("11434/tcp")]
if len(portBindings) == 0 {
t.Fatalf("no port bindings found for port 11434/tcp")
}
hostPort := portBindings[0].HostPort

// Pull the model
_, _, err = ollamaContainer.Exec(ctx, []string{"ollama", "pull", "tinyllama"})
if err != nil {
t.Fatalf("failed to pull model: %s", err)
}

// Initialize the Ollama plugin
serverAddress := fmt.Sprintf("http://localhost:%s", hostPort)
err = ollamaPlugin.Init(ctx, &ollamaPlugin.Config{
ServerAddress: "http://localhost:11434",
ServerAddress: serverAddress,
})
if err != nil {
t.Fatalf("failed to initialize Ollama plugin: %s", err)
Expand Down

0 comments on commit 775d7e4

Please sign in to comment.