service_test.go

  1package ollama
  2
  3import (
  4	"context"
  5	"testing"
  6	"time"
  7)
  8
  9func TestStartOllamaService(t *testing.T) {
 10	if !IsInstalled() {
 11		t.Skip("Ollama is not installed, skipping StartOllamaService test")
 12	}
 13
 14	ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
 15	defer cancel()
 16
 17	// First check if it's already running
 18	if IsRunning(ctx) {
 19		t.Log("✓ Ollama is already running, skipping start test")
 20		return
 21	}
 22
 23	t.Log("Starting Ollama service...")
 24	err := StartOllamaService(ctx)
 25	if err != nil {
 26		t.Fatalf("Failed to start Ollama service: %v", err)
 27	}
 28
 29	// Verify it's now running
 30	if !IsRunning(ctx) {
 31		t.Fatal("Ollama service was started but IsRunning still returns false")
 32	}
 33
 34	t.Log("✓ Ollama service started successfully")
 35
 36	// Clean up - stop the service we started
 37	cleanupProcesses()
 38}
 39
 40func TestEnsureOllamaRunning(t *testing.T) {
 41	if !IsInstalled() {
 42		t.Skip("Ollama is not installed, skipping EnsureOllamaRunning test")
 43	}
 44
 45	ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
 46	defer cancel()
 47
 48	// Test that EnsureOllamaRunning works whether Ollama is running or not
 49	err := EnsureOllamaRunning(ctx)
 50	if err != nil {
 51		t.Fatalf("EnsureOllamaRunning failed: %v", err)
 52	}
 53
 54	// Verify Ollama is running
 55	if !IsRunning(ctx) {
 56		t.Fatal("EnsureOllamaRunning succeeded but Ollama is not running")
 57	}
 58
 59	t.Log("✓ EnsureOllamaRunning succeeded")
 60
 61	// Test calling it again when already running
 62	err = EnsureOllamaRunning(ctx)
 63	if err != nil {
 64		t.Fatalf("EnsureOllamaRunning failed on second call: %v", err)
 65	}
 66
 67	t.Log("✓ EnsureOllamaRunning works when already running")
 68}
 69
 70func TestStartModel(t *testing.T) {
 71	if !IsInstalled() {
 72		t.Skip("Ollama is not installed, skipping StartModel test")
 73	}
 74
 75	ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
 76	defer cancel()
 77
 78	// Ensure Ollama is running
 79	if !IsRunning(ctx) {
 80		t.Log("Starting Ollama service...")
 81		if err := StartOllamaService(ctx); err != nil {
 82			t.Fatalf("Failed to start Ollama service: %v", err)
 83		}
 84		defer cleanupProcesses()
 85	}
 86
 87	// Get available models
 88	models, err := GetModels(ctx)
 89	if err != nil {
 90		t.Fatalf("Failed to get models: %v", err)
 91	}
 92
 93	if len(models) == 0 {
 94		t.Skip("No models available, skipping StartModel test")
 95	}
 96
 97	// Pick a smaller model if available, otherwise use the first one
 98	testModel := models[0].ID
 99	for _, model := range models {
100		if model.ID == "phi3:3.8b" || model.ID == "llama3.2:3b" {
101			testModel = model.ID
102			break
103		}
104	}
105
106	t.Logf("Testing with model: %s", testModel)
107
108	// Check if model is already loaded
109	loaded, err := IsModelLoaded(ctx, testModel)
110	if err != nil {
111		t.Fatalf("Failed to check if model is loaded: %v", err)
112	}
113
114	if loaded {
115		t.Log("✓ Model is already loaded, skipping start test")
116		return
117	}
118
119	t.Log("Starting model...")
120	err = StartModel(ctx, testModel)
121	if err != nil {
122		t.Fatalf("Failed to start model: %v", err)
123	}
124
125	// Verify model is now loaded
126	loaded, err = IsModelLoaded(ctx, testModel)
127	if err != nil {
128		t.Fatalf("Failed to check if model is loaded after start: %v", err)
129	}
130
131	if !loaded {
132		t.Fatal("StartModel succeeded but model is not loaded")
133	}
134
135	t.Log("✓ Model started successfully")
136}
137
138func TestEnsureModelRunning(t *testing.T) {
139	if !IsInstalled() {
140		t.Skip("Ollama is not installed, skipping EnsureModelRunning test")
141	}
142
143	ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
144	defer cancel()
145
146	// Ensure Ollama is running
147	if !IsRunning(ctx) {
148		t.Log("Starting Ollama service...")
149		if err := StartOllamaService(ctx); err != nil {
150			t.Fatalf("Failed to start Ollama service: %v", err)
151		}
152		defer cleanupProcesses()
153	}
154
155	// Get available models
156	models, err := GetModels(ctx)
157	if err != nil {
158		t.Fatalf("Failed to get models: %v", err)
159	}
160
161	if len(models) == 0 {
162		t.Skip("No models available, skipping EnsureModelRunning test")
163	}
164
165	testModel := models[0].ID
166	t.Logf("Testing with model: %s", testModel)
167
168	// Test EnsureModelRunning
169	err = EnsureModelRunning(ctx, testModel)
170	if err != nil {
171		t.Fatalf("EnsureModelRunning failed: %v", err)
172	}
173
174	// Verify model is running
175	loaded, err := IsModelLoaded(ctx, testModel)
176	if err != nil {
177		t.Fatalf("Failed to check if model is loaded: %v", err)
178	}
179
180	if !loaded {
181		t.Fatal("EnsureModelRunning succeeded but model is not loaded")
182	}
183
184	t.Log("✓ EnsureModelRunning succeeded")
185
186	// Test calling it again when already running
187	err = EnsureModelRunning(ctx, testModel)
188	if err != nil {
189		t.Fatalf("EnsureModelRunning failed on second call: %v", err)
190	}
191
192	t.Log("✓ EnsureModelRunning works when model already running")
193}