Skip to content

Commit 25b6c16

Browse files
committed
Unit testing for SetNodeNameOrDie in package cmd/options
1. Why is this change necessary ? fixes: #161 2. How does this change address the issue ? Under package cmd/options, the testing for SetNodeNameOrDie need to decide Nodename based on environment variable "NODE_NAME" or hostname or hostnameoverride variable. 3. How to verify this change ? Run "go test" with admin privilege Signed-off-by: gkGaneshR <[email protected]>
1 parent cf3519a commit 25b6c16

File tree

1 file changed

+134
-0
lines changed

1 file changed

+134
-0
lines changed

cmd/options/options_test.go

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
package options
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"os/exec"
7+
"testing"
8+
)
9+
10+
type Options struct {
11+
Nodename string
12+
Hostname string
13+
HostnameOverride string
14+
}
15+
16+
//TestSetNodeNameOrDie tests for permutations of nodename, hostname and hostnameoverride
17+
func TestSetNodeNameOrDie(t *testing.T) {
18+
options := map[string]struct {
19+
Expected Options
20+
ObtainedNodeName string
21+
}{
22+
"Check Node and HostnameOverride only": {
23+
Expected: Options{
24+
Nodename: "my-node-name",
25+
Hostname: "",
26+
HostnameOverride: "override",
27+
},
28+
},
29+
"Check Nodename only": {
30+
Expected: Options{
31+
Nodename: "my-node-name",
32+
Hostname: "",
33+
HostnameOverride: "",
34+
},
35+
},
36+
37+
"Check HostnameOverride only": {
38+
Expected: Options{
39+
Nodename: "",
40+
Hostname: "",
41+
HostnameOverride: "override",
42+
},
43+
},
44+
"Check Hostname only": {
45+
Expected: Options{
46+
Nodename: "",
47+
Hostname: "my-host-name",
48+
HostnameOverride: "",
49+
},
50+
},
51+
"Check Node, host and HostnameOverride only": {
52+
Expected: Options{
53+
Nodename: "my-node-name",
54+
Hostname: "my-host-name",
55+
HostnameOverride: "override",
56+
},
57+
},
58+
59+
"Check Host and HostnameOverride only": {
60+
Expected: Options{
61+
Nodename: "",
62+
Hostname: "my-host-name",
63+
HostnameOverride: "override",
64+
},
65+
},
66+
67+
"Check Node and hostname": {
68+
Expected: Options{
69+
Nodename: "my-node-name",
70+
Hostname: "my-host-name",
71+
HostnameOverride: "",
72+
},
73+
},
74+
}
75+
76+
orig_node_name := os.Getenv("NODE_NAME")
77+
orig_host_name, err := os.Hostname()
78+
if err != nil {
79+
fmt.Println("Unable to get hostname")
80+
}
81+
82+
for str, opt := range options {
83+
if opt.Expected.Nodename != "" {
84+
err = os.Setenv("NODE_NAME", opt.Expected.Nodename)
85+
if err != nil {
86+
t.Errorf("Unable to set env NODE_NAME")
87+
}
88+
}
89+
90+
if opt.Expected.Hostname != "" {
91+
//Changing hostname
92+
cmd := exec.Command("hostname", opt.Expected.Hostname)
93+
_, err := cmd.CombinedOutput()
94+
if err != nil {
95+
//If changing hostname requires admin privilege
96+
cmd = exec.Command("sudo", "hostname", opt.Expected.Hostname)
97+
_, err := cmd.CombinedOutput()
98+
if err != nil {
99+
fmt.Println("Unable to change hostname")
100+
return
101+
}
102+
}
103+
}
104+
105+
npdObj := NewNodeProblemDetectorOptions()
106+
npdObj.HostnameOverride = opt.Expected.HostnameOverride
107+
npdObj.SetNodeNameOrDie()
108+
opt.ObtainedNodeName = npdObj.NodeName
109+
110+
if opt.ObtainedNodeName != opt.Expected.HostnameOverride &&
111+
opt.ObtainedNodeName != opt.Expected.Nodename &&
112+
opt.ObtainedNodeName != opt.Expected.Hostname {
113+
t.Errorf("Error at : %+v", str)
114+
t.Errorf("Wanted: %+v. \nGot: %+v", opt.Expected.Nodename, opt.ObtainedNodeName)
115+
}
116+
117+
err = os.Setenv("NODE_NAME", "")
118+
if err != nil {
119+
t.Errorf("Unable to set env NODE_NAME empty")
120+
}
121+
122+
}
123+
124+
err = os.Setenv("NODE_NAME", orig_node_name)
125+
if err != nil {
126+
fmt.Println("Unable to set original : env NODE_NAME")
127+
}
128+
cmd := exec.Command("sudo", "hostname", orig_host_name)
129+
_, err = cmd.CombinedOutput()
130+
if err != nil {
131+
fmt.Println("Unable to set hostname")
132+
}
133+
134+
}

0 commit comments

Comments
 (0)