k8s.io/apiserver@v0.31.1/pkg/endpoints/openapi/openapi_test.go (about)

     1  /*
     2  Copyright 2016 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 openapi
    18  
    19  import (
    20  	"reflect"
    21  	"testing"
    22  
    23  	"k8s.io/apimachinery/pkg/runtime"
    24  	openapitesting "k8s.io/apiserver/pkg/endpoints/openapi/testing"
    25  	"k8s.io/kube-openapi/pkg/validation/spec"
    26  )
    27  
    28  func assertEqual(t *testing.T, expected, actual interface{}) {
    29  	var equal bool
    30  	if expected == nil || actual == nil {
    31  		equal = expected == actual
    32  	} else {
    33  		equal = reflect.DeepEqual(expected, actual)
    34  	}
    35  	if !equal {
    36  		t.Errorf("%v != %v", expected, actual)
    37  	}
    38  }
    39  
    40  func TestGetDefinitionName(t *testing.T) {
    41  	testType := openapitesting.TestType{}
    42  	// in production, the name is stripped of ".*vendor/" prefix before passed
    43  	// to GetDefinitionName, so here typePkgName does not have the
    44  	// "k8s.io/kubernetes/vendor" prefix.
    45  	typePkgName := "k8s.io/apiserver/pkg/endpoints/openapi/testing.TestType"
    46  	typeFriendlyName := "io.k8s.apiserver.pkg.endpoints.openapi.testing.TestType"
    47  	s := runtime.NewScheme()
    48  	s.AddKnownTypeWithName(testType.GroupVersionKind(), &testType)
    49  	namer := NewDefinitionNamer(s)
    50  	n, e := namer.GetDefinitionName(typePkgName)
    51  	assertEqual(t, typeFriendlyName, n)
    52  	assertEqual(t, []interface{}{
    53  		map[string]interface{}{
    54  			"group":   "test",
    55  			"version": "v1",
    56  			"kind":    "TestType",
    57  		},
    58  	}, e["x-kubernetes-group-version-kind"])
    59  	n, e2 := namer.GetDefinitionName("test.com/another.Type")
    60  	assertEqual(t, "com.test.another.Type", n)
    61  	assertEqual(t, e2, spec.Extensions(nil))
    62  }
    63  
    64  func TestToValidOperationID(t *testing.T) {
    65  	scenarios := []struct {
    66  		s                     string
    67  		capitalizeFirstLetter bool
    68  		expectedResult        string
    69  	}{
    70  		{
    71  			s:                     "test_operation",
    72  			capitalizeFirstLetter: true,
    73  			expectedResult:        "Test_operation",
    74  		},
    75  		{
    76  			s:                     "test operation& test",
    77  			capitalizeFirstLetter: true,
    78  			expectedResult:        "TestOperationTest",
    79  		},
    80  		{
    81  			s:                     "test78operation",
    82  			capitalizeFirstLetter: false,
    83  			expectedResult:        "test78operation",
    84  		},
    85  	}
    86  	for _, tt := range scenarios {
    87  		result := ToValidOperationID(tt.s, tt.capitalizeFirstLetter)
    88  		if result != tt.expectedResult {
    89  			t.Errorf("expected result: %s, got: %s", tt.expectedResult, result)
    90  		}
    91  	}
    92  }