实现 client-go Pod backend
This commit is contained in:
@@ -0,0 +1,72 @@
|
||||
package podbackend
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
corev1 "k8s.io/api/core/v1"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
"k8s.io/apimachinery/pkg/types"
|
||||
dynamicfake "k8s.io/client-go/dynamic/fake"
|
||||
"k8s.io/client-go/kubernetes/fake"
|
||||
)
|
||||
|
||||
func testClient(objects ...runtime.Object) *Client {
|
||||
return &Client{
|
||||
Kubernetes: fake.NewSimpleClientset(objects...),
|
||||
Dynamic: dynamicfake.NewSimpleDynamicClient(runtime.NewScheme()),
|
||||
}
|
||||
}
|
||||
|
||||
func TestClientPodLifecycleUsesTypedClient(t *testing.T) {
|
||||
client := testClient()
|
||||
created, err := client.CreatePod(context.Background(), PodManifest{
|
||||
Name: "gitea-task-42", Namespace: "gitea-actions",
|
||||
Labels: map[string]string{assignmentLabel: "gitea-task-42"},
|
||||
Image: "zot/ci-executor:main", ServiceAccount: "gitea-task-executor",
|
||||
Args: []string{"execute", "gitea-task-42"},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
pod, err := client.Kubernetes.CoreV1().Pods("gitea-actions").Get(context.Background(), created.Name, metav1.GetOptions{})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
pod.UID = types.UID("pod-uid")
|
||||
pod.Status.Phase = corev1.PodRunning
|
||||
if _, err := client.Kubernetes.CoreV1().Pods("gitea-actions").Update(context.Background(), pod, metav1.UpdateOptions{}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
pods, err := client.ListPods(context.Background(), "gitea-actions", assignmentLabel+"=gitea-task-42")
|
||||
if err != nil || len(pods) != 1 || pods[0].UID != "pod-uid" || pods[0].Phase != "Running" {
|
||||
t.Fatalf("pods=%#v err=%v", pods, err)
|
||||
}
|
||||
if err := client.DeletePod(context.Background(), "gitea-actions", created.Name); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestClientEnsuresIdempotentClusterStaticEntry(t *testing.T) {
|
||||
client := testClient()
|
||||
entry := IdentityEntry{
|
||||
Name: "gitea-task-42", Labels: map[string]string{assignmentLabel: "gitea-task-42"},
|
||||
ClassName: "spire-mgmt-spire",
|
||||
ParentID: "spiffe://ddupan.top/spire/agent/k8s_psat/homelab/pod/pod-uid",
|
||||
SPIFFEID: "spiffe://ddupan.top/ci/owner/repo/publish",
|
||||
Selectors: []string{"unix:uid:2000"},
|
||||
}
|
||||
if err := client.EnsureIdentityEntry(context.Background(), entry); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := client.EnsureIdentityEntry(context.Background(), entry); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := client.DeleteIdentityEntry(context.Background(), entry.Name); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := client.DeleteIdentityEntry(context.Background(), entry.Name); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user