...

Source file src/k8s.io/kubernetes/pkg/apis/discovery/v1beta1/conversion_test.go

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

     1  /*
     2  Copyright 2021 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
    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  	"k8s.io/api/discovery/v1beta1"
    26  	"k8s.io/kubernetes/pkg/apis/discovery"
    27  	utilpointer "k8s.io/utils/pointer"
    28  )
    29  
    30  func TestEndpointZoneConverstion(t *testing.T) {
    31  	testcases := []struct {
    32  		desc     string
    33  		external v1beta1.Endpoint
    34  		internal discovery.Endpoint
    35  	}{
    36  		{
    37  			desc:     "no topology field",
    38  			external: v1beta1.Endpoint{},
    39  			internal: discovery.Endpoint{},
    40  		},
    41  		{
    42  			desc: "non empty topology map, but no zone",
    43  			external: v1beta1.Endpoint{
    44  				Topology: map[string]string{
    45  					"key1": "val1",
    46  				},
    47  			},
    48  			internal: discovery.Endpoint{
    49  				DeprecatedTopology: map[string]string{
    50  					"key1": "val1",
    51  				},
    52  			},
    53  		},
    54  		{
    55  			desc: "non empty topology map, with zone",
    56  			external: v1beta1.Endpoint{
    57  				Topology: map[string]string{
    58  					"key1":                   "val1",
    59  					corev1.LabelTopologyZone: "zone1",
    60  				},
    61  			},
    62  			internal: discovery.Endpoint{
    63  				DeprecatedTopology: map[string]string{
    64  					"key1": "val1",
    65  				},
    66  				Zone: utilpointer.String("zone1"),
    67  			},
    68  		},
    69  		{
    70  			desc: "only zone in topology map",
    71  			external: v1beta1.Endpoint{
    72  				Topology: map[string]string{
    73  					corev1.LabelTopologyZone: "zone1",
    74  				},
    75  			},
    76  			internal: discovery.Endpoint{
    77  				Zone: utilpointer.String("zone1"),
    78  			},
    79  		},
    80  		{
    81  			desc: "nodeName and topology[hostname] are populated with different values",
    82  			external: v1beta1.Endpoint{
    83  				NodeName: utilpointer.String("node-1"),
    84  				Topology: map[string]string{
    85  					corev1.LabelHostname: "node-2",
    86  				},
    87  			},
    88  			internal: discovery.Endpoint{
    89  				NodeName: utilpointer.String("node-1"),
    90  				DeprecatedTopology: map[string]string{
    91  					corev1.LabelHostname: "node-2",
    92  				},
    93  			},
    94  		},
    95  		{
    96  			desc: "nodeName and topology[hostname] are populated with same values",
    97  			external: v1beta1.Endpoint{
    98  				NodeName: utilpointer.String("node-1"),
    99  				Topology: map[string]string{
   100  					corev1.LabelHostname: "node-1",
   101  				},
   102  			},
   103  			internal: discovery.Endpoint{
   104  				NodeName: utilpointer.String("node-1"),
   105  			},
   106  		},
   107  		{
   108  			desc: "only topology[hostname] is populated",
   109  			external: v1beta1.Endpoint{
   110  				Topology: map[string]string{
   111  					corev1.LabelHostname: "node-1",
   112  				},
   113  			},
   114  			internal: discovery.Endpoint{
   115  				DeprecatedTopology: map[string]string{
   116  					corev1.LabelHostname: "node-1",
   117  				},
   118  			},
   119  		},
   120  		{
   121  			desc: "only nodeName is populated",
   122  			external: v1beta1.Endpoint{
   123  				NodeName: utilpointer.String("node-1"),
   124  				Topology: map[string]string{
   125  					corev1.LabelHostname: "node-1",
   126  				},
   127  			},
   128  			internal: discovery.Endpoint{
   129  				NodeName: utilpointer.String("node-1"),
   130  			},
   131  		},
   132  	}
   133  
   134  	for _, tc := range testcases {
   135  		t.Run(tc.desc, func(t *testing.T) {
   136  			convertedInternal := discovery.Endpoint{}
   137  			require.NoError(t, Convert_v1beta1_Endpoint_To_discovery_Endpoint(&tc.external, &convertedInternal, nil))
   138  			assert.Equal(t, tc.internal, convertedInternal, "v1beta1.Endpoint -> discovery.Endpoint")
   139  
   140  			convertedV1beta1 := v1beta1.Endpoint{}
   141  			require.NoError(t, Convert_discovery_Endpoint_To_v1beta1_Endpoint(&tc.internal, &convertedV1beta1, nil))
   142  			assert.Equal(t, tc.external, convertedV1beta1, "discovery.Endpoint -> v1beta1.Endpoint")
   143  		})
   144  	}
   145  }
   146  

View as plain text