109 lines
3.0 KiB
Go
109 lines
3.0 KiB
Go
|
|
package agent
|
||
|
|
|
||
|
|
import (
|
||
|
|
"testing"
|
||
|
|
|
||
|
|
corev1 "k8s.io/api/core/v1"
|
||
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||
|
|
)
|
||
|
|
|
||
|
|
func ptrBool(b bool) *bool { return &b }
|
||
|
|
|
||
|
|
func mkPod(name string, owner *metav1.OwnerReference, labels map[string]string, image string) *corev1.Pod {
|
||
|
|
p := &corev1.Pod{
|
||
|
|
ObjectMeta: metav1.ObjectMeta{Name: name, Labels: labels},
|
||
|
|
}
|
||
|
|
if owner != nil {
|
||
|
|
p.OwnerReferences = []metav1.OwnerReference{*owner}
|
||
|
|
}
|
||
|
|
if image != "" {
|
||
|
|
p.Spec.Containers = []corev1.Container{{Image: image}}
|
||
|
|
}
|
||
|
|
return p
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestDeriveAppName_DeploymentReplicaSet(t *testing.T) {
|
||
|
|
owner := &metav1.OwnerReference{
|
||
|
|
Kind: "ReplicaSet",
|
||
|
|
Name: "traefik-789df685f",
|
||
|
|
Controller: ptrBool(true),
|
||
|
|
}
|
||
|
|
pod := mkPod("traefik-789df685f-hqvfl", owner,
|
||
|
|
map[string]string{podTemplateHashLabel: "789df685f"}, "")
|
||
|
|
if got := deriveAppName(pod); got != "traefik" {
|
||
|
|
t.Fatalf("got %q, want %q", got, "traefik")
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestDeriveAppName_StatefulSet(t *testing.T) {
|
||
|
|
owner := &metav1.OwnerReference{
|
||
|
|
Kind: "StatefulSet",
|
||
|
|
Name: "gitea",
|
||
|
|
Controller: ptrBool(true),
|
||
|
|
}
|
||
|
|
pod := mkPod("gitea-0", owner, nil, "")
|
||
|
|
if got := deriveAppName(pod); got != "gitea" {
|
||
|
|
t.Fatalf("got %q, want %q", got, "gitea")
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestDeriveAppName_DaemonSet(t *testing.T) {
|
||
|
|
owner := &metav1.OwnerReference{
|
||
|
|
Kind: "DaemonSet",
|
||
|
|
Name: "flock-agent",
|
||
|
|
Controller: ptrBool(true),
|
||
|
|
}
|
||
|
|
pod := mkPod("flock-agent-abcde", owner, nil, "")
|
||
|
|
if got := deriveAppName(pod); got != "flock-agent" {
|
||
|
|
t.Fatalf("got %q, want %q", got, "flock-agent")
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestDeriveAppName_BarePod(t *testing.T) {
|
||
|
|
pod := mkPod("standalone", nil, nil, "")
|
||
|
|
if got := deriveAppName(pod); got != "standalone" {
|
||
|
|
t.Fatalf("got %q, want %q", got, "standalone")
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// TestDeriveAppName_RSWithoutTemplateHash — ReplicaSet owners that don't
|
||
|
|
// follow the standard "<deploy>-<hash>" naming convention (e.g. a custom
|
||
|
|
// controller) keep the RS name as-is. All replicas of that RS still align,
|
||
|
|
// which is the second-best correctness offer.
|
||
|
|
func TestDeriveAppName_RSWithoutTemplateHash(t *testing.T) {
|
||
|
|
owner := &metav1.OwnerReference{
|
||
|
|
Kind: "ReplicaSet",
|
||
|
|
Name: "weird-rs-name",
|
||
|
|
Controller: ptrBool(true),
|
||
|
|
}
|
||
|
|
pod := mkPod("weird-rs-name-xyz", owner, nil, "")
|
||
|
|
if got := deriveAppName(pod); got != "weird-rs-name" {
|
||
|
|
t.Fatalf("got %q, want %q", got, "weird-rs-name")
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestDeriveAppName_NonControllerOwnerIgnored(t *testing.T) {
|
||
|
|
// OwnerReference without Controller=true must be ignored — only the
|
||
|
|
// controller owner is the canonical workload.
|
||
|
|
owner := &metav1.OwnerReference{
|
||
|
|
Kind: "Foo",
|
||
|
|
Name: "irrelevant",
|
||
|
|
// Controller pointer left nil.
|
||
|
|
}
|
||
|
|
pod := mkPod("solo", owner, nil, "")
|
||
|
|
if got := deriveAppName(pod); got != "solo" {
|
||
|
|
t.Fatalf("got %q, want %q", got, "solo")
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestPodImageRef(t *testing.T) {
|
||
|
|
pod := mkPod("p", nil, nil, "traefik:v3.5")
|
||
|
|
if got := podImageRef(pod); got != "traefik:v3.5" {
|
||
|
|
t.Fatalf("got %q, want %q", got, "traefik:v3.5")
|
||
|
|
}
|
||
|
|
empty := mkPod("p", nil, nil, "")
|
||
|
|
if got := podImageRef(empty); got != "" {
|
||
|
|
t.Fatalf("got %q, want \"\"", got)
|
||
|
|
}
|
||
|
|
}
|