~james/core

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

import (
	"context"
	"strings"
	"testing"
	"time"

	"tangled.org/core/spindle/db"
	"tangled.org/core/spindle/models"
	"tangled.org/core/spindle/observability"
	"tangled.org/core/spindle/quota"
)

func TestStartWorkflows_RefusalExplainsItself(t *testing.T) {
	cases := []struct {
		name     string
		defaults quota.Defaults
		amounts  []string
		owner    bool
	}{
		{"per-workflow repo limit", quota.Defaults{quota.ScopeRepo: {quota.ResourceWorkflowVCPUs: 2}}, []string{"6 vCPUs", "2 vCPUs"}, false},
		{"per-workflow owner limit", quota.Defaults{quota.ScopeUser: {quota.ResourceWorkflowMemoryMiB: 4096}}, []string{"8 GiB of memory", "4 GiB"}, true},
		{"bigger than the whole repo limit", quota.Defaults{quota.ScopeRepo: {quota.ResourceVCPUs: 4}}, []string{"6 vCPUs", "4 vCPUs"}, false},
		{"bigger than the whole owner limit", quota.Defaults{quota.ScopeUser: {quota.ResourceDiskMiB: 10000}}, []string{"20 GiB of disk", "10000 MiB"}, true},
	}
	for _, tc := range cases {
		t.Run(tc.name, func(t *testing.T) {
			database := newTestDB(t)
			qm := quota.NewManager(db.NewQuotaStore(database, tc.defaults), 50*time.Millisecond, nil)
			t.Cleanup(qm.Close)
			metrics := observability.NewMetrics()
			ctx := observability.WithMetrics(context.Background(), metrics)
			eng := &mockResourceUsageReporter{mockEngine: &mockEngine{}, workflows: 1, vcpus: 6, memory: 8192, disk: 20480}
			wid := runMinutesWorkflow(t, qm, database, ctx, eng, "big")

			if st, err := database.GetStatus(wid); err != nil || st != models.StatusKindFailed {
				t.Fatalf("status = %q, %v, want failed", st, err)
			}
			msg := workflowError(t, database, wid)
			for _, want := range tc.amounts {
				if !strings.Contains(msg, want) {
					t.Errorf("status message %q doesn't say %q", msg, want)
				}
			}
			if strings.Contains(msg, "owner") != tc.owner || !strings.Contains(msg, "repository") {
				t.Errorf("status message %q blames the wrong one of repository and owner", msg)
			}
			for _, internal := range []string{"quota", "denied", "limit", "scope", "_mib", "vcpus", "workflow_"} {
				if strings.Contains(msg, internal) {
					t.Errorf("status message %q leaks %q", msg, internal)
				}
			}
			if !quotaDeniedRecorded(t, metrics) {
				t.Fatal("refusal was not attributed to quota_denied")
			}
		})
	}
}