feat: bootstrap official runner through SPIFFE facade
This commit is contained in:
@@ -0,0 +1,74 @@
|
||||
package runnerbootstrap
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"testing"
|
||||
|
||||
"git.ddupan.top/panxiao81/gitea-dynamic-runner/internal/runnerfacade"
|
||||
"git.ddupan.top/panxiao81/gitea-dynamic-runner/internal/taskassignment"
|
||||
"git.ddupan.top/panxiao81/gitea-dynamic-runner/internal/taskidentity"
|
||||
)
|
||||
|
||||
func testBootstrap(t *testing.T) Bootstrap {
|
||||
t.Helper()
|
||||
capabilities, err := runnerfacade.NewCapabilities([]byte("0123456789abcdef0123456789abcdef"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return Bootstrap{
|
||||
Capabilities: capabilities,
|
||||
FacadeURL: "https://runner-facade.gitea-actions.svc:8443",
|
||||
FacadeSPIFFEID: "spiffe://ddupan.top/ns/gitea-actions/sa/gitea-dynamic-runner",
|
||||
}
|
||||
}
|
||||
|
||||
func testAssignment() taskassignment.Assignment {
|
||||
return taskassignment.Assignment{
|
||||
ID: "gitea-task-42", Backend: taskassignment.BackendPod,
|
||||
Identity: taskidentity.Identity{SPIFFEID: "spiffe://ddupan.top/ci/owner/repo/publish"},
|
||||
}
|
||||
}
|
||||
|
||||
func TestEnvironmentIsDeterministicAndAssignmentScoped(t *testing.T) {
|
||||
bootstrap := testBootstrap(t)
|
||||
first, err := bootstrap.Environment(testAssignment())
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
second, err := bootstrap.Environment(testAssignment())
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if first[EnvCapability] == "" || first[EnvCapability] != second[EnvCapability] {
|
||||
t.Fatalf("capabilities = %q, %q", first[EnvCapability], second[EnvCapability])
|
||||
}
|
||||
if first[EnvAssignmentID] != "gitea-task-42" || first[EnvSPIFFEID] != testAssignment().Identity.SPIFFEID {
|
||||
t.Fatalf("environment = %#v", first)
|
||||
}
|
||||
if first[EnvBackend] != "pod" || first[EnvFacadeID] == "" {
|
||||
t.Fatalf("environment = %#v", first)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRegistrationMatchesOfficialRunnerSchema(t *testing.T) {
|
||||
data, err := RegistrationJSON("gitea-task-42", "capability", "http://127.0.0.1:8080", taskassignment.BackendVM)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var registration Registration
|
||||
if err := json.Unmarshal(data, ®istration); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if registration.UUID != "gitea-task-42" || registration.Token != "capability" || registration.Address != "http://127.0.0.1:8080" || !registration.Ephemeral {
|
||||
t.Fatalf("registration = %#v", registration)
|
||||
}
|
||||
if len(registration.Labels) != 2 || registration.Labels[0] != "self-hosted" || registration.Labels[1] != "vm" {
|
||||
t.Fatalf("labels = %#v", registration.Labels)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRegistrationRejectsNonLocalTLSAddress(t *testing.T) {
|
||||
if _, err := RegistrationJSON("id", "capability", "https://facade.example", taskassignment.BackendPod); err == nil {
|
||||
t.Fatal("expected local proxy URL validation error")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user