-
Notifications
You must be signed in to change notification settings - Fork 52
/
script_exporter_test.go
90 lines (71 loc) · 1.99 KB
/
script_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
package main
import (
"testing"
)
var config = &Config{
Scripts: []*Script{
{"success", "exit 0", 1},
{"failure", "exit 1", 1},
{"timeout", "sleep 5", 2},
},
}
func TestRunScripts(t *testing.T) {
measurements := runScripts(config.Scripts)
expectedResults := map[string]struct {
success int
minDuration float64
}{
"success": {1, 0},
"failure": {0, 0},
"timeout": {0, 2},
}
for _, measurement := range measurements {
expectedResult := expectedResults[measurement.Script.Name]
if measurement.Success != expectedResult.success {
t.Errorf("Expected result not found: %s", measurement.Script.Name)
}
if measurement.Duration < expectedResult.minDuration {
t.Errorf("Expected duration %f < %f: %s", measurement.Duration, expectedResult.minDuration, measurement.Script.Name)
}
}
}
func TestScriptFilter(t *testing.T) {
t.Run("RequiredParameters", func(t *testing.T) {
_, err := scriptFilter(config.Scripts, "", "")
if err.Error() != "`name` or `pattern` required" {
t.Errorf("Expected failure when supplying no parameters")
}
})
t.Run("NameMatch", func(t *testing.T) {
scripts, err := scriptFilter(config.Scripts, "success", "")
if err != nil {
t.Errorf("Unexpected: %s", err.Error())
}
if len(scripts) != 1 || scripts[0] != config.Scripts[0] {
t.Errorf("Expected script not found")
}
})
t.Run("PatternMatch", func(t *testing.T) {
scripts, err := scriptFilter(config.Scripts, "", "fail.*")
if err != nil {
t.Errorf("Unexpected: %s", err.Error())
}
if len(scripts) != 1 || scripts[0] != config.Scripts[1] {
t.Errorf("Expected script not found")
}
})
t.Run("AllMatch", func(t *testing.T) {
scripts, err := scriptFilter(config.Scripts, "success", ".*")
if err != nil {
t.Errorf("Unexpected: %s", err.Error())
}
if len(scripts) != 3 {
t.Fatalf("Expected 3 scripts, received %d", len(scripts))
}
for i, script := range config.Scripts {
if scripts[i] != script {
t.Fatalf("Expected script not found")
}
}
})
}