/
test.go
418 lines (353 loc) · 12.5 KB
/
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
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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
package test
import (
"os"
"time"
"github.com/golang/protobuf/ptypes/empty"
"github.com/gomodule/redigo/redis"
"github.com/jmoiron/sqlx"
migrate "github.com/rubenv/sql-migrate"
log "github.com/sirupsen/logrus"
context "golang.org/x/net/context"
"google.golang.org/grpc"
"github.com/brocaar/loraserver/api/as"
"github.com/brocaar/loraserver/api/geo"
"github.com/brocaar/loraserver/api/gw"
"github.com/brocaar/loraserver/api/nc"
"github.com/brocaar/loraserver/internal/api/client/asclient"
"github.com/brocaar/loraserver/internal/common"
"github.com/brocaar/loraserver/internal/config"
"github.com/brocaar/loraserver/internal/joinserver"
"github.com/brocaar/loraserver/internal/migrations"
"github.com/brocaar/lorawan"
"github.com/brocaar/lorawan/backend"
"github.com/brocaar/lorawan/band"
)
func init() {
log.SetLevel(log.ErrorLevel)
config.C.NetworkServer.DeviceSessionTTL = time.Hour
config.C.NetworkServer.Band.Name = band.EU_863_870
config.C.NetworkServer.Band.Band, _ = band.GetConfig(config.C.NetworkServer.Band.Name, false, lorawan.DwellTimeNoLimit)
config.C.NetworkServer.DeduplicationDelay = 5 * time.Millisecond
config.C.NetworkServer.GetDownlinkDataDelay = 5 * time.Millisecond
config.C.NetworkServer.NetworkSettings.DownlinkTXPower = -1
}
// Config contains the test configuration.
type Config struct {
RedisURL string
PostgresDSN string
}
// GetConfig returns the test configuration.
func GetConfig() *Config {
var err error
log.SetLevel(log.FatalLevel)
config.C.NetworkServer.Band.Band, err = band.GetConfig(band.EU_863_870, false, lorawan.DwellTimeNoLimit)
if err != nil {
panic(err)
}
config.C.NetworkServer.NetworkSettings.RX2Frequency = config.C.NetworkServer.Band.Band.GetDefaults().RX2Frequency
config.C.NetworkServer.NetworkSettings.RX2DR = config.C.NetworkServer.Band.Band.GetDefaults().RX2DataRate
config.C.NetworkServer.NetworkSettings.RX1Delay = 0
c := &Config{
RedisURL: "redis://localhost:6379",
PostgresDSN: "postgres://localhost/loraserver_ns_test?sslmode=disable",
}
if v := os.Getenv("TEST_REDIS_URL"); v != "" {
c.RedisURL = v
}
if v := os.Getenv("TEST_POSTGRES_DSN"); v != "" {
c.PostgresDSN = v
}
return c
}
// MustFlushRedis flushes the Redis storage.
func MustFlushRedis(p *redis.Pool) {
c := p.Get()
defer c.Close()
if _, err := c.Do("FLUSHALL"); err != nil {
log.Fatal(err)
}
}
// MustPrefillRedisPool pre-fills the pool with count connections.
func MustPrefillRedisPool(p *redis.Pool, count int) {
conns := []redis.Conn{}
for i := 0; i < count; i++ {
conns = append(conns, p.Get())
}
for i := range conns {
conns[i].Close()
}
}
// MustResetDB re-applies all database migrations.
func MustResetDB(db *common.DBLogger) {
m := &migrate.AssetMigrationSource{
Asset: migrations.Asset,
AssetDir: migrations.AssetDir,
Dir: "",
}
if _, err := migrate.Exec(db.DB.DB, "postgres", m, migrate.Down); err != nil {
log.Fatal(err)
}
if _, err := migrate.Exec(db.DB.DB, "postgres", m, migrate.Up); err != nil {
log.Fatal(err)
}
}
// GatewayBackend is a test gateway backend.
type GatewayBackend struct {
rxPacketChan chan gw.UplinkFrame
TXPacketChan chan gw.DownlinkFrame
GatewayConfigPacketChan chan gw.GatewayConfiguration
statsPacketChan chan gw.GatewayStats
downlinkTXAckChan chan gw.DownlinkTXAck
}
// NewGatewayBackend returns a new GatewayBackend.
func NewGatewayBackend() *GatewayBackend {
return &GatewayBackend{
rxPacketChan: make(chan gw.UplinkFrame, 100),
TXPacketChan: make(chan gw.DownlinkFrame, 100),
GatewayConfigPacketChan: make(chan gw.GatewayConfiguration, 100),
downlinkTXAckChan: make(chan gw.DownlinkTXAck, 100),
}
}
// SendTXPacket method.
func (b *GatewayBackend) SendTXPacket(txPacket gw.DownlinkFrame) error {
b.TXPacketChan <- txPacket
return nil
}
// SendGatewayConfigPacket method.
func (b *GatewayBackend) SendGatewayConfigPacket(config gw.GatewayConfiguration) error {
b.GatewayConfigPacketChan <- config
return nil
}
// RXPacketChan method.
func (b *GatewayBackend) RXPacketChan() chan gw.UplinkFrame {
return b.rxPacketChan
}
// StatsPacketChan method.
func (b *GatewayBackend) StatsPacketChan() chan gw.GatewayStats {
return b.statsPacketChan
}
// DownlinkTXAckChan method.
func (b *GatewayBackend) DownlinkTXAckChan() chan gw.DownlinkTXAck {
return b.downlinkTXAckChan
}
// Close method.
func (b *GatewayBackend) Close() error {
if b.rxPacketChan != nil {
close(b.rxPacketChan)
}
return nil
}
// JoinServerPool is a join-server pool for testing.
type JoinServerPool struct {
Client joinserver.Client
GetJoinEUI lorawan.EUI64
}
// NewJoinServerPool create a join-server pool for testing.
func NewJoinServerPool(client joinserver.Client) joinserver.Pool {
return &JoinServerPool{
Client: client,
}
}
// Get method.
func (p *JoinServerPool) Get(joinEUI lorawan.EUI64) (joinserver.Client, error) {
p.GetJoinEUI = joinEUI
return p.Client, nil
}
// JoinServerClient is a join-server client for testing.
type JoinServerClient struct {
JoinReqPayloadChan chan backend.JoinReqPayload
RejoinReqPayloadChan chan backend.RejoinReqPayload
JoinReqError error
RejoinReqError error
JoinAnsPayload backend.JoinAnsPayload
RejoinAnsPayload backend.RejoinAnsPayload
}
// NewJoinServerClient creates a new join-server client.
func NewJoinServerClient() *JoinServerClient {
return &JoinServerClient{
JoinReqPayloadChan: make(chan backend.JoinReqPayload, 100),
RejoinReqPayloadChan: make(chan backend.RejoinReqPayload, 100),
}
}
// JoinReq method.
func (c *JoinServerClient) JoinReq(pl backend.JoinReqPayload) (backend.JoinAnsPayload, error) {
c.JoinReqPayloadChan <- pl
return c.JoinAnsPayload, c.JoinReqError
}
// RejoinReq method.
func (c *JoinServerClient) RejoinReq(pl backend.RejoinReqPayload) (backend.RejoinAnsPayload, error) {
c.RejoinReqPayloadChan <- pl
return c.RejoinAnsPayload, c.RejoinReqError
}
// ApplicationServerPool is an application-server pool for testing.
type ApplicationServerPool struct {
Client as.ApplicationServerServiceClient
GetHostname string
}
// Get returns the Client.
func (p *ApplicationServerPool) Get(hostname string, caCert, tlsCert, tlsKey []byte) (as.ApplicationServerServiceClient, error) {
p.GetHostname = hostname
return p.Client, nil
}
// NewApplicationServerPool create an application-server client pool which
// always returns the given client on Get.
func NewApplicationServerPool(client *ApplicationClient) asclient.Pool {
return &ApplicationServerPool{
Client: client,
}
}
// ApplicationClient is an application client for testing.
type ApplicationClient struct {
HandleDataUpErr error
HandleProprietaryUpErr error
HandleDownlinkACKErr error
SetDeviceStatusError error
SetDeviceLocationErrror error
HandleDataUpChan chan as.HandleUplinkDataRequest
HandleProprietaryUpChan chan as.HandleProprietaryUplinkRequest
HandleErrorChan chan as.HandleErrorRequest
HandleDownlinkACKChan chan as.HandleDownlinkACKRequest
SetDeviceStatusChan chan as.SetDeviceStatusRequest
SetDeviceLocationChan chan as.SetDeviceLocationRequest
HandleDataUpResponse empty.Empty
HandleProprietaryUpResponse empty.Empty
HandleErrorResponse empty.Empty
HandleDownlinkACKResponse empty.Empty
SetDeviceStatusResponse empty.Empty
SetDeviceLocationResponse empty.Empty
}
// NewApplicationClient returns a new ApplicationClient.
func NewApplicationClient() *ApplicationClient {
return &ApplicationClient{
HandleDataUpChan: make(chan as.HandleUplinkDataRequest, 100),
HandleProprietaryUpChan: make(chan as.HandleProprietaryUplinkRequest, 100),
HandleErrorChan: make(chan as.HandleErrorRequest, 100),
HandleDownlinkACKChan: make(chan as.HandleDownlinkACKRequest, 100),
SetDeviceStatusChan: make(chan as.SetDeviceStatusRequest, 100),
SetDeviceLocationChan: make(chan as.SetDeviceLocationRequest, 100),
}
}
// HandleUplinkData method.
func (t *ApplicationClient) HandleUplinkData(ctx context.Context, in *as.HandleUplinkDataRequest, opts ...grpc.CallOption) (*empty.Empty, error) {
if t.HandleDataUpErr != nil {
return nil, t.HandleDataUpErr
}
t.HandleDataUpChan <- *in
return &t.HandleDataUpResponse, nil
}
// HandleProprietaryUplink method.
func (t *ApplicationClient) HandleProprietaryUplink(ctx context.Context, in *as.HandleProprietaryUplinkRequest, opts ...grpc.CallOption) (*empty.Empty, error) {
if t.HandleProprietaryUpErr != nil {
return nil, t.HandleProprietaryUpErr
}
t.HandleProprietaryUpChan <- *in
return &t.HandleProprietaryUpResponse, nil
}
// HandleError method.
func (t *ApplicationClient) HandleError(ctx context.Context, in *as.HandleErrorRequest, opts ...grpc.CallOption) (*empty.Empty, error) {
t.HandleErrorChan <- *in
return &t.HandleErrorResponse, nil
}
// HandleDownlinkACK method.
func (t *ApplicationClient) HandleDownlinkACK(ctx context.Context, in *as.HandleDownlinkACKRequest, opts ...grpc.CallOption) (*empty.Empty, error) {
t.HandleDownlinkACKChan <- *in
return &t.HandleDownlinkACKResponse, nil
}
// SetDeviceStatus method.
func (t *ApplicationClient) SetDeviceStatus(ctx context.Context, in *as.SetDeviceStatusRequest, opts ...grpc.CallOption) (*empty.Empty, error) {
t.SetDeviceStatusChan <- *in
return &t.SetDeviceStatusResponse, t.SetDeviceStatusError
}
// SetDeviceLocation method.
func (t *ApplicationClient) SetDeviceLocation(ctx context.Context, in *as.SetDeviceLocationRequest, opts ...grpc.CallOption) (*empty.Empty, error) {
t.SetDeviceLocationChan <- *in
return &t.SetDeviceLocationResponse, t.SetDeviceLocationErrror
}
// NetworkControllerClient is a network-controller client for testing.
type NetworkControllerClient struct {
HandleRXInfoChan chan nc.HandleUplinkMetaDataRequest
HandleDataUpMACCommandChan chan nc.HandleUplinkMACCommandRequest
HandleRXInfoResponse empty.Empty
HandleDataUpMACCommandResponse empty.Empty
}
// NewNetworkControllerClient returns a new NetworkControllerClient.
func NewNetworkControllerClient() *NetworkControllerClient {
return &NetworkControllerClient{
HandleRXInfoChan: make(chan nc.HandleUplinkMetaDataRequest, 100),
HandleDataUpMACCommandChan: make(chan nc.HandleUplinkMACCommandRequest, 100),
}
}
// HandleUplinkMetaData method.
func (t *NetworkControllerClient) HandleUplinkMetaData(ctx context.Context, in *nc.HandleUplinkMetaDataRequest, opts ...grpc.CallOption) (*empty.Empty, error) {
t.HandleRXInfoChan <- *in
return &empty.Empty{}, nil
}
// HandleUplinkMACCommand method.
func (t *NetworkControllerClient) HandleUplinkMACCommand(ctx context.Context, in *nc.HandleUplinkMACCommandRequest, opts ...grpc.CallOption) (*empty.Empty, error) {
t.HandleDataUpMACCommandChan <- *in
return &empty.Empty{}, nil
}
// GeolocationClient is a geolocation client for testing.
type GeolocationClient struct {
ResolveTDOAChan chan geo.ResolveTDOARequest
ResolveTDOAResponse geo.ResolveTDOAResponse
}
// NewGeolocationClient creates a new GeolocationClient.
func NewGeolocationClient() *GeolocationClient {
return &GeolocationClient{
ResolveTDOAChan: make(chan geo.ResolveTDOARequest, 100),
}
}
// ResolveTDOA method.
func (g *GeolocationClient) ResolveTDOA(ctx context.Context, in *geo.ResolveTDOARequest, opts ...grpc.CallOption) (*geo.ResolveTDOAResponse, error) {
g.ResolveTDOAChan <- *in
return &g.ResolveTDOAResponse, nil
}
// DatabaseTestSuiteBase provides the setup and teardown of the database
// for every test-run.
type DatabaseTestSuiteBase struct {
db *common.DBLogger
tx *common.TxLogger
p *redis.Pool
}
// SetupSuite is called once before starting the test-suite.
func (b *DatabaseTestSuiteBase) SetupSuite() {
conf := GetConfig()
db, err := common.OpenDatabase(conf.PostgresDSN)
if err != nil {
panic(err)
}
b.db = db
MustResetDB(db)
b.p = common.NewRedisPool(conf.RedisURL, 10, 0)
config.C.PostgreSQL.DB = db
config.C.Redis.Pool = b.p
}
// SetupTest is called before every test.
func (b *DatabaseTestSuiteBase) SetupTest() {
tx, err := b.db.Beginx()
if err != nil {
panic(err)
}
b.tx = tx
MustFlushRedis(b.p)
}
// TearDownTest is called after every test.
func (b *DatabaseTestSuiteBase) TearDownTest() {
if err := b.tx.Rollback(); err != nil {
panic(err)
}
}
// Tx returns a database transaction (which is rolled back after every
// test).
func (b *DatabaseTestSuiteBase) Tx() sqlx.Ext {
return b.tx
}
// DB returns the database object.
func (b *DatabaseTestSuiteBase) DB() *common.DBLogger {
return b.db
}
// RedisPool returns the redis.Pool object.
func (b *DatabaseTestSuiteBase) RedisPool() *redis.Pool {
return b.p
}