65 lines
1.7 KiB
Go
65 lines
1.7 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}, "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 TestFromTaskMatchesBootstrapNormalization(t *testing.T) {
|
|
ctx, err := structpb.NewStruct(map[string]any{
|
|
"repository": "panxiao81/postgresql-tenant-operator",
|
|
"job": "Run on Ubuntu",
|
|
})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
got, err := FromTask(&runnerv1.Task{Context: ctx}, "ddupan.top")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
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)
|
|
}
|
|
}
|
|
|
|
func TestFromTaskRejectsIncompleteServerContext(t *testing.T) {
|
|
for _, fields := range []map[string]any{
|
|
{"repository": "invalid", "job": "test"},
|
|
{"repository": "owner/repo", "job": ""},
|
|
} {
|
|
ctx, err := structpb.NewStruct(fields)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := FromTask(&runnerv1.Task{Context: ctx}, "ddupan.top"); err == nil {
|
|
t.Fatalf("expected invalid task context to fail: %#v", fields)
|
|
}
|
|
}
|
|
}
|