This commit introduces test cases for the API, archive, store, and filesystem functionalities, as well as a functional test for a full workflow. It ensures robust testing for snapshot operations, archiving, and blob management, significantly improving reliability.
116 lines
3.1 KiB
Go
116 lines
3.1 KiB
Go
package remote
|
|
|
|
import (
|
|
"context"
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
|
|
"gitea.unprism.ru/KRBL/Agate/store"
|
|
)
|
|
|
|
// TestClientConnect tests that the client can connect to a server
|
|
func TestClientConnect(t *testing.T) {
|
|
// Skip this test in short mode
|
|
if testing.Short() {
|
|
t.Skip("Skipping remote test in short mode")
|
|
}
|
|
|
|
// This test requires a running server
|
|
// For a real test, you would need to start a server
|
|
// Here we'll just test the client creation
|
|
_, err := NewClient("localhost:50051")
|
|
if err != nil {
|
|
// It's expected that this will fail if no server is running
|
|
t.Logf("Failed to connect to server: %v", err)
|
|
}
|
|
}
|
|
|
|
// TestMockClient tests the client functionality with a mock
|
|
func TestMockClient(t *testing.T) {
|
|
// Create a mock client
|
|
client := &MockClient{}
|
|
|
|
// Test ListSnapshots
|
|
snapshots, err := client.ListSnapshots(context.Background())
|
|
if err != nil {
|
|
t.Fatalf("MockClient.ListSnapshots failed: %v", err)
|
|
}
|
|
if len(snapshots) != 1 {
|
|
t.Errorf("Expected 1 snapshot, got %d", len(snapshots))
|
|
}
|
|
|
|
// Test FetchSnapshotDetails
|
|
snapshot, err := client.FetchSnapshotDetails(context.Background(), "mock-snapshot-id")
|
|
if err != nil {
|
|
t.Fatalf("MockClient.FetchSnapshotDetails failed: %v", err)
|
|
}
|
|
if snapshot.ID != "mock-snapshot-id" {
|
|
t.Errorf("Expected snapshot ID 'mock-snapshot-id', got '%s'", snapshot.ID)
|
|
}
|
|
|
|
// Test DownloadSnapshot
|
|
tempDir, err := os.MkdirTemp("", "agate-mock-test-*")
|
|
if err != nil {
|
|
t.Fatalf("Failed to create temp directory: %v", err)
|
|
}
|
|
defer os.RemoveAll(tempDir)
|
|
|
|
err = client.DownloadSnapshot(context.Background(), "mock-snapshot-id", tempDir, "")
|
|
if err != nil {
|
|
t.Fatalf("MockClient.DownloadSnapshot failed: %v", err)
|
|
}
|
|
|
|
// Check that the mock file was created
|
|
mockFilePath := filepath.Join(tempDir, "mock-file.txt")
|
|
if _, err := os.Stat(mockFilePath); os.IsNotExist(err) {
|
|
t.Errorf("Mock file was not created")
|
|
}
|
|
}
|
|
|
|
// MockClient is a mock implementation of the Client for testing
|
|
type MockClient struct{}
|
|
|
|
// ListSnapshots returns a mock list of snapshots
|
|
func (m *MockClient) ListSnapshots(ctx context.Context) ([]store.SnapshotInfo, error) {
|
|
return []store.SnapshotInfo{
|
|
{
|
|
ID: "mock-snapshot-id",
|
|
Name: "Mock Snapshot",
|
|
ParentID: "",
|
|
},
|
|
}, nil
|
|
}
|
|
|
|
// FetchSnapshotDetails returns mock snapshot details
|
|
func (m *MockClient) FetchSnapshotDetails(ctx context.Context, snapshotID string) (*store.Snapshot, error) {
|
|
return &store.Snapshot{
|
|
ID: snapshotID,
|
|
Name: "Mock Snapshot",
|
|
ParentID: "",
|
|
Files: []store.FileInfo{
|
|
{
|
|
Path: "mock-file.txt",
|
|
Size: 100,
|
|
IsDir: false,
|
|
SHA256: "mock-hash",
|
|
},
|
|
},
|
|
}, nil
|
|
}
|
|
|
|
// DownloadSnapshot simulates downloading a snapshot
|
|
func (m *MockClient) DownloadSnapshot(ctx context.Context, snapshotID string, targetDir string, localParentID string) error {
|
|
// Create a mock file
|
|
mockFilePath := filepath.Join(targetDir, "mock-file.txt")
|
|
if err := os.MkdirAll(filepath.Dir(mockFilePath), 0755); err != nil {
|
|
return err
|
|
}
|
|
return os.WriteFile(mockFilePath, []byte("Mock file content"), 0644)
|
|
}
|
|
|
|
// Close is a no-op for the mock client
|
|
func (m *MockClient) Close() error {
|
|
return nil
|
|
}
|