...

Source file src/k8s.io/kubernetes/plugin/pkg/admission/storage/storageobjectinuseprotection/admission_test.go

Documentation: k8s.io/kubernetes/plugin/pkg/admission/storage/storageobjectinuseprotection

     1  /*
     2  Copyright 2017 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 storageobjectinuseprotection
    18  
    19  import (
    20  	"context"
    21  	"reflect"
    22  	"testing"
    23  
    24  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    25  	"k8s.io/apimachinery/pkg/runtime"
    26  	"k8s.io/apimachinery/pkg/runtime/schema"
    27  	"k8s.io/apimachinery/pkg/util/dump"
    28  	"k8s.io/apiserver/pkg/admission"
    29  	api "k8s.io/kubernetes/pkg/apis/core"
    30  	volumeutil "k8s.io/kubernetes/pkg/volume/util"
    31  )
    32  
    33  func TestAdmit(t *testing.T) {
    34  	claim := &api.PersistentVolumeClaim{
    35  		TypeMeta: metav1.TypeMeta{
    36  			Kind: "PersistentVolumeClaim",
    37  		},
    38  		ObjectMeta: metav1.ObjectMeta{
    39  			Name:      "claim",
    40  			Namespace: "ns",
    41  		},
    42  	}
    43  
    44  	pv := &api.PersistentVolume{
    45  		TypeMeta: metav1.TypeMeta{
    46  			Kind: "PersistentVolume",
    47  		},
    48  		ObjectMeta: metav1.ObjectMeta{
    49  			Name: "pv",
    50  		},
    51  	}
    52  	claimWithFinalizer := claim.DeepCopy()
    53  	claimWithFinalizer.Finalizers = []string{volumeutil.PVCProtectionFinalizer}
    54  
    55  	pvWithFinalizer := pv.DeepCopy()
    56  	pvWithFinalizer.Finalizers = []string{volumeutil.PVProtectionFinalizer}
    57  
    58  	tests := []struct {
    59  		name           string
    60  		resource       schema.GroupVersionResource
    61  		object         runtime.Object
    62  		expectedObject runtime.Object
    63  		namespace      string
    64  	}{
    65  		{
    66  			"create -> add finalizer",
    67  			api.SchemeGroupVersion.WithResource("persistentvolumeclaims"),
    68  			claim,
    69  			claimWithFinalizer,
    70  			claim.Namespace,
    71  		},
    72  		{
    73  			"finalizer already exists -> no new finalizer",
    74  			api.SchemeGroupVersion.WithResource("persistentvolumeclaims"),
    75  			claimWithFinalizer,
    76  			claimWithFinalizer,
    77  			claimWithFinalizer.Namespace,
    78  		},
    79  		{
    80  			"create -> add finalizer",
    81  			api.SchemeGroupVersion.WithResource("persistentvolumes"),
    82  			pv,
    83  			pvWithFinalizer,
    84  			pv.Namespace,
    85  		},
    86  		{
    87  			"finalizer already exists -> no new finalizer",
    88  			api.SchemeGroupVersion.WithResource("persistentvolumes"),
    89  			pvWithFinalizer,
    90  			pvWithFinalizer,
    91  			pvWithFinalizer.Namespace,
    92  		},
    93  	}
    94  
    95  	for _, test := range tests {
    96  		t.Run(test.name, func(t *testing.T) {
    97  			ctrl := newPlugin()
    98  
    99  			obj := test.object.DeepCopyObject()
   100  			attrs := admission.NewAttributesRecord(
   101  				obj,                  // new object
   102  				obj.DeepCopyObject(), // old object, copy to be sure it's not modified
   103  				schema.GroupVersionKind{},
   104  				test.namespace,
   105  				"foo",
   106  				test.resource,
   107  				"", // subresource
   108  				admission.Create,
   109  				&metav1.CreateOptions{},
   110  				false, // dryRun
   111  				nil,   // userInfo
   112  			)
   113  
   114  			err := ctrl.Admit(context.TODO(), attrs, nil)
   115  			if err != nil {
   116  				t.Errorf("Test %q: got unexpected error: %v", test.name, err)
   117  			}
   118  			if !reflect.DeepEqual(test.expectedObject, obj) {
   119  				t.Errorf("Test %q: Expected object:\n%s\ngot:\n%s", test.name, dump.Pretty(test.expectedObject), dump.Pretty(obj))
   120  			}
   121  		})
   122  	}
   123  }
   124  

View as plain text