Files
ayatori/internal/adapter/kubernetes/job_test.go
T
panxiao81 86effb72a8
Verify / test (pull_request) Successful in 7m25s
Verify / lint (pull_request) Successful in 8m9s
feat: execute jobs on Kubernetes
2026-09-18 18:20:10 +00:00

72 lines
3.1 KiB
Go

package kubernetes
import (
"testing"
executionv1alpha1 "git.ddupan.top/panxiao81/ayatori/api/execution/v1alpha1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
)
func TestBuildJob(t *testing.T) {
literal := "world"
cpuRequest := resource.MustParse("100m")
memoryLimit := resource.MustParse("128Mi")
//nolint:modernize // ObjectMeta is promoted through embedded TypeMeta; embedlit produces invalid Go here.
job := &executionv1alpha1.Job{
ObjectMeta: metav1.ObjectMeta{Name: "hello", Namespace: "ci", UID: types.UID("job-uid")},
Spec: executionv1alpha1.JobSpec{Task: executionv1alpha1.TaskSpec{
Image: "alpine:3.22", Command: []string{"echo"}, Args: []string{"hello"},
Env: []executionv1alpha1.EnvVar{
{Name: "TARGET", Value: &literal},
{Name: "TOKEN", ValueFrom: &executionv1alpha1.EnvVarSource{
//nolint:modernize // LocalObjectReference is an embedded Kubernetes API field.
SecretKeyRef: &corev1.SecretKeySelector{LocalObjectReference: corev1.LocalObjectReference{Name: "token"}, Key: "value"},
}},
},
}},
}
parameters := &executionv1alpha1.KubernetesExecutionParameters{Spec: executionv1alpha1.KubernetesExecutionParametersSpec{
ServiceAccountName: "runner", RuntimeClassName: "runc", ImagePullPolicy: corev1.PullIfNotPresent,
Scheduling: executionv1alpha1.KubernetesSchedulingParameters{NodeSelector: map[string]string{"role": "execution"}},
}}
resources := executionv1alpha1.ExecutionResourceRequirements{
Requests: executionv1alpha1.ResourceValues{CPU: &cpuRequest},
Limits: executionv1alpha1.ResourceValues{Memory: &memoryLimit},
}
backend := BuildJob(job, parameters, resources)
pod := backend.Spec.Template.Spec
if backend.Spec.BackoffLimit == nil || *backend.Spec.BackoffLimit != 0 {
t.Fatalf("backoffLimit = %v, want 0", backend.Spec.BackoffLimit)
}
if pod.RestartPolicy != corev1.RestartPolicyNever || pod.ServiceAccountName != "runner" {
t.Fatalf("unexpected pod execution policy: %#v", pod)
}
if pod.RuntimeClassName == nil || *pod.RuntimeClassName != "runc" {
t.Fatalf("runtimeClassName = %v, want runc", pod.RuntimeClassName)
}
container := pod.Containers[0]
if container.Resources.Requests.Cpu().Cmp(cpuRequest) != 0 || container.Resources.Limits.Memory().Cmp(memoryLimit) != 0 {
t.Fatalf("resources were not mapped: %#v", container.Resources)
}
if container.Env[1].ValueFrom == nil || container.Env[1].ValueFrom.SecretKeyRef.Name != "token" {
t.Fatalf("secret reference was not preserved: %#v", container.Env[1])
}
if backend.Labels[JobUIDLabel] != "job-uid" || backend.OwnerReferences[0].UID != job.UID {
t.Fatalf("ownership identity was not preserved: %#v", backend.ObjectMeta)
}
}
func TestValidateOwnership(t *testing.T) {
job := &executionv1alpha1.Job{}
job.UID = types.UID("expected")
backend := BuildJob(job, &executionv1alpha1.KubernetesExecutionParameters{}, executionv1alpha1.ExecutionResourceRequirements{})
backend.Labels[JobUIDLabel] = "different"
if err := ValidateOwnership(job, backend); err == nil {
t.Fatal("ValidateOwnership() succeeded for a different Job UID")
}
}