2019-12-18 20:08:46 +00:00
|
|
|
package docker
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"github.com/testcontainers/testcontainers-go"
|
|
|
|
"github.com/testcontainers/testcontainers-go/wait"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func MqttContainer(t *testing.T) (context.Context, testcontainers.Container, string) {
|
2021-10-12 21:39:07 +00:00
|
|
|
ctx := context.Background()
|
|
|
|
req := testcontainers.ContainerRequest{
|
|
|
|
Image: "eclipse-mosquitto",
|
|
|
|
ExposedPorts: []string{"1883/tcp"},
|
|
|
|
WaitingFor: wait.ForLog("listen socket on port 1883."),
|
|
|
|
}
|
|
|
|
mqttC, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
|
|
|
|
ContainerRequest: req,
|
|
|
|
Started: true,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
2019-12-18 20:08:46 +00:00
|
|
|
|
2021-10-12 21:39:07 +00:00
|
|
|
ip, err := mqttC.Host(ctx)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
port, err := mqttC.MappedPort(ctx, "1883/tcp")
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
2019-12-18 20:08:46 +00:00
|
|
|
|
2021-10-12 21:39:07 +00:00
|
|
|
mqttUri := fmt.Sprintf("tcp://%s:%d", ip, port.Int())
|
|
|
|
return ctx, mqttC, mqttUri
|
2019-12-18 20:08:46 +00:00
|
|
|
}
|