forked from mantzas/patron
-
Notifications
You must be signed in to change notification settings - Fork 66
/
service_test.go
215 lines (192 loc) · 5.88 KB
/
service_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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
package patron
import (
"context"
"errors"
"log/slog"
"os"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestNew(t *testing.T) {
httpBuilderAllErrors := "attributes are empty\nprovided WithSIGHUP handler was nil"
tests := map[string]struct {
name string
fields []slog.Attr
sighupHandler func()
uncompressedPaths []string
wantErr string
}{
"success": {
name: "name",
fields: []slog.Attr{slog.String("env", "dev")},
sighupHandler: func() { slog.Info("WithSIGHUP received: nothing setup") },
uncompressedPaths: []string{"/foo", "/bar"},
wantErr: "",
},
"name missing": {
sighupHandler: nil,
uncompressedPaths: nil,
wantErr: "name is required",
},
"nil inputs steps": {
name: "name",
sighupHandler: nil,
uncompressedPaths: nil,
wantErr: httpBuilderAllErrors,
},
"error in all builder steps": {
name: "name",
sighupHandler: nil,
uncompressedPaths: []string{},
wantErr: httpBuilderAllErrors,
},
}
for name, tt := range tests {
temp := tt
t.Run(name, func(t *testing.T) {
gotService, gotErr := New(tt.name, "1.0", WithLogFields(temp.fields...), WithJSONLogger(),
WithSIGHUP(temp.sighupHandler))
if temp.wantErr != "" {
assert.EqualError(t, gotErr, temp.wantErr)
assert.Nil(t, gotService)
} else {
assert.Nil(t, gotErr)
assert.NotNil(t, gotService)
assert.IsType(t, &Service{}, gotService)
assert.NotNil(t, gotService.termSig)
assert.NotNil(t, gotService.sighupHandler)
}
})
}
}
func TestServer_Run_Shutdown(t *testing.T) {
tests := map[string]struct {
cp Component
wantErr bool
}{
"success": {cp: &testComponent{}, wantErr: false},
"failed to run": {cp: &testComponent{errorRunning: true}, wantErr: true},
}
for name, tt := range tests {
temp := tt
t.Run(name, func(t *testing.T) {
defer func() {
os.Clearenv()
}()
t.Setenv("PATRON_HTTP_DEFAULT_PORT", "50099")
svc, err := New("test", "", WithJSONLogger())
assert.NoError(t, err)
err = svc.Run(context.Background(), tt.cp)
if temp.wantErr {
assert.Error(t, err)
} else {
assert.NoError(t, err)
}
})
}
}
func TestServer_SetupTracing(t *testing.T) {
tests := []struct {
name string
cp Component
host string
port string
buckets string
}{
{name: "success w/ empty tracing vars", cp: &testComponent{}},
{name: "success w/ empty tracing host", cp: &testComponent{}, port: "6831"},
{name: "success w/ empty tracing port", cp: &testComponent{}, host: "127.0.0.1"},
{name: "success", cp: &testComponent{}, host: "127.0.0.1", port: "6831"},
{name: "success w/ custom default buckets", cp: &testComponent{}, host: "127.0.0.1", port: "6831", buckets: ".1, .3"},
}
for _, tt := range tests {
temp := tt
t.Run(temp.name, func(t *testing.T) {
defer os.Clearenv()
if temp.host != "" {
err := os.Setenv("PATRON_JAEGER_AGENT_HOST", temp.host)
assert.NoError(t, err)
}
if temp.port != "" {
err := os.Setenv("PATRON_JAEGER_AGENT_PORT", temp.port)
assert.NoError(t, err)
}
if temp.buckets != "" {
err := os.Setenv("PATRON_JAEGER_DEFAULT_BUCKETS", temp.buckets)
assert.NoError(t, err)
}
svc, err := New("test", "", WithJSONLogger())
assert.NoError(t, err)
err = svc.Run(context.Background(), tt.cp)
assert.NoError(t, err)
})
}
}
func TestNewServer_FailingConditions(t *testing.T) {
tests := map[string]struct {
jaegerSamplerParam string
jaegerBuckets string
expectedConstructorError string
}{
"failure w/ sampler param": {jaegerSamplerParam: "foo", expectedConstructorError: "env var for jaeger sampler param is not valid: strconv.ParseFloat: parsing \"foo\": invalid syntax"},
"failure w/ overflowing sampler param": {jaegerSamplerParam: "8", expectedConstructorError: "cannot initialize jaeger tracer: invalid Param for probabilistic sampler; expecting value between 0 and 1, received 8"},
"failure w/ custom default buckets": {jaegerSamplerParam: "1", jaegerBuckets: "foo", expectedConstructorError: "env var for jaeger default buckets contains invalid value: strconv.ParseFloat: parsing \"foo\": invalid syntax"},
}
for name, tt := range tests {
tt := tt
t.Run(name, func(t *testing.T) {
defer os.Clearenv()
if tt.jaegerSamplerParam != "" {
err := os.Setenv("PATRON_JAEGER_SAMPLER_PARAM", tt.jaegerSamplerParam)
require.NoError(t, err)
}
if tt.jaegerBuckets != "" {
err := os.Setenv("PATRON_JAEGER_DEFAULT_BUCKETS", tt.jaegerBuckets)
require.NoError(t, err)
}
svc, err := New("test", "", WithJSONLogger())
if tt.expectedConstructorError != "" {
require.EqualError(t, err, tt.expectedConstructorError)
require.Nil(t, svc)
return
}
require.NoError(t, err)
require.NotNil(t, svc)
// start running with a canceled context, on purpose
ctx, cancel := context.WithCancel(context.Background())
cancel()
err = svc.Run(ctx)
require.NoError(t, err)
require.Equal(t, err, context.Canceled)
})
}
}
type testComponent struct {
errorRunning bool
}
func (ts testComponent) Run(_ context.Context) error {
if ts.errorRunning {
return errors.New("failed to run component")
}
return nil
}
func Test_getLogLevel(t *testing.T) {
tests := map[string]struct {
lvl string
want slog.Level
}{
"debug": {lvl: "debug", want: slog.LevelDebug},
"info": {lvl: "info", want: slog.LevelInfo},
"warn": {lvl: "warn", want: slog.LevelWarn},
"error": {lvl: "error", want: slog.LevelError},
"invalid level": {lvl: "invalid", want: slog.LevelInfo},
}
for name, tt := range tests {
tt := tt
t.Run(name, func(t *testing.T) {
t.Setenv("PATRON_LOG_LEVEL", tt.lvl)
assert.Equal(t, tt.want, getLogLevel())
})
}
}