...

Source file src/k8s.io/apimachinery/pkg/util/managedfields/internal/versionconverter_test.go

Documentation: k8s.io/apimachinery/pkg/util/managedfields/internal

     1  /*
     2  Copyright 2018 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 internal
    18  
    19  import (
    20  	"encoding/json"
    21  	"fmt"
    22  	"os"
    23  	"path/filepath"
    24  	"reflect"
    25  	"testing"
    26  
    27  	"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
    28  	"k8s.io/apimachinery/pkg/runtime"
    29  	"k8s.io/apimachinery/pkg/runtime/schema"
    30  	"k8s.io/kube-openapi/pkg/validation/spec"
    31  	"sigs.k8s.io/structured-merge-diff/v4/fieldpath"
    32  )
    33  
    34  var testTypeConverter = func() TypeConverter {
    35  	data, err := os.ReadFile(filepath.Join("testdata", "swagger.json"))
    36  	if err != nil {
    37  		panic(err)
    38  	}
    39  	swag := spec.Swagger{}
    40  	if err := json.Unmarshal(data, &swag); err != nil {
    41  		panic(err)
    42  	}
    43  
    44  	convertedDefs := map[string]*spec.Schema{}
    45  	for k, v := range swag.Definitions {
    46  		vCopy := v
    47  		convertedDefs[k] = &vCopy
    48  	}
    49  	typeConverter, err := NewTypeConverter(convertedDefs, false)
    50  	if err != nil {
    51  		panic(err)
    52  	}
    53  	return typeConverter
    54  }()
    55  
    56  // TestVersionConverter tests the version converter
    57  func TestVersionConverter(t *testing.T) {
    58  	oc := fakeObjectConvertorForTestSchema{
    59  		gvkForVersion("v1beta1"): objForGroupVersion("apps/v1beta1"),
    60  		gvkForVersion("v1"):      objForGroupVersion("apps/v1"),
    61  	}
    62  	vc := newVersionConverter(testTypeConverter, oc, schema.GroupVersion{Group: "apps", Version: runtime.APIVersionInternal})
    63  
    64  	input, err := testTypeConverter.ObjectToTyped(objForGroupVersion("apps/v1beta1"))
    65  	if err != nil {
    66  		t.Fatalf("error creating converting input object to a typed value: %v", err)
    67  	}
    68  	expected := objForGroupVersion("apps/v1")
    69  	output, err := vc.Convert(input, fieldpath.APIVersion("apps/v1"))
    70  	if err != nil {
    71  		t.Fatalf("expected err to be nil but got %v", err)
    72  	}
    73  	actual, err := testTypeConverter.TypedToObject(output)
    74  	if err != nil {
    75  		t.Fatalf("error converting output typed value to an object %v", err)
    76  	}
    77  
    78  	if !reflect.DeepEqual(expected, actual) {
    79  		t.Fatalf("expected to get %v but got %v", expected, actual)
    80  	}
    81  }
    82  
    83  func gvkForVersion(v string) schema.GroupVersionKind {
    84  	return schema.GroupVersionKind{
    85  		Group:   "apps",
    86  		Version: v,
    87  		Kind:    "Deployment",
    88  	}
    89  }
    90  
    91  func objForGroupVersion(gv string) runtime.Object {
    92  	return &unstructured.Unstructured{
    93  		Object: map[string]interface{}{
    94  			"apiVersion": gv,
    95  			"kind":       "Deployment",
    96  		},
    97  	}
    98  }
    99  
   100  type fakeObjectConvertorForTestSchema map[schema.GroupVersionKind]runtime.Object
   101  
   102  var _ runtime.ObjectConvertor = fakeObjectConvertorForTestSchema{}
   103  
   104  func (c fakeObjectConvertorForTestSchema) ConvertToVersion(_ runtime.Object, gv runtime.GroupVersioner) (runtime.Object, error) {
   105  	allKinds := make([]schema.GroupVersionKind, 0)
   106  	for kind := range c {
   107  		allKinds = append(allKinds, kind)
   108  	}
   109  	gvk, _ := gv.KindForGroupVersionKinds(allKinds)
   110  	return c[gvk], nil
   111  }
   112  
   113  func (fakeObjectConvertorForTestSchema) Convert(_, _, _ interface{}) error {
   114  	return fmt.Errorf("function not implemented")
   115  }
   116  
   117  func (fakeObjectConvertorForTestSchema) ConvertFieldLabel(_ schema.GroupVersionKind, _, _ string) (string, string, error) {
   118  	return "", "", fmt.Errorf("function not implemented")
   119  }
   120  

View as plain text