Skip to content

Commit a9ed037

Browse files
Owen Nelsonscbrickley
Owen Nelson
andauthoredMar 17, 2022
feat: add mqtt docker integration test (#4556)
* feat: add MQTT broker container for integ testing * fix: spawn-containers was not launching anything for the "all" case Looks like when the script was refactored to allow for individual services to be launched, the "all" case was not well tested. As such, when we didn't name any services to launch nothing would launch. After fixing this issue, it seems the hdb container doesn't launch properly in the current state of the script. Something about having those docker commands in a function or the function being sent to the background disconnects it from the main tty for the script causing it to fail to perform the readiness check properly. In practice it seemed this meant hdb would report "ready" too soon and the seed data insert would fail, as well as the tests. For now, I'm skipping hdb. We'll have to loop back to try and find a good alternative approach. * test: add test for mqtt.publish This test currently fails because the client dependency isn't injected. * chore: inject mqtt dialer for test dependencies * fix: some containers take time to clean, ignore fails Sometimes when we run `docker stop` the engine can take a beat before the container is removed from the active list. The follow-up `docker rm` can therefore try to destroy the container while it's stopping (and `rm`'ing itself). In such a case, ignore the failure. If the container truly sticks around beyond these 2 commands, we'll see a different failure when we try to relaunch the container with an identical name. * chore: print message while NOT launching SAP HANA * fix: launch SAP HANA without sending job to background * chore: improve doc for how to shutdown all integ test containers * chore: make generate * fix: add span to context for all dependency tests (#4568) Co-authored-by: Sean Brickley <sean@wabr.io>
1 parent a3a82bc commit a9ed037

File tree

7 files changed

+111
-16
lines changed

7 files changed

+111
-16
lines changed
 

‎Makefile

+2-2
Original file line numberDiff line numberDiff line change
@@ -132,15 +132,15 @@ test-rust:
132132
$(CARGO) clippy $(CARGO_ARGS) -- -Dclippy::all -Dclippy::undocumented_unsafe_blocks
133133

134134
INTEGRATION_INJECTION_TESTS=integration_hdb_injection,integration_sqlite_injection,integration_vertica_injection,integration_mssql_injection,integration_mysql_injection,integration_mariadb_injection,integration_pg_injection
135-
INTEGRATION_WRITE_TESTS=integration_hdb_write_to,integration_sqlite_write_to,integration_vertica_write_to,integration_mssql_write_to,integration_mysql_write_to,integration_mariadb_write_to,integration_pg_write_to
135+
INTEGRATION_WRITE_TESTS=integration_mqtt_pub,integration_hdb_write_to,integration_sqlite_write_to,integration_vertica_write_to,integration_mssql_write_to,integration_mysql_write_to,integration_mariadb_write_to,integration_pg_write_to
136136
INTEGRATION_READ_TESTS=integration_hdb_read_from_seed,integration_hdb_read_from_nonseed,integration_sqlite_read_from_seed,integration_sqlite_read_from_nonseed,integration_vertica_read_from_seed,integration_vertica_read_from_nonseed,integration_mssql_read_from_seed,integration_mssql_read_from_nonseed,integration_mariadb_read_from_seed,integration_mariadb_read_from_nonseed,integration_mysql_read_from_seed,integration_mysql_read_from_nonseed,integration_pg_read_from_seed,integration_pg_read_from_nonseed
137137
INTEGRATION_TESTS="$(INTEGRATION_INJECTION_TESTS),$(INTEGRATION_WRITE_TESTS),$(INTEGRATION_READ_TESTS)"
138138

139139
test-flux:
140140
$(GO_RUN) ./cmd/flux test -v --skip $(INTEGRATION_TESTS)
141141

142142
test-flux-integration:
143-
./etc/spawn-dbs.sh
143+
./etc/spawn-containers.sh
144144
$(GO_RUN) ./cmd/flux test -v --test $(INTEGRATION_INJECTION_TESTS)
145145
$(GO_RUN) ./cmd/flux test -v --test $(INTEGRATION_WRITE_TESTS)
146146
$(GO_RUN) ./cmd/flux test -v --test $(INTEGRATION_READ_TESTS)

‎dependencies/dependenciestest/dependencies.go

+10-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ import (
88
"github.com/influxdata/flux"
99
"github.com/influxdata/flux/dependencies/filesystem"
1010
"github.com/influxdata/flux/dependencies/influxdb"
11+
"github.com/influxdata/flux/dependencies/mqtt"
1112
"github.com/influxdata/flux/dependencies/url"
13+
"github.com/influxdata/flux/dependency"
1214
"github.com/influxdata/flux/execute"
1315
"github.com/influxdata/flux/mock"
1416
)
@@ -39,10 +41,14 @@ func defaultTestFunction(req *http.Request) *http.Response {
3941
type Deps struct {
4042
flux.Deps
4143
influxdb influxdb.Dependency
44+
mqtt mqtt.Dependency
4245
}
4346

4447
func (d Deps) Inject(ctx context.Context) context.Context {
45-
return d.influxdb.Inject(d.Deps.Inject(ctx))
48+
ctx = d.Deps.Inject(ctx)
49+
ctx = d.influxdb.Inject(ctx)
50+
ctx, _ = dependency.Inject(ctx)
51+
return d.mqtt.Inject(ctx)
4652
}
4753

4854
func Default() Deps {
@@ -62,6 +68,9 @@ func Default() Deps {
6268
influxdb: influxdb.Dependency{
6369
Provider: influxdb.HttpProvider{},
6470
},
71+
mqtt: mqtt.Dependency{
72+
Dialer: mqtt.DefaultDialer{},
73+
},
6574
}
6675
}
6776

‎etc/spawn-dbs.sh renamed to ‎etc/spawn-containers.sh

+52-13
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/bin/bash
22

3-
# This script will try to spin up a docker container for a series of database
4-
# engines which select flux tests will run against.
3+
# This script will try to spin up a docker container for a series of services
4+
# (mostly database engines) which select flux tests will run against.
55
#
66
# Typically you will not invoke this script yourself.
77
# Instead use: `make test-flux-integration` from the repo root since the Make
@@ -19,7 +19,12 @@
1919
# To shutdown all the containers (after you're done running
2020
# integration tests), you should be able to do something like:
2121
# ```
22-
# docker ps --format '{{.Names}}' | grep flux-integ-tests | xargs docker rm -f
22+
# docker ps --format '{{.Names}}' | grep flux-integ-tests | xargs docker stop
23+
# ```
24+
# If you only happen to be using docker _for these integration tests_ you can
25+
# more succinctly stop everything all at once with:
26+
# ```
27+
# docker stop $(docker ps -q)
2328
# ```
2429

2530
set -e
@@ -34,6 +39,9 @@ MARIADB_NAME="${PREFIX}-mariadb"
3439
MARIADB_TAG="mariadb:10"
3540
MS_NAME="${PREFIX}-mssql"
3641
MS_TAG="mcr.microsoft.com/mssql/server:2019-latest"
42+
MQTT_TAG="eclipse-mosquitto:2.0.14"
43+
MQTT_NAME="${PREFIX}-mqtt"
44+
MQTT_CONFIG_FILE="/tmp/${PREFIX}-mosquitto.conf"
3745
VERTICA_NAME="${PREFIX}-vertica"
3846
VERTICA_TAG="vertica/vertica-ce:11.0.0-0"
3947
SQLITE_DB_PATH="/tmp/${PREFIX}-sqlite.db"
@@ -158,8 +166,11 @@ rm -f "$SQLITE_DB_PATH"
158166
# volumes that would otherwise be left orphaned.
159167
# Each container we run _should be_ launched with the `--rm` flag to help cleanup
160168
# these spent containers as we go.
161-
docker stop "${HDB_NAME}" "${PG_NAME}" "${MYSQL_NAME}" "${MARIADB_NAME}" "${MS_NAME}" "${VERTICA_NAME}" \
162-
|| docker rm -f "${HDB_NAME}" "${PG_NAME}" "${MYSQL_NAME}" "${MARIADB_NAME}" "${MS_NAME}" "${VERTICA_NAME}"
169+
docker stop \
170+
"${HDB_NAME}" "${PG_NAME}" "${MYSQL_NAME}" "${MARIADB_NAME}" "${MS_NAME}" "${VERTICA_NAME}" "${MQTT_NAME}" \
171+
|| docker rm -f \
172+
"${HDB_NAME}" "${PG_NAME}" "${MYSQL_NAME}" "${MARIADB_NAME}" "${MS_NAME}" "${VERTICA_NAME}" "${MQTT_NAME}" \
173+
|| true
163174

164175
function wait_for () {
165176
name="${1}"
@@ -259,10 +270,31 @@ function run_pg {
259270
docker exec "${PG_NAME}" psql -U postgres -c "${PG_SEED}"
260271
}
261272

273+
function run_mqtt {
274+
cat <<'EOF' > "${MQTT_CONFIG_FILE}"
275+
listener 1883
276+
allow_anonymous true
277+
EOF
278+
279+
docker run --rm --detach \
280+
--name "${MQTT_NAME}" \
281+
--publish 1883:1883 \
282+
-v "${MQTT_CONFIG_FILE}:/mosquitto/config/mosquitto.conf:ro" \
283+
"${MQTT_TAG}" \
284+
mosquitto -v -c /mosquitto/config/mosquitto.conf
285+
286+
wait_for "MQTT" "docker exec ${MQTT_NAME} mosquitto_pub -t 'liveness' -n"
287+
# XXX: the flux mqtt support currently only exposes write operations.
288+
# If we also supported reads, we could seed the queue here with some retained
289+
# messages or by setting the QoS value.
290+
}
291+
292+
# trailing args to the script can be service names, checked by `should_start`
293+
TAIL="$*"
262294
function should_start {
263-
# Start a databases if no the script is invoked without any arguments, or if the database is
264-
# among the arguments
265-
[ "$1" == "" ] || (echo "${@:2}" | grep "$1")
295+
# Start a container if no the script is invoked without any arguments, or if
296+
# the service is among the arguments
297+
[ "${TAIL}" == "" ] || (echo "$TAIL" | grep "$1")
266298
}
267299

268300
if should_start "mariadb" "$@" ; then
@@ -285,11 +317,6 @@ if should_start "vertica" "$@" ; then
285317
run_vertica &
286318
fi
287319

288-
if should_start "saphana" "$@" ; then
289-
echo "Starting SAP Hana"
290-
run_sap_hana &
291-
fi
292-
293320
if should_start "postgres" "$@" ; then
294321
echo "Starting Postgres"
295322
run_pg &
@@ -300,4 +327,16 @@ if should_start "sqlite" "$@" ; then
300327
sqlite3 "${SQLITE_DB_PATH}" "${SQLITE_SEED}" &
301328
fi
302329

330+
if should_start "mqtt" "$@" ; then
331+
echo "Starting MQTT"
332+
run_mqtt &
333+
fi
334+
335+
if should_start "saphana" "$@" ; then
336+
echo "Starting SAP Hana"
337+
# n.b. this relies on docker commands that bind tty (via the `-it` flags).
338+
# This means it cannot be backgrounded like the others (with `&`).
339+
run_sap_hana
340+
fi
341+
303342
wait

‎libflux/go/libflux/buildinfo.gen.go

+1
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,7 @@ var sourceHashes = map[string]string{
201201
"stdlib/experimental/min_test.flux": "9c50f8ed865bc4ce29596c58be44e76071be9ca16ef57d65156d563e920ea922",
202202
"stdlib/experimental/mode_test.flux": "e125d0d2de23a705e3593ce8f414a3f3519b852083ebd989748ab7dd5d203257",
203203
"stdlib/experimental/mqtt/mqtt.flux": "31aeae56a961429a0a69f930f2184a006b6f4269923d73dde98883fa5bec8805",
204+
"stdlib/experimental/mqtt/mqtt_test.flux": "bfb77c41bccffcf5890027ec49e29c9c607c7bb59459dabec772fb989031c5a9",
204205
"stdlib/experimental/oee/apq_test.flux": "6a64721efd123bbfd40ca90cbebc7cfd2dccbd651da5e1ee4d33e0833256be60",
205206
"stdlib/experimental/oee/computeapq_test.flux": "a5069718fb11cee0ffc8b345ead279ccb7f4bcc4644ffdc8d641c310be1969ec",
206207
"stdlib/experimental/oee/oee.flux": "30d6ce4144d1425cb9b294bd17fd73395d2a7c87b86c089aa89e73969a5b98f3",
+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// DO NOT EDIT: This file is autogenerated via the builtin command.
2+
3+
package mqtt
4+
5+
import ast "github.com/influxdata/flux/ast"
6+
7+
var FluxTestPackages = []*ast.Package{&ast.Package{
8+
BaseNode: ast.BaseNode{
9+
Comments: nil,
10+
Errors: nil,
11+
Loc: nil,
12+
},
13+
Files: []*ast.File{},
14+
Package: "mqtt_test",
15+
Path: "experimental/mqtt",
16+
}}
+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package mqtt_test
2+
3+
4+
import "array"
5+
import "experimental/mqtt"
6+
import "testing"
7+
8+
testcase integration_mqtt_pub {
9+
got =
10+
array.from(
11+
rows: [
12+
{
13+
ok:
14+
mqtt.publish(
15+
broker: "tcp://127.0.0.1:1883",
16+
topic: "test/topic",
17+
message: "smoke test",
18+
qos: 0,
19+
retain: false,
20+
clientid: "fluxtest",
21+
),
22+
},
23+
],
24+
)
25+
want = array.from(rows: [{ok: true}])
26+
27+
testing.diff(want: want, got: got) |> yield()
28+
}

‎stdlib/test_packages.go

+2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
bitwise "github.com/influxdata/flux/stdlib/experimental/bitwise"
2222
geo "github.com/influxdata/flux/stdlib/experimental/geo"
2323
json "github.com/influxdata/flux/stdlib/experimental/json"
24+
mqtt "github.com/influxdata/flux/stdlib/experimental/mqtt"
2425
oee "github.com/influxdata/flux/stdlib/experimental/oee"
2526
prometheus "github.com/influxdata/flux/stdlib/experimental/prometheus"
2627
record "github.com/influxdata/flux/stdlib/experimental/record"
@@ -73,6 +74,7 @@ var FluxTestPackages = func() []*ast.Package {
7374
pkgs = append(pkgs, bitwise.FluxTestPackages...)
7475
pkgs = append(pkgs, geo.FluxTestPackages...)
7576
pkgs = append(pkgs, json.FluxTestPackages...)
77+
pkgs = append(pkgs, mqtt.FluxTestPackages...)
7678
pkgs = append(pkgs, oee.FluxTestPackages...)
7779
pkgs = append(pkgs, prometheus.FluxTestPackages...)
7880
pkgs = append(pkgs, record.FluxTestPackages...)

0 commit comments

Comments
 (0)
Please sign in to comment.