...

Source file src/k8s.io/kubernetes/pkg/registry/resource/resourceclass/strategy_test.go

Documentation: k8s.io/kubernetes/pkg/registry/resource/resourceclass

     1  /*
     2  Copyright 2022 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 resourceclass
    18  
    19  import (
    20  	"testing"
    21  
    22  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    23  	genericapirequest "k8s.io/apiserver/pkg/endpoints/request"
    24  	"k8s.io/kubernetes/pkg/apis/resource"
    25  )
    26  
    27  var resourceClass = &resource.ResourceClass{
    28  	ObjectMeta: metav1.ObjectMeta{
    29  		Name: "valid-class",
    30  	},
    31  	DriverName: "resource-driver.example.com",
    32  }
    33  
    34  func TestClassStrategy(t *testing.T) {
    35  	if Strategy.NamespaceScoped() {
    36  		t.Errorf("ResourceClass must not be namespace scoped")
    37  	}
    38  	if Strategy.AllowCreateOnUpdate() {
    39  		t.Errorf("ResourceClass should not allow create on update")
    40  	}
    41  }
    42  
    43  func TestClassStrategyCreate(t *testing.T) {
    44  	ctx := genericapirequest.NewDefaultContext()
    45  	resourceClass := resourceClass.DeepCopy()
    46  
    47  	Strategy.PrepareForCreate(ctx, resourceClass)
    48  	errs := Strategy.Validate(ctx, resourceClass)
    49  	if len(errs) != 0 {
    50  		t.Errorf("unexpected error validating for create %v", errs)
    51  	}
    52  }
    53  
    54  func TestClassStrategyUpdate(t *testing.T) {
    55  	t.Run("no-changes-okay", func(t *testing.T) {
    56  		ctx := genericapirequest.NewDefaultContext()
    57  		resourceClass := resourceClass.DeepCopy()
    58  		newClass := resourceClass.DeepCopy()
    59  		newClass.ResourceVersion = "4"
    60  
    61  		Strategy.PrepareForUpdate(ctx, newClass, resourceClass)
    62  		errs := Strategy.ValidateUpdate(ctx, newClass, resourceClass)
    63  		if len(errs) != 0 {
    64  			t.Errorf("unexpected validation errors: %v", errs)
    65  		}
    66  	})
    67  
    68  	t.Run("name-change-not-allowed", func(t *testing.T) {
    69  		ctx := genericapirequest.NewDefaultContext()
    70  		resourceClass := resourceClass.DeepCopy()
    71  		newClass := resourceClass.DeepCopy()
    72  		newClass.Name = "valid-class-2"
    73  		newClass.ResourceVersion = "4"
    74  
    75  		Strategy.PrepareForUpdate(ctx, newClass, resourceClass)
    76  		errs := Strategy.ValidateUpdate(ctx, newClass, resourceClass)
    77  		if len(errs) == 0 {
    78  			t.Errorf("expected a validation error")
    79  		}
    80  	})
    81  }
    82  

View as plain text