Files
gitea-dynamic-runner/internal/taskidentity/identity_test.go
T

78 lines
2.1 KiB
Go

package taskidentity
import (
"testing"
runnerv1 "gitea.dev/actionslib/runner/v1"
"google.golang.org/protobuf/types/known/structpb"
)
func TestFromTaskUsesFetchedContext(t *testing.T) {
ctx, err := structpb.NewStruct(map[string]any{
"repository": "panxiao81/gitea-dynamic-runner",
"job": "Publish images",
})
if err != nil {
t.Fatal(err)
}
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)
}
if got.Repository != "panxiao81/gitea-dynamic-runner" || got.Task != "publish-images" {
t.Fatalf("unexpected task identity context: %#v", got)
}
want := "spiffe://ddupan.top/ci/panxiao81/gitea-dynamic-runner/publish-images"
if got.SPIFFEID != want {
t.Fatalf("SPIFFE ID = %q, want %q", got.SPIFFEID, want)
}
}
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")
}
}
func TestBackoffTaskSegmentIsStableAndCollisionResistant(t *testing.T) {
got := BackoffTaskSegment("Run on Ubuntu")
if got != "Run-on-Ubuntu-8b7cd4c244fb" {
t.Fatalf("backoff segment = %q", got)
}
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"},
{"repository": ""},
} {
ctx, err := structpb.NewStruct(fields)
if err != nil {
t.Fatal(err)
}
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)
}
}
}