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

Commit 1e84435

Browse files
authored
Merge pull request #277 from arduino/integration-functional-build-tags
Use build tags to separate functional and integration tests
2 parents c272a1f + 1ab374e commit 1e84435

File tree

5 files changed

+81
-52
lines changed

5 files changed

+81
-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+
}

handlers_containers_test.go renamed to handlers_containers_integration_test.go

Lines changed: 2 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// +build integration
2+
13
//
24
// This file is part of arduino-connector
35
//
@@ -22,63 +24,15 @@ import (
2224
"encoding/json"
2325
"fmt"
2426
"os"
25-
"os/exec"
2627
"strings"
2728
"testing"
2829
"time"
2930

3031
"github.com/docker/docker/api/types"
31-
docker "github.com/docker/docker/client"
32-
mqtt "github.com/eclipse/paho.mqtt.golang"
3332

3433
"github.com/stretchr/testify/assert"
3534
)
3635

37-
func TestDockerPsApi(t *testing.T) {
38-
ui := NewMqttTestClientLocal()
39-
defer ui.Close()
40-
41-
status := NewStatus(program{}.Config, nil, nil, "")
42-
status.dockerClient, _ = docker.NewClientWithOpts(docker.WithVersion("1.38"))
43-
acOptions := mqtt.NewClientOptions().AddBroker("tcp://localhost:1883").SetClientID("arduino-connector")
44-
status.mqttClient = mqtt.NewClient(acOptions)
45-
46-
if token := status.mqttClient.Connect(); token.Wait() && token.Error() != nil {
47-
t.Fatal(token.Error())
48-
}
49-
50-
subscribeTopic(status.mqttClient, "0", "/containers/ps/post", status, status.ContainersPsEvent, false)
51-
52-
resp := ui.MqttSendAndReceiveTimeout(t, "/containers/ps", "{}", 50*time.Millisecond)
53-
54-
// ask Docker about containers effectively running
55-
cmd := exec.Command("bash", "-c", "docker ps -a")
56-
out, err := cmd.CombinedOutput()
57-
if err != nil {
58-
t.Fatal(err)
59-
}
60-
61-
lines := strings.Split(string(out), "\n")
62-
// Remove the first line (command output header) and the last line (empty line)
63-
lines = lines[1 : len(lines)-1]
64-
65-
// Take json without INFO tag
66-
resp = strings.TrimPrefix(resp, "INFO: ")
67-
resp = strings.TrimSuffix(resp, "\n\n")
68-
var result []types.Container
69-
if err := json.Unmarshal([]byte(resp), &result); err != nil {
70-
t.Fatal(err)
71-
}
72-
73-
assert.Equal(t, len(result), len(lines))
74-
for i, line := range lines {
75-
containerId := strings.Fields(line)[0]
76-
assert.True(t, strings.HasPrefix(result[i].ID, containerId))
77-
}
78-
79-
status.mqttClient.Disconnect(100)
80-
}
81-
8236
func TestConnectorProcessIsRunning(t *testing.T) {
8337
outputMessage, err := ExecAsVagrantSshCmd("systemctl status ArduinoConnector | grep running")
8438
if err != nil {

scripts/functional-tests.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ set -euo pipefail
55
trap 'kill "$(pidof mosquitto)"' EXIT
66

77
mosquitto > /dev/null &
8-
go test -v --run="TestDockerPsApi"
8+
go test -v --tags=functional --run="TestDockerPsApi"

0 commit comments

Comments
 (0)