forked from Nordstrom/ssh_exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ssh_exporter_test.go
209 lines (175 loc) · 5.13 KB
/
ssh_exporter_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
209
package main
//
// Copyright 2017 Nordstrom. All rights reserved.
//
//
// Provides Unit and Integration tests for ssh_exporter.go
//
// TODO: More tests! Always more tests.
//
import (
"github.com/Nordstrom/ssh_exporter/util"
"fmt"
"io/ioutil"
"net/http"
"os"
"os/exec"
"testing"
"time"
"gopkg.in/yaml.v2"
)
const (
// Used to parse a config and run integration tests
config = "test/config.yml"
// used to execute the local server
binary = "./ssh_exporter"
// Used to connect to the local server
address = "localhost:9428"
)
//
// Compares a string and a file.
// They should be identical.
//
func compare(computed, file_path string, t *testing.T) bool {
data, err := ioutil.ReadFile(file_path)
if err != nil {
t.Errorf("Error opening %s: %s", file_path, err)
}
expected := string(data)
if expected != computed {
t.Errorf("Expected output did not match computed output:\nExpected:\n%sGot:\n%s", expected, computed)
return false
}
return true
}
//
// Tests that the config parser is working correctly.
//
// This is done because ParseConfig does more than just yaml.Unmarshal,
// it also adds internal fields.
//
// Should produce a marshalled config similar to `test/config.yml` with some additional fields.
//
func TestUnitParseConfig(t *testing.T) {
fmt.Println("Running TestUnitParseConfig")
// Ensure the config file exist before continuing
if _, err := os.Stat(config); err != nil {
t.Errorf("%s config not avaliable, add it to continue first: %s", config, err)
t.Fail()
}
// Parse the config
conf, err := util.ParseConfig(config)
if err != nil {
t.Errorf("There was an error parsing config %s: %s", config, err)
t.Fail()
}
// Marshal the new config, should include more fields
marshalled_conf, err := yaml.Marshal(&conf)
if err != nil {
t.Errorf("Error Marshaling loaded config file: %s", err)
t.Fail()
}
// Compare to the test's source of truth
compare(string(marshalled_conf), "test/parse_config.unit.txt", t)
}
//
// Tests that the we're able to output Prometheus data correctly.
//
// Should produce a string similar to the HTTP endpoint result.
//
func TestUnitPrometheusFormatResponse(t *testing.T) {
fmt.Println("Running TestUnitPrometheusFormatResponse")
parsedTime, _ := time.ParseDuration("1s")
prom_conf := util.Config{
Version: "v0",
Scripts: []util.ScriptConfig{
util.ScriptConfig{
Name: "scriptName",
Script: "echo foo",
Timeout: "5s",
Pattern: "foo",
Credentials: []util.CredentialConfig{
util.CredentialConfig{
Host: "localhost",
Port: "2222",
User: "username",
KeyFile: "/noop",
ScriptResult: "foo",
ScriptReturnCode: 0,
ScriptError: "",
ResultPatternMatch: 1,
},
},
ParsedTimeout: parsedTime,
Ignored: false,
},
},
}
// PrometheusFormatResponse formats the output based on the modified Config
output, err := util.PrometheusFormatResponse(prom_conf)
if err != nil {
t.Errorf("Error formatting output for Prometheus: %s", err)
t.Fail()
}
// Compare the Prometheus formatted output we expect vs what we actually got
compare(string(output), "test/prometheus_format.unit.txt", t)
}
//
// Simple integration test, ensuring the 'happy path' works
//
// NOTE: This requires a host to run tests on.
// A host is provided via Vagrant for local testing, however the host used for integration tests can be changed by editing `test/config.yml`.
//
func TestIntegrationHappyPath(t *testing.T) {
fmt.Println("Running TestIntegrationHappyPath")
// Make sure we have a binary to run
if _, err := os.Stat(binary); err != nil {
t.Errorf("%s binary not available, try to run `go build` first: %s", binary, err)
t.Fail()
}
// Make sure we have a config to read
if _, err := os.Stat(config); err != nil {
t.Errorf("%s config not available, add it to run integration tests: %s", config, err)
t.Fail()
}
// Run the exporter locally
cmd := exec.Command(binary, "--config", config)
cmd.Stdout = os.Stdout
err := cmd.Start()
if err != nil {
t.Errorf("Failed to start exporter: %s", err)
t.Fail()
}
// Wait for the exporter to startup
select {
case <-time.After(100 * time.Millisecond):
}
// Fetch the default "all" pattern for the metrics
resp, err := http.Get(fmt.Sprintf("http://%s/probe?pattern=.*", address))
if err != nil {
t.Errorf("Error fetching endpoint: %s\nIs the integration test host running?", err)
t.Fail()
}
// Read the body into a bytes variable
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
t.Errorf("Error parsing response body: %s", err)
t.Fail()
}
// Close the response body
if err := resp.Body.Close(); err != nil {
t.Errorf("Error closing body: %s", err)
t.Fail()
}
// Make sure the status is correct
// If this fails we have weirder problems
if want, have := http.StatusOK, resp.StatusCode; want != have {
t.Errorf("Status code was not OK: %s != %s\n%s", want, have, string(data))
t.Fail()
}
// Compare the body with what we want the body to be
if !compare(string(data), "test/happy_path.integration.txt", t) {
t.Error("Is the integration host running?")
t.Fail()
}
}