-
Notifications
You must be signed in to change notification settings - Fork 1
/
getters_test.go
137 lines (122 loc) · 3.47 KB
/
getters_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package befehl
import (
"fmt"
"os"
"testing"
"time"
"github.com/sgsullivan/befehl/helpers/filesystem"
)
func getZeroValOpts() *Instance {
if i, e := New(&Options{
PrivateKeyFile: "",
LogDir: "",
SshHostKeyConfig: SshHostKeyConfig{
Enabled: false,
KnownHostsPath: "",
},
RunConfigPath: "unit-test-resources/zero-hosts.json",
}); e != nil {
panic(e)
} else {
return i
}
}
func getNonZeroValOpts() *Instance {
i, err := New(&Options{
PrivateKeyFile: "foo",
LogDir: "baz",
SshHostKeyConfig: SshHostKeyConfig{
Enabled: true,
KnownHostsPath: defaultKnownHosts,
},
RunConfigPath: "unit-test-resources/hosts.json",
})
if err != nil {
panic(err)
}
return i
}
func getDuplicateHostsInConfigInstance(configPath string) (*Instance, error) {
return New(&Options{
PrivateKeyFile: "foo",
LogDir: "baz",
SshHostKeyConfig: SshHostKeyConfig{
Enabled: true,
KnownHostsPath: defaultKnownHosts,
},
RunConfigPath: configPath,
})
}
var defaultSshPath = os.Getenv("HOME") + "/.ssh"
var defaultKnownHosts = defaultSshPath + "/known_hosts"
func init() {
if !filesystem.PathExists(defaultSshPath) {
if err := os.Mkdir(defaultSshPath, os.ModePerm); err != nil {
panic(fmt.Sprintf("failed to create %s: %s", defaultSshPath, err))
}
}
if !filesystem.FileExists(defaultKnownHosts) {
f, err := os.Create(defaultKnownHosts)
if err != nil {
panic(fmt.Sprintf("failed to create %s: %s", defaultKnownHosts, err))
}
f.Close()
}
}
func TestDuplicateHosts(t *testing.T) {
if _, e := getDuplicateHostsInConfigInstance("unit-test-resources/hosts-duplicate.json"); e == nil {
t.Fatal("didn't return an error for duplicate host entries")
}
}
func TestGetSshUser(t *testing.T) {
zuser := getZeroValOpts().getDefaultSshUser()
if zuser != "root" {
t.Fatalf("User [%s] for zeroval is unexpected", zuser)
}
nuser := getNonZeroValOpts().getDefaultSshUser()
if nuser != "r00t" {
t.Fatalf("User [%s] for nonzeroval is unexpected", nuser)
}
}
func TestGetLogDir(t *testing.T) {
logDirZero := getZeroValOpts().getLogDir()
if logDirZero != os.Getenv("HOME")+"/befehl/logs" {
t.Fatalf("LogDir of %s for zeroval is unexpected", logDirZero)
}
logDir := getNonZeroValOpts().getLogDir()
if logDir != "baz" {
t.Fatalf("LogDir of %s for zeroval is unexpected", logDir)
}
}
func TestGetLogFilePath(t *testing.T) {
got := getZeroValOpts().getLogFilePath("server")
expected := os.Getenv("HOME") + "/befehl/logs" + "/server"
if got != expected {
t.Fatalf("getLogFilePath got [%s] expected [%s]", got, expected)
}
}
func TestGetPrivKeyFile(t *testing.T) {
if getZeroValOpts().getPrivKeyFile() != os.Getenv("HOME")+"/.ssh/id_rsa" {
t.Fatal("PrivateKeyFile for zeroval is unexpected")
}
if getNonZeroValOpts().getPrivKeyFile() != "foo" {
t.Fatal("PrivateKeyFile for nonzeroval is unexpected")
}
}
func TestGetSshClientConfig(t *testing.T) {
got, err := getNonZeroValOpts().getSshClientConfig(func() string { return getNonZeroValOpts().getDefaultSshUser() })
if err != nil {
t.Fatal(err)
}
if got.Timeout != time.Duration(10)*time.Second {
t.Fatalf("returned timeout %s is unexpected", got.Timeout)
}
}
func TestGetSshKnowHostsPath(t *testing.T) {
if getZeroValOpts().getSshKnowHostsPath() != os.Getenv("HOME")+"/.ssh/known_hosts" {
t.Fatal("getSshKnowHostsPath for zeroval is unexpected")
}
if getNonZeroValOpts().getSshKnowHostsPath() != defaultKnownHosts {
t.Fatal("PrivateKeyFile for nonzeroval is unexpected")
}
}