1
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",
64 hostDomain: "1234567.1234567.1234567.1234567.1234567.1234567.1234567",
65 setHostnameAsFQDN: false,
66 expectedHostname: "1234567.1234567",
67 expectError: false,
68 }, {
69 description: "FQDN is too long, setHostnameAsFQDN true",
70 hostname: "1234567.1234567",
71 hostDomain: "1234567.1234567.1234567.1234567.1234567.1234567.1234567",
72 setHostnameAsFQDN: true,
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 }
89
View as plain text