-
Notifications
You must be signed in to change notification settings - Fork 326
/
Copy pathtilt_analytics_test.go
192 lines (169 loc) · 5.38 KB
/
tilt_analytics_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
package analytics
import (
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/tilt-dev/wmclient/pkg/analytics"
)
const versionTest = "v0.0.0"
type testCase struct {
name string
opt analytics.Opt
expectRecord bool
}
var testTags = map[string]string{"bar": "baz"}
func testCases(expectedWhenOptedIn, expectedWhenOptedOut, expectedWhenNoOpt bool) []testCase {
return []testCase{
{"opt in", analytics.OptIn, expectedWhenOptedIn},
{"opt out", analytics.OptOut, expectedWhenOptedOut},
{"opt default", analytics.OptDefault, expectedWhenNoOpt},
}
}
type userOptSetting struct {
opt analytics.Opt
}
func (os *userOptSetting) ReadUserOpt() (analytics.Opt, error) {
return os.opt, nil
}
func (os *userOptSetting) SetUserOpt(opt analytics.Opt) error {
os.opt = opt
return nil
}
func TestCount(t *testing.T) {
for _, test := range testCases(true, false, true) {
t.Run(test.name, func(t *testing.T) {
ma := analytics.NewMemoryAnalytics()
os := &userOptSetting{opt: test.opt}
a, _ := NewTiltAnalytics(os, ma, versionTest)
a.opt.env = analytics.OptDefault
a.Count("foo", testTags, 1)
var expectedCounts []analytics.CountEvent
if test.expectRecord {
expectedCounts = append(expectedCounts, analytics.CountEvent{
Name: "foo",
Tags: testTags,
N: 1,
})
}
assert.Equal(t, expectedCounts, ma.Counts)
})
}
}
func TestIncr(t *testing.T) {
for _, test := range testCases(true, false, true) {
t.Run(test.name, func(t *testing.T) {
ma := analytics.NewMemoryAnalytics()
os := &userOptSetting{opt: test.opt}
a, _ := NewTiltAnalytics(os, ma, versionTest)
a.opt.env = analytics.OptDefault
a.Incr("foo", testTags)
var expectedCounts []analytics.CountEvent
if test.expectRecord {
expectedCounts = append(expectedCounts, analytics.CountEvent{
Name: "foo",
Tags: testTags,
N: 1,
})
}
assert.Equal(t, expectedCounts, ma.Counts)
})
}
}
func TestTimer(t *testing.T) {
for _, test := range testCases(true, false, true) {
t.Run(test.name, func(t *testing.T) {
ma := analytics.NewMemoryAnalytics()
os := &userOptSetting{opt: test.opt}
a, _ := NewTiltAnalytics(os, ma, versionTest)
a.opt.env = analytics.OptDefault
a.Timer("foo", time.Second, testTags)
var expectedTimes []analytics.TimeEvent
if test.expectRecord {
expectedTimes = append(expectedTimes, analytics.TimeEvent{
Name: "foo",
Tags: testTags,
Dur: time.Second,
})
}
assert.Equal(t, expectedTimes, ma.Timers)
})
}
}
func TestWithoutGlobalTags(t *testing.T) {
ma := analytics.NewMemoryAnalytics()
os := &userOptSetting{opt: analytics.OptIn}
a, _ := NewTiltAnalytics(os, ma, versionTest)
a.opt.env = analytics.OptDefault
a.WithoutGlobalTags().Incr("foo", testTags)
// memory analytics doesn't have global tags, so there's really
// nothing to test. We mainly want to make sure this doesn't crash.
assert.Equal(t, 1, len(ma.Counts))
}
func TestOptPrecedence(t *testing.T) {
for _, tc := range []struct {
name string
envOpt analytics.Opt
userOpt analytics.Opt
tiltfileOpt analytics.Opt
expectedEffectiveOpt analytics.Opt
}{
{"env opt overrides all", analytics.OptOut, analytics.OptIn, analytics.OptIn, analytics.OptOut},
{"tiltfile opt overrides user", analytics.OptDefault, analytics.OptOut, analytics.OptIn, analytics.OptIn},
{"user opt controls if others unset", analytics.OptDefault, analytics.OptOut, analytics.OptDefault, analytics.OptOut},
{"default opt if none set", analytics.OptDefault, analytics.OptDefault, analytics.OptDefault, analytics.OptDefault},
} {
t.Run(tc.name, func(t *testing.T) {
ma := analytics.NewMemoryAnalytics()
os := &userOptSetting{opt: tc.userOpt}
a, _ := NewTiltAnalytics(os, ma, versionTest)
a.opt.env = tc.envOpt
a.SetTiltfileOpt(tc.tiltfileOpt)
// compare strings for readability
assert.Equal(t, tc.expectedEffectiveOpt.String(), a.EffectiveOpt().String())
})
}
}
func analyticsViaTransition(t *testing.T, initialOpt, newOpt analytics.Opt) (*TiltAnalytics, *analytics.MemoryAnalytics) {
ma := analytics.NewMemoryAnalytics()
os := &userOptSetting{opt: initialOpt}
a, _ := NewTiltAnalytics(os, ma, versionTest)
a.opt.env = analytics.OptDefault
err := a.SetUserOpt(newOpt)
if !assert.NoError(t, err) {
assert.FailNow(t, err.Error())
}
if !assert.Equal(t, newOpt, os.opt) {
t.FailNow()
}
// wipe out the reports of opt-in/out, since those are side effects of test setup, not the test itself
ma.Counts = nil
return a, ma
}
type transitionTestCase struct {
name string
initialOpt analytics.Opt
newOpt analytics.Opt
expectRecord bool
}
func TestOptTransitionIncr(t *testing.T) {
for _, test := range []transitionTestCase{
{"default -> out", analytics.OptDefault, analytics.OptOut, false},
{"default -> in", analytics.OptDefault, analytics.OptIn, true},
{"in -> out", analytics.OptIn, analytics.OptOut, false},
{"out -> in", analytics.OptOut, analytics.OptIn, true},
} {
t.Run(test.name, func(t *testing.T) {
a, ma := analyticsViaTransition(t, test.initialOpt, test.newOpt)
a.Incr("foo", testTags)
var expectedCounts []analytics.CountEvent
if test.expectRecord {
expectedCounts = append(expectedCounts, analytics.CountEvent{
Name: "foo",
Tags: testTags,
N: 1,
})
}
assert.Equal(t, expectedCounts, ma.Counts)
})
}
}