固定 workflow job key 身份契约
This commit is contained in:
@@ -10,13 +10,17 @@ import (
|
||||
func TestFromTaskUsesFetchedContext(t *testing.T) {
|
||||
ctx, err := structpb.NewStruct(map[string]any{
|
||||
"repository": "panxiao81/gitea-dynamic-runner",
|
||||
"job": "publish-images",
|
||||
"job": "Publish images",
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
got, err := FromTask(&runnerv1.Task{Id: 900, Context: ctx}, "ddupan.top")
|
||||
got, err := FromTask(&runnerv1.Task{
|
||||
Id: 900,
|
||||
Context: ctx,
|
||||
WorkflowPayload: []byte("jobs:\n publish-images:\n runs-on: [self-hosted, vm]\n steps: []\n"),
|
||||
}, "ddupan.top")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -29,35 +33,44 @@ func TestFromTaskUsesFetchedContext(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestFromTaskMatchesBootstrapNormalization(t *testing.T) {
|
||||
ctx, err := structpb.NewStruct(map[string]any{
|
||||
"repository": "panxiao81/postgresql-tenant-operator",
|
||||
"job": "Run on Ubuntu",
|
||||
})
|
||||
func TestFromTaskRejectsUnsafeWorkflowJobKey(t *testing.T) {
|
||||
ctx, err := structpb.NewStruct(map[string]any{"repository": "owner/repo"})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
task := &runnerv1.Task{
|
||||
Context: ctx,
|
||||
WorkflowPayload: []byte("jobs:\n 'Run on Ubuntu':\n runs-on: self-hosted\n steps: []\n"),
|
||||
}
|
||||
if _, err := FromTask(task, "ddupan.top"); err == nil {
|
||||
t.Fatal("expected unsafe job key to fail")
|
||||
}
|
||||
}
|
||||
|
||||
got, err := FromTask(&runnerv1.Task{Context: ctx}, "ddupan.top")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
func TestBackoffTaskSegmentIsStableAndCollisionResistant(t *testing.T) {
|
||||
got := BackoffTaskSegment("Run on Ubuntu")
|
||||
if got != "Run-on-Ubuntu-8b7cd4c244fb" {
|
||||
t.Fatalf("backoff segment = %q", got)
|
||||
}
|
||||
want := "spiffe://ddupan.top/ci/panxiao81/postgresql-tenant-operator/Run-on-Ubuntu-8b7cd4c244fb"
|
||||
if got.SPIFFEID != want {
|
||||
t.Fatalf("SPIFFE ID = %q, want %q", got.SPIFFEID, want)
|
||||
if got == BackoffTaskSegment("Run@on Ubuntu") {
|
||||
t.Fatal("different legacy names must not collide after slugging")
|
||||
}
|
||||
}
|
||||
|
||||
func TestFromTaskRejectsIncompleteServerContext(t *testing.T) {
|
||||
for _, fields := range []map[string]any{
|
||||
{"repository": "invalid", "job": "test"},
|
||||
{"repository": "owner/repo", "job": ""},
|
||||
{"repository": "invalid"},
|
||||
{"repository": ""},
|
||||
} {
|
||||
ctx, err := structpb.NewStruct(fields)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := FromTask(&runnerv1.Task{Context: ctx}, "ddupan.top"); err == nil {
|
||||
task := &runnerv1.Task{
|
||||
Context: ctx,
|
||||
WorkflowPayload: []byte("jobs:\n test:\n runs-on: self-hosted\n steps: []\n"),
|
||||
}
|
||||
if _, err := FromTask(task, "ddupan.top"); err == nil {
|
||||
t.Fatalf("expected invalid task context to fail: %#v", fields)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user