...
1
16
17 package v1beta1_test
18
19 import (
20 "testing"
21
22 "github.com/google/go-cmp/cmp"
23 v1 "k8s.io/api/core/v1"
24 discovery "k8s.io/api/discovery/v1beta1"
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
31 func 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.Int32(80),
44 }}},
45 expected: &discovery.EndpointSlice{
46 Ports: []discovery.EndpointPort{{
47 Name: &emptyStr,
48 Protocol: &protoTCP,
49 Port: utilpointer.Int32(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
View as plain text