k8s.io/kubernetes@v1.29.3/pkg/apis/node/v1alpha1/conversion_test.go (about) 1 /* 2 Copyright 2019 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 v1alpha1 18 19 import ( 20 "testing" 21 22 "github.com/stretchr/testify/assert" 23 "github.com/stretchr/testify/require" 24 corev1 "k8s.io/api/core/v1" 25 v1alpha1 "k8s.io/api/node/v1alpha1" 26 "k8s.io/apimachinery/pkg/api/resource" 27 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 28 core "k8s.io/kubernetes/pkg/apis/core" 29 node "k8s.io/kubernetes/pkg/apis/node" 30 ) 31 32 func TestRuntimeClassConversion(t *testing.T) { 33 const ( 34 name = "puppy" 35 handler = "heidi" 36 cpuOverhead = "100" 37 ) 38 tests := map[string]struct { 39 internal *node.RuntimeClass 40 external *v1alpha1.RuntimeClass 41 }{ 42 "fully-specified": { 43 internal: &node.RuntimeClass{ 44 ObjectMeta: metav1.ObjectMeta{Name: name}, 45 Handler: handler, 46 Overhead: &node.Overhead{ 47 PodFixed: core.ResourceList{ 48 core.ResourceCPU: resource.MustParse(cpuOverhead), 49 }, 50 }, 51 Scheduling: &node.Scheduling{ 52 NodeSelector: map[string]string{"extra-soft": "true"}, 53 Tolerations: []core.Toleration{{ 54 Key: "stinky", 55 Operator: core.TolerationOpExists, 56 Effect: core.TaintEffectNoSchedule, 57 }}, 58 }, 59 }, 60 external: &v1alpha1.RuntimeClass{ 61 ObjectMeta: metav1.ObjectMeta{Name: name}, 62 Spec: v1alpha1.RuntimeClassSpec{ 63 RuntimeHandler: handler, 64 Overhead: &v1alpha1.Overhead{ 65 PodFixed: corev1.ResourceList{ 66 corev1.ResourceCPU: resource.MustParse(cpuOverhead), 67 }, 68 }, 69 Scheduling: &v1alpha1.Scheduling{ 70 NodeSelector: map[string]string{"extra-soft": "true"}, 71 Tolerations: []corev1.Toleration{{ 72 Key: "stinky", 73 Operator: corev1.TolerationOpExists, 74 Effect: corev1.TaintEffectNoSchedule, 75 }}, 76 }, 77 }, 78 }, 79 }, 80 "empty-overhead": { 81 internal: &node.RuntimeClass{ 82 ObjectMeta: metav1.ObjectMeta{Name: name}, 83 Handler: handler, 84 Overhead: &node.Overhead{}, 85 }, 86 external: &v1alpha1.RuntimeClass{ 87 ObjectMeta: metav1.ObjectMeta{Name: name}, 88 Spec: v1alpha1.RuntimeClassSpec{ 89 RuntimeHandler: handler, 90 Overhead: &v1alpha1.Overhead{}, 91 }, 92 }, 93 }, 94 "empty-scheduling": { 95 internal: &node.RuntimeClass{ 96 ObjectMeta: metav1.ObjectMeta{Name: name}, 97 Handler: handler, 98 Scheduling: &node.Scheduling{}, 99 }, 100 external: &v1alpha1.RuntimeClass{ 101 ObjectMeta: metav1.ObjectMeta{Name: name}, 102 Spec: v1alpha1.RuntimeClassSpec{ 103 RuntimeHandler: handler, 104 Scheduling: &v1alpha1.Scheduling{}, 105 }, 106 }, 107 }, 108 "empty": { 109 internal: &node.RuntimeClass{ 110 ObjectMeta: metav1.ObjectMeta{Name: name}, 111 Handler: handler, 112 }, 113 external: &v1alpha1.RuntimeClass{ 114 ObjectMeta: metav1.ObjectMeta{Name: name}, 115 Spec: v1alpha1.RuntimeClassSpec{ 116 RuntimeHandler: handler, 117 }, 118 }, 119 }, 120 } 121 122 for name, test := range tests { 123 t.Run(name, func(t *testing.T) { 124 convertedInternal := &node.RuntimeClass{} 125 require.NoError(t, 126 Convert_v1alpha1_RuntimeClass_To_node_RuntimeClass(test.external, convertedInternal, nil)) 127 assert.Equal(t, test.internal, convertedInternal, "external -> internal") 128 129 convertedV1alpha1 := &v1alpha1.RuntimeClass{} 130 require.NoError(t, 131 Convert_node_RuntimeClass_To_v1alpha1_RuntimeClass(test.internal, convertedV1alpha1, nil)) 132 assert.Equal(t, test.external, convertedV1alpha1, "internal -> external") 133 }) 134 } 135 }