Skip to content

Commit 8dff9be

Browse files
committed
Unit tests for main and config
Low value, but still...
1 parent 85715a5 commit 8dff9be

File tree

2 files changed

+79
-0
lines changed

2 files changed

+79
-0
lines changed

config/config_test.go

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package config
2+
3+
import (
4+
"os"
5+
"testing"
6+
7+
"github.com/bpineau/katafygio/pkg/log"
8+
)
9+
10+
const nonExistentPath = "\\/hopefully/non/existent/path"
11+
12+
func TestConfig(t *testing.T) {
13+
conf := &KfConfig{
14+
DryRun: true,
15+
Logger: log.New("info", "", "test"),
16+
}
17+
18+
err := conf.Init("http://127.0.0.1", nonExistentPath)
19+
if err == nil {
20+
t.Error("conf.Init() should fail on non existent kubeconfig path")
21+
}
22+
23+
here, _ := os.Getwd()
24+
_ = os.Setenv("HOME", here+"/../assets")
25+
err = conf.Init("", "")
26+
if err != nil {
27+
t.Error("conf.Init() with no arguments should find a .kube/config in $HOME")
28+
}
29+
30+
}

main_test.go

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package main
2+
3+
import (
4+
"bytes"
5+
"testing"
6+
7+
"github.com/bpineau/katafygio/cmd"
8+
)
9+
10+
func TestMain(t *testing.T) {
11+
var ok = true
12+
13+
privateExitHandler = func(c int) {
14+
ok = false
15+
}
16+
17+
//cmd.FakeCS = true
18+
cmd.RootCmd.SetOutput(new(bytes.Buffer))
19+
20+
// test with normal exit
21+
cmd.RootCmd.SetArgs([]string{"--help"})
22+
main()
23+
24+
if !ok {
25+
t.Errorf("main() failed")
26+
}
27+
28+
// test with failure
29+
cmd.RootCmd.SetArgs([]string{"--unexpected-arg"})
30+
main()
31+
32+
if ok {
33+
t.Errorf("main() should fail with unexpected arguments")
34+
}
35+
}
36+
37+
func TestExitWrapper(t *testing.T) {
38+
var ok = false
39+
40+
privateExitHandler = func(c int) {
41+
ok = true
42+
}
43+
44+
ExitWrapper(1)
45+
46+
if !ok {
47+
t.Errorf("Error in ExitWrapper()")
48+
}
49+
}

0 commit comments

Comments
 (0)