1/*
2Copyright 2021 The Kubernetes Authors.
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8    http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17package v1_test
18
19import (
20	"testing"
21
22	"github.com/google/go-cmp/cmp"
23	v1 "k8s.io/api/core/v1"
24	discovery "k8s.io/api/discovery/v1"
25	apiequality "k8s.io/apimachinery/pkg/api/equality"
26	"k8s.io/kubernetes/pkg/api/legacyscheme"
27	_ "k8s.io/kubernetes/pkg/apis/discovery/install"
28	utilpointer "k8s.io/utils/pointer"
29)
30
31func TestSetDefaultEndpointPort(t *testing.T) {
32	emptyStr := ""
33	fooStr := "foo"
34	protoTCP := v1.ProtocolTCP
35	protoUDP := v1.ProtocolUDP
36
37	tests := map[string]struct {
38		original *discovery.EndpointSlice
39		expected *discovery.EndpointSlice
40	}{
41		"should set appropriate defaults": {
42			original: &discovery.EndpointSlice{Ports: []discovery.EndpointPort{{
43				Port: utilpointer.Int32Ptr(80),
44			}}},
45			expected: &discovery.EndpointSlice{
46				Ports: []discovery.EndpointPort{{
47					Name:     &emptyStr,
48					Protocol: &protoTCP,
49					Port:     utilpointer.Int32Ptr(80),
50				}},
51			},
52		},
53		"should not overwrite values with defaults when set": {
54			original: &discovery.EndpointSlice{
55				Ports: []discovery.EndpointPort{{
56					Name:     &fooStr,
57					Protocol: &protoUDP,
58				}},
59			},
60			expected: &discovery.EndpointSlice{
61				Ports: []discovery.EndpointPort{{
62					Name:     &fooStr,
63					Protocol: &protoUDP,
64				}},
65			},
66		},
67	}
68
69	for _, test := range tests {
70		actual := test.original
71		expected := test.expected
72		legacyscheme.Scheme.Default(actual)
73		if !apiequality.Semantic.DeepEqual(actual, expected) {
74			t.Error(cmp.Diff(expected, actual))
75		}
76	}
77}
78