refactor: move V1 code into v1/ subdirectory
- git mv cmd/ internal/ pkg/ test/ go.mod go.sum Makefile build.sh docs/ v1/ - Create v1/README.md with V1 documentation - Update root README for V1 + V2 structure - V1 still builds (cd v1 && go build ./cmd/hostkeeper) and 105 tests pass - Root is now clean for V2 development
This commit is contained in:
@@ -0,0 +1,83 @@
|
||||
package storage_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"git.tukangketik.id/swanadiva/hostkeeper/internal/models"
|
||||
"git.tukangketik.id/swanadiva/hostkeeper/pkg/storage"
|
||||
)
|
||||
|
||||
func TestExportImport(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()
|
||||
|
||||
testHost := &models.Host{
|
||||
ID: "test-host-1",
|
||||
Name: "Test Server",
|
||||
Hostname: "192.168.1.100",
|
||||
Port: 22,
|
||||
Username: "admin",
|
||||
Auth: models.AuthConfig{Type: "password"},
|
||||
CreatedAt: time.Now(),
|
||||
}
|
||||
|
||||
if err := store.SaveHost(ctx, testHost); err != nil {
|
||||
t.Fatalf("Failed to save host: %v", err)
|
||||
}
|
||||
|
||||
exportedData, err := store.ExportData(ctx)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to export data: %v", err)
|
||||
}
|
||||
|
||||
if exportedData == nil {
|
||||
t.Fatal("Exported data is nil")
|
||||
}
|
||||
|
||||
if len(exportedData.Hosts) != 1 {
|
||||
t.Fatalf("Expected 1 host, got %d", len(exportedData.Hosts))
|
||||
}
|
||||
|
||||
// Simulate writing to file and reading back
|
||||
jsonBytes, err := json.Marshal(exportedData)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to marshal export data: %v", err)
|
||||
}
|
||||
|
||||
var importedData storage.ExportData
|
||||
if err := json.Unmarshal(jsonBytes, &importedData); err != nil {
|
||||
t.Fatalf("Failed to unmarshal export data: %v", err)
|
||||
}
|
||||
|
||||
importDir := t.TempDir()
|
||||
importStore, err := storage.NewJSONStorage(importDir)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to create import storage: %v", err)
|
||||
}
|
||||
|
||||
if err := importStore.ImportData(ctx, &importedData, storage.MergeStrategyReplace); err != nil {
|
||||
t.Fatalf("Failed to import data: %v", err)
|
||||
}
|
||||
|
||||
importedHosts, err := importStore.ListHosts(ctx)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to list imported hosts: %v", err)
|
||||
}
|
||||
|
||||
if len(importedHosts) != 1 {
|
||||
t.Errorf("Expected 1 imported host, got %d", len(importedHosts))
|
||||
}
|
||||
|
||||
if importedHosts[0].Name != testHost.Name {
|
||||
t.Errorf("Expected host name '%s', got '%s'", testHost.Name, importedHosts[0].Name)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user