Skip to content

Commit

Permalink
test(namespace): add a couple more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
massix committed Aug 14, 2024
1 parent 14045cd commit 1bcca72
Showing 1 changed file with 96 additions and 13 deletions.
109 changes: 96 additions & 13 deletions internal/watcher/namespace_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package watcher_test
package watcher

import (
"context"
Expand All @@ -12,7 +12,6 @@ import (
typedcmc "github.com/massix/chaos-monkey/internal/apis/clientset/versioned"
fakecmc "github.com/massix/chaos-monkey/internal/apis/clientset/versioned/fake"
"github.com/massix/chaos-monkey/internal/configuration"
"github.com/massix/chaos-monkey/internal/watcher"
"github.com/sirupsen/logrus"
corev1 "k8s.io/api/core/v1"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -68,23 +67,107 @@ var cmcClientset = fakecmc.NewSimpleClientset()

func TestNamespaceWatcher_Create(t *testing.T) {
metricsClientset := fakemetricsv.NewSimpleClientset()
w := watcher.NewNamespaceWatcher(kubernetes.NewSimpleClientset(), cmcClientset, metricsClientset, record.NewFakeRecorder(1024), "chaos-monkey", configuration.BehaviorAllowAll)
w := NewNamespaceWatcher(kubernetes.NewSimpleClientset(), cmcClientset, metricsClientset, record.NewFakeRecorder(1024), "chaos-monkey", configuration.BehaviorAllowAll)
defer w.Close()

if w.IsRunning() {
t.Errorf("Watcher should not be running")
}
}

func TestNamespaceWatcher_IsNamespaceAllowed(t *testing.T) {
genNamespace := func(label string) *corev1.Namespace {
if label == "" {
return &corev1.Namespace{
ObjectMeta: v1.ObjectMeta{
Labels: map[string]string{},
},
}
} else {
return &corev1.Namespace{
ObjectMeta: v1.ObjectMeta{
Labels: map[string]string{
configuration.NamespaceLabel: label,
},
},
}
}
}

nsWithoutLabel := genNamespace("")
nsWithOkLabel := genNamespace("true")
nsWithKoLabel := genNamespace("false")

t.Run("BehaviorAllowAll", func(t *testing.T) {
ns := &NamespaceWatcher{
Behavior: configuration.BehaviorAllowAll,
}

if ns.IsNamespaceAllowed(nsWithoutLabel) != true {
t.Error("should be true")
}

if ns.IsNamespaceAllowed(nsWithOkLabel) != true {
t.Error("should be true")
}

if ns.IsNamespaceAllowed(nsWithKoLabel) != false {
t.Error("should be false")
}
})

t.Run("BehaviorDenyAll", func(t *testing.T) {
ns := &NamespaceWatcher{
Behavior: configuration.BehaviorDenyAll,
}

if ns.IsNamespaceAllowed(nsWithoutLabel) != false {
t.Error("should be false")
}

if ns.IsNamespaceAllowed(nsWithOkLabel) != true {
t.Error("should be true")
}

if ns.IsNamespaceAllowed(nsWithKoLabel) != false {
t.Error("should be false")
}
})
}

func TestNamespaceWatcher_RestartWatch(t *testing.T) {
ns := &NamespaceWatcher{
NamespaceInterface: kubernetes.NewSimpleClientset().CoreV1().Namespaces(),
Mutex: &sync.Mutex{},
Logrus: logrus.New(),
CrdWatchers: map[string]Watcher{
"w1": &FakeCrdWatcher{Mutex: &sync.Mutex{}},
"w2": &FakeCrdWatcher{Mutex: &sync.Mutex{}},
"w3": &FakeCrdWatcher{Mutex: &sync.Mutex{}},
},
}

t.Run("Cleans the watchers properly", func(t *testing.T) {
wg := &sync.WaitGroup{}
if _, err := ns.restartWatch(context.TODO(), wg); err != nil {
t.Errorf("received error: %s", err)
}

if l := len(ns.CrdWatchers); l != 0 {
t.Errorf("still %d watchers remaining", l)
}
})
}

func TestNamespaceWatcher_BasicBehaviour(t *testing.T) {
logrus.SetLevel(logrus.DebugLevel)
clientSet := kubernetes.NewSimpleClientset()
metricsClientset := fakemetricsv.NewSimpleClientset()
w := watcher.NewNamespaceWatcher(clientSet, cmcClientset, metricsClientset, record.NewFakeRecorder(1024), "chaos-monkey", configuration.BehaviorAllowAll)
w := NewNamespaceWatcher(clientSet, cmcClientset, metricsClientset, record.NewFakeRecorder(1024), "chaos-monkey", configuration.BehaviorAllowAll)
w.CleanupTimeout = 1 * time.Second

// Inject my CRD Factory
watcher.DefaultCrdFactory = func(k.Interface, typedcmc.Interface, metricsv.Interface, record.EventRecorderLogger, string) watcher.Watcher {
DefaultCrdFactory = func(k.Interface, typedcmc.Interface, metricsv.Interface, record.EventRecorderLogger, string) Watcher {
return &FakeCrdWatcher{Mutex: &sync.Mutex{}}
}

Expand Down Expand Up @@ -166,7 +249,7 @@ func TestNamespaceWatcher_Error(t *testing.T) {
logrus.SetLevel(logrus.DebugLevel)
clientset := kubernetes.NewSimpleClientset()
metricsClientset := fakemetricsv.NewSimpleClientset()
w := watcher.NewNamespaceWatcher(clientset, cmcClientset, metricsClientset, record.NewFakeRecorder(1024), "chaos-monkey", configuration.BehaviorAllowAll)
w := NewNamespaceWatcher(clientset, cmcClientset, metricsClientset, record.NewFakeRecorder(1024), "chaos-monkey", configuration.BehaviorAllowAll)

clientset.PrependWatchReactor("namespaces", func(action ktest.Action) (handled bool, ret watch.Interface, err error) {
fakeWatch := watch.NewFake()
Expand Down Expand Up @@ -207,11 +290,11 @@ func TestNamespaceWatcher_Cleanup(t *testing.T) {
logrus.SetLevel(logrus.DebugLevel)
clientset := kubernetes.NewSimpleClientset()
metricsClientset := fakemetricsv.NewSimpleClientset()
w := watcher.NewNamespaceWatcher(clientset, cmcClientset, metricsClientset, record.NewFakeRecorder(1024), "chaos-monkey", configuration.BehaviorAllowAll)
w := NewNamespaceWatcher(clientset, cmcClientset, metricsClientset, record.NewFakeRecorder(1024), "chaos-monkey", configuration.BehaviorAllowAll)
w.CleanupTimeout = 1 * time.Second

// Add some fake watchers
w.CrdWatchers = map[string]watcher.Watcher{
w.CrdWatchers = map[string]Watcher{
"test-1": &FakeCrdWatcher{Mutex: &sync.Mutex{}, Running: true},
"test-2": &FakeCrdWatcher{Mutex: &sync.Mutex{}, Running: false},
"test-3": &FakeCrdWatcher{Mutex: &sync.Mutex{}, Running: false},
Expand Down Expand Up @@ -261,12 +344,12 @@ func TestNamespaceWatcher_Cleanup(t *testing.T) {
func TestNamespaceWatcher_RestartWatcher(t *testing.T) {
clientset := kubernetes.NewSimpleClientset()
metricsClientset := fakemetricsv.NewSimpleClientset()
w := watcher.NewNamespaceWatcher(clientset, cmcClientset, metricsClientset, record.NewFakeRecorder(1024), "chaos-monkey", configuration.BehaviorAllowAll)
w := NewNamespaceWatcher(clientset, cmcClientset, metricsClientset, record.NewFakeRecorder(1024), "chaos-monkey", configuration.BehaviorAllowAll)
w.CleanupTimeout = 1 * time.Second
timeAsked := &atomic.Int32{}
timeAsked.Store(0)

watcher.DefaultCrdFactory = func(clientset k.Interface, cmcClientset typedcmc.Interface, _ metricsv.Interface, recorder record.EventRecorderLogger, namespace string) watcher.Watcher {
DefaultCrdFactory = func(clientset k.Interface, cmcClientset typedcmc.Interface, _ metricsv.Interface, recorder record.EventRecorderLogger, namespace string) Watcher {
return &FakeCrdWatcher{Mutex: &sync.Mutex{}}
}

Expand Down Expand Up @@ -317,7 +400,7 @@ func TestNamespaceWatcher_ModifyNamespace(t *testing.T) {
return true, fakeWatch, nil
})

watcher.DefaultCrdFactory = func(clientset k.Interface, cmcClientset typedcmc.Interface, _ metricsv.Interface, recorder record.EventRecorderLogger, namespace string) watcher.Watcher {
DefaultCrdFactory = func(clientset k.Interface, cmcClientset typedcmc.Interface, _ metricsv.Interface, recorder record.EventRecorderLogger, namespace string) Watcher {
return &FakeCrdWatcher{Mutex: &sync.Mutex{}}
}

Expand All @@ -332,7 +415,7 @@ func TestNamespaceWatcher_ModifyNamespace(t *testing.T) {
}

metricsClientset := fakemetricsv.NewSimpleClientset()
w := watcher.NewNamespaceWatcher(clientset, nil, metricsClientset, record.NewFakeRecorder(1024), "chaosmonkey", configuration.BehaviorAllowAll)
w := NewNamespaceWatcher(clientset, nil, metricsClientset, record.NewFakeRecorder(1024), "chaosmonkey", configuration.BehaviorAllowAll)
w.WatcherTimeout = 24 * time.Hour
w.CleanupTimeout = 300 * time.Millisecond

Expand Down Expand Up @@ -397,7 +480,7 @@ func TestNamespaceWatcher_ModifyNamespace(t *testing.T) {
// Change the behavior to "DenyAll" and reset the watcher
w.Mutex.Lock()
w.Behavior = configuration.BehaviorDenyAll
w.CrdWatchers = map[string]watcher.Watcher{}
w.CrdWatchers = map[string]Watcher{}
w.Mutex.Unlock()

t.Run("DenyAll", func(t *testing.T) {
Expand Down

0 comments on commit 1bcca72

Please sign in to comment.