knative.dev/pkg@v0.0.0-20260602142205-ac97e43f6622/network/domain_test.go (about) 1 /* 2 Copyright 2019 The Knative 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 network 18 19 import ( 20 "strings" 21 "testing" 22 ) 23 24 func TestGetDomainName(t *testing.T) { 25 tests := []struct { 26 name string 27 env string 28 resolvConf string 29 want string 30 }{{ 31 name: "all good", 32 resolvConf: ` 33 nameserver 1.1.1.1 34 search default.svc.abc.com svc.abc.com abc.com 35 options ndots:5 36 `, 37 want: "abc.com", 38 }, { 39 name: "env variable takes precedence over resolv.conf", 40 resolvConf: ` 41 nameserver 1.1.1.1 42 search default.svc.abc.com svc.abc.com abc.com 43 options ndots:5 44 `, 45 env: "mesh.net", 46 want: "mesh.net", 47 }, { 48 name: "env variable used when resolv.conf is empty", 49 resolvConf: ``, 50 env: "abc.com", 51 want: "abc.com", 52 }, { 53 name: "all good with trailing dot", 54 resolvConf: ` 55 nameserver 1.1.1.1 56 search default.svc.abc.com. svc.abc.com. abc.com. 57 options ndots:5 58 `, 59 want: "abc.com", 60 }, { 61 name: "missing search line", 62 resolvConf: ` 63 nameserver 1.1.1.1 64 options ndots:5 65 `, 66 want: defaultDomainName, 67 }, { 68 name: "non k8s resolv.conf format", 69 resolvConf: ` 70 nameserver 1.1.1.1 71 search abc.com xyz.com 72 options ndots:5 73 `, 74 want: defaultDomainName, 75 }} 76 77 for _, tt := range tests { 78 t.Run(tt.name, func(t *testing.T) { 79 if len(tt.env) > 0 { 80 t.Setenv(clusterDomainEnvKey, tt.env) 81 } 82 got := getClusterDomainName(strings.NewReader(tt.resolvConf)) 83 if got != tt.want { 84 t.Errorf("Test %s failed expected: %s but got: %s", tt.name, tt.want, got) 85 } 86 }) 87 } 88 }