forked from teslamotors/fleet-telemetry
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig_test.go
More file actions
359 lines (299 loc) · 11.8 KB
/
Copy pathconfig_test.go
File metadata and controls
359 lines (299 loc) · 11.8 KB
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
package config
import (
"io"
"os"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
confluent "github.com/confluentinc/confluent-kafka-go/v2/kafka"
githublogrus "github.com/sirupsen/logrus"
logrus "github.com/teslamotors/fleet-telemetry/logger"
"github.com/teslamotors/fleet-telemetry/metrics"
"github.com/teslamotors/fleet-telemetry/server/airbrake"
"github.com/teslamotors/fleet-telemetry/telemetry"
)
var _ = Describe("Test full application config", func() {
var (
config *Config
producers map[string][]telemetry.Producer
log *logrus.Logger
)
BeforeEach(func() {
log, _ = logrus.NoOpLogger()
config = &Config{
Host: "127.0.0.1",
Port: 443,
StatusPort: 8080,
Namespace: "tesla_telemetry",
TLS: &TLS{CAFile: "tesla.ca", ServerCert: "your_own_cert.crt", ServerKey: "your_own_key.key"},
RateLimit: &RateLimit{Enabled: true, MessageLimit: 1000, MessageInterval: 30},
Kafka: &confluent.ConfigMap{
"bootstrap.servers": "some.broker:9093",
"ssl.ca.location": "kafka.ca",
"ssl.certificate.location": "kafka.crt",
"ssl.key.location": "kafka.key",
},
Monitoring: &metrics.MonitoringConfig{PrometheusMetricsPort: 9090, ProfilerPort: 4269, ProfilingPath: "/tmp/fleet-telemetry/profile/"},
LogLevel: "info",
JSONLogEnable: true,
Records: map[string][]telemetry.Dispatcher{"V": {"kafka"}},
}
})
AfterEach(func() {
os.Clearenv()
type Closer interface {
Close() error
}
for _, typeProducers := range producers {
for _, producer := range typeProducers {
if closer, ok := producer.(Closer); ok {
err := closer.Close()
Expect(err).NotTo(HaveOccurred())
}
}
}
})
Context("ExtractServiceTLSConfig", func() {
It("fails when TLS is nil ", func() {
config = &Config{}
_, err := config.ExtractServiceTLSConfig(log)
Expect(err).To(MatchError("tls config is empty - telemetry server is mTLS only, make sure to provide certificates in the config"))
})
It("fails when files are missing", func() {
_, err := config.ExtractServiceTLSConfig(log)
Expect(err).To(MatchError("open tesla.ca: no such file or directory"))
})
It("fails when pem file is invalid", func() {
tmpCA, err := os.CreateTemp(GinkgoT().TempDir(), "tmpCA")
Expect(err).NotTo(HaveOccurred())
_, err = io.WriteString(tmpCA, "-----BEGIN CERTIFICATE-----\nFAKECA\n-----END CERTIFICATE-----")
Expect(err).NotTo(HaveOccurred())
config.TLS.CAFile = tmpCA.Name()
_, err = config.ExtractServiceTLSConfig(log)
Expect(err).To(MatchError(MatchRegexp("custom ca not properly loaded: .*tmpCA.*")))
})
It("uses prod CA", func() {
config.TLS.CAFile = ""
tls, err := config.ExtractServiceTLSConfig(log)
Expect(err).NotTo(HaveOccurred())
Expect(tls).NotTo(BeNil())
Expect(tls.ClientCAs).NotTo(BeNil())
Expect(tls.ClientCAs.Subjects()).To(HaveLen(14)) //nolint:staticcheck
})
It("uses eng CA", func() {
config.TLS.CAFile = ""
config.UseDefaultEngCA = true
tls, err := config.ExtractServiceTLSConfig(log)
Expect(err).NotTo(HaveOccurred())
Expect(tls).NotTo(BeNil())
Expect(tls.ClientCAs).NotTo(BeNil())
Expect(tls.ClientCAs.Subjects()).To(HaveLen(8)) //nolint:staticcheck
})
})
Context("basic config", func() {
It("use correct ports", func() {
config, err := loadTestApplicationConfig(TestSmallConfig)
Expect(err).NotTo(HaveOccurred())
Expect(config.Port).To(BeEquivalentTo(443))
Expect(config.StatusPort).To(BeEquivalentTo(8080))
})
It("transmitrecords disabled by default", func() {
config, err := loadTestApplicationConfig(TestSmallConfig)
Expect(err).NotTo(HaveOccurred())
Expect(config.TransmitDecodedRecords).To(BeFalse())
})
It("transmitrecords enabled", func() {
config, err := loadTestApplicationConfig(TestTransmitDecodedRecords)
Expect(err).NotTo(HaveOccurred())
Expect(config.TransmitDecodedRecords).To(BeTrue())
})
})
Context("configure kafka", func() {
It("converts floats to int", func() {
config, err := loadTestApplicationConfig(TestSmallConfig)
Expect(err).NotTo(HaveOccurred())
_, producers, err = config.ConfigureProducers(airbrake.NewAirbrakeHandler(nil), log, true)
Expect(err).NotTo(HaveOccurred())
Expect(producers["V"]).To(HaveLen(1))
value, err := config.Kafka.Get("queue.buffering.max.messages", 10)
Expect(err).NotTo(HaveOccurred())
Expect(value.(int)).To(Equal(1000000))
})
})
Context("configure airbrake", func() {
It("gets config from file", func() {
config, err := loadTestApplicationConfig(TestAirbrakeConfig)
Expect(err).NotTo(HaveOccurred())
_, options, err := config.CreateAirbrakeNotifier(log)
Expect(err).NotTo(HaveOccurred())
Expect(options.ProjectKey).To(Equal("test1"))
})
It("gets config from env variable", func() {
projectKey := "environmentProjectKey"
err := os.Setenv("AIRBRAKE_PROJECT_KEY", projectKey)
Expect(err).NotTo(HaveOccurred())
config, err := loadTestApplicationConfig(TestAirbrakeConfig)
Expect(err).NotTo(HaveOccurred())
_, options, err := config.CreateAirbrakeNotifier(log)
Expect(err).NotTo(HaveOccurred())
Expect(options.ProjectKey).To(Equal(projectKey))
})
})
Context("configure reliable acks", func() {
It("configures each datasource", func() {
config, err := loadTestApplicationConfig(TestMultipleTxTypeReliableAckConfig)
Expect(err).NotTo(HaveOccurred())
reliableAcks, err := config.configureReliableAckSources()
Expect(err).ToNot(HaveOccurred())
Expect(reliableAcks["kafka"]).To(HaveLen(2))
Expect(reliableAcks["kafka"]["V"]).To(BeTrue())
Expect(reliableAcks["kafka"]["errors"]).To(BeTrue())
Expect(reliableAcks["mqtt"]).To(HaveLen(1))
Expect(reliableAcks["mqtt"]["alerts"]).To(BeTrue())
})
DescribeTable("fails",
func(configInput string, errMessage string) {
config, err := loadTestApplicationConfig(configInput)
Expect(err).NotTo(HaveOccurred())
_, producers, err = config.ConfigureProducers(airbrake.NewAirbrakeHandler(nil), log, true)
Expect(err).To(MatchError(errMessage))
Expect(producers).To(BeNil())
},
Entry("when reliable ack is mapped incorrectly", TestBadReliableAckConfig, "pubsub cannot be configured as reliable ack for record: V. Valid datastores configured [kafka]"),
Entry("when logger is configured as reliable ack", TestLoggerAsReliableAckConfig, "logger cannot be configured as reliable ack for record: V"),
Entry("when reliable ack is configured for unmapped txtype", TestUnusedTxTypeAsReliableAckConfig, "kafka cannot be configured as reliable ack for record: error since no record mapping exists"),
Entry("when reliable ack is mapped with unsupported txtype", TestBadTxTypeReliableAckConfig, "reliable ack not needed for txType: connectivity"),
)
})
Context("configure kinesis", func() {
It("returns an error if kinesis isn't included", func() {
log, _ := logrus.NoOpLogger()
config.Records = map[string][]telemetry.Dispatcher{"V": {"kinesis"}}
var err error
_, producers, err = config.ConfigureProducers(airbrake.NewAirbrakeHandler(nil), log, true)
Expect(err).To(MatchError("expected Kinesis to be configured"))
Expect(producers).To(BeNil())
})
It("returns a map", func() {
config.Kinesis = &Kinesis{Streams: map[string]string{"V": "mystream_V", "errors": "mystream_errors"}}
err := os.Setenv("KINESIS_STREAM_ERRORS", "test_errors")
Expect(err).NotTo(HaveOccurred())
streamMapping := config.CreateKinesisStreamMapping([]string{"V", "errors", "alerts"})
Expect(streamMapping).To(Equal(map[string]string{
"V": "mystream_V",
"errors": "test_errors",
"alerts": "tesla_telemetry_alerts",
}))
})
})
Context("configure redis", func() {
AfterEach(func() {
_ = os.Unsetenv("REDIS_PASSWORD")
})
It("uses the password from config when REDIS_PASSWORD is unset", func() {
redis := &Redis{Addrs: []string{"redis:6379"}, Password: "config-password"}
options, err := redis.options()
Expect(err).NotTo(HaveOccurred())
Expect(options.Password).To(Equal("config-password"))
})
It("overrides the password with the REDIS_PASSWORD env variable when set", func() {
err := os.Setenv("REDIS_PASSWORD", "env-password")
Expect(err).NotTo(HaveOccurred())
redis := &Redis{Addrs: []string{"redis:6379"}, Password: "config-password"}
options, err := redis.options()
Expect(err).NotTo(HaveOccurred())
Expect(options.Password).To(Equal("env-password"))
})
})
Context("VinsToTrack", func() {
AfterEach(func() {
maxVinsToTrack = 20
})
It("empty vins to track", func() {
config, err := loadTestApplicationConfig(TestSmallConfig)
Expect(err).NotTo(HaveOccurred())
Expect(config.VinsToTrack()).To(BeEmpty())
})
It("valid vins to track", func() {
config, err := loadTestApplicationConfig(TestVinsToTrackConfig)
Expect(err).NotTo(HaveOccurred())
Expect(config.VinsToTrack()).To(HaveLen(2))
})
It("returns an error when `vins_signal_tracking_enabled` exceeds limit", func() {
maxVinsToTrack = 2
_, err := loadTestApplicationConfig(BadVinsConfig)
Expect(err).To(MatchError("set the value of `vins_signal_tracking_enabled` less than 2 unique vins"))
})
})
Context("configure pubsub", func() {
var (
pubsubConfig *Config
)
BeforeEach(func() {
var err error
pubsubConfig, err = loadTestApplicationConfig(TestPubsubConfig)
Expect(err).NotTo(HaveOccurred())
})
It("pubsub does not work when both the environment variables are set", func() {
log, _ := logrus.NoOpLogger()
_ = os.Setenv("PUBSUB_EMULATOR_HOST", "some_url")
_ = os.Setenv("GOOGLE_APPLICATION_CREDENTIALS", "some_service_account_path")
_, _, err := pubsubConfig.ConfigureProducers(airbrake.NewAirbrakeHandler(nil), log, true)
Expect(err).To(MatchError("pubsub_connect_error pubsub cannot initialize with both emulator and GCP resource"))
})
It("pubsub config works", func() {
log, _ := logrus.NoOpLogger()
_ = os.Setenv("PUBSUB_EMULATOR_HOST", "some_url")
var err error
_, producers, err = pubsubConfig.ConfigureProducers(airbrake.NewAirbrakeHandler(nil), log, true)
Expect(err).NotTo(HaveOccurred())
Expect(producers["V"]).NotTo(BeNil())
})
})
Context("configure zmq", func() {
var zmqConfig *Config
BeforeEach(func() {
var err error
zmqConfig, err = loadTestApplicationConfig(TestZMQConfig)
Expect(err).NotTo(HaveOccurred())
})
It("returns an error if zmq isn't included", func() {
log, _ := logrus.NoOpLogger()
config.Records = map[string][]telemetry.Dispatcher{"V": {"zmq"}}
var err error
_, producers, err = config.ConfigureProducers(airbrake.NewAirbrakeHandler(nil), log, true)
Expect(err).To(MatchError("expected ZMQ to be configured"))
Expect(producers).To(BeNil())
_, producers, err = zmqConfig.ConfigureProducers(airbrake.NewAirbrakeHandler(nil), log, true)
Expect(err).NotTo(HaveOccurred())
})
It("zmq config works", func() {
// ZMQ close is async, this removes the need to sync between tests.
zmqConfig.ZMQ.Addr = "tcp://127.0.0.1:5285"
log, _ := logrus.NoOpLogger()
var err error
_, producers, err = zmqConfig.ConfigureProducers(airbrake.NewAirbrakeHandler(nil), log, true)
Expect(err).NotTo(HaveOccurred())
Expect(producers["V"]).NotTo(BeNil())
})
})
Context("configureMetricsCollector", func() {
It("does not fail when TLS is nil ", func() {
log, _ := logrus.NoOpLogger()
config = &Config{}
config.configureMetricsCollector(log)
Expect(config.Monitoring).To(BeNil())
})
It("fails if not reachable", func() {
log, _ := logrus.NoOpLogger()
config.configureMetricsCollector(log)
Expect(config.MetricCollector).NotTo(BeNil())
})
})
Context("configureLogger", func() {
It("Should properly configure logger", func() {
log, _ := logrus.NoOpLogger()
config.configureLogger(log)
Expect(githublogrus.GetLevel().String()).To(Equal("info"))
})
})
})