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
package requester
import (
"context"
"errors"
"testing"
"time"
commonv1 "github.com/johnandersen777/socialweb-computer-kcp/api/common/v1alpha1"
"github.com/johnandersen777/socialweb-computer-kcp/api/v1alpha1"
"github.com/johnandersen777/socialweb-computer-kcp/internal/policyengine/policytest"
)
func recordedContract(h *policytest.Harness) *v1alpha1.NodeClaimContract {
text, _ := PolicyWorkflowText(DefaultPolicyWorkflow)
policyRef := h.World.PolicyRecord(policytest.Requester, "policy-recorded", DefaultPolicyWorkflow, text)
rfp := h.World.RFP(policytest.Requester, "rfp-recorded", policyRef)
bid := h.World.Bid(policytest.Bidder, "bid-recorded", true, 0)
return &v1alpha1.NodeClaimContract{
Winner: &v1alpha1.ContractWinner{DID: policytest.Bidder},
Policy: &commonv1.StrongRef{URI: policyRef.URI, CID: policyRef.CID},
RFP: &commonv1.StrongRef{URI: rfp.URI, CID: rfp.CID},
Bid: &commonv1.StrongRef{URI: bid.URI, CID: bid.CID},
}
}
func TestRequesterRecordedWinnerRelease(t *testing.T) {
t.Parallel()
for _, tc := range []struct {
name string
setup func(h *policytest.Harness)
ran bool
release bool
allow bool
}{
{"completed-allow-resumes", func(h *policytest.Harness) { h.World.Vouch(policytest.Requester, policytest.Bidder) }, true, false, true},
{"completed-deny-releases", func(h *policytest.Harness) {}, true, true, false},
{"failed-run-keeps", func(h *policytest.Harness) {
h.World.Vouch(policytest.Requester, policytest.Bidder)
h.Sandbox.SetMode(policytest.SandboxUnfenced)
}, true, false, false},
{"cancelled-run-keeps", func(h *policytest.Harness) {
h.World.Vouch(policytest.Requester, policytest.Bidder)
h.Sandbox.SetMode(policytest.SandboxHung)
started := h.Sandbox.HungStarted()
go func() {
<-started
h.Runner.CancelAll()
}()
}, true, false, false},
{"wait-error-keeps", func(h *policytest.Harness) {
h.World.Vouch(policytest.Requester, policytest.Bidder)
h.Timeout = 2 * time.Second
h.Sandbox.SetMode(policytest.SandboxHung)
}, true, false, false},
{"run-not-created-keeps", func(h *policytest.Harness) {
h.World.Vouch(policytest.Requester, policytest.Bidder)
h.FailResolve(errors.New("no policy engine on the bus"))
}, false, false, false},
} {
for _, site := range []string{"resumed-contract", "held-session"} {
t.Run("always-run/requester-"+site+"/default/full/"+tc.name, func(t *testing.T) {
t.Parallel()
h := policytest.New(t)
state := recordedContract(h)
tc.setup(h)
q := specRequester(t, h, "")
mark := h.Runner.Mark()
var err error
if site == "held-session" {
q.contracts.Put("held", v1alpha1.ComputeContract{Status: v1alpha1.ComputeContractStatus{Contract: state}})
err = q.heldSessionAllowed(context.Background(), "held", "")
} else {
err = q.recordedWinnerAllowed(context.Background(), state, "")
}
runs := h.Runner.Since(mark)
if tc.ran && len(runs) != 1 {
t.Fatalf("want one policy run for the recorded winner, got %d", len(runs))
}
if !tc.ran && len(runs) != 0 {
t.Fatalf("a run was recorded although the engine refused it")
}
if tc.ran && runs[0].Workflow != state.Policy.URI {
t.Fatalf("the recorded winner ran %s, want the attached policy %s", runs[0].Workflow, state.Policy.URI)
}
switch {
case tc.allow && err != nil:
t.Fatalf("an allowed recorded winner was refused: %v", err)
case !tc.allow && err == nil:
t.Fatalf("the recorded winner went ahead without an allow")
case !tc.allow && errors.Is(err, ErrPolicyRejected) != tc.release:
t.Fatalf("release=%t for %s, want %t: %v", errors.Is(err, ErrPolicyRejected), tc.name, tc.release, err)
}
})
}
}
}