mirror of
https://github.com/lordmathis/llamactl.git
synced 2025-12-22 17:14:22 +00:00
Implement SQLite database persistence for instance management
This commit is contained in:
@@ -4,20 +4,34 @@ import (
|
||||
"fmt"
|
||||
"llamactl/pkg/backends"
|
||||
"llamactl/pkg/config"
|
||||
"llamactl/pkg/database"
|
||||
"llamactl/pkg/instance"
|
||||
"llamactl/pkg/manager"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestManager_PersistsAndLoadsInstances(t *testing.T) {
|
||||
tempDir := t.TempDir()
|
||||
appConfig := createTestAppConfig(tempDir)
|
||||
// Use file-based database for this test since we need to persist across connections
|
||||
appConfig.Database.Path = tempDir + "/test.db"
|
||||
|
||||
// Create instance and check file was created
|
||||
manager1 := manager.New(appConfig)
|
||||
// Create instance and check database was created
|
||||
db1, err := database.Open(&database.Config{
|
||||
Path: appConfig.Database.Path,
|
||||
MaxOpenConnections: appConfig.Database.MaxOpenConnections,
|
||||
MaxIdleConnections: appConfig.Database.MaxIdleConnections,
|
||||
ConnMaxLifetime: appConfig.Database.ConnMaxLifetime,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to open database: %v", err)
|
||||
}
|
||||
if err := database.RunMigrations(db1); err != nil {
|
||||
t.Fatalf("Failed to run migrations: %v", err)
|
||||
}
|
||||
manager1 := manager.New(appConfig, db1)
|
||||
options := &instance.Options{
|
||||
BackendOptions: backends.Options{
|
||||
BackendType: backends.BackendTypeLlamaCpp,
|
||||
@@ -28,18 +42,28 @@ func TestManager_PersistsAndLoadsInstances(t *testing.T) {
|
||||
},
|
||||
}
|
||||
|
||||
_, err := manager1.CreateInstance("test-instance", options)
|
||||
_, err = manager1.CreateInstance("test-instance", options)
|
||||
if err != nil {
|
||||
t.Fatalf("CreateInstance failed: %v", err)
|
||||
}
|
||||
|
||||
expectedPath := filepath.Join(tempDir, "test-instance.json")
|
||||
if _, err := os.Stat(expectedPath); os.IsNotExist(err) {
|
||||
t.Errorf("Expected persistence file %s to exist", expectedPath)
|
||||
}
|
||||
// Shutdown first manager to close database connection
|
||||
manager1.Shutdown()
|
||||
|
||||
// Load instances from disk
|
||||
manager2 := manager.New(appConfig)
|
||||
// Load instances from database
|
||||
db2, err := database.Open(&database.Config{
|
||||
Path: appConfig.Database.Path,
|
||||
MaxOpenConnections: appConfig.Database.MaxOpenConnections,
|
||||
MaxIdleConnections: appConfig.Database.MaxIdleConnections,
|
||||
ConnMaxLifetime: appConfig.Database.ConnMaxLifetime,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to open database: %v", err)
|
||||
}
|
||||
if err := database.RunMigrations(db2); err != nil {
|
||||
t.Fatalf("Failed to run migrations: %v", err)
|
||||
}
|
||||
manager2 := manager.New(appConfig, db2)
|
||||
instances, err := manager2.ListInstances()
|
||||
if err != nil {
|
||||
t.Fatalf("ListInstances failed: %v", err)
|
||||
@@ -50,13 +74,29 @@ func TestManager_PersistsAndLoadsInstances(t *testing.T) {
|
||||
if instances[0].Name != "test-instance" {
|
||||
t.Errorf("Expected loaded instance name 'test-instance', got %q", instances[0].Name)
|
||||
}
|
||||
|
||||
manager2.Shutdown()
|
||||
}
|
||||
|
||||
func TestDeleteInstance_RemovesPersistenceFile(t *testing.T) {
|
||||
func TestDeleteInstance_RemovesFromDatabase(t *testing.T) {
|
||||
tempDir := t.TempDir()
|
||||
appConfig := createTestAppConfig(tempDir)
|
||||
|
||||
mgr := manager.New(appConfig)
|
||||
db, err := database.Open(&database.Config{
|
||||
Path: appConfig.Database.Path,
|
||||
MaxOpenConnections: appConfig.Database.MaxOpenConnections,
|
||||
MaxIdleConnections: appConfig.Database.MaxIdleConnections,
|
||||
ConnMaxLifetime: appConfig.Database.ConnMaxLifetime,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to open database: %v", err)
|
||||
}
|
||||
if err := database.RunMigrations(db); err != nil {
|
||||
t.Fatalf("Failed to run migrations: %v", err)
|
||||
}
|
||||
mgr := manager.New(appConfig, db)
|
||||
defer mgr.Shutdown()
|
||||
|
||||
options := &instance.Options{
|
||||
BackendOptions: backends.Options{
|
||||
BackendType: backends.BackendTypeLlamaCpp,
|
||||
@@ -67,20 +107,33 @@ func TestDeleteInstance_RemovesPersistenceFile(t *testing.T) {
|
||||
},
|
||||
}
|
||||
|
||||
_, err := mgr.CreateInstance("test-instance", options)
|
||||
_, err = mgr.CreateInstance("test-instance", options)
|
||||
if err != nil {
|
||||
t.Fatalf("CreateInstance failed: %v", err)
|
||||
}
|
||||
|
||||
expectedPath := filepath.Join(tempDir, "test-instance.json")
|
||||
// Verify instance exists
|
||||
instances, err := mgr.ListInstances()
|
||||
if err != nil {
|
||||
t.Fatalf("ListInstances failed: %v", err)
|
||||
}
|
||||
if len(instances) != 1 {
|
||||
t.Fatalf("Expected 1 instance, got %d", len(instances))
|
||||
}
|
||||
|
||||
// Delete instance
|
||||
err = mgr.DeleteInstance("test-instance")
|
||||
if err != nil {
|
||||
t.Fatalf("DeleteInstance failed: %v", err)
|
||||
}
|
||||
|
||||
if _, err := os.Stat(expectedPath); !os.IsNotExist(err) {
|
||||
t.Error("Expected persistence file to be deleted")
|
||||
// Verify instance was deleted from database
|
||||
instances, err = mgr.ListInstances()
|
||||
if err != nil {
|
||||
t.Fatalf("ListInstances failed: %v", err)
|
||||
}
|
||||
if len(instances) != 0 {
|
||||
t.Errorf("Expected 0 instances after deletion, got %d", len(instances))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -158,6 +211,12 @@ func createTestAppConfig(instancesDir string) *config.AppConfig {
|
||||
DefaultRestartDelay: 5,
|
||||
TimeoutCheckInterval: 5,
|
||||
},
|
||||
Database: config.DatabaseConfig{
|
||||
Path: ":memory:",
|
||||
MaxOpenConnections: 25,
|
||||
MaxIdleConnections: 5,
|
||||
ConnMaxLifetime: 5 * time.Minute,
|
||||
},
|
||||
LocalNode: "main",
|
||||
Nodes: map[string]config.NodeConfig{},
|
||||
}
|
||||
@@ -166,5 +225,17 @@ func createTestAppConfig(instancesDir string) *config.AppConfig {
|
||||
func createTestManager(t *testing.T) manager.InstanceManager {
|
||||
tempDir := t.TempDir()
|
||||
appConfig := createTestAppConfig(tempDir)
|
||||
return manager.New(appConfig)
|
||||
db, err := database.Open(&database.Config{
|
||||
Path: appConfig.Database.Path,
|
||||
MaxOpenConnections: appConfig.Database.MaxOpenConnections,
|
||||
MaxIdleConnections: appConfig.Database.MaxIdleConnections,
|
||||
ConnMaxLifetime: appConfig.Database.ConnMaxLifetime,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to open database: %v", err)
|
||||
}
|
||||
if err := database.RunMigrations(db); err != nil {
|
||||
t.Fatalf("Failed to run migrations: %v", err)
|
||||
}
|
||||
return manager.New(appConfig, db)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user