Files

135 lines
4.8 KiB
Go

package judgement
import (
"context"
"errors"
"testing"
"time"
"google.golang.org/grpc"
gpb "github.com/samcharles93/archie-core/internal/contracts/guard/v1"
"github.com/samcharles93/archie-core/modules/connectors"
)
// fakeGuardClient answers Judge as a guard extension's client would.
type fakeGuardClient struct {
resp *gpb.JudgeResponse
err error
// hang blocks until the host's deadline ends the call.
hang bool
}
func (*fakeGuardClient) Configure(context.Context, *gpb.ConfigureRequest, ...grpc.CallOption) (*gpb.ConfigureResponse, error) {
return &gpb.ConfigureResponse{}, nil
}
func (*fakeGuardClient) Describe(context.Context, *gpb.DescribeRequest, ...grpc.CallOption) (*gpb.DescribeResponse, error) {
return &gpb.DescribeResponse{}, nil
}
func (c *fakeGuardClient) Judge(ctx context.Context, _ *gpb.JudgeRequest, _ ...grpc.CallOption) (*gpb.JudgeResponse, error) {
if c.hang {
<-ctx.Done()
return nil, ctx.Err()
}
return c.resp, c.err
}
type extensions []*connectors.Guard
func (e extensions) Guards() []*connectors.Guard { return e }
// TestGuardExtensionsDecideAtTheLowestCeiling guards the extension path of the
// weakest-link decision (spec section 5, R22): two guard extensions judging
// one action run it at the lower ceiling, a guard that errors, times out or
// answers without a ceiling holds it, an advisory verdict is recorded without
// setting the level, and every verdict names the package that answered.
func TestGuardExtensionsDecideAtTheLowestCeiling(t *testing.T) {
answer := func(c gpb.Ceiling) *gpb.JudgeResponse {
return &gpb.JudgeResponse{Ceiling: c, Findings: []*gpb.Finding{{Text: "seen", Evidence: "log", Strength: gpb.Strength_STRENGTH_STRONG}}}
}
guard := func(name string, advisory bool, c *fakeGuardClient) *connectors.Guard {
return connectors.NewGuard(name, "1.0.0", "digest-"+name, advisory, nil, nil, c)
}
tests := []struct {
name string
guards []*connectors.Guard
want Level
wantFailed string
}{
{
name: "the lower of two ceilings wins",
guards: []*connectors.Guard{
guard("hours", false, &fakeGuardClient{resp: answer(gpb.Ceiling_CEILING_ACT_AND_TELL)}),
guard("budget", false, &fakeGuardClient{resp: answer(gpb.Ceiling_CEILING_ESCALATE)}),
},
want: Escalate,
},
{
name: "a failing guard holds the action",
guards: []*connectors.Guard{
guard("hours", false, &fakeGuardClient{resp: answer(gpb.Ceiling_CEILING_ACT_QUIETLY)}),
guard("broken", false, &fakeGuardClient{err: errors.New("unavailable")}),
},
want: ProposeAndWait, wantFailed: "broken",
},
{
name: "a guard that outlasts the deadline holds the action",
guards: []*connectors.Guard{
guard("hours", false, &fakeGuardClient{resp: answer(gpb.Ceiling_CEILING_ACT_QUIETLY)}),
guard("slow", false, &fakeGuardClient{hang: true}),
},
want: ProposeAndWait, wantFailed: "slow",
},
{
name: "a guard answering without a ceiling holds the action",
guards: []*connectors.Guard{
guard("hours", false, &fakeGuardClient{resp: answer(gpb.Ceiling_CEILING_ACT_QUIETLY)}),
guard("partial", false, &fakeGuardClient{resp: &gpb.JudgeResponse{}}),
},
want: ProposeAndWait, wantFailed: "partial",
},
{
name: "an advisory verdict sets no level",
guards: []*connectors.Guard{
guard("hours", false, &fakeGuardClient{resp: answer(gpb.Ceiling_CEILING_ACT_QUIETLY)}),
guard("opinion", true, &fakeGuardClient{resp: answer(gpb.Ceiling_CEILING_NEVER)}),
},
want: ActQuietly,
},
{
name: "a failing advisory guard sets no level",
guards: []*connectors.Guard{
guard("hours", false, &fakeGuardClient{resp: answer(gpb.Ceiling_CEILING_ACT_AND_TELL)}),
guard("opinion", true, &fakeGuardClient{err: errors.New("unavailable")}),
},
want: ActAndTell, wantFailed: "opinion",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
m := &Module{timeout: 50 * time.Millisecond}
m.UseExtensions(extensions(tt.guards))
d := m.decide(t.Context(), Action{Connector: "c", Name: "delete"})
if d.Level != tt.want {
t.Fatalf("level = %s, want %s (verdicts %+v)", d.Level, tt.want, d.Verdicts)
}
if len(d.Verdicts) != len(tt.guards) {
t.Fatalf("verdicts = %d, want one per guard", len(d.Verdicts))
}
for i, v := range d.Verdicts {
if v.Guard != tt.guards[i].Name() || v.Digest != "digest-"+v.Guard || v.Version != "1.0.0" {
t.Fatalf("verdict %d = %+v, want the guard's name, version and package digest", i, v)
}
if (v.Failed != "") != (v.Guard == tt.wantFailed) {
t.Fatalf("verdict %q failed = %q, want failure only for %q", v.Guard, v.Failed, tt.wantFailed)
}
if v.Failed == "" && (len(v.Findings) != 1 || v.Findings[0].Strength != "strong" || v.Findings[0].Evidence != "log") {
t.Fatalf("verdict %q lost its findings: %+v", v.Guard, v.Findings)
}
}
})
}
}