k8s.io/kubernetes@v1.31.0-alpha.0.0.20240520171757-56147500dadc/pkg/kubelet/util/util_test.go (about) 1 /* 2 Copyright 2020 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/stretchr/testify/assert" 23 ) 24 25 func TestGetNodenameForKernel(t *testing.T) { 26 testcases := []struct { 27 description string 28 hostname string 29 hostDomain string 30 setHostnameAsFQDN bool 31 expectedHostname string 32 expectError bool 33 }{{ 34 description: "no hostDomain, setHostnameAsFQDN false", 35 hostname: "test.pod.hostname", 36 hostDomain: "", 37 setHostnameAsFQDN: false, 38 expectedHostname: "test.pod.hostname", 39 expectError: false, 40 }, { 41 description: "no hostDomain, setHostnameAsFQDN true", 42 hostname: "test.pod.hostname", 43 hostDomain: "", 44 setHostnameAsFQDN: true, 45 expectedHostname: "test.pod.hostname", 46 expectError: false, 47 }, { 48 description: "valid hostDomain, setHostnameAsFQDN false", 49 hostname: "test.pod.hostname", 50 hostDomain: "svc.subdomain.local", 51 setHostnameAsFQDN: false, 52 expectedHostname: "test.pod.hostname", 53 expectError: false, 54 }, { 55 description: "valid hostDomain, setHostnameAsFQDN true", 56 hostname: "test.pod.hostname", 57 hostDomain: "svc.subdomain.local", 58 setHostnameAsFQDN: true, 59 expectedHostname: "test.pod.hostname.svc.subdomain.local", 60 expectError: false, 61 }, { 62 description: "FQDN is too long, setHostnameAsFQDN false", 63 hostname: "1234567.1234567", //8*2-1=15 chars 64 hostDomain: "1234567.1234567.1234567.1234567.1234567.1234567.1234567", //8*7-1=55 chars 65 setHostnameAsFQDN: false, //FQDN=15 + 1(dot) + 55 = 71 chars 66 expectedHostname: "1234567.1234567", 67 expectError: false, 68 }, { 69 description: "FQDN is too long, setHostnameAsFQDN true", 70 hostname: "1234567.1234567", //8*2-1=15 chars 71 hostDomain: "1234567.1234567.1234567.1234567.1234567.1234567.1234567", //8*7-1=55 chars 72 setHostnameAsFQDN: true, //FQDN=15 + 1(dot) + 55 = 71 chars 73 expectedHostname: "", 74 expectError: true, 75 }} 76 77 for _, tc := range testcases { 78 t.Logf("TestCase: %q", tc.description) 79 outputHostname, err := GetNodenameForKernel(tc.hostname, tc.hostDomain, &tc.setHostnameAsFQDN) 80 if tc.expectError { 81 assert.Error(t, err) 82 } else { 83 assert.NoError(t, err) 84 } 85 assert.Equal(t, tc.expectedHostname, outputHostname) 86 } 87 88 }