This repository has been archived by the owner on Nov 1, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstackdriveraggregation_test.go
121 lines (101 loc) · 2.89 KB
/
stackdriveraggregation_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
package monitoring
import (
"context"
"errors"
"os"
"sync"
"testing"
"time"
"github.com/sirupsen/logrus"
"github.com/sirupsen/logrus/hooks/test"
"github.com/stretchr/testify/assert"
gmonitoring "google.golang.org/api/monitoring/v3"
)
func TestMain(m *testing.M) {
// log.SetLevel(log.PanicLevel)
os.Exit(m.Run())
}
func TestWrite(t *testing.T) {
sa := StackdriverAggregation{}
// TODO need a standalone constructor, make it private / non exported?
sa.points = []*gmonitoring.Point{}
sa.mutex = &sync.Mutex{}
sa.in = make(chan *gmonitoring.Point)
point := CreateDataPoint(42.42)
go func() {
received := <-sa.in
assert.Equal(t, point, received)
}()
sa.Write(point)
}
func TestWriteFromChannelToSlice(t *testing.T) {
sa := StackdriverAggregation{
in: make(chan *gmonitoring.Point),
points: []*gmonitoring.Point{},
mutex: &sync.Mutex{},
}
go sa.writeFromChannelToSlice()
point := CreateDataPoint(42.42)
sa.in <- point
time.Sleep(100 * time.Millisecond)
assert.Equal(t, 1, len(sa.points))
}
func TestTicker(t *testing.T) {
count := 0
sender := func(points []*gmonitoring.Point) {
count++
}
sa := StackdriverAggregation{
in: make(chan *gmonitoring.Point),
points: []*gmonitoring.Point{},
mutex: &sync.Mutex{},
}
sa.points = append(sa.points, CreateDataPoint(42.42))
go sa.ticker(time.Millisecond*1, sender)
time.Sleep(time.Millisecond * 10)
assert.Equal(t, 0, len(sa.points))
assert.Equal(t, 1, count)
}
func TestProjectResource(t *testing.T) {
sa := StackdriverAggregation{projectID: "PROJECTID"}
assert.Equal(t, "projects/PROJECTID", sa.projectResource())
}
func TestSend(t *testing.T) {
hook := test.NewGlobal()
sa := StackdriverAggregation{isDebug: true, projectID: "PROJECTID"}
points := []*gmonitoring.Point{CreateDataPoint(42.42)}
sa.send(points)
assert.Equal(t, 1, len(hook.Entries))
assert.Equal(t, logrus.InfoLevel, hook.LastEntry().Level)
assert.Equal(t, "Stackdriver metric not sent in debug mode", hook.LastEntry().Message)
}
func TestDoError(t *testing.T) {
hook := test.NewGlobal()
s, err := NewStackdriver(context.TODO(), false, "PROJECTID")
assert.Nil(t, err)
sa := s.CreateMetric("", nil, "", nil)
sa.do = func(ptscc *gmonitoring.ProjectsTimeSeriesCreateCall) error {
return errors.New("ERROR")
}
points := []*gmonitoring.Point{CreateDataPoint(42.42)}
sa.send(points)
assert.Equal(t, 1, len(hook.Entries))
assert.Equal(t, logrus.ErrorLevel, hook.LastEntry().Level)
assert.Equal(
t,
"Failed to write time series data to Stackdriver",
hook.LastEntry().Message)
}
func TestDoSuccess(t *testing.T) {
s, err := NewStackdriver(context.TODO(), false, "PROJECTID")
assert.Nil(t, err)
sa := s.CreateMetric("", nil, "", nil)
count := 0
sa.do = func(ptscc *gmonitoring.ProjectsTimeSeriesCreateCall) error {
count++
return nil
}
points := []*gmonitoring.Point{CreateDataPoint(42.42)}
sa.send(points)
assert.Equal(t, 1, count)
}