Skip to content

Commit eda20e8

Browse files
authored
Merge pull request #162 from gkGaneshR/unit-test-options
Unit testing for SetNodeNameOrDie in package cmd/options
2 parents 24ae2d8 + 821b8f4 commit eda20e8

File tree

2 files changed

+85
-1
lines changed

2 files changed

+85
-1
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ Dockerfile: Dockerfile.in
8686
sed -e 's|@BASEIMAGE@|$(BASEIMAGE)|g' $< >$@
8787

8888
test: vet fmt
89-
go test -timeout=1m -v -race ./pkg/... $(BUILD_TAGS)
89+
go test -timeout=1m -v -race ./cmd/options ./pkg/... $(BUILD_TAGS)
9090

9191
build-container: ./bin/node-problem-detector Dockerfile
9292
docker build -t $(IMAGE) .

cmd/options/options_test.go

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/*
2+
Copyright 2018 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 options
18+
19+
import (
20+
"os"
21+
"testing"
22+
)
23+
24+
type options struct {
25+
Nodename string
26+
HostnameOverride string
27+
}
28+
29+
// TestSetNodeNameOrDie tests for permutations of nodename, hostname and hostnameoverride.
30+
func TestSetNodeNameOrDie(t *testing.T) {
31+
hostName, err := os.Hostname()
32+
if err != nil {
33+
t.Errorf("Query hostname error: %v", err)
34+
}
35+
36+
uts := map[string]struct {
37+
WantedNodeName string
38+
Meta options
39+
}{
40+
"Check hostname override only": {
41+
WantedNodeName: "hostname-override",
42+
Meta: options{
43+
Nodename: "node-name-env",
44+
HostnameOverride: "hostname-override",
45+
},
46+
},
47+
"Check hostname override and NDDE_NAME env": {
48+
WantedNodeName: "node-name-env",
49+
Meta: options{
50+
Nodename: "node-name-env",
51+
HostnameOverride: "",
52+
},
53+
},
54+
"Check hostname override, NODE_NAME env and hostname": {
55+
WantedNodeName: hostName,
56+
Meta: options{
57+
Nodename: "",
58+
HostnameOverride: "",
59+
},
60+
},
61+
}
62+
63+
for desc, ut := range uts {
64+
err := os.Unsetenv("NODE_NAME")
65+
if err != nil {
66+
t.Errorf("Desc: %v. Unset NODE_NAME env error: %v", desc, err)
67+
}
68+
69+
if len(ut.Meta.Nodename) != 0 {
70+
err := os.Setenv("NODE_NAME", ut.Meta.Nodename)
71+
if err != nil {
72+
t.Errorf("Desc: %v. Set NODE_NAME env error: %v", desc, err)
73+
}
74+
}
75+
76+
npdOpts := NewNodeProblemDetectorOptions()
77+
npdOpts.HostnameOverride = ut.Meta.HostnameOverride
78+
npdOpts.SetNodeNameOrDie()
79+
80+
if npdOpts.NodeName != ut.WantedNodeName {
81+
t.Errorf("Desc: %v. Set node name error. Wanted: %v. Got: %v", desc, ut.WantedNodeName, npdOpts.NodeName)
82+
}
83+
}
84+
}

0 commit comments

Comments
 (0)