mirror of
https://github.com/kubernetes/node-problem-detector.git
synced 2026-02-14 18:09:57 +00:00
Unit testing for SetNodeNameOrDie in package cmd/options
1. Why is this change necessary ? fixes: kubernetes/node-problem-detector#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 <gkganesh126@gmail.com>
This commit is contained in:
134
cmd/options/options_test.go
Normal file
134
cmd/options/options_test.go
Normal file
@@ -0,0 +1,134 @@
|
||||
package options
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type Options struct {
|
||||
Nodename string
|
||||
Hostname string
|
||||
HostnameOverride string
|
||||
}
|
||||
|
||||
//TestSetNodeNameOrDie tests for permutations of nodename, hostname and hostnameoverride
|
||||
func TestSetNodeNameOrDie(t *testing.T) {
|
||||
options := map[string]struct {
|
||||
Expected Options
|
||||
ObtainedNodeName string
|
||||
}{
|
||||
"Check Node and HostnameOverride only": {
|
||||
Expected: Options{
|
||||
Nodename: "my-node-name",
|
||||
Hostname: "",
|
||||
HostnameOverride: "override",
|
||||
},
|
||||
},
|
||||
"Check Nodename only": {
|
||||
Expected: Options{
|
||||
Nodename: "my-node-name",
|
||||
Hostname: "",
|
||||
HostnameOverride: "",
|
||||
},
|
||||
},
|
||||
|
||||
"Check HostnameOverride only": {
|
||||
Expected: Options{
|
||||
Nodename: "",
|
||||
Hostname: "",
|
||||
HostnameOverride: "override",
|
||||
},
|
||||
},
|
||||
"Check Hostname only": {
|
||||
Expected: Options{
|
||||
Nodename: "",
|
||||
Hostname: "my-host-name",
|
||||
HostnameOverride: "",
|
||||
},
|
||||
},
|
||||
"Check Node, host and HostnameOverride only": {
|
||||
Expected: Options{
|
||||
Nodename: "my-node-name",
|
||||
Hostname: "my-host-name",
|
||||
HostnameOverride: "override",
|
||||
},
|
||||
},
|
||||
|
||||
"Check Host and HostnameOverride only": {
|
||||
Expected: Options{
|
||||
Nodename: "",
|
||||
Hostname: "my-host-name",
|
||||
HostnameOverride: "override",
|
||||
},
|
||||
},
|
||||
|
||||
"Check Node and hostname": {
|
||||
Expected: Options{
|
||||
Nodename: "my-node-name",
|
||||
Hostname: "my-host-name",
|
||||
HostnameOverride: "",
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
orig_node_name := os.Getenv("NODE_NAME")
|
||||
orig_host_name, err := os.Hostname()
|
||||
if err != nil {
|
||||
fmt.Println("Unable to get hostname")
|
||||
}
|
||||
|
||||
for str, opt := range options {
|
||||
if opt.Expected.Nodename != "" {
|
||||
err = os.Setenv("NODE_NAME", opt.Expected.Nodename)
|
||||
if err != nil {
|
||||
t.Errorf("Unable to set env NODE_NAME")
|
||||
}
|
||||
}
|
||||
|
||||
if opt.Expected.Hostname != "" {
|
||||
//Changing hostname
|
||||
cmd := exec.Command("hostname", opt.Expected.Hostname)
|
||||
_, err := cmd.CombinedOutput()
|
||||
if err != nil {
|
||||
//If changing hostname requires admin privilege
|
||||
cmd = exec.Command("sudo", "hostname", opt.Expected.Hostname)
|
||||
_, err := cmd.CombinedOutput()
|
||||
if err != nil {
|
||||
fmt.Println("Unable to change hostname")
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
npdObj := NewNodeProblemDetectorOptions()
|
||||
npdObj.HostnameOverride = opt.Expected.HostnameOverride
|
||||
npdObj.SetNodeNameOrDie()
|
||||
opt.ObtainedNodeName = npdObj.NodeName
|
||||
|
||||
if opt.ObtainedNodeName != opt.Expected.HostnameOverride &&
|
||||
opt.ObtainedNodeName != opt.Expected.Nodename &&
|
||||
opt.ObtainedNodeName != opt.Expected.Hostname {
|
||||
t.Errorf("Error at : %+v", str)
|
||||
t.Errorf("Wanted: %+v. \nGot: %+v", opt.Expected.Nodename, opt.ObtainedNodeName)
|
||||
}
|
||||
|
||||
err = os.Setenv("NODE_NAME", "")
|
||||
if err != nil {
|
||||
t.Errorf("Unable to set env NODE_NAME empty")
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
err = os.Setenv("NODE_NAME", orig_node_name)
|
||||
if err != nil {
|
||||
fmt.Println("Unable to set original : env NODE_NAME")
|
||||
}
|
||||
cmd := exec.Command("sudo", "hostname", orig_host_name)
|
||||
_, err = cmd.CombinedOutput()
|
||||
if err != nil {
|
||||
fmt.Println("Unable to set hostname")
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user