44 "context"
55 "fmt"
66 "slices"
7+ "strings"
78 "sync"
89 "testing"
910 "time"
@@ -43,7 +44,7 @@ func (m *mockRecordPoster) getRecords() []*deploymentrecord.DeploymentRecord {
4344
4445const testControllerNamespace = "test-controller-ns"
4546
46- func setup (t * testing.T , onlyNamepsace string , excludeNamespaces string ) (* kubernetes.Clientset , * mockRecordPoster ) {
47+ func setup (t * testing.T , onlyNamespace string , excludeNamespaces string ) (* kubernetes.Clientset , * mockRecordPoster ) {
4748 t .Helper ()
4849 testEnv := & envtest.Environment {}
4950
@@ -69,6 +70,24 @@ func setup(t *testing.T, onlyNamepsace string, excludeNamespaces string) (*kuber
6970 t .Fatalf ("failed to create namespace: %v" , err )
7071 }
7172
73+ if onlyNamespace != "" {
74+ ns = & corev1.Namespace {ObjectMeta : metav1.ObjectMeta {Name : onlyNamespace }}
75+ _ , err = clientset .CoreV1 ().Namespaces ().Create (ctx , ns , metav1.CreateOptions {})
76+ if err != nil {
77+ t .Fatalf ("failed to create onlyNamespace: %v" , err )
78+ }
79+ }
80+
81+ if excludeNamespaces != "" {
82+ for _ , nsName := range strings .Split (excludeNamespaces , "," ) {
83+ ns = & corev1.Namespace {ObjectMeta : metav1.ObjectMeta {Name : nsName }}
84+ _ , err = clientset .CoreV1 ().Namespaces ().Create (ctx , ns , metav1.CreateOptions {})
85+ if err != nil {
86+ t .Fatalf ("failed to create excludeNamespace %s: %v" , nsName , err )
87+ }
88+ }
89+ }
90+
7291 metadataClient , err := k8smetadata .NewForConfig (cfg )
7392 if err != nil {
7493 t .Fatalf ("failed to create Kubernetes metadata client: %v" , err )
@@ -79,7 +98,7 @@ func setup(t *testing.T, onlyNamepsace string, excludeNamespaces string) (*kuber
7998 ctrl , err := New (
8099 clientset ,
81100 metadataAggregator ,
82- onlyNamepsace ,
101+ onlyNamespace ,
83102 excludeNamespaces ,
84103 & Config {
85104 "{{namespace}}/{{deploymentName}}/{{containerName}}" ,
@@ -301,7 +320,7 @@ func TestControllerIntegration_KubernetesDeployment(t *testing.T) {
301320 assert .Equal (t , deploymentrecord .StatusDeployed , records [0 ].Status )
302321
303322 // Create another pod in replicaset; the dedup cache should prevent a new record as there is only one worker
304- // and no risk of multiple works processing before cache is set.
323+ // and no risk of multiple workers processing before cache is set.
305324 _ = makePod (t , clientset , []metav1.OwnerReference {{
306325 APIVersion : "apps/v1" ,
307326 Kind : "ReplicaSet" ,
@@ -398,13 +417,9 @@ func TestControllerIntegration_OnlyWatchOneNamespace(t *testing.T) {
398417 namespace2 := "namespace2"
399418 clientset , mock := setup (t , namespace1 , "" )
400419
401- ns1 := & corev1.Namespace {ObjectMeta : metav1.ObjectMeta {Name : namespace1 }}
402- _ , err := clientset .CoreV1 ().Namespaces ().Create (context .Background (), ns1 , metav1.CreateOptions {})
403- if err != nil {
404- t .Fatalf ("failed to create namespace: %v" , err )
405- }
420+ // Make invalid namespaces
406421 ns2 := & corev1.Namespace {ObjectMeta : metav1.ObjectMeta {Name : namespace2 }}
407- _ , err = clientset .CoreV1 ().Namespaces ().Create (context .Background (), ns2 , metav1.CreateOptions {})
422+ _ , err : = clientset .CoreV1 ().Namespaces ().Create (context .Background (), ns2 , metav1.CreateOptions {})
408423 if err != nil {
409424 t .Fatalf ("failed to create namespace: %v" , err )
410425 }
@@ -456,21 +471,12 @@ func TestControllerIntegration_ExcludeNamespaces(t *testing.T) {
456471 namespace3 := "namespace3"
457472 clientset , mock := setup (t , "" , fmt .Sprintf ("%s,%s" , namespace2 , namespace3 ))
458473
474+ // Make valid namespace
459475 ns1 := & corev1.Namespace {ObjectMeta : metav1.ObjectMeta {Name : namespace1 }}
460476 _ , err := clientset .CoreV1 ().Namespaces ().Create (context .Background (), ns1 , metav1.CreateOptions {})
461477 if err != nil {
462478 t .Fatalf ("failed to create namespace: %v" , err )
463479 }
464- ns2 := & corev1.Namespace {ObjectMeta : metav1.ObjectMeta {Name : namespace2 }}
465- _ , err = clientset .CoreV1 ().Namespaces ().Create (context .Background (), ns2 , metav1.CreateOptions {})
466- if err != nil {
467- t .Fatalf ("failed to create namespace: %v" , err )
468- }
469- ns3 := & corev1.Namespace {ObjectMeta : metav1.ObjectMeta {Name : namespace3 }}
470- _ , err = clientset .CoreV1 ().Namespaces ().Create (context .Background (), ns3 , metav1.CreateOptions {})
471- if err != nil {
472- t .Fatalf ("failed to create namespace: %v" , err )
473- }
474480
475481 // Make new deployment in namespace1; expect 1 record
476482 deployment1 := makeDeployment (t , clientset , []metav1.OwnerReference {}, namespace1 , "init-deployment" )
0 commit comments