Skip to content
This repository was archived by the owner on Mar 27, 2025. It is now read-only.

Commit 1914813

Browse files
author
Fabio Falzoi
committed
Use build tags to separate func and integ tests
Using the build tags "integration" and "functional" we separate the two class of tests.
1 parent c272a1f commit 1914813

File tree

6 files changed

+667
-52
lines changed

6 files changed

+667
-52
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ integ-test:
2727
$(GOBUILD) -ldflags "-X main.version=1.0.0-dev" github.com/arduino/arduino-connector
2828
cd ./test && ./upload_dev_artifacts_on_s3.sh
2929
cd ./test && vagrant provision
30-
source ./test/setup_host_test_env.sh && $(GOTEST) ./... -timeout $(GOTEST_TIMEOUT)
30+
source ./test/setup_host_test_env.sh && $(GOTEST) --tags=integration ./... -timeout $(GOTEST_TIMEOUT)
3131

3232
teardown-test:
3333
cd ./test && ./teardown_iot_device.sh

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,8 @@ See [API](./API.md)
6969
This type of tests are executed all locally and you need to configure a dedicated docker container:
7070
- `docker pull guerra1994/go-mqtt-docker-env`
7171
- follow the steps [here](https://hub.docker.com/r/guerra1994/go-mqtt-docker-env) to run it
72-
- run mosquitto broker in background mode using `mosquitto &`
73-
- then run your test, for example `go test -v --run="TestDockerPsApi"`
72+
- run mosquitto broker in background mode using `mosquitto > /dev/null 2>&1 &`
73+
- then run your test, for example `go test -v --tags=functional --run="TestDockerPsApi"`
7474

7575

7676
## Integration tests disclaimer
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
// +build functional
2+
3+
package main
4+
5+
import (
6+
"encoding/json"
7+
"os"
8+
"os/exec"
9+
"strings"
10+
"testing"
11+
"time"
12+
13+
"github.com/docker/docker/api/types"
14+
docker "github.com/docker/docker/client"
15+
mqtt "github.com/eclipse/paho.mqtt.golang"
16+
"github.com/stretchr/testify/assert"
17+
)
18+
19+
type testStatus struct {
20+
appStatus *Status
21+
ui *MqttTestClient
22+
}
23+
24+
var ts testStatus
25+
26+
func TestMain(m *testing.M) {
27+
os.Exit(setupAndRun(m))
28+
}
29+
30+
func setupAndRun(m *testing.M) int {
31+
ts.ui = NewMqttTestClientLocal()
32+
defer ts.ui.Close()
33+
34+
ts.appStatus = NewStatus(program{}.Config, nil, nil, "")
35+
ts.appStatus.dockerClient, _ = docker.NewClientWithOpts(docker.WithVersion("1.38"))
36+
ts.appStatus.mqttClient = mqtt.NewClient(mqtt.NewClientOptions().AddBroker("tcp://localhost:1883").SetClientID("arduino-connector"))
37+
38+
defer ts.appStatus.mqttClient.Disconnect(100)
39+
40+
return m.Run()
41+
}
42+
43+
func TestDockerPsApi(t *testing.T) {
44+
if token := ts.appStatus.mqttClient.Connect(); token.Wait() && token.Error() != nil {
45+
t.Fatal(token.Error())
46+
}
47+
48+
subscribeTopic(ts.appStatus.mqttClient, "0", "/containers/ps/post", ts.appStatus, ts.appStatus.ContainersPsEvent, false)
49+
resp := ts.ui.MqttSendAndReceiveTimeout(t, "/containers/ps", "{}", 50*time.Millisecond)
50+
51+
// ask Docker about containers effectively running
52+
cmd := exec.Command("bash", "-c", "docker ps -a")
53+
out, err := cmd.CombinedOutput()
54+
if err != nil {
55+
t.Fatal(err)
56+
}
57+
58+
lines := strings.Split(string(out), "\n")
59+
// Remove the first line (command output header) and the last line (empty line)
60+
lines = lines[1 : len(lines)-1]
61+
62+
// Take json without INFO tag
63+
resp = strings.TrimPrefix(resp, "INFO: ")
64+
resp = strings.TrimSuffix(resp, "\n\n")
65+
var result []types.Container
66+
if err := json.Unmarshal([]byte(resp), &result); err != nil {
67+
t.Fatal(err)
68+
}
69+
70+
assert.Equal(t, len(result), len(lines))
71+
for i, line := range lines {
72+
containerId := strings.Fields(line)[0]
73+
assert.True(t, strings.HasPrefix(result[i].ID, containerId))
74+
}
75+
}

0 commit comments

Comments
 (0)