-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcheck_test.go
208 lines (156 loc) · 3.95 KB
/
check_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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
package main
import (
"fmt"
"os"
"strconv"
"strings"
"testing"
"time"
"github.com/spf13/pflag"
"github.com/stretchr/testify/assert"
)
const DefaultTimeout = 15 * time.Second
func TestConfig_Validate(t *testing.T) {
c := &Config{}
assert.Error(t, c.Validate())
// Most basic settings
c.Host = "localhost"
c.Command = "Get-Something"
c.User = "administrator"
c.Password = "verysecret"
assert.NoError(t, c.Validate())
assert.Equal(t, c.Port, TlsPort)
assert.False(t, c.NoTls)
assert.Equal(t, c.AuthType, AuthDefault)
assert.True(t, c.validated)
}
func TestBuildConfigFlags(t *testing.T) {
fs := &pflag.FlagSet{}
config := BuildConfigFlags(fs)
assert.True(t, fs.HasFlags())
assert.False(t, config.validated)
}
func TestConfig_BuildCommand(t *testing.T) {
c := &Config{Command: "Get-Something"}
assert.Contains(t, c.BuildCommand(), "powershell.exe -EncodedCommand")
c = &Config{IcingaCommand: "Icinga-CheckSomething"}
assert.Contains(t, c.BuildCommand(), "powershell.exe -EncodedCommand")
}
func TestConfig_Run_WithError(t *testing.T) {
c := &Config{
Host: "192.0.2.11",
User: "admin",
Password: "test",
Command: "Get-Host",
NoTls: true,
}
err := c.Validate()
assert.NoError(t, err)
err, _, _ = c.Run(1 * time.Second)
assert.Error(t, err)
assert.Contains(t, err.Error(), "dial tcp 192.0.2.11:")
}
func TestConfig_Run_Basic(t *testing.T) {
if os.Getenv("WINRM_SKIP_BASIC") != "" {
t.Skip("WINRM_SKIP_BASIC has been set")
}
if os.Getenv("WINRM_SKIP_UNENCRYPTED") != "" {
t.Skip("WINRM_SKIP_UNENCRYPTED has been set")
}
c := buildEnvConfig(t, AuthBasic)
c.NoTls = true
fmt.Printf("%v\n", c)
runCheck(t, c)
}
func TestConfig_Run_Basic_WithTLS(t *testing.T) {
if os.Getenv("WINRM_SKIP_BASIC") != "" {
t.Skip("WINRM_SKIP_BASIC has been set")
}
c := buildEnvConfig(t, AuthBasic)
setupTlsFromEnv(t, c)
err := c.Validate()
assert.NoError(t, err)
fmt.Printf("%v\n", c)
runCheck(t, c)
}
func TestConfig_Run_NTLM(t *testing.T) {
if os.Getenv("WINRM_SKIP_UNENCRYPTED") != "" {
t.Skip("WINRM_SKIP_UNENCRYPTED has been set")
}
c := buildEnvConfig(t, AuthNTLM)
c.NoTls = true
err := c.Validate()
assert.NoError(t, err)
fmt.Printf("%v\n", c)
runCheck(t, c)
}
func TestConfig_Run_NTLM_WithTls(t *testing.T) {
c := buildEnvConfig(t, AuthNTLM)
setupTlsFromEnv(t, c)
err := c.Validate()
assert.NoError(t, err)
fmt.Printf("%v\n", c)
runCheck(t, c)
}
func TestConfig_Run_TLS(t *testing.T) {
c := buildEnvConfig(t, AuthTLS)
setupTlsFromEnv(t, c)
if c.TlsCertPath == "" {
t.Skip("WINRM_TLS_CERT not set")
}
err := c.Validate()
assert.NoError(t, err)
fmt.Printf("%v\n", c)
runCheck(t, c)
}
func runCheck(t *testing.T, c *Config) {
err := c.Validate()
assert.NoError(t, err)
err, rc, output := c.Run(DefaultTimeout)
assert.NoError(t, err)
assert.Equal(t, 0, rc)
assert.Contains(t, output, "ConsoleHost")
}
func buildEnvConfig(t *testing.T, auth string) *Config {
host := os.Getenv("WINRM_HOST")
if host == "" {
t.Skip("No env config for WINRM_*")
}
c := &Config{
Host: host,
User: os.Getenv("WINRM_USER"),
Password: os.Getenv("WINRM_PASSWORD"),
Command: "Get-Host",
AuthType: auth,
}
verb := strings.ToUpper(auth)
if user := os.Getenv("WINRM_" + verb + "_USER"); user != "" {
c.User = user
}
if password := os.Getenv("WINRM_" + verb + "_PASSWORD"); password != "" {
c.Password = password
}
return c
}
func setupTlsFromEnv(t *testing.T, c *Config) {
if os.Getenv("WINRM_SKIP_TLS") != "" {
t.Skip("WINRM_SKIP_TLS has been set")
}
if os.Getenv("WINRM_INSECURE") != "" {
c.Insecure = true
}
if file := os.Getenv("WINRM_TLS_CA"); file != "" {
c.TlsCAPath = file
}
if file := os.Getenv("WINRM_TLS_CERT"); file != "" {
c.TlsCertPath = file
}
if file := os.Getenv("WINRM_TLS_KEY"); file != "" {
c.TlsKeyPath = file
}
if file := os.Getenv("WINRM_TLS_PORT"); file != "" {
tmp, err := strconv.ParseInt(file, 10, 16)
assert.NoError(t, err)
c.Port = int(tmp)
}
}