...

Source file src/k8s.io/kubernetes/pkg/registry/networking/ingressclass/strategy_test.go

Documentation: k8s.io/kubernetes/pkg/registry/networking/ingressclass

     1  /*
     2  Copyright 2020 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 ingressclass
    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/networking"
    25  )
    26  
    27  func TestIngressClassStrategy(t *testing.T) {
    28  	ctx := genericapirequest.NewDefaultContext()
    29  	if Strategy.NamespaceScoped() {
    30  		t.Errorf("IngressClass must not be namespace scoped")
    31  	}
    32  	if Strategy.AllowCreateOnUpdate() {
    33  		t.Errorf("IngressClass should not allow create on update")
    34  	}
    35  
    36  	ingressClass := networking.IngressClass{
    37  		ObjectMeta: metav1.ObjectMeta{
    38  			Name:            "foo",
    39  			ResourceVersion: "1",
    40  		},
    41  		Spec: networking.IngressClassSpec{
    42  			Controller: "example.com/controller",
    43  		},
    44  	}
    45  
    46  	Strategy.PrepareForCreate(ctx, &ingressClass)
    47  	if ingressClass.Generation != 1 {
    48  		t.Error("IngressClass generation should be 1")
    49  	}
    50  	errs := Strategy.Validate(ctx, &ingressClass)
    51  	if len(errs) != 0 {
    52  		t.Errorf("Unexpected error from validation for IngressClass: %v", errs)
    53  	}
    54  
    55  	newIngressClass := ingressClass.DeepCopy()
    56  	Strategy.PrepareForUpdate(ctx, newIngressClass, &ingressClass)
    57  	errs = Strategy.ValidateUpdate(ctx, newIngressClass, &ingressClass)
    58  	if len(errs) != 0 {
    59  		t.Errorf("Unexpected error from update validation for IngressClass: %v", errs)
    60  	}
    61  
    62  	ingressClass.Name = "invalid/name"
    63  
    64  	errs = Strategy.Validate(ctx, &ingressClass)
    65  	if len(errs) == 0 {
    66  		t.Errorf("Expected error from validation for IngressClass, got none")
    67  	}
    68  	errs = Strategy.ValidateUpdate(ctx, &ingressClass, &ingressClass)
    69  	if len(errs) == 0 {
    70  		t.Errorf("Expected error from update validation for IngressClass, got none")
    71  	}
    72  }
    73  

View as plain text