-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathexpand_correctness_test.go
More file actions
240 lines (199 loc) · 6.19 KB
/
expand_correctness_test.go
File metadata and controls
240 lines (199 loc) · 6.19 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
package expand
import (
"context"
"fmt"
"os"
"path/filepath"
"runtime"
"sort"
"testing"
"time"
v2 "github.com/conductorone/baton-sdk/pb/c1/connector/v2"
"github.com/conductorone/baton-sdk/pkg/dotc1z"
"github.com/stretchr/testify/require"
)
func getTestdataPathWithSuffix(syncID, suffix string) string {
_, filename, _, _ := runtime.Caller(0)
return filepath.Join(filepath.Dir(filename), "testdata", fmt.Sprintf("sync.%s.%s", syncID, suffix))
}
// grantKey creates a unique key for a grant based on entitlement and principal.
func grantKey(g *v2.Grant) string {
return fmt.Sprintf("%s|%s|%s",
g.GetEntitlement().GetId(),
g.GetPrincipal().GetId().GetResourceType(),
g.GetPrincipal().GetId().GetResource(),
)
}
// grantInfo holds grant data for comparison.
type grantInfo struct {
ID string
Sources map[string]bool // Set of source entitlement IDs
}
// loadAllGrants loads all grants from a c1z file into a map keyed by grantKey.
func loadAllGrants(ctx context.Context, c1f *dotc1z.C1File) (map[string]*grantInfo, error) {
grants := make(map[string]*grantInfo)
pageToken := ""
for {
resp, err := c1f.ListGrants(ctx, v2.GrantsServiceListGrantsRequest_builder{PageToken: pageToken}.Build())
if err != nil {
return nil, err
}
for _, g := range resp.GetList() {
key := grantKey(g)
sources := make(map[string]bool)
for sourceID := range g.GetSources().GetSources() {
sources[sourceID] = true
}
grants[key] = &grantInfo{
ID: g.GetId(),
Sources: sources,
}
}
pageToken = resp.GetNextPageToken()
if pageToken == "" {
break
}
}
return grants, nil
}
func TestExpandCorrectness(t *testing.T) {
if testing.Short() {
t.Skip("skipping slow expansion correctness test in short mode")
}
testCases := []struct {
name string
syncID string
}{
{
name: "Small",
syncID: "36zfOAoWMxQdISbixanQCRBiX7E",
},
// {
// name: "SmallMedium",
// syncID: "36zM46KKuaBq0wjSSvKh5o0350y",
// },
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
unexpandedPath := getTestdataPathWithSuffix(tc.syncID, "unexpanded")
expectedPath := getTestdataPathWithSuffix(tc.syncID, "expanded")
if _, err := os.Stat(unexpandedPath); os.IsNotExist(err) {
t.Skipf("unexpanded testdata file not found: %s", unexpandedPath)
}
if _, err := os.Stat(expectedPath); os.IsNotExist(err) {
t.Skipf("expanded testdata file not found: %s", expectedPath)
}
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Minute)
defer cancel()
// Copy unexpanded file to temp location
tmpFile, err := os.CreateTemp("", "test-expand-*.c1z")
require.NoError(t, err)
tmpPath := tmpFile.Name()
tmpFile.Close()
defer os.Remove(tmpPath)
srcData, err := os.ReadFile(unexpandedPath)
require.NoError(t, err)
err = os.WriteFile(tmpPath, srcData, 0600)
require.NoError(t, err)
// Open the temp file and run expansion
actualC1f, err := dotc1z.NewC1ZFile(ctx, tmpPath)
require.NoError(t, err)
defer actualC1f.Close()
err = actualC1f.SetSyncID(ctx, tc.syncID)
require.NoError(t, err)
// Load graph and run expansion
graph, err := loadEntitlementGraphFromC1Z(ctx, actualC1f)
require.NoError(t, err)
t.Logf("Graph loaded: %d nodes, %d edges", len(graph.Nodes), len(graph.Edges))
expander := NewExpander(actualC1f, graph)
err = expander.Run(ctx)
require.NoError(t, err)
// Open expected file
expectedC1f, err := dotc1z.NewC1ZFile(ctx, expectedPath, dotc1z.WithReadOnly(true))
require.NoError(t, err)
defer expectedC1f.Close()
err = expectedC1f.SetSyncID(ctx, tc.syncID)
require.NoError(t, err)
// Load grants from both
actualGrants, err := loadAllGrants(ctx, actualC1f)
require.NoError(t, err)
expectedGrants, err := loadAllGrants(ctx, expectedC1f)
require.NoError(t, err)
t.Logf("Actual grants: %d, Expected grants: %d", len(actualGrants), len(expectedGrants))
// Compare grant counts
require.Equal(t, len(expectedGrants), len(actualGrants), "grant count mismatch")
// Find missing and extra grants
var missingGrants []string
var extraGrants []string
for key := range expectedGrants {
if _, ok := actualGrants[key]; !ok {
missingGrants = append(missingGrants, key)
}
}
for key := range actualGrants {
if _, ok := expectedGrants[key]; !ok {
extraGrants = append(extraGrants, key)
}
}
sort.Strings(missingGrants)
sort.Strings(extraGrants)
if len(missingGrants) > 0 {
t.Errorf("Missing %d grants (first 10):", len(missingGrants))
for i, key := range missingGrants {
if i >= 10 {
break
}
t.Errorf(" - %s", key)
}
}
if len(extraGrants) > 0 {
t.Errorf("Extra %d grants (first 10):", len(extraGrants))
for i, key := range extraGrants {
if i >= 10 {
break
}
t.Errorf(" - %s", key)
}
}
// Compare sources for matching grants
var sourceMismatches []string
for key, expected := range expectedGrants {
actual, ok := actualGrants[key]
if !ok {
continue // Already reported as missing
}
// Compare sources
if len(expected.Sources) != len(actual.Sources) {
sourceMismatches = append(sourceMismatches,
fmt.Sprintf("%s: expected %d sources, got %d", key, len(expected.Sources), len(actual.Sources)))
continue
}
for sourceID := range expected.Sources {
if !actual.Sources[sourceID] {
sourceMismatches = append(sourceMismatches,
fmt.Sprintf("%s: missing source %s", key, sourceID))
}
}
for sourceID := range actual.Sources {
if !expected.Sources[sourceID] {
sourceMismatches = append(sourceMismatches,
fmt.Sprintf("%s: extra source %s", key, sourceID))
}
}
}
sort.Strings(sourceMismatches)
if len(sourceMismatches) > 0 {
t.Errorf("Source mismatches (%d total, first 20):", len(sourceMismatches))
for i, mismatch := range sourceMismatches {
if i >= 20 {
break
}
t.Errorf(" - %s", mismatch)
}
}
require.Empty(t, missingGrants, "should have no missing grants")
require.Empty(t, extraGrants, "should have no extra grants")
require.Empty(t, sourceMismatches, "should have no source mismatches")
})
}
}