mirror of
				https://github.com/k3s-io/kubernetes.git
				synced 2025-11-03 23:40:03 +00:00 
			
		
		
		
	
		
			
				
	
	
		
			124 lines
		
	
	
		
			3.5 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			124 lines
		
	
	
		
			3.5 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
/*
 | 
						|
Copyright 2019 The Kubernetes Authors.
 | 
						|
 | 
						|
Licensed under the Apache License, Version 2.0 (the "License");
 | 
						|
you may not use this file except in compliance with the License.
 | 
						|
You may obtain a copy of the License at
 | 
						|
 | 
						|
    http://www.apache.org/licenses/LICENSE-2.0
 | 
						|
 | 
						|
Unless required by applicable law or agreed to in writing, software
 | 
						|
distributed under the License is distributed on an "AS IS" BASIS,
 | 
						|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 | 
						|
See the License for the specific language governing permissions and
 | 
						|
limitations under the License.
 | 
						|
*/
 | 
						|
 | 
						|
package v1alpha1
 | 
						|
 | 
						|
import (
 | 
						|
	"testing"
 | 
						|
 | 
						|
	"github.com/stretchr/testify/assert"
 | 
						|
	"github.com/stretchr/testify/require"
 | 
						|
	corev1 "k8s.io/api/core/v1"
 | 
						|
	v1alpha1 "k8s.io/api/node/v1alpha1"
 | 
						|
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
 | 
						|
	core "k8s.io/kubernetes/pkg/apis/core"
 | 
						|
	node "k8s.io/kubernetes/pkg/apis/node"
 | 
						|
)
 | 
						|
 | 
						|
func TestRuntimeClassConversion(t *testing.T) {
 | 
						|
	const (
 | 
						|
		name    = "puppy"
 | 
						|
		handler = "heidi"
 | 
						|
	)
 | 
						|
	tests := map[string]struct {
 | 
						|
		internal *node.RuntimeClass
 | 
						|
		external *v1alpha1.RuntimeClass
 | 
						|
	}{
 | 
						|
		"fully-specified": {
 | 
						|
			internal: &node.RuntimeClass{
 | 
						|
				ObjectMeta: metav1.ObjectMeta{Name: name},
 | 
						|
				Handler:    handler,
 | 
						|
				Topology: &node.Topology{
 | 
						|
					NodeSelector: &core.NodeSelector{
 | 
						|
						NodeSelectorTerms: []core.NodeSelectorTerm{{
 | 
						|
							MatchExpressions: []core.NodeSelectorRequirement{{
 | 
						|
								Key:      "extra-soft",
 | 
						|
								Operator: core.NodeSelectorOpExists,
 | 
						|
							}},
 | 
						|
						}},
 | 
						|
					},
 | 
						|
					Tolerations: []core.Toleration{{
 | 
						|
						Key:      "stinky",
 | 
						|
						Operator: core.TolerationOpExists,
 | 
						|
						Effect:   core.TaintEffectNoSchedule,
 | 
						|
					}},
 | 
						|
				},
 | 
						|
			},
 | 
						|
			external: &v1alpha1.RuntimeClass{
 | 
						|
				ObjectMeta: metav1.ObjectMeta{Name: name},
 | 
						|
				Spec: v1alpha1.RuntimeClassSpec{
 | 
						|
					RuntimeHandler: handler,
 | 
						|
					Topology: &v1alpha1.Topology{
 | 
						|
						NodeSelector: &corev1.NodeSelector{
 | 
						|
							NodeSelectorTerms: []corev1.NodeSelectorTerm{{
 | 
						|
								MatchExpressions: []corev1.NodeSelectorRequirement{{
 | 
						|
									Key:      "extra-soft",
 | 
						|
									Operator: corev1.NodeSelectorOpExists,
 | 
						|
								}},
 | 
						|
							}},
 | 
						|
						},
 | 
						|
						Tolerations: []corev1.Toleration{{
 | 
						|
							Key:      "stinky",
 | 
						|
							Operator: corev1.TolerationOpExists,
 | 
						|
							Effect:   corev1.TaintEffectNoSchedule,
 | 
						|
						}},
 | 
						|
					},
 | 
						|
				},
 | 
						|
			},
 | 
						|
		},
 | 
						|
		"empty-topology": {
 | 
						|
			internal: &node.RuntimeClass{
 | 
						|
				ObjectMeta: metav1.ObjectMeta{Name: name},
 | 
						|
				Handler:    handler,
 | 
						|
				Topology:   &node.Topology{},
 | 
						|
			},
 | 
						|
			external: &v1alpha1.RuntimeClass{
 | 
						|
				ObjectMeta: metav1.ObjectMeta{Name: name},
 | 
						|
				Spec: v1alpha1.RuntimeClassSpec{
 | 
						|
					RuntimeHandler: handler,
 | 
						|
					Topology:       &v1alpha1.Topology{},
 | 
						|
				},
 | 
						|
			},
 | 
						|
		},
 | 
						|
		"empty": {
 | 
						|
			internal: &node.RuntimeClass{
 | 
						|
				ObjectMeta: metav1.ObjectMeta{Name: name},
 | 
						|
				Handler:    handler,
 | 
						|
			},
 | 
						|
			external: &v1alpha1.RuntimeClass{
 | 
						|
				ObjectMeta: metav1.ObjectMeta{Name: name},
 | 
						|
				Spec: v1alpha1.RuntimeClassSpec{
 | 
						|
					RuntimeHandler: handler,
 | 
						|
				},
 | 
						|
			},
 | 
						|
		},
 | 
						|
	}
 | 
						|
 | 
						|
	for name, test := range tests {
 | 
						|
		t.Run(name, func(t *testing.T) {
 | 
						|
			convertedInternal := &node.RuntimeClass{}
 | 
						|
			require.NoError(t,
 | 
						|
				Convert_v1alpha1_RuntimeClass_To_node_RuntimeClass(test.external, convertedInternal, nil))
 | 
						|
			assert.Equal(t, test.internal, convertedInternal, "external -> internal")
 | 
						|
 | 
						|
			convertedV1alpha1 := &v1alpha1.RuntimeClass{}
 | 
						|
			require.NoError(t,
 | 
						|
				Convert_node_RuntimeClass_To_v1alpha1_RuntimeClass(test.internal, convertedV1alpha1, nil))
 | 
						|
			assert.Equal(t, test.external, convertedV1alpha1, "internal -> external")
 | 
						|
		})
 | 
						|
	}
 | 
						|
}
 |