Files
fission-src/pkg/utils/namespace_manager_model_test.go
T

89 lines
2.5 KiB
Go

package utils
import (
"testing"
"time"
)
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")
}
}