-
Notifications
You must be signed in to change notification settings - Fork 2
/
stats_collector_test.go
91 lines (71 loc) · 3.13 KB
/
stats_collector_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
package collectdvsphere
import (
"io/ioutil"
"sync"
"testing"
"time"
"github.com/Sirupsen/logrus"
"collectd.org/api"
)
type fakeAPIWriter struct {
metricsMutex sync.Mutex
metrics map[string]api.Value
}
func (w *fakeAPIWriter) readMetric(metric string) api.Value {
w.metricsMutex.Lock()
defer w.metricsMutex.Unlock()
return w.metrics[metric]
}
func (w *fakeAPIWriter) Write(vl api.ValueList) error {
w.metricsMutex.Lock()
defer w.metricsMutex.Unlock()
w.metrics[vl.Identifier.String()] = vl.Values[0]
return nil
}
func TestStatsCollector(t *testing.T) {
apiWriter := &fakeAPIWriter{metrics: make(map[string]api.Value)}
nullLogger := logrus.New()
nullLogger.Out = ioutil.Discard
collector := NewStatsCollector(apiWriter, time.Millisecond, nullLogger, "foo-instance")
collector.MarkPowerOnSuccess("on-yes-host")
collector.MarkPowerOnSuccess("on-yes-host")
collector.MarkPowerOnFailure("on-no-host")
collector.MarkPowerOffSuccess("off-yes-host")
collector.MarkPowerOffFailure("off-no-host")
collector.MarkCloneSuccess("yes-image")
collector.MarkCloneFailure("no-image")
// Sleep for 2 milliseconds to allow the metrics to be written to the
// fakeAPIWriter.
time.Sleep(2 * time.Millisecond)
expectedMetrics := []struct {
metric string
value api.Value
}{
{"on-yes-host/vsphere-foo-instance/operations-power_on_success", api.Derive(2)},
{"on-yes-host/vsphere-foo-instance/operations-power_on_failure", api.Derive(0)},
{"on-yes-host/vsphere-foo-instance/operations-power_off_success", api.Derive(0)},
{"on-yes-host/vsphere-foo-instance/operations-power_off_failure", api.Derive(0)},
{"on-no-host/vsphere-foo-instance/operations-power_on_success", api.Derive(0)},
{"on-no-host/vsphere-foo-instance/operations-power_on_failure", api.Derive(1)},
{"on-no-host/vsphere-foo-instance/operations-power_off_success", api.Derive(0)},
{"on-no-host/vsphere-foo-instance/operations-power_off_failure", api.Derive(0)},
{"off-yes-host/vsphere-foo-instance/operations-power_on_success", api.Derive(0)},
{"off-yes-host/vsphere-foo-instance/operations-power_on_failure", api.Derive(0)},
{"off-yes-host/vsphere-foo-instance/operations-power_off_success", api.Derive(1)},
{"off-yes-host/vsphere-foo-instance/operations-power_off_failure", api.Derive(0)},
{"off-no-host/vsphere-foo-instance/operations-power_on_success", api.Derive(0)},
{"off-no-host/vsphere-foo-instance/operations-power_on_failure", api.Derive(0)},
{"off-no-host/vsphere-foo-instance/operations-power_off_success", api.Derive(0)},
{"off-no-host/vsphere-foo-instance/operations-power_off_failure", api.Derive(1)},
{"yes-image/vsphere-foo-instance/operations-clone_success", api.Derive(1)},
{"yes-image/vsphere-foo-instance/operations-clone_failure", api.Derive(0)},
{"no-image/vsphere-foo-instance/operations-clone_success", api.Derive(0)},
{"no-image/vsphere-foo-instance/operations-clone_failure", api.Derive(1)},
}
for _, expected := range expectedMetrics {
actualValue := apiWriter.readMetric(expected.metric)
if actualValue != expected.value {
t.Errorf("expected %s to be %+v, but was %+v", expected.metric, expected.value, actualValue)
}
}
}