-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathzz_executors_test.go
More file actions
228 lines (202 loc) · 6.12 KB
/
Copy pathzz_executors_test.go
File metadata and controls
228 lines (202 loc) · 6.12 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
// SPDX-License-Identifier: AGPL-3.0-or-later
package policy
import (
"testing"
"time"
)
// makeCyclePolicy returns a policy whose cycle event triggers various
// action directives so we can drive executeTag/executeEvict/executePrune/etc.
func makeCyclePolicy(t *testing.T, actions []Action) *CompiledPolicy {
t.Helper()
doc := &PolicyDocument{
Version: 1,
Rules: []Rule{
{
Name: "cycle-rule",
On: EventCycle,
Match: "true",
Actions: actions,
},
},
}
cp, err := Compile(doc)
if err != nil {
t.Fatalf("Compile: %v", err)
}
return cp
}
// runnerWithPeers builds a PolicyRunner with N peers pre-populated.
func runnerWithPeers(t *testing.T, cp *CompiledPolicy, peerIDs ...uint32) *PolicyRunner {
t.Helper()
pr := NewPolicyRunner(uniqueNetID(), cp, &fakeRuntime{})
pr.mu.Lock()
for i, id := range peerIDs {
pr.peers[id] = &managedPeer{
NodeID: id,
AddedAt: time.Now().Add(time.Duration(-i) * time.Minute),
}
}
pr.recentlyEvicted = map[uint32]time.Time{}
pr.mu.Unlock()
return pr
}
func TestExecuteEvict_RemovesPeer(t *testing.T) {
t.Parallel()
cp := makeCyclePolicy(t, []Action{{Type: ActionEvict}})
pr := runnerWithPeers(t, cp, 1, 2, 3)
ctx := map[string]interface{}{"peer_id": int(2)}
pr.executeEvict(ctx)
pr.mu.RLock()
defer pr.mu.RUnlock()
if _, ok := pr.peers[2]; ok {
t.Error("peer 2 should be evicted")
}
if _, ok := pr.recentlyEvicted[2]; !ok {
t.Error("peer 2 should be in recentlyEvicted")
}
}
func TestExecuteEvict_ZeroPeerIDIsNoop(t *testing.T) {
t.Parallel()
cp := makeCyclePolicy(t, []Action{{Type: ActionEvict}})
pr := runnerWithPeers(t, cp, 1)
pr.executeEvict(map[string]interface{}{"peer_id": int(0)})
pr.mu.RLock()
defer pr.mu.RUnlock()
if _, ok := pr.peers[1]; !ok {
t.Error("peer 1 should NOT be evicted (peer_id=0)")
}
}
func TestExecutePrune_RemovesNOldest(t *testing.T) {
t.Parallel()
cp := makeCyclePolicy(t, []Action{
{Type: ActionPrune, Params: map[string]interface{}{"count": float64(2), "by": "age"}},
})
pr := runnerWithPeers(t, cp, 1, 2, 3, 4, 5)
pr.executePrune(Directive{
Type: DirectivePrune,
Rule: "cycle-rule",
Params: map[string]interface{}{"count": float64(2), "by": "age"},
})
pr.mu.RLock()
defer pr.mu.RUnlock()
if len(pr.peers) != 3 {
t.Errorf("peers len = %d, want 3 (pruned 2)", len(pr.peers))
}
}
func TestExecuteEvictWhere_MatchAll(t *testing.T) {
t.Parallel()
cp := makeCyclePolicy(t, []Action{
{Type: ActionEvictWhere, Params: map[string]interface{}{"match": "true"}},
})
pr := runnerWithPeers(t, cp, 10, 20, 30)
pr.executeEvictWhere(Directive{
Type: DirectiveEvictWhere,
Rule: "cycle-rule",
ActionIdx: 0,
}, 0)
pr.mu.RLock()
defer pr.mu.RUnlock()
if len(pr.peers) != 0 {
t.Errorf("peers len = %d, want 0 (all evicted)", len(pr.peers))
}
}
func TestExecuteEvictWhere_MatchNone(t *testing.T) {
t.Parallel()
cp := makeCyclePolicy(t, []Action{
{Type: ActionEvictWhere, Params: map[string]interface{}{"match": "false"}},
})
pr := runnerWithPeers(t, cp, 10, 20)
pr.executeEvictWhere(Directive{
Type: DirectiveEvictWhere,
Rule: "cycle-rule",
ActionIdx: 0,
}, 0)
pr.mu.RLock()
defer pr.mu.RUnlock()
if len(pr.peers) != 2 {
t.Errorf("peers len = %d, want 2 (no match)", len(pr.peers))
}
}
func TestExecuteTag_AddAndRemove(t *testing.T) {
t.Parallel()
cp := makeCyclePolicy(t, []Action{
{Type: ActionTag, Params: map[string]interface{}{"add": []interface{}{"flagged"}}},
})
pr := runnerWithPeers(t, cp, 0xCAFE)
pr.executeTag(Directive{
Type: DirectiveTag,
Rule: "cycle-rule",
Params: map[string]interface{}{"add": []interface{}{"flagged", "later"}},
}, map[string]interface{}{"peer_id": int(0xCAFE)})
pr.mu.RLock()
tags := pr.peers[0xCAFE].tags()
pr.mu.RUnlock()
if len(tags) != 2 {
t.Errorf("tags = %v, want 2", tags)
}
// Remove one tag.
pr.executeTag(Directive{
Type: DirectiveTag,
Rule: "cycle-rule",
Params: map[string]interface{}{"remove": []interface{}{"later"}},
}, map[string]interface{}{"peer_id": int(0xCAFE)})
pr.mu.RLock()
defer pr.mu.RUnlock()
tags2 := pr.peers[0xCAFE].tags()
if len(tags2) != 1 || tags2[0] != "flagged" {
t.Errorf("after remove: tags = %v, want [flagged]", tags2)
}
}
func TestExecuteTag_ZeroPeerIDIsNoop(t *testing.T) {
t.Parallel()
cp := makeCyclePolicy(t, []Action{{Type: ActionTag, Params: map[string]interface{}{"add": []interface{}{"x"}}}})
pr := runnerWithPeers(t, cp, 1)
pr.executeTag(Directive{Params: map[string]interface{}{"add": []interface{}{"x"}}},
map[string]interface{}{"peer_id": int(0)})
}
func TestExecuteTag_AbsentPeerIsNoop(t *testing.T) {
t.Parallel()
cp := makeCyclePolicy(t, []Action{{Type: ActionTag, Params: map[string]interface{}{"add": []interface{}{"x"}}}})
pr := runnerWithPeers(t, cp, 1)
pr.executeTag(Directive{Params: map[string]interface{}{"add": []interface{}{"x"}}},
map[string]interface{}{"peer_id": int(9999)})
}
// TestEvaluateActions_DispatchesAllDirectiveTypes drives EvaluateActions
// through a cycle event so every switch case in the executor table is
// covered.
func TestEvaluateActions_DispatchesAllDirectiveTypes(t *testing.T) {
t.Parallel()
cp := makeCyclePolicy(t, []Action{
{Type: ActionLog, Params: map[string]interface{}{"message": "hi"}},
{Type: ActionWebhook, Params: map[string]interface{}{"event": "test"}},
})
pr := runnerWithPeers(t, cp, 1)
pr.EvaluateActions(EventCycle, map[string]interface{}{
"peer_id": int(1),
"network_id": int(1),
"local_tags": []string{},
"peer_tags": []string{},
"peer_age_s": 0.0,
"members": 1,
})
}
// TestRankTrustLinks drives the ranking helper directly.
func TestRankTrustLinks(t *testing.T) {
t.Parallel()
t1 := time.Now().Add(-3 * time.Hour)
t2 := time.Now().Add(-time.Hour)
records := []TrustRecord{
{NodeID: 1, ApprovedAt: t2},
{NodeID: 2, ApprovedAt: t1},
{NodeID: 3, ApprovedAt: t2.Add(time.Minute)},
}
pr := &PolicyRunner{}
ranked := pr.rankTrustLinks(records, "age")
if len(ranked) != 3 {
t.Fatalf("len = %d, want 3", len(ranked))
}
// Oldest first by ApprovedAt: record with t1 (NodeID 2) comes first.
if ranked[0].NodeID != 2 {
t.Errorf("first = %d, want 2 (oldest)", ranked[0].NodeID)
}
}