Test for namespace selector

This commit is contained in:
Avi Huly
2022-11-15 11:11:11 +02:00
parent 82ee3ef3d1
commit 363fbd3b77
2 changed files with 128 additions and 3 deletions
+127 -2
View File
@@ -1,11 +1,13 @@
package controller
import (
"github.com/stakater/Reloader/internal/pkg/constants"
"context"
"os"
"testing"
"time"
"github.com/stakater/Reloader/internal/pkg/constants"
"github.com/stakater/Reloader/internal/pkg/metrics"
"github.com/sirupsen/logrus"
@@ -14,7 +16,10 @@ import (
"github.com/stakater/Reloader/internal/pkg/testutil"
"github.com/stakater/Reloader/internal/pkg/util"
"github.com/stakater/Reloader/pkg/kube"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/kubernetes/fake"
"k8s.io/client-go/tools/cache"
"k8s.io/client-go/util/workqueue"
)
@@ -40,7 +45,7 @@ func TestMain(m *testing.M) {
logrus.Infof("Creating controller")
for k := range kube.ResourceMap {
c, err := NewController(clients.KubernetesClient, k, namespace, []string{}, collectors)
c, err := NewController(clients.KubernetesClient, k, namespace, []string{}, map[string]string{}, collectors)
if err != nil {
logrus.Fatalf("%s", err)
}
@@ -2279,3 +2284,123 @@ func TestController_resourceInIgnoredNamespace(t *testing.T) {
})
}
}
func TestController_resourceInNamespaceSelector(t *testing.T) {
type fields struct {
indexer cache.Indexer
queue workqueue.RateLimitingInterface
informer cache.Controller
namespace v1.Namespace
namespaceSelector util.Map
}
type args struct {
raw interface{}
}
tests := []struct {
name string
fields fields
args args
want bool
}{
{
name: "TestConfigMapResourceInNamespaceSelector",
fields: fields{
namespaceSelector: util.Map{
"select": "this",
"select2": "this2",
},
namespace: v1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: "selected-namespace",
Labels: map[string]string{
"select": "this",
"select2": "this2",
},
},
},
},
args: args{
raw: testutil.GetConfigmap("selected-namespace", "testcm", "test"),
},
want: true,
}, {
name: "TestConfigMapResourceNotInNamespaceSelector",
fields: fields{
namespaceSelector: util.Map{
"select": "this",
"select2": "this2",
},
namespace: v1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: "not-selected-namespace",
Labels: map[string]string{},
},
},
},
args: args{
raw: testutil.GetConfigmap("not-selected-namespace", "testcm", "test"),
},
want: false,
},
{
name: "TestSecretResourceInNamespaceSelector",
fields: fields{
namespaceSelector: util.Map{
"select": "this",
"select2": "this2",
},
namespace: v1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: "selected-namespace",
Labels: map[string]string{
"select": "this",
"select2": "this2",
},
},
},
},
args: args{
raw: testutil.GetSecret("selected-namespace", "testsecret", "test"),
},
want: true,
}, {
name: "TestSecretResourceNotInNamespaceSelector",
fields: fields{
namespaceSelector: util.Map{
"select": "this",
"select2": "this2",
},
namespace: v1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: "not-selected-namespace",
Labels: map[string]string{},
},
},
},
args: args{
raw: testutil.GetSecret("not-selected-namespace", "secret", "test"),
},
want: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
fakeClient := fake.NewSimpleClientset()
fakeClient.CoreV1().Namespaces().Create(context.Background(), &tt.fields.namespace, metav1.CreateOptions{})
c := &Controller{
client: fakeClient,
indexer: tt.fields.indexer,
queue: tt.fields.queue,
informer: tt.fields.informer,
namespace: tt.fields.namespace.ObjectMeta.Name,
namespaceSelector: tt.fields.namespaceSelector,
}
if got := c.resourceInNamespaceSelector(tt.args.raw); got != tt.want {
t.Errorf("Controller.resourceInNamespaceSelector() = %v, want %v", got, tt.want)
}
})
}
}
+1 -1
View File
@@ -119,7 +119,7 @@ func TestRunLeaderElectionWithControllers(t *testing.T) {
t.Logf("Creating controller")
var controllers []*controller.Controller
for k := range kube.ResourceMap {
c, err := controller.NewController(testutil.Clients.KubernetesClient, k, testutil.Namespace, []string{}, metrics.NewCollectors())
c, err := controller.NewController(testutil.Clients.KubernetesClient, k, testutil.Namespace, []string{}, map[string]string{}, metrics.NewCollectors())
if err != nil {
logrus.Fatalf("%s", err)
}