...

Source file src/sigs.k8s.io/cli-utils/pkg/apply/filter/local-namespaces-filter_test.go

Documentation: sigs.k8s.io/cli-utils/pkg/apply/filter

     1  // Copyright 2019 The Kubernetes Authors.
     2  // SPDX-License-Identifier: Apache-2.0
     3  
     4  package filter
     5  
     6  import (
     7  	"testing"
     8  
     9  	"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
    10  	"k8s.io/apimachinery/pkg/util/sets"
    11  	"sigs.k8s.io/cli-utils/pkg/testutil"
    12  )
    13  
    14  var testNamespace = &unstructured.Unstructured{
    15  	Object: map[string]interface{}{
    16  		"apiVersion": "v1",
    17  		"kind":       "Namespace",
    18  		"metadata": map[string]interface{}{
    19  			"name": "test-namespace",
    20  		},
    21  	},
    22  }
    23  
    24  func TestLocalNamespacesFilter(t *testing.T) {
    25  	tests := map[string]struct {
    26  		localNamespaces sets.String
    27  		namespace       string
    28  		expectedError   error
    29  	}{
    30  		"No local namespaces, namespace is not filtered": {
    31  			localNamespaces: sets.NewString(),
    32  			namespace:       "test-namespace",
    33  		},
    34  		"Namespace not in local namespaces, namespace is not filtered": {
    35  			localNamespaces: sets.NewString("foo", "bar"),
    36  			namespace:       "test-namespace",
    37  		},
    38  		"Namespace is in local namespaces, namespace is filtered": {
    39  			localNamespaces: sets.NewString("foo", "test-namespace", "bar"),
    40  			namespace:       "test-namespace",
    41  			expectedError: &NamespaceInUseError{
    42  				Namespace: "test-namespace",
    43  			},
    44  		},
    45  	}
    46  
    47  	for name, tc := range tests {
    48  		t.Run(name, func(t *testing.T) {
    49  			filter := LocalNamespacesFilter{
    50  				LocalNamespaces: tc.localNamespaces,
    51  			}
    52  			obj := testNamespace.DeepCopy()
    53  			obj.SetName(tc.namespace)
    54  			err := filter.Filter(obj)
    55  			testutil.AssertEqual(t, tc.expectedError, err)
    56  		})
    57  	}
    58  }
    59  

View as plain text