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")
}
})
}
}