110 lines
3.2 KiB
Go
110 lines
3.2 KiB
Go
package utils
|
|
|
|
import (
|
|
"testing"
|
|
"time"
|
|
|
|
corev1 "k8s.io/api/core/v1"
|
|
)
|
|
|
|
func TestNamespaceRecordClone(t *testing.T) {
|
|
now := time.Now().UTC()
|
|
record := NamespaceRecord{
|
|
Name: "tenant-a",
|
|
Source: NamespaceSourceWatcher,
|
|
Phase: NamespacePhaseRegistering,
|
|
Generation: 7,
|
|
UpdatedAt: now,
|
|
Labels: map[string]string{
|
|
"fission.io/managed": "true",
|
|
},
|
|
RegisteredParts: map[string]NamespacePartState{
|
|
"router": {
|
|
State: "active",
|
|
UpdatedAt: now,
|
|
},
|
|
},
|
|
}
|
|
|
|
clone := record.Clone()
|
|
clone.Labels["fission.io/managed"] = "false"
|
|
clone.RegisteredParts["router"] = NamespacePartState{State: "failed", LastError: "boom"}
|
|
|
|
if record.Labels["fission.io/managed"] != "true" {
|
|
t.Fatalf("expected labels to be deep copied")
|
|
}
|
|
|
|
if record.RegisteredParts["router"].State != "active" {
|
|
t.Fatalf("expected registered parts to be deep copied")
|
|
}
|
|
}
|
|
|
|
func TestNamespaceRecordIsActive(t *testing.T) {
|
|
record := NamespaceRecord{Phase: NamespacePhaseActive}
|
|
if !record.IsActive() {
|
|
t.Fatalf("expected active phase to be reported as active")
|
|
}
|
|
|
|
record.Phase = NamespacePhaseRegistering
|
|
if record.IsActive() {
|
|
t.Fatalf("expected non-active phase to be reported as inactive")
|
|
}
|
|
}
|
|
|
|
func TestNamespaceRecordIsTerminal(t *testing.T) {
|
|
for _, test := range []struct {
|
|
phase NamespacePhase
|
|
expected bool
|
|
}{
|
|
{phase: NamespacePhaseRemoved, expected: true},
|
|
{phase: NamespacePhaseFailed, expected: true},
|
|
{phase: NamespacePhaseActive, expected: false},
|
|
{phase: NamespacePhaseRegistering, expected: false},
|
|
} {
|
|
record := NamespaceRecord{Phase: test.phase}
|
|
if record.IsTerminal() != test.expected {
|
|
t.Fatalf("expected terminal=%v for phase %s", test.expected, test.phase)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestNewNamespaceEventClonesLabels(t *testing.T) {
|
|
now := time.Now().UTC()
|
|
labels := map[string]string{ManagedNamespaceLabelKey: ManagedNamespaceLabelValue}
|
|
event := NewNamespaceEvent(NamespaceEventAdd, "tenant-a", labels, NamespaceSourceWatcher, now)
|
|
labels[ManagedNamespaceLabelKey] = "false"
|
|
|
|
if event.Labels[ManagedNamespaceLabelKey] != ManagedNamespaceLabelValue {
|
|
t.Fatalf("expected namespace event labels to be cloned")
|
|
}
|
|
}
|
|
|
|
func TestManagedNamespaceEvent(t *testing.T) {
|
|
event := ManagedNamespaceEvent(NamespaceEventAdd, "tenant-a", NamespaceSourceWatcher, time.Now().UTC())
|
|
if event.Labels[ManagedNamespaceLabelKey] != ManagedNamespaceLabelValue {
|
|
t.Fatalf("expected managed namespace label in event")
|
|
}
|
|
if event.Name != "tenant-a" {
|
|
t.Fatalf("expected tenant-a event name")
|
|
}
|
|
}
|
|
|
|
func TestNamespaceEventFromNamespace(t *testing.T) {
|
|
namespace := &corev1.Namespace{}
|
|
namespace.Name = "tenant-a"
|
|
namespace.Labels = map[string]string{ManagedNamespaceLabelKey: ManagedNamespaceLabelValue}
|
|
|
|
event := NamespaceEventFromNamespace(NamespaceEventAdd, namespace, NamespaceSourceWatcher, time.Now().UTC())
|
|
if event.Name != "tenant-a" {
|
|
t.Fatalf("expected tenant-a event name")
|
|
}
|
|
if event.Labels[ManagedNamespaceLabelKey] != ManagedNamespaceLabelValue {
|
|
t.Fatalf("expected namespace labels to be copied into event")
|
|
}
|
|
|
|
namespace.Labels[ManagedNamespaceLabelKey] = "false"
|
|
if event.Labels[ManagedNamespaceLabelKey] != ManagedNamespaceLabelValue {
|
|
t.Fatalf("expected event labels to stay detached from original namespace")
|
|
}
|
|
}
|