|
| 1 | +/* |
| 2 | +Copyright 2022 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 util |
| 18 | + |
| 19 | +import ( |
| 20 | + "testing" |
| 21 | + |
| 22 | + "github.com/onsi/gomega" |
| 23 | +) |
| 24 | + |
| 25 | +func TestSanitizeHostInfoLabel(t *testing.T) { |
| 26 | + t.Run("for IP addresses", testIPAddressLogic) |
| 27 | + t.Run("for DNS entries", testDNSLogic) |
| 28 | +} |
| 29 | + |
| 30 | +func testIPAddressLogic(t *testing.T) { |
| 31 | + tests := []struct { |
| 32 | + name, input, expected string |
| 33 | + }{ |
| 34 | + { |
| 35 | + name: "for valid IPv4 address", |
| 36 | + input: "1.2.3.4", |
| 37 | + expected: "1.2.3.4", |
| 38 | + }, |
| 39 | + { |
| 40 | + name: "for valid IPv6 address", |
| 41 | + input: "2620:124:6020:c003:0:69ff:fe59:80ac", |
| 42 | + expected: "2620-124-6020-c003-0-69ff-fe59-80ac.ipv6-literal", |
| 43 | + }, |
| 44 | + { |
| 45 | + name: "for a shorthand valid IPv6 address", |
| 46 | + input: "2620::c003:0:69ff:fe59:80ac", |
| 47 | + expected: "2620--c003-0-69ff-fe59-80ac.ipv6-literal", |
| 48 | + }, |
| 49 | + { |
| 50 | + name: "for a valid IPv6 address with zone index", |
| 51 | + input: "2620:124:6020:c003:0:69ff:fe59:80ac%3", |
| 52 | + expected: "2620-124-6020-c003-0-69ff-fe59-80ac.ipv6-literal", |
| 53 | + }, |
| 54 | + } |
| 55 | + |
| 56 | + for _, tt := range tests { |
| 57 | + t.Run(tt.name, func(t *testing.T) { |
| 58 | + g := gomega.NewGomegaWithT(t) |
| 59 | + g.Expect(SanitizeHostInfoLabel(tt.input)).To(gomega.Equal(tt.expected)) |
| 60 | + }) |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +func testDNSLogic(t *testing.T) { |
| 65 | + tests := []struct { |
| 66 | + name, input, expected string |
| 67 | + }{ |
| 68 | + { |
| 69 | + name: "for DNS entry with less than 63 characters", |
| 70 | + input: "foo-1.bar.com", |
| 71 | + expected: "foo-1.bar.com", |
| 72 | + }, |
| 73 | + { |
| 74 | + name: "for DNS entry with 63 characters", |
| 75 | + input: "esx13-r09.p01.1d91f0ee4f14b7e83bd42.australiaeast.avs.belch.com", |
| 76 | + expected: "esx13-r09.p01.1d91f0ee4f14b7e83bd42.australiaeast.avs.belch.com", |
| 77 | + }, |
| 78 | + { |
| 79 | + name: "for DNS entry with > 63 characters", |
| 80 | + input: "esx13-r09.p01.1d91f0ee4f14b7e83bd420.australiaeast.avs.belch.com", |
| 81 | + expected: "esx13-r09.p01.1d91f0ee4f14b7e83bd420.australiaeast.avs.belch", |
| 82 | + }, |
| 83 | + { |
| 84 | + name: "for DNS entry with > 63 characters with multiple segments dropped", |
| 85 | + input: "esx13-r09.p01.1d91f0ee4f14b7e83bd420.australiaeast.avs.belch.com.us", |
| 86 | + expected: "esx13-r09.p01.1d91f0ee4f14b7e83bd420.australiaeast.avs.belch", |
| 87 | + }, |
| 88 | + { |
| 89 | + name: "for DNS entry with > 63 characters with length = 63 after truncation", |
| 90 | + input: "esx13-r09.p01.1d91f0ee4f14b7e83bd420az.australiaeast.avs.belch.us", |
| 91 | + expected: "esx13-r09.p01.1d91f0ee4f14b7e83bd420az.australiaeast.avs.belch", |
| 92 | + }, |
| 93 | + { |
| 94 | + name: "for DNS entry with > 63 characters with first segment > 63 characters", |
| 95 | + input: "esx-zcvU3CecjX8Tr5qXQgztj9ZKCp369p3hLFdzAu8VwEyWGq4hzkLTNZq089TI.p01.1d91f0ee4f14b7e83bd420.australiaeast.avs.belch.com", |
| 96 | + expected: "esx-zcvU3CecjX8Tr5qXQgztj9ZKCp369p3hLFdzAu8VwEyWGq4hzkLTNZq089T", |
| 97 | + }, |
| 98 | + { |
| 99 | + name: "for DNS entry with > 63 characters with first segment = 63 characters", |
| 100 | + input: "esx-zcvU3CecjX8Tr5qXQgztj9ZKCp369p3hLFdzAu8VwEyWGq4hzkLTNZq089T.p01.1d91f0ee4f14b7e83bd420.australiaeast.avs.belch.com", |
| 101 | + expected: "esx-zcvU3CecjX8Tr5qXQgztj9ZKCp369p3hLFdzAu8VwEyWGq4hzkLTNZq089T", |
| 102 | + }, |
| 103 | + } |
| 104 | + |
| 105 | + for _, tt := range tests { |
| 106 | + t.Run(tt.name, func(t *testing.T) { |
| 107 | + g := gomega.NewGomegaWithT(t) |
| 108 | + g.Expect(SanitizeHostInfoLabel(tt.input)).To(gomega.Equal(tt.expected)) |
| 109 | + }) |
| 110 | + } |
| 111 | +} |
0 commit comments