...

Source file src/sigs.k8s.io/cli-utils/pkg/apply/filter/prevent-remove-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  	"sigs.k8s.io/cli-utils/pkg/common"
    11  	"sigs.k8s.io/cli-utils/pkg/testutil"
    12  )
    13  
    14  var defaultObj = &unstructured.Unstructured{
    15  	Object: map[string]interface{}{
    16  		"apiVersion": "v1",
    17  		"kind":       "Pod",
    18  		"metadata": map[string]interface{}{
    19  			"name":      "pod-name",
    20  			"namespace": "test-namespace",
    21  		},
    22  	},
    23  }
    24  
    25  func TestPreventDeleteAnnotation(t *testing.T) {
    26  	tests := map[string]struct {
    27  		annotations   map[string]string
    28  		expectedError error
    29  	}{
    30  		"Nil map returns false": {
    31  			annotations: nil,
    32  		},
    33  		"Empty map returns false": {
    34  			annotations: map[string]string{},
    35  		},
    36  		"Wrong annotation key/value is false": {
    37  			annotations: map[string]string{
    38  				"foo": "bar",
    39  			},
    40  		},
    41  		"Annotation key without value is false": {
    42  			annotations: map[string]string{
    43  				common.OnRemoveAnnotation: "bar",
    44  			},
    45  		},
    46  		"Annotation key and value is true": {
    47  			annotations: map[string]string{
    48  				common.OnRemoveAnnotation: common.OnRemoveKeep,
    49  			},
    50  			expectedError: &AnnotationPreventedDeletionError{
    51  				Annotation: common.OnRemoveAnnotation,
    52  				Value:      common.OnRemoveKeep,
    53  			},
    54  		},
    55  		"Annotation key client.lifecycle.config.k8s.io/deletion without value is false": {
    56  			annotations: map[string]string{
    57  				common.LifecycleDeleteAnnotation: "any",
    58  			},
    59  		},
    60  		"Annotation key client.lifecycle.config.k8s.io/deletion and value is true": {
    61  			annotations: map[string]string{
    62  				common.LifecycleDeleteAnnotation: common.PreventDeletion,
    63  			},
    64  			expectedError: &AnnotationPreventedDeletionError{
    65  				Annotation: common.LifecycleDeleteAnnotation,
    66  				Value:      common.PreventDeletion,
    67  			},
    68  		},
    69  	}
    70  
    71  	for name, tc := range tests {
    72  		t.Run(name, func(t *testing.T) {
    73  			filter := PreventRemoveFilter{}
    74  			obj := defaultObj.DeepCopy()
    75  			obj.SetAnnotations(tc.annotations)
    76  			err := filter.Filter(obj)
    77  			testutil.AssertEqual(t, tc.expectedError, err)
    78  		})
    79  	}
    80  }
    81  

View as plain text