...

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

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

     1  /*
     2  Copyright 2023 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 servicecidr
    18  
    19  import (
    20  	"context"
    21  	"reflect"
    22  	"testing"
    23  
    24  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    25  
    26  	"k8s.io/kubernetes/pkg/apis/networking"
    27  )
    28  
    29  func newServiceCIDR() *networking.ServiceCIDR {
    30  	return &networking.ServiceCIDR{
    31  		ObjectMeta: metav1.ObjectMeta{
    32  			Name:            "servicecidr-test",
    33  			ResourceVersion: "1",
    34  		},
    35  		Spec: networking.ServiceCIDRSpec{
    36  			CIDRs: []string{"10.10.0.0/24"},
    37  		},
    38  	}
    39  }
    40  
    41  func TestServiceCIDRStrategy(t *testing.T) {
    42  	if Strategy.NamespaceScoped() {
    43  		t.Errorf("Expected ServiceCIDR to be cluster-scoped")
    44  	}
    45  
    46  	resetFields := Strategy.GetResetFields()
    47  	if len(resetFields) != 1 {
    48  		t.Errorf("ResetFields should have 1 element, but have %d", len(resetFields))
    49  	}
    50  	obj := &networking.ServiceCIDR{Spec: networking.ServiceCIDRSpec{CIDRs: []string{"bad cidr"}}}
    51  
    52  	errors := Strategy.Validate(context.TODO(), obj)
    53  	if len(errors) != 2 {
    54  		t.Errorf("Expected 2 validation errors for invalid object, got %d", len(errors))
    55  	}
    56  
    57  	oldObj := newServiceCIDR()
    58  	newObj := oldObj.DeepCopy()
    59  	newObj.Spec.CIDRs = []string{"bad cidr"}
    60  	errors = Strategy.ValidateUpdate(context.TODO(), newObj, oldObj)
    61  	if len(errors) != 2 {
    62  		t.Errorf("Expected 2 validation errors for invalid update, got %d", len(errors))
    63  	}
    64  }
    65  
    66  func TestServiceCIDRStatusStrategy(t *testing.T) {
    67  	resetFields := StatusStrategy.GetResetFields()
    68  	if len(resetFields) != 1 {
    69  		t.Errorf("ResetFields should have 1 element, but have %d", len(resetFields))
    70  	}
    71  
    72  	oldObj := &networking.ServiceCIDR{Spec: networking.ServiceCIDRSpec{}}
    73  	newObj := &networking.ServiceCIDR{
    74  		Spec: networking.ServiceCIDRSpec{
    75  			CIDRs: []string{"10.10.0.0/16"},
    76  		},
    77  	}
    78  	StatusStrategy.PrepareForUpdate(context.TODO(), newObj, oldObj)
    79  	if !reflect.DeepEqual(newObj.Spec, networking.ServiceCIDRSpec{}) {
    80  		t.Errorf("Expected spec field to be preserved from old object during status update")
    81  	}
    82  
    83  	newObj = &networking.ServiceCIDR{
    84  		Status: networking.ServiceCIDRStatus{
    85  			Conditions: []metav1.Condition{
    86  				{
    87  					Type:   "bad type",
    88  					Status: "bad status",
    89  				},
    90  			},
    91  		},
    92  	}
    93  	oldObj = &networking.ServiceCIDR{}
    94  	errors := StatusStrategy.ValidateUpdate(context.TODO(), newObj, oldObj)
    95  	if len(errors) != 1 {
    96  		t.Errorf("Expected 1 validation errors for invalid update, got %d", len(errors))
    97  	}
    98  }
    99  

View as plain text