...

Source file src/k8s.io/kubernetes/pkg/apis/networking/v1beta1/defaults_test.go

Documentation: k8s.io/kubernetes/pkg/apis/networking/v1beta1

     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 v1beta1_test
    18  
    19  import (
    20  	"reflect"
    21  	"testing"
    22  
    23  	networkingv1beta1 "k8s.io/api/networking/v1beta1"
    24  	apiequality "k8s.io/apimachinery/pkg/api/equality"
    25  	"k8s.io/apimachinery/pkg/runtime"
    26  	"k8s.io/kubernetes/pkg/api/legacyscheme"
    27  	_ "k8s.io/kubernetes/pkg/apis/networking/install"
    28  	. "k8s.io/kubernetes/pkg/apis/networking/v1beta1"
    29  )
    30  
    31  func TestSetIngressPathDefaults(t *testing.T) {
    32  	pathTypeImplementationSpecific := networkingv1beta1.PathTypeImplementationSpecific
    33  	pathTypeExact := networkingv1beta1.PathTypeExact
    34  
    35  	testCases := map[string]struct {
    36  		original *networkingv1beta1.HTTPIngressPath
    37  		expected *networkingv1beta1.HTTPIngressPath
    38  	}{
    39  		"empty pathType should default to ImplementationSpecific": {
    40  			original: &networkingv1beta1.HTTPIngressPath{},
    41  			expected: &networkingv1beta1.HTTPIngressPath{PathType: &pathTypeImplementationSpecific},
    42  		},
    43  		"ImplementationSpecific pathType should not change": {
    44  			original: &networkingv1beta1.HTTPIngressPath{PathType: &pathTypeImplementationSpecific},
    45  			expected: &networkingv1beta1.HTTPIngressPath{PathType: &pathTypeImplementationSpecific},
    46  		},
    47  		"Exact pathType should not change": {
    48  			original: &networkingv1beta1.HTTPIngressPath{PathType: &pathTypeExact},
    49  			expected: &networkingv1beta1.HTTPIngressPath{PathType: &pathTypeExact},
    50  		},
    51  	}
    52  	for name, testCase := range testCases {
    53  		t.Run(name, func(t *testing.T) {
    54  			ingressOriginal := wrapIngressPath(testCase.original)
    55  			ingressExpected := wrapIngressPath(testCase.expected)
    56  			runtimeObj := roundTrip(t, runtime.Object(ingressOriginal))
    57  			ingressActual, ok := runtimeObj.(*networkingv1beta1.Ingress)
    58  			if !ok {
    59  				t.Fatalf("Unexpected object: %v", ingressActual)
    60  			}
    61  			if !apiequality.Semantic.DeepEqual(ingressActual.Spec, ingressExpected.Spec) {
    62  				t.Errorf("Expected: %+v, got: %+v", ingressExpected.Spec, ingressActual.Spec)
    63  			}
    64  		})
    65  	}
    66  }
    67  
    68  func wrapIngressPath(path *networkingv1beta1.HTTPIngressPath) *networkingv1beta1.Ingress {
    69  	return &networkingv1beta1.Ingress{
    70  		Spec: networkingv1beta1.IngressSpec{
    71  			Rules: []networkingv1beta1.IngressRule{{
    72  				IngressRuleValue: networkingv1beta1.IngressRuleValue{
    73  					HTTP: &networkingv1beta1.HTTPIngressRuleValue{
    74  						Paths: []networkingv1beta1.HTTPIngressPath{*path},
    75  					},
    76  				},
    77  			}},
    78  		},
    79  	}
    80  }
    81  
    82  func roundTrip(t *testing.T, obj runtime.Object) runtime.Object {
    83  	t.Helper()
    84  	data, err := runtime.Encode(legacyscheme.Codecs.LegacyCodec(SchemeGroupVersion), obj)
    85  	if err != nil {
    86  		t.Errorf("%v\n %#v", err, obj)
    87  		return nil
    88  	}
    89  	obj2, err := runtime.Decode(legacyscheme.Codecs.UniversalDecoder(), data)
    90  	if err != nil {
    91  		t.Errorf("%v\nData: %s\nSource: %#v", err, string(data), obj)
    92  		return nil
    93  	}
    94  	obj3 := reflect.New(reflect.TypeOf(obj).Elem()).Interface().(runtime.Object)
    95  	err = legacyscheme.Scheme.Convert(obj2, obj3, nil)
    96  	if err != nil {
    97  		t.Errorf("%v\nSource: %#v", err, obj2)
    98  		return nil
    99  	}
   100  	return obj3
   101  }
   102  

View as plain text