forked from viam-modules/viam-cartographer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
viam-cartographer_test.go
349 lines (291 loc) · 11.9 KB
/
viam-cartographer_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
// Package viamcartographer_test tests the functions that require injected components (such as robot and camera)
// in order to be run. It utilizes the internal package located in testhelper.go to access
// certain exported functions which we do not want to make available to the user. It also runs integration tests
// that test the interaction with the core C++ viam-cartographer code and the Golang implementation of the
// cartographer slam service.
package viamcartographer_test
import (
"context"
"fmt"
"net"
"os"
"strconv"
"testing"
"github.com/edaniels/golog"
"github.com/pkg/errors"
slamConfig "go.viam.com/slam/config"
"go.viam.com/slam/sensors/lidar"
slamTesthelper "go.viam.com/slam/testhelper"
"go.viam.com/test"
"google.golang.org/grpc"
"github.com/viamrobotics/viam-cartographer/internal/testhelper"
)
const (
testExecutableName = "true" // the program "true", not the boolean value
testDataRateMsec = 200
)
var (
testMapRateSec = 200
_true = true
_false = false
)
func TestNew(t *testing.T) {
logger := golog.NewTestLogger(t)
dataDir, err := slamTesthelper.CreateTempFolderArchitecture(logger)
test.That(t, err, test.ShouldBeNil)
t.Run("Successful creation of cartographer slam service with no sensor", func(t *testing.T) {
grpcServer, port := setupTestGRPCServer(t)
test.That(t, err, test.ShouldBeNil)
attrCfg := &slamConfig.Config{
Sensors: []string{},
ConfigParams: map[string]string{"mode": "2d"},
DataDirectory: dataDir,
Port: "localhost:" + strconv.Itoa(port),
UseLiveData: &_false,
}
svc, err := testhelper.CreateSLAMService(t, attrCfg, logger, false, testExecutableName)
test.That(t, err, test.ShouldBeNil)
grpcServer.Stop()
test.That(t, svc.Close(context.Background()), test.ShouldBeNil)
})
t.Run("Failed creation of cartographer slam service with more than one sensor", func(t *testing.T) {
grpcServer, port := setupTestGRPCServer(t)
test.That(t, err, test.ShouldBeNil)
attrCfg := &slamConfig.Config{
Sensors: []string{"lidar", "one-too-many"},
ConfigParams: map[string]string{"mode": "2d"},
DataDirectory: dataDir,
Port: "localhost:" + strconv.Itoa(port),
UseLiveData: &_false,
}
_, err := testhelper.CreateSLAMService(t, attrCfg, logger, false, testExecutableName)
test.That(t, err, test.ShouldBeError,
errors.New("configuring lidar camera error: 'sensors' must contain only one "+
"lidar camera, but is 'sensors: [lidar, one-too-many]'"))
grpcServer.Stop()
})
t.Run("Failed creation of cartographer slam service with non-existing sensor", func(t *testing.T) {
attrCfg := &slamConfig.Config{
Sensors: []string{"gibberish"},
ConfigParams: map[string]string{"mode": "2d"},
DataDirectory: dataDir,
DataRateMsec: testDataRateMsec,
UseLiveData: &_true,
}
_, err := testhelper.CreateSLAMService(t, attrCfg, logger, false, testExecutableName)
test.That(t, err, test.ShouldBeError,
errors.New("configuring lidar camera error: error getting lidar camera "+
"gibberish for slam service: \"rdk:component:camera/gibberish\" missing from dependencies"))
})
t.Run("Successful creation of cartographer slam service with good lidar", func(t *testing.T) {
grpcServer, port := setupTestGRPCServer(t)
attrCfg := &slamConfig.Config{
Sensors: []string{"good_lidar"},
ConfigParams: map[string]string{"mode": "2d"},
DataDirectory: dataDir,
DataRateMsec: testDataRateMsec,
Port: "localhost:" + strconv.Itoa(port),
UseLiveData: &_true,
}
svc, err := testhelper.CreateSLAMService(t, attrCfg, logger, false, testExecutableName)
test.That(t, err, test.ShouldBeNil)
grpcServer.Stop()
test.That(t, svc.Close(context.Background()), test.ShouldBeNil)
})
t.Run("Failed creation of cartographer slam service with invalid sensor "+
"that errors during call to NextPointCloud", func(t *testing.T) {
attrCfg := &slamConfig.Config{
Sensors: []string{"invalid_sensor"},
ConfigParams: map[string]string{"mode": "2d"},
DataDirectory: dataDir,
DataRateMsec: testDataRateMsec,
UseLiveData: &_true,
}
_, err = testhelper.CreateSLAMService(t, attrCfg, logger, false, testExecutableName)
test.That(t, err, test.ShouldBeError,
errors.New("getting and saving data failed: error getting data from sensor: invalid sensor"))
})
testhelper.ClearDirectory(t, dataDir)
}
func TestDataProcess(t *testing.T) {
logger, obs := golog.NewObservedTestLogger(t)
dataDir, err := slamTesthelper.CreateTempFolderArchitecture(logger)
test.That(t, err, test.ShouldBeNil)
grpcServer, port := setupTestGRPCServer(t)
attrCfg := &slamConfig.Config{
Sensors: []string{"good_lidar"},
ConfigParams: map[string]string{"mode": "2d"},
DataDirectory: dataDir,
DataRateMsec: testDataRateMsec,
Port: "localhost:" + strconv.Itoa(port),
UseLiveData: &_true,
}
svc, err := testhelper.CreateSLAMService(t, attrCfg, logger, false, testExecutableName)
test.That(t, err, test.ShouldBeNil)
grpcServer.Stop()
test.That(t, svc.Close(context.Background()), test.ShouldBeNil)
slamSvc := svc.(testhelper.Service)
t.Run("Successful startup of data process with good lidar", func(t *testing.T) {
sensors := []string{"good_lidar"}
lidar, err := lidar.New(testhelper.SetupDeps(sensors), sensors, 0)
test.That(t, err, test.ShouldBeNil)
cancelCtx, cancelFunc := context.WithCancel(context.Background())
c := make(chan int, 100)
slamSvc.StartDataProcess(cancelCtx, lidar, c)
<-c
cancelFunc()
files, err := os.ReadDir(dataDir + "/data/")
test.That(t, len(files), test.ShouldBeGreaterThanOrEqualTo, 1)
test.That(t, err, test.ShouldBeNil)
})
t.Run("Failed startup of data process with invalid sensor "+
"that errors during call to NextPointCloud", func(t *testing.T) {
sensors := []string{"invalid_sensor"}
lidar, err := lidar.New(testhelper.SetupDeps(sensors), sensors, 0)
test.That(t, err, test.ShouldBeNil)
cancelCtx, cancelFunc := context.WithCancel(context.Background())
c := make(chan int, 100)
slamSvc.StartDataProcess(cancelCtx, lidar, c)
<-c
allObs := obs.All()
latestLoggedEntry := allObs[len(allObs)-1]
cancelFunc()
test.That(t, fmt.Sprint(latestLoggedEntry), test.ShouldContainSubstring, "invalid sensor")
})
test.That(t, svc.Close(context.Background()), test.ShouldBeNil)
testhelper.ClearDirectory(t, dataDir)
}
func TestEndpointFailures(t *testing.T) {
logger := golog.NewTestLogger(t)
dataDir, err := slamTesthelper.CreateTempFolderArchitecture(logger)
test.That(t, err, test.ShouldBeNil)
grpcServer, port := setupTestGRPCServer(t)
attrCfg := &slamConfig.Config{
Sensors: []string{"good_lidar"},
ConfigParams: map[string]string{"mode": "2d", "test_param": "viam"},
DataDirectory: dataDir,
MapRateSec: &testMapRateSec,
DataRateMsec: testDataRateMsec,
Port: "localhost:" + strconv.Itoa(port),
UseLiveData: &_true,
}
svc, err := testhelper.CreateSLAMService(t, attrCfg, logger, false, testExecutableName)
test.That(t, err, test.ShouldBeNil)
pNew, frame, err := svc.GetPosition(context.Background())
test.That(t, pNew, test.ShouldBeNil)
test.That(t, frame, test.ShouldBeEmpty)
test.That(t, fmt.Sprint(err), test.ShouldContainSubstring, "error getting SLAM position")
callbackPointCloud, err := svc.GetPointCloudMap(context.Background())
test.That(t, err, test.ShouldBeNil)
test.That(t, callbackPointCloud, test.ShouldNotBeNil)
chunkPCD, err := callbackPointCloud()
test.That(t, err.Error(), test.ShouldContainSubstring, "error receiving pointcloud chunk")
test.That(t, chunkPCD, test.ShouldBeNil)
callbackInternalState, err := svc.GetInternalState(context.Background())
test.That(t, err, test.ShouldBeNil)
test.That(t, callbackInternalState, test.ShouldNotBeNil)
chunkInternalState, err := callbackInternalState()
test.That(t, err.Error(), test.ShouldContainSubstring, "error receiving internal state chunk")
test.That(t, chunkInternalState, test.ShouldBeNil)
grpcServer.Stop()
test.That(t, svc.Close(context.Background()), test.ShouldBeNil)
testhelper.ClearDirectory(t, dataDir)
}
func TestSLAMProcess(t *testing.T) {
logger := golog.NewTestLogger(t)
dataDir, err := slamTesthelper.CreateTempFolderArchitecture(logger)
test.That(t, err, test.ShouldBeNil)
t.Run("Successful start of live SLAM process with default parameters", func(t *testing.T) {
grpcServer, port := setupTestGRPCServer(t)
attrCfg := &slamConfig.Config{
Sensors: []string{"good_lidar"},
ConfigParams: map[string]string{"mode": "2d", "test_param": "viam"},
DataDirectory: dataDir,
Port: "localhost:" + strconv.Itoa(port),
UseLiveData: &_true,
}
svc, err := testhelper.CreateSLAMService(t, attrCfg, logger, false, testExecutableName)
test.That(t, err, test.ShouldBeNil)
slamSvc := svc.(testhelper.Service)
processCfg := slamSvc.GetSLAMProcessConfig()
cmd := append([]string{processCfg.Name}, processCfg.Args...)
cmdResult := [][]string{
{testExecutableName},
{"-sensors=good_lidar"},
{"-config_param={test_param=viam,mode=2d}", "-config_param={mode=2d,test_param=viam}"},
{"-data_rate_ms=200"},
{"-map_rate_sec=60"},
{"-data_dir=" + dataDir},
{"-delete_processed_data=true"},
{"-use_live_data=true"},
{"-port=localhost:" + strconv.Itoa(port)},
{"--aix-auto-update"},
}
for i, s := range cmd {
t.Run(fmt.Sprintf("Test command argument %v at index %v", s, i), func(t *testing.T) {
test.That(t, s, test.ShouldBeIn, cmdResult[i])
})
}
grpcServer.Stop()
test.That(t, svc.Close(context.Background()), test.ShouldBeNil)
})
t.Run("Successful start of offline SLAM process with default parameters", func(t *testing.T) {
grpcServer, port := setupTestGRPCServer(t)
attrCfg := &slamConfig.Config{
Sensors: []string{},
ConfigParams: map[string]string{"mode": "2d", "test_param": "viam"},
DataDirectory: dataDir,
Port: "localhost:" + strconv.Itoa(port),
UseLiveData: &_false,
}
svc, err := testhelper.CreateSLAMService(t, attrCfg, logger, false, testExecutableName)
test.That(t, err, test.ShouldBeNil)
slamSvc := svc.(testhelper.Service)
processCfg := slamSvc.GetSLAMProcessConfig()
cmd := append([]string{processCfg.Name}, processCfg.Args...)
cmdResult := [][]string{
{testExecutableName},
{"-sensors="},
{"-config_param={mode=2d,test_param=viam}", "-config_param={test_param=viam,mode=2d}"},
{"-data_rate_ms=200"},
{"-map_rate_sec=60"},
{"-data_dir=" + dataDir},
{"-delete_processed_data=false"},
{"-use_live_data=false"},
{"-port=localhost:" + strconv.Itoa(port)},
{"--aix-auto-update"},
}
for i, s := range cmd {
t.Run(fmt.Sprintf("Test command argument %v at index %v", s, i), func(t *testing.T) {
test.That(t, s, test.ShouldBeIn, cmdResult[i])
})
}
grpcServer.Stop()
test.That(t, svc.Close(context.Background()), test.ShouldBeNil)
})
t.Run("Failed start of SLAM process that errors out due to invalid binary location", func(t *testing.T) {
grpcServer, port := setupTestGRPCServer(t)
attrCfg := &slamConfig.Config{
Sensors: []string{"good_lidar"},
ConfigParams: map[string]string{"mode": "2d", "test_param": "viam"},
DataDirectory: dataDir,
MapRateSec: &testMapRateSec,
DataRateMsec: testDataRateMsec,
Port: "localhost:" + strconv.Itoa(port),
UseLiveData: &_true,
}
_, err := testhelper.CreateSLAMService(t, attrCfg, logger, false, "hokus_pokus_does_not_exist_filename")
test.That(t, fmt.Sprint(err), test.ShouldContainSubstring, "executable file not found in $PATH")
grpcServer.Stop()
})
testhelper.ClearDirectory(t, dataDir)
}
// SetupTestGRPCServer sets up and starts a grpc server.
// It returns the grpc server and the port at which it is served.
func setupTestGRPCServer(tb testing.TB) (*grpc.Server, int) {
listener, err := net.Listen("tcp", ":0")
test.That(tb, err, test.ShouldBeNil)
grpcServer := grpc.NewServer()
go grpcServer.Serve(listener)
return grpcServer, listener.Addr().(*net.TCPAddr).Port
}