-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstorage_paths_test.go
More file actions
121 lines (113 loc) · 4 KB
/
Copy pathstorage_paths_test.go
File metadata and controls
121 lines (113 loc) · 4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package starmap
import (
"context"
stderrors "errors"
"os"
"path/filepath"
"strings"
"testing"
"github.com/agentstation/starmap/pkg/catalogs"
"github.com/agentstation/starmap/pkg/catalogstore"
"github.com/agentstation/starmap/pkg/constants"
starmaperrors "github.com/agentstation/starmap/pkg/errors"
"github.com/agentstation/starmap/pkg/save"
pkgsync "github.com/agentstation/starmap/pkg/sync"
)
func TestDefaultCatalogDatabaseAndExportPathsAreDisjoint(t *testing.T) {
home := filepath.Join(string(filepath.Separator), "home", "starmap")
database := strings.Replace(constants.DefaultCatalogDatabasePath, "~", home, 1)
export := strings.Replace(constants.DefaultCatalogExportPath, "~", home, 1)
if pathsContainEachOther(database, export) {
t.Fatalf("default durable database %q overlaps editable export %q", database, export)
}
if pathContains(export, database) || pathContains(database, export) {
t.Fatal("default lifecycle roots contain one another")
}
}
func TestCatalogExportReplacementCannotTouchSiblingDatabase(t *testing.T) {
root := t.TempDir()
database := filepath.Join(root, "catalog")
export := filepath.Join(root, "exports", "catalog")
if pathsContainEachOther(database, export) {
t.Fatal("test lifecycle roots overlap")
}
if err := os.MkdirAll(database, constants.DirPermissions); err != nil {
t.Fatalf("MkdirAll database: %v", err)
}
markerPath := filepath.Join(database, "current")
marker := []byte("immutable-generation\n")
if err := os.WriteFile(markerPath, marker, constants.FilePermissions); err != nil {
t.Fatalf("WriteFile marker: %v", err)
}
builder := catalogs.NewEmpty()
if err := builder.SetProvider(catalogs.Provider{ID: "example", Name: "Example"}); err != nil {
t.Fatalf("SetProvider: %v", err)
}
if err := builder.Save(save.WithPath(export)); err != nil {
t.Fatalf("Save export: %v", err)
}
if err := builder.DeleteProvider("example"); err != nil {
t.Fatalf("DeleteProvider: %v", err)
}
if err := builder.Save(save.WithPath(export)); err != nil {
t.Fatalf("replacement Save export: %v", err)
}
retained, err := os.ReadFile(markerPath)
if err != nil {
t.Fatalf("ReadFile marker: %v", err)
}
if string(retained) != string(marker) {
t.Fatalf("database marker changed: %q", retained)
}
}
func TestClientRejectsCatalogDatabaseAndExportOverlap(t *testing.T) {
root := t.TempDir()
store, err := catalogstore.NewFilesystem(filepath.Join(root, "catalog"))
if err != nil {
t.Fatalf("NewFilesystem: %v", err)
}
_, err = New(WithCatalogStore(store), WithCatalogExportPath(root))
assertCatalogLayoutError(t, err)
}
func TestClientRejectsSymlinkedCatalogDatabaseAndExportOverlap(t *testing.T) {
root := t.TempDir()
database := filepath.Join(root, "database")
if err := os.MkdirAll(database, constants.DirPermissions); err != nil {
t.Fatalf("MkdirAll: %v", err)
}
alias := filepath.Join(root, "alias")
if err := os.Symlink(database, alias); err != nil {
t.Fatalf("Symlink: %v", err)
}
store, err := catalogstore.NewFilesystem(database)
if err != nil {
t.Fatalf("NewFilesystem: %v", err)
}
_, err = New(WithCatalogStore(store), WithCatalogExportPath(alias))
assertCatalogLayoutError(t, err)
}
func TestClientSaveAndSyncRejectDurableDatabaseTargets(t *testing.T) {
root := t.TempDir()
database := filepath.Join(root, "catalog")
store, err := catalogstore.NewFilesystem(database)
if err != nil {
t.Fatalf("NewFilesystem: %v", err)
}
client, err := New(WithCatalogStore(store))
if err != nil {
t.Fatalf("New: %v", err)
}
assertCatalogLayoutError(t, client.Save(save.WithPath(filepath.Join(database, "exports"))))
_, err = client.Sync(context.Background(), pkgsync.WithDryRun(true), pkgsync.WithOutputPath(root))
assertCatalogLayoutError(t, err)
}
func assertCatalogLayoutError(t *testing.T, err error) {
t.Helper()
var configError *starmaperrors.ConfigError
if !stderrors.As(err, &configError) {
t.Fatalf("error = %T %v, want ConfigError", err, err)
}
if configError.Component != "catalog filesystem layout" {
t.Fatalf("component = %q", configError.Component)
}
}