Files
HostKeeper/test/integration/integration_test.go
T
swanadiva c2d6aabea0 feat: add build system and integration tests
- Update Makefile with test-coverage and verify targets
- Add build.sh script for cross-platform builds with SHA256 checksums
- Implement integration tests for full workflow (add/list/get/update/export/delete)
- Add config integration test
- Update project state documentation
2026-06-23 14:03:08 +07:00

154 lines
3.7 KiB
Go

package integration
import (
"context"
"testing"
"time"
"git.tukangketik.id/swanadiva/hostkeeper/internal/models"
"git.tukangketik.id/swanadiva/hostkeeper/pkg/config"
"git.tukangketik.id/swanadiva/hostkeeper/pkg/storage"
)
func TestIntegrationWorkflow(t *testing.T) {
tempDir := t.TempDir()
store, err := storage.NewJSONStorage(tempDir)
if err != nil {
t.Fatalf("Failed to create storage: %v", err)
}
ctx := context.Background()
// Test 1: Add hosts
t.Run("AddHosts", func(t *testing.T) {
testHost := &models.Host{
ID: "integration-test-1",
Name: "Integration Test Server",
Hostname: "test.example.com",
Port: 22,
Username: "testuser",
Auth: models.AuthConfig{Type: "password", Password: "testpass"},
Tags: []string{"test", "integration"},
CreatedAt: time.Now(),
}
if err := store.SaveHost(ctx, testHost); err != nil {
t.Errorf("Failed to save host: %v", err)
}
})
// Test 2: List hosts
t.Run("ListHosts", func(t *testing.T) {
hosts, err := store.ListHosts(ctx)
if err != nil {
t.Errorf("Failed to list hosts: %v", err)
}
if len(hosts) != 1 {
t.Errorf("Expected 1 host, got %d", len(hosts))
}
})
// Test 3: Get host
t.Run("GetHost", func(t *testing.T) {
host, err := store.GetHost(ctx, "integration-test-1")
if err != nil {
t.Errorf("Failed to get host: %v", err)
}
if host.Name != "Integration Test Server" {
t.Errorf("Expected name 'Integration Test Server', got '%s'", host.Name)
}
})
// Test 4: Update host
t.Run("UpdateHost", func(t *testing.T) {
host, err := store.GetHost(ctx, "integration-test-1")
if err != nil {
t.Errorf("Failed to get host: %v", err)
}
host.Name = "Updated Test Server"
if err := store.SaveHost(ctx, host); err != nil {
t.Errorf("Failed to update host: %v", err)
}
updated, err := store.GetHost(ctx, "integration-test-1")
if err != nil {
t.Errorf("Failed to get updated host: %v", err)
}
if updated.Name != "Updated Test Server" {
t.Errorf("Update failed: expected 'Updated Test Server', got '%s'", updated.Name)
}
})
// Test 5: Export/Import
t.Run("ExportImport", func(t *testing.T) {
exportData, err := store.ExportData(ctx)
if err != nil {
t.Errorf("Failed to export: %v", err)
}
if exportData == nil {
t.Error("Exported data is nil")
return
}
if len(exportData.Hosts) != 1 {
t.Errorf("Expected 1 host in export, got %d", len(exportData.Hosts))
}
importDir := t.TempDir()
importStore, err := storage.NewJSONStorage(importDir)
if err != nil {
t.Errorf("Failed to create import storage: %v", err)
}
if err := importStore.ImportData(ctx, exportData, storage.MergeStrategyReplace); err != nil {
t.Errorf("Failed to import: %v", err)
}
importedHosts, err := importStore.ListHosts(ctx)
if err != nil {
t.Errorf("Failed to list imported hosts: %v", err)
}
if len(importedHosts) != 1 {
t.Errorf("Expected 1 imported host, got %d", len(importedHosts))
}
})
// Test 6: Delete host
t.Run("DeleteHost", func(t *testing.T) {
if err := store.DeleteHost(ctx, "integration-test-1"); err != nil {
t.Errorf("Failed to delete host: %v", err)
}
hosts, err := store.ListHosts(ctx)
if err != nil {
t.Errorf("Failed to list hosts after deletion: %v", err)
}
if len(hosts) != 0 {
t.Errorf("Expected 0 hosts after deletion, got %d", len(hosts))
}
})
}
func TestConfigIntegration(t *testing.T) {
cfg, err := config.New()
if err != nil {
t.Errorf("Failed to load/create config: %v", err)
}
if cfg.GetAppConfig().DefaultPort != 22 {
t.Errorf("Expected default port 22, got %d", cfg.GetAppConfig().DefaultPort)
}
if cfg.GetAppConfig().ConnectionTimeout != 30 {
t.Errorf("Expected default timeout 30, got %d", cfg.GetAppConfig().ConnectionTimeout)
}
}