|
| 1 | +/* |
| 2 | +Copyright 2019 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 metaproxier |
| 18 | + |
| 19 | +import ( |
| 20 | + "reflect" |
| 21 | + "testing" |
| 22 | + |
| 23 | + v1 "k8s.io/api/core/v1" |
| 24 | +) |
| 25 | + |
| 26 | +func Test_endpointsIPFamily(t *testing.T) { |
| 27 | + |
| 28 | + ipv4 := v1.IPv4Protocol |
| 29 | + ipv6 := v1.IPv6Protocol |
| 30 | + |
| 31 | + tests := []struct { |
| 32 | + name string |
| 33 | + endpoints *v1.Endpoints |
| 34 | + want *v1.IPFamily |
| 35 | + wantErr bool |
| 36 | + errorMsg string |
| 37 | + }{ |
| 38 | + { |
| 39 | + name: "Endpoints No Subsets", |
| 40 | + endpoints: &v1.Endpoints{}, |
| 41 | + want: nil, |
| 42 | + wantErr: true, |
| 43 | + errorMsg: "failed to identify ipfamily for endpoints (no subsets)", |
| 44 | + }, |
| 45 | + { |
| 46 | + name: "Endpoints No Addresses", |
| 47 | + endpoints: &v1.Endpoints{Subsets: []v1.EndpointSubset{{NotReadyAddresses: []v1.EndpointAddress{}}}}, |
| 48 | + want: nil, |
| 49 | + wantErr: true, |
| 50 | + errorMsg: "failed to identify ipfamily for endpoints (no addresses)", |
| 51 | + }, |
| 52 | + { |
| 53 | + name: "Endpoints Address Has No IP", |
| 54 | + endpoints: &v1.Endpoints{Subsets: []v1.EndpointSubset{{Addresses: []v1.EndpointAddress{{Hostname: "testhost", IP: ""}}}}}, |
| 55 | + want: nil, |
| 56 | + wantErr: true, |
| 57 | + errorMsg: "failed to identify ipfamily for endpoints (address has no ip)", |
| 58 | + }, |
| 59 | + { |
| 60 | + name: "Endpoints Address IPv4", |
| 61 | + endpoints: &v1.Endpoints{Subsets: []v1.EndpointSubset{{Addresses: []v1.EndpointAddress{{IP: "1.2.3.4"}}}}}, |
| 62 | + want: &ipv4, |
| 63 | + wantErr: false, |
| 64 | + }, |
| 65 | + { |
| 66 | + name: "Endpoints Address IPv6", |
| 67 | + endpoints: &v1.Endpoints{Subsets: []v1.EndpointSubset{{Addresses: []v1.EndpointAddress{{IP: "2001:db9::2"}}}}}, |
| 68 | + want: &ipv6, |
| 69 | + wantErr: false, |
| 70 | + }, |
| 71 | + } |
| 72 | + |
| 73 | + for _, tt := range tests { |
| 74 | + t.Run(tt.name, func(t *testing.T) { |
| 75 | + got, err := endpointsIPFamily(tt.endpoints) |
| 76 | + if (err != nil) != tt.wantErr { |
| 77 | + t.Errorf("endpointsIPFamily() error = %v, wantErr %v", err, tt.wantErr) |
| 78 | + return |
| 79 | + } |
| 80 | + if err != nil && err.Error() != tt.errorMsg { |
| 81 | + t.Errorf("endpointsIPFamily() error = %v, wantErr %v", err, tt.errorMsg) |
| 82 | + return |
| 83 | + } |
| 84 | + if !reflect.DeepEqual(got, tt.want) { |
| 85 | + t.Errorf("endpointsIPFamily() = %v, want %v", got, tt.want) |
| 86 | + } |
| 87 | + }) |
| 88 | + } |
| 89 | +} |
0 commit comments