diff --git a/pkg/manager/manager_test.go b/pkg/manager/manager_test.go index 34f63cf..ed9cdcb 100644 --- a/pkg/manager/manager_test.go +++ b/pkg/manager/manager_test.go @@ -134,30 +134,6 @@ func TestConcurrentAccess(t *testing.T) { } } -func TestShutdown(t *testing.T) { - mgr := createTestManager() - - // Create test instance - options := &instance.Options{ - BackendOptions: backends.Options{ - BackendType: backends.BackendTypeLlamaCpp, - LlamaServerOptions: &backends.LlamaServerOptions{ - Model: "/path/to/model.gguf", - }, - }, - } - _, err := mgr.CreateInstance("test-instance", options) - if err != nil { - t.Fatalf("CreateInstance failed: %v", err) - } - - // Shutdown should not panic - mgr.Shutdown() - - // Multiple shutdowns should not panic - mgr.Shutdown() -} - // Helper functions for test configuration func createBackendConfig() config.BackendConfig { // Use 'sleep' as a test command instead of 'llama-server' @@ -195,57 +171,3 @@ func createTestManager() manager.InstanceManager { } return manager.New(createBackendConfig(), cfg, map[string]config.NodeConfig{}, "main") } - -func TestManager_DoesNotAutoRestartWhenDisabled(t *testing.T) { - tempDir := t.TempDir() - cfg := createPersistenceConfig(tempDir) - backendConfig := createBackendConfig() - - // Create first manager and instance with auto-restart disabled - manager1 := manager.New(backendConfig, cfg, map[string]config.NodeConfig{}, "main") - - autoRestart := false - options := &instance.Options{ - AutoRestart: &autoRestart, - BackendOptions: backends.Options{ - BackendType: backends.BackendTypeLlamaCpp, - LlamaServerOptions: &backends.LlamaServerOptions{ - Model: "/path/to/model.gguf", - Port: 8080, - }, - }, - } - - inst, err := manager1.CreateInstance("test-instance", options) - if err != nil { - t.Fatalf("CreateInstance failed: %v", err) - } - - // Simulate instance being in running state when persisted - // (this would happen if the instance was running when llamactl was stopped) - inst.SetStatus(instance.Running) - - // Shutdown first manager - manager1.Shutdown() - - // Create second manager (simulating restart of llamactl) - manager2 := manager.New(backendConfig, cfg, map[string]config.NodeConfig{}, "main") - - // Get the loaded instance - loadedInst, err := manager2.GetInstance("test-instance") - if err != nil { - t.Fatalf("GetInstance failed: %v", err) - } - - // The instance should be marked as Stopped, not Running - // because auto-restart is disabled - if loadedInst.IsRunning() { - t.Errorf("Expected instance with auto-restart disabled to be stopped after manager restart, but it was running") - } - - if loadedInst.GetStatus() != instance.Stopped { - t.Errorf("Expected instance status to be Stopped, got %v", loadedInst.GetStatus()) - } - - manager2.Shutdown() -} diff --git a/pkg/manager/operations_test.go b/pkg/manager/operations_test.go index 976fb39..47396fa 100644 --- a/pkg/manager/operations_test.go +++ b/pkg/manager/operations_test.go @@ -9,36 +9,7 @@ import ( "testing" ) -func TestCreateInstance_Success(t *testing.T) { - manager := createTestManager() - - options := &instance.Options{ - BackendOptions: backends.Options{ - BackendType: backends.BackendTypeLlamaCpp, - LlamaServerOptions: &backends.LlamaServerOptions{ - Model: "/path/to/model.gguf", - Port: 8080, - }, - }, - } - - inst, err := manager.CreateInstance("test-instance", options) - if err != nil { - t.Fatalf("CreateInstance failed: %v", err) - } - - if inst.Name != "test-instance" { - t.Errorf("Expected instance name 'test-instance', got %q", inst.Name) - } - if inst.GetStatus() != instance.Stopped { - t.Error("New instance should not be running") - } - if inst.GetPort() != 8080 { - t.Errorf("Expected port 8080, got %d", inst.GetPort()) - } -} - -func TestCreateInstance_DuplicateName(t *testing.T) { +func TestCreateInstance_FailsWithDuplicateName(t *testing.T) { mngr := createTestManager() options := &instance.Options{ BackendOptions: backends.Options{ @@ -64,7 +35,7 @@ func TestCreateInstance_DuplicateName(t *testing.T) { } } -func TestCreateInstance_MaxInstancesLimit(t *testing.T) { +func TestCreateInstance_FailsWhenMaxInstancesReached(t *testing.T) { backendConfig := config.BackendConfig{ LlamaCpp: config.BackendSettings{ Command: "llama-server", @@ -101,30 +72,7 @@ func TestCreateInstance_MaxInstancesLimit(t *testing.T) { } } -func TestCreateInstance_AutoAssignsPort(t *testing.T) { - manager := createTestManager() - - options := &instance.Options{ - BackendOptions: backends.Options{ - BackendType: backends.BackendTypeLlamaCpp, - LlamaServerOptions: &backends.LlamaServerOptions{ - Model: "/path/to/model.gguf", - }, - }, - } - - inst, err := manager.CreateInstance("instance1", options) - if err != nil { - t.Fatalf("CreateInstance failed: %v", err) - } - - port := inst.GetPort() - if port < 8000 || port > 9000 { - t.Errorf("Expected port in range 8000-9000, got %d", port) - } -} - -func TestCreateInstance_PortConflict(t *testing.T) { +func TestCreateInstance_FailsWithPortConflict(t *testing.T) { manager := createTestManager() options1 := &instance.Options{ @@ -162,74 +110,7 @@ func TestCreateInstance_PortConflict(t *testing.T) { } } -func TestDeleteInstance_ReleasesPort(t *testing.T) { - manager := createTestManager() - - options := &instance.Options{ - BackendOptions: backends.Options{ - BackendType: backends.BackendTypeLlamaCpp, - LlamaServerOptions: &backends.LlamaServerOptions{ - Model: "/path/to/model.gguf", - Port: 8080, - }, - }, - } - - _, err := manager.CreateInstance("port-test", options) - if err != nil { - t.Fatalf("CreateInstance failed: %v", err) - } - - err = manager.DeleteInstance("port-test") - if err != nil { - t.Fatalf("DeleteInstance failed: %v", err) - } - - // Should be able to create new instance with same port - _, err = manager.CreateInstance("new-port-test", options) - if err != nil { - t.Errorf("Expected to reuse port after deletion, got error: %v", err) - } -} - -func TestUpdateInstance(t *testing.T) { - manager := createTestManager() - - options := &instance.Options{ - BackendOptions: backends.Options{ - BackendType: backends.BackendTypeLlamaCpp, - LlamaServerOptions: &backends.LlamaServerOptions{ - Model: "/path/to/model.gguf", - Port: 8080, - }, - }, - } - - _, err := manager.CreateInstance("test-instance", options) - if err != nil { - t.Fatalf("CreateInstance failed: %v", err) - } - - newOptions := &instance.Options{ - BackendOptions: backends.Options{ - BackendType: backends.BackendTypeLlamaCpp, - LlamaServerOptions: &backends.LlamaServerOptions{ - Model: "/path/to/new-model.gguf", - Port: 8081, - }, - }, - } - - updated, err := manager.UpdateInstance("test-instance", newOptions) - if err != nil { - t.Fatalf("UpdateInstance failed: %v", err) - } - if updated.GetOptions().BackendOptions.LlamaServerOptions.Model != "/path/to/new-model.gguf" { - t.Errorf("Expected model '/path/to/new-model.gguf', got %q", updated.GetOptions().BackendOptions.LlamaServerOptions.Model) - } -} - -func TestInstanceOperations_NonExistentInstance(t *testing.T) { +func TestInstanceOperations_FailWithNonExistentInstance(t *testing.T) { manager := createTestManager() options := &instance.Options{ @@ -257,179 +138,6 @@ func TestInstanceOperations_NonExistentInstance(t *testing.T) { } } -func TestStartInstance(t *testing.T) { - mgr := createTestManager() - defer mgr.Shutdown() - - options := &instance.Options{ - BackendOptions: backends.Options{ - BackendType: backends.BackendTypeLlamaCpp, - LlamaServerOptions: &backends.LlamaServerOptions{ - Model: "/path/to/model.gguf", - }, - }, - } - - inst, err := mgr.CreateInstance("test-instance", options) - if err != nil { - t.Fatalf("CreateInstance failed: %v", err) - } - - if inst.IsRunning() { - t.Error("New instance should not be running") - } - - // Start the instance - started, err := mgr.StartInstance("test-instance") - if err != nil { - t.Fatalf("StartInstance failed: %v", err) - } - - if !started.IsRunning() { - t.Error("Instance should be running after start") - } -} - -func TestStartInstance_Idempotent(t *testing.T) { - mgr := createTestManager() - defer mgr.Shutdown() - - options := &instance.Options{ - BackendOptions: backends.Options{ - BackendType: backends.BackendTypeLlamaCpp, - LlamaServerOptions: &backends.LlamaServerOptions{ - Model: "/path/to/model.gguf", - }, - }, - } - - inst, err := mgr.CreateInstance("test-instance", options) - if err != nil { - t.Fatalf("CreateInstance failed: %v", err) - } - - // Start the instance - _, err = mgr.StartInstance("test-instance") - if err != nil { - t.Fatalf("First StartInstance failed: %v", err) - } - - // Start again - should be idempotent - started, err := mgr.StartInstance("test-instance") - if err != nil { - t.Fatalf("Second StartInstance failed: %v", err) - } - - if !started.IsRunning() { - t.Error("Instance should still be running") - } - - if inst.GetStatus() != instance.Running { - t.Errorf("Expected Running status, got %v", inst.GetStatus()) - } -} - -func TestStopInstance(t *testing.T) { - mgr := createTestManager() - defer mgr.Shutdown() - - options := &instance.Options{ - BackendOptions: backends.Options{ - BackendType: backends.BackendTypeLlamaCpp, - LlamaServerOptions: &backends.LlamaServerOptions{ - Model: "/path/to/model.gguf", - }, - }, - } - - _, err := mgr.CreateInstance("test-instance", options) - if err != nil { - t.Fatalf("CreateInstance failed: %v", err) - } - - _, err = mgr.StartInstance("test-instance") - if err != nil { - t.Fatalf("StartInstance failed: %v", err) - } - - // Stop the instance - stopped, err := mgr.StopInstance("test-instance") - if err != nil { - t.Fatalf("StopInstance failed: %v", err) - } - - if stopped.IsRunning() { - t.Error("Instance should not be running after stop") - } -} - -func TestStopInstance_Idempotent(t *testing.T) { - mgr := createTestManager() - defer mgr.Shutdown() - - options := &instance.Options{ - BackendOptions: backends.Options{ - BackendType: backends.BackendTypeLlamaCpp, - LlamaServerOptions: &backends.LlamaServerOptions{ - Model: "/path/to/model.gguf", - }, - }, - } - - inst, err := mgr.CreateInstance("test-instance", options) - if err != nil { - t.Fatalf("CreateInstance failed: %v", err) - } - - // Stop when already stopped - should be idempotent - stopped, err := mgr.StopInstance("test-instance") - if err != nil { - t.Fatalf("StopInstance failed: %v", err) - } - - if stopped.IsRunning() { - t.Error("Instance should not be running") - } - - if inst.GetStatus() != instance.Stopped { - t.Errorf("Expected Stopped status, got %v", inst.GetStatus()) - } -} - -func TestRestartInstance(t *testing.T) { - mgr := createTestManager() - defer mgr.Shutdown() - - options := &instance.Options{ - BackendOptions: backends.Options{ - BackendType: backends.BackendTypeLlamaCpp, - LlamaServerOptions: &backends.LlamaServerOptions{ - Model: "/path/to/model.gguf", - }, - }, - } - - _, err := mgr.CreateInstance("test-instance", options) - if err != nil { - t.Fatalf("CreateInstance failed: %v", err) - } - - _, err = mgr.StartInstance("test-instance") - if err != nil { - t.Fatalf("StartInstance failed: %v", err) - } - - // Restart the instance - restarted, err := mgr.RestartInstance("test-instance") - if err != nil { - t.Fatalf("RestartInstance failed: %v", err) - } - - if !restarted.IsRunning() { - t.Error("Instance should be running after restart") - } -} - func TestDeleteInstance_RunningInstanceFails(t *testing.T) { mgr := createTestManager() defer mgr.Shutdown() @@ -460,7 +168,7 @@ func TestDeleteInstance_RunningInstanceFails(t *testing.T) { } } -func TestUpdateInstance_OnRunningInstance(t *testing.T) { +func TestUpdateInstance(t *testing.T) { mgr := createTestManager() defer mgr.Shutdown() @@ -510,7 +218,7 @@ func TestUpdateInstance_OnRunningInstance(t *testing.T) { } } -func TestUpdateInstance_PortChange(t *testing.T) { +func TestUpdateInstance_ReleasesOldPort(t *testing.T) { mgr := createTestManager() defer mgr.Shutdown()