-
Notifications
You must be signed in to change notification settings - Fork 5
/
agent_test.go
195 lines (156 loc) · 5.49 KB
/
agent_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
package iopipe
import (
"fmt"
"os"
"testing"
"time"
. "github.com/smartystreets/goconvey/convey"
)
func TestAgent_NewAgent(t *testing.T) {
Convey("An agent created with empty Config should have default configuration", t, func() {
// TODO: A util to handle env var boilerplate like this
oldDebugValue := os.Getenv("IOPIPE_DEBUG")
oldEnabledValue := os.Getenv("IOPIPE_ENABLED")
oldTimeoutWindowValue := os.Getenv("IOPIPE_TIMEOUT_WINDOW")
oldTokenValue := os.Getenv("IOPIPE_TOKEN")
os.Setenv("IOPIPE_DEBUG", "")
os.Setenv("IOPIPE_ENABLED", "")
os.Setenv("IOPIPE_TIMEOUT_WINDOW", "")
os.Setenv("IOPIPE_TOKEN", "")
agentWithDefaultConfig := NewAgent(Config{})
os.Setenv("IOPIPE_DEBUG", oldDebugValue)
os.Setenv("IOPIPE_ENABLED", oldEnabledValue)
os.Setenv("IOPIPE_TIMEOUT_WINDOW", oldTimeoutWindowValue)
os.Setenv("IOPIPE_TOKEN", oldTokenValue)
Convey("Agent should disable debug mode by default", func() {
So(*agentWithDefaultConfig.Debug, ShouldBeFalse)
})
Convey("Agent should be enabled by default", func() {
So(*agentWithDefaultConfig.Enabled, ShouldBeTrue)
})
Convey("Plugins should be empty by default", func() {
So(agentWithDefaultConfig.plugins, ShouldBeEmpty)
})
Convey("TimeoutWindow should be 150 ms by default", func() {
So(*agentWithDefaultConfig.TimeoutWindow, ShouldEqual, 150*time.Millisecond)
})
Convey("Token should be empty by default", func() {
So(*agentWithDefaultConfig.Token, ShouldBeEmpty)
})
})
Convey("An agent created with custom Config should assume that configuration", t, func() {
var tests = []struct {
inputEnabled bool
inputTimeoutWindow time.Duration
inputToken string
}{
{
inputEnabled: false,
inputTimeoutWindow: 0 * time.Millisecond,
inputToken: "",
},
{
inputEnabled: true,
inputTimeoutWindow: 20 * time.Millisecond,
inputToken: "non-empty-value",
},
}
for _, test := range tests {
inputConfig := Config{
Enabled: &test.inputEnabled,
TimeoutWindow: &test.inputTimeoutWindow,
Token: &test.inputToken,
}
agentWithCustomConfig := NewAgent(inputConfig)
Convey(fmt.Sprintf(`Config with inputToken "%s" should be mapped corectly in the agent instance`, test.inputToken), func() {
So(*agentWithCustomConfig.Enabled, ShouldEqual, test.inputEnabled)
So(*agentWithCustomConfig.TimeoutWindow, ShouldEqual, test.inputTimeoutWindow)
So(*agentWithCustomConfig.Token, ShouldEqual, test.inputToken)
So(agentWithCustomConfig.plugins, ShouldBeEmpty)
})
}
})
Convey("An agent should check environment variables for configuration", t, func() {
Convey("IOPIPE_DEBUG should enable debug mode", func() {
oldValue := os.Getenv("IOPIPE_DEBUG")
os.Setenv("IOPIPE_DEBUG", "true")
a := NewAgent(Config{})
So(*a.Debug, ShouldBeTrue)
os.Setenv("IOPIPE_DEBUG", oldValue)
})
Convey("IOPIPE_ENABLED should disable agent", func() {
oldValue := os.Getenv("IOPIPE_ENABLED")
os.Setenv("IOPIPE_DEBUG", "false")
a := NewAgent(Config{})
So(*a.Debug, ShouldBeFalse)
os.Setenv("IOPIPE_DEBUG", oldValue)
})
Convey("IOPIPE_TIMEOUT_WINDOW should set the timeout window", func() {
oldValue := os.Getenv("IOPIPE_TIMEOUT_WINDOW")
os.Setenv("IOPIPE_TIMEOUT_WINDOW", "300")
a := NewAgent(Config{})
So(*a.TimeoutWindow, ShouldEqual, time.Duration(300*time.Millisecond))
os.Setenv("IOPIPE_DEBUG", oldValue)
})
Convey("IOPIPE_TOKEN should set the token", func() {
oldValue := os.Getenv("IOPIPE_TOKEN")
os.Setenv("IOPIPE_TOKEN", "foobar")
a := NewAgent(Config{})
So(*a.Token, ShouldEqual, "foobar")
os.Setenv("IOPIPE_TOKEN", oldValue)
})
})
Convey("An agent with plugins should initialize them", t, func() {
agentWithPlugin := NewAgent(Config{
Plugins: []PluginInstantiator{
TestPlugin(TestPluginConfig{}),
},
})
Convey("Should only have one plugin", func() {
So(len(agentWithPlugin.plugins), ShouldEqual, 1)
})
plugin, _ := agentWithPlugin.plugins[0].(*testPlugin)
Convey("Pre setup hooks should be called once", func() {
So(plugin.preSetupCalled, ShouldEqual, 1)
})
Convey("Post setup hooks should be called once", func() {
So(plugin.postSetupCalled, ShouldEqual, 1)
})
Convey("No other rhooks should be called", func() {
So(plugin.preInvokeCalled, ShouldEqual, 0)
So(plugin.postInvokeCalled, ShouldEqual, 0)
So(plugin.preReportCalled, ShouldEqual, 0)
So(plugin.postReportCalled, ShouldEqual, 0)
})
})
}
func TestAgent_WrapHandler(t *testing.T) {
Convey("Returns original handler when Config.Enabled = false", t, func() {
handler := func() {}
enabled := false
token := "someToken"
wrappedHandler := NewAgent(Config{
Enabled: &enabled,
Token: &token,
}).WrapHandler(handler)
So(wrappedHandler, ShouldEqual, handler)
})
Convey("Returns original handler when no token provided or is empty string", t, func() {
handler := func() {}
wrappedHandler := NewAgent(Config{}).WrapHandler(handler)
emptyStringToken := ""
wrappedHandlerEmptyStringToken := NewAgent(Config{Token: &emptyStringToken}).WrapHandler(handler)
So(wrappedHandler, ShouldEqual, handler)
So(wrappedHandlerEmptyStringToken, ShouldEqual, handler)
})
Convey("Returns wrapped handler when Config.Enabled = true", t, func() {
handler := func() {}
enabled := true
token := "someToken"
wrappedHandler := NewAgent(Config{
Token: &token,
Enabled: &enabled,
}).WrapHandler(handler)
So(wrappedHandler, ShouldNotEqual, handler)
})
}