~james/core

core/spindle/engine/failure_metrics_test.go -rw-r--r-- 2.2 KiB
452f17b7 — james chore(import): snapshot james.dad.town/core at f70c71c7696a 16 hours ago
                                                                                
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
package engine

import (
	"errors"
	"testing"
)

func TestFailureAttribution(t *testing.T) {
	base := errors.New("detail")
	tests := []struct {
		name       string
		result     string
		err        error
		wantClass  string
		wantReason string
	}{
		{name: "success", result: "success", wantClass: "none", wantReason: "success"},
		{name: "timeout", result: "timeout", err: ErrTimedOut, wantClass: "user", wantReason: "timeout"},
		{name: "cancelled", result: "cancelled", err: ErrWorkflowCanceled, wantClass: "user", wantReason: "cancelled"},
		{
			name:       "user command",
			result:     "failure",
			err:        ClassifiedFailure(FailureClassUser, FailureReasonCommandFailed, base),
			wantClass:  "user",
			wantReason: "command_failed",
		},
		{
			name:       "infrastructure",
			result:     "failure",
			err:        ClassifiedFailure(FailureClassInfrastructure, FailureReasonRuntimeFailed, base),
			wantClass:  "infrastructure",
			wantReason: "runtime_failed",
		},
		{
			name:       "policy",
			result:     "failure",
			err:        ClassifiedFailure(FailureClassPolicy, FailureReasonQuotaDenied, base),
			wantClass:  "policy",
			wantReason: "quota_denied",
		},
		{name: "capacity", result: "failure", err: ErrNoWorkflowSlots, wantClass: "infrastructure", wantReason: "capacity_unavailable"},
		{name: "unclassified", result: "failure", err: base, wantClass: "infrastructure", wantReason: "runtime_failed"},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			gotClass, gotReason := FailureAttribution(tt.result, tt.err)
			if gotClass != tt.wantClass || gotReason != tt.wantReason {
				t.Fatalf("FailureAttribution() = (%q, %q), want (%q, %q)", gotClass, gotReason, tt.wantClass, tt.wantReason)
			}
		})
	}
}

func TestClassifiedFailurePreservesCauseAndFirstClassification(t *testing.T) {
	cause := errors.New("cause")
	first := ClassifiedFailure(FailureClassUser, FailureReasonCommandFailed, cause)
	second := ClassifiedFailure(FailureClassInfrastructure, FailureReasonRuntimeFailed, first)
	if second != first {
		t.Fatal("nested classification replaced the original leaf classification")
	}
	if !errors.Is(second, cause) {
		t.Fatal("classified failure does not preserve errors.Is")
	}
}