|
| 1 | +/* |
| 2 | +Copyright 2024 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 endpoint |
| 18 | + |
| 19 | +import ( |
| 20 | + "strings" |
| 21 | + "testing" |
| 22 | + |
| 23 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 24 | + api "k8s.io/kubernetes/pkg/apis/core" |
| 25 | +) |
| 26 | + |
| 27 | +func Test_endpointsWarning(t *testing.T) { |
| 28 | + tests := []struct { |
| 29 | + name string |
| 30 | + endpoints *api.Endpoints |
| 31 | + warnings []string |
| 32 | + }{ |
| 33 | + { |
| 34 | + name: "empty Endpoints", |
| 35 | + endpoints: &api.Endpoints{}, |
| 36 | + warnings: nil, |
| 37 | + }, |
| 38 | + { |
| 39 | + name: "valid Endpoints", |
| 40 | + endpoints: &api.Endpoints{ |
| 41 | + Subsets: []api.EndpointSubset{ |
| 42 | + { |
| 43 | + Addresses: []api.EndpointAddress{ |
| 44 | + {IP: "1.2.3.4"}, |
| 45 | + {IP: "fd00::1234"}, |
| 46 | + }, |
| 47 | + }, |
| 48 | + }, |
| 49 | + }, |
| 50 | + warnings: nil, |
| 51 | + }, |
| 52 | + { |
| 53 | + name: "bad Endpoints", |
| 54 | + endpoints: &api.Endpoints{ |
| 55 | + Subsets: []api.EndpointSubset{ |
| 56 | + { |
| 57 | + Addresses: []api.EndpointAddress{ |
| 58 | + {IP: "fd00::1234"}, |
| 59 | + {IP: "01.02.03.04"}, |
| 60 | + }, |
| 61 | + }, |
| 62 | + { |
| 63 | + Addresses: []api.EndpointAddress{ |
| 64 | + {IP: "::ffff:1.2.3.4"}, |
| 65 | + }, |
| 66 | + }, |
| 67 | + { |
| 68 | + Addresses: []api.EndpointAddress{ |
| 69 | + {IP: "1.2.3.4"}, |
| 70 | + }, |
| 71 | + NotReadyAddresses: []api.EndpointAddress{ |
| 72 | + {IP: "::ffff:1.2.3.4"}, |
| 73 | + }, |
| 74 | + }, |
| 75 | + }, |
| 76 | + }, |
| 77 | + warnings: []string{ |
| 78 | + "subsets[0].addresses[1].ip", |
| 79 | + "subsets[1].addresses[0].ip", |
| 80 | + "subsets[2].notReadyAddresses[0].ip", |
| 81 | + }, |
| 82 | + }, |
| 83 | + { |
| 84 | + // We don't actually want to let bad IPs through in this case; the |
| 85 | + // point here is that we trust the Endpoints controller to not do |
| 86 | + // that, and we're testing that the checks correctly get skipped. |
| 87 | + name: "bad Endpoints ignored because of label", |
| 88 | + endpoints: &api.Endpoints{ |
| 89 | + ObjectMeta: metav1.ObjectMeta{ |
| 90 | + Labels: map[string]string{ |
| 91 | + "endpoints.kubernetes.io/managed-by": "endpoint-controller", |
| 92 | + }, |
| 93 | + }, |
| 94 | + Subsets: []api.EndpointSubset{ |
| 95 | + { |
| 96 | + Addresses: []api.EndpointAddress{ |
| 97 | + {IP: "fd00::1234"}, |
| 98 | + {IP: "01.02.03.04"}, |
| 99 | + }, |
| 100 | + }, |
| 101 | + }, |
| 102 | + }, |
| 103 | + warnings: nil, |
| 104 | + }, |
| 105 | + } |
| 106 | + |
| 107 | + for _, test := range tests { |
| 108 | + t.Run(test.name, func(t *testing.T) { |
| 109 | + warnings := endpointsWarnings(test.endpoints) |
| 110 | + ok := len(warnings) == len(test.warnings) |
| 111 | + if ok { |
| 112 | + for i := range warnings { |
| 113 | + if !strings.HasPrefix(warnings[i], test.warnings[i]) { |
| 114 | + ok = false |
| 115 | + break |
| 116 | + } |
| 117 | + } |
| 118 | + } |
| 119 | + if !ok { |
| 120 | + t.Errorf("Expected warnings for %v, got %v", test.warnings, warnings) |
| 121 | + } |
| 122 | + }) |
| 123 | + } |
| 124 | +} |
0 commit comments