k8s.io/kubernetes@v1.31.0-alpha.0.0.20240520171757-56147500dadc/pkg/controlplane/controller/systemnamespaces/system_namespaces_controller_test.go (about)

     1  /*
     2  Copyright 2023 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package systemnamespaces
    18  
    19  import (
    20  	"testing"
    21  
    22  	v1 "k8s.io/api/core/v1"
    23  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    24  	"k8s.io/apimachinery/pkg/labels"
    25  	"k8s.io/apimachinery/pkg/runtime"
    26  	"k8s.io/client-go/informers"
    27  	"k8s.io/client-go/kubernetes/fake"
    28  	k8stesting "k8s.io/client-go/testing"
    29  )
    30  
    31  // Test_Controller validates the garbage collection logic for the apiserverleasegc controller.
    32  func Test_Controller(t *testing.T) {
    33  	tests := []struct {
    34  		name       string
    35  		namespaces []string
    36  		actions    [][]string // verb and resource
    37  	}{
    38  		{
    39  			name: "no system namespaces",
    40  			actions: [][]string{
    41  				{"create", "namespaces"},
    42  				{"create", "namespaces"},
    43  				{"create", "namespaces"},
    44  				{"create", "namespaces"},
    45  			},
    46  		},
    47  		{
    48  			name:       "no system namespaces but others",
    49  			namespaces: []string{"foo", "bar"},
    50  			actions: [][]string{
    51  				{"create", "namespaces"},
    52  				{"create", "namespaces"},
    53  				{"create", "namespaces"},
    54  				{"create", "namespaces"},
    55  			},
    56  		},
    57  		{
    58  			name:       "one system namespace",
    59  			namespaces: []string{metav1.NamespaceSystem},
    60  			actions: [][]string{
    61  				{"create", "namespaces"},
    62  				{"create", "namespaces"},
    63  				{"create", "namespaces"},
    64  			},
    65  		},
    66  		{
    67  			name:       "two system namespaces",
    68  			namespaces: []string{metav1.NamespaceSystem, metav1.NamespacePublic},
    69  			actions: [][]string{
    70  				{"create", "namespaces"},
    71  				{"create", "namespaces"},
    72  			},
    73  		},
    74  		{
    75  			name:       "three namespaces",
    76  			namespaces: []string{metav1.NamespaceSystem, metav1.NamespacePublic, v1.NamespaceNodeLease},
    77  			actions: [][]string{
    78  				{"create", "namespaces"},
    79  			},
    80  		},
    81  
    82  		{
    83  			name:       "the four namespaces",
    84  			namespaces: []string{metav1.NamespaceSystem, metav1.NamespacePublic, v1.NamespaceNodeLease, v1.NamespaceDefault},
    85  		},
    86  	}
    87  
    88  	for _, test := range tests {
    89  		t.Run(test.name, func(t *testing.T) {
    90  			objs := []runtime.Object{}
    91  			for _, ns := range test.namespaces {
    92  				objs = append(objs,
    93  					&v1.Namespace{
    94  						ObjectMeta: metav1.ObjectMeta{
    95  							Name:      ns,
    96  							Namespace: "",
    97  						},
    98  					},
    99  				)
   100  			}
   101  			clientset := fake.NewSimpleClientset(objs...)
   102  			informerFactory := informers.NewSharedInformerFactory(clientset, 0)
   103  			namespaceInformer := informerFactory.Core().V1().Namespaces()
   104  			for _, obj := range objs {
   105  				namespaceInformer.Informer().GetIndexer().Add(obj)
   106  			}
   107  
   108  			systemNamespaces := []string{metav1.NamespaceSystem, metav1.NamespacePublic, v1.NamespaceNodeLease, metav1.NamespaceDefault}
   109  			controller := NewController(systemNamespaces, clientset, namespaceInformer)
   110  
   111  			clientset.PrependReactor("create", "namespaces", func(action k8stesting.Action) (bool, runtime.Object, error) {
   112  				create := action.(k8stesting.CreateAction)
   113  				namespaceInformer.Informer().GetIndexer().Add(create.GetObject())
   114  				return true, create.GetObject(), nil
   115  			})
   116  
   117  			controller.sync()
   118  
   119  			expectAction(t, clientset.Actions(), test.actions)
   120  			namespaces, err := controller.namespaceLister.List(labels.Everything())
   121  			if err != nil {
   122  				t.Errorf("unexpected error: %v", err)
   123  			}
   124  
   125  			got := map[string]bool{}
   126  			for _, ns := range namespaces {
   127  				got[ns.Name] = true
   128  			}
   129  
   130  			for _, ns := range systemNamespaces {
   131  				if !got[ns] {
   132  					t.Errorf("unexpected namespaces: %v", namespaces)
   133  					break
   134  				}
   135  			}
   136  		})
   137  	}
   138  }
   139  
   140  func expectAction(t *testing.T, actions []k8stesting.Action, expected [][]string) {
   141  	t.Helper()
   142  	if len(actions) != len(expected) {
   143  		t.Fatalf("Expected at least %d actions, got %d", len(expected), len(actions))
   144  	}
   145  
   146  	for i, action := range actions {
   147  		verb := expected[i][0]
   148  		if action.GetVerb() != verb {
   149  			t.Errorf("Expected action %d verb to be %s, got %s", i, verb, action.GetVerb())
   150  		}
   151  		resource := expected[i][1]
   152  		if action.GetResource().Resource != resource {
   153  			t.Errorf("Expected action %d resource to be %s, got %s", i, resource, action.GetResource().Resource)
   154  		}
   155  	}
   156  }