test: refactor testcontainer port lookup (#11198)

This commit is contained in:
Joshua Powers 2022-05-27 10:24:31 -06:00 committed by GitHub
parent 02dd7c1752
commit 1200f3c87e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
15 changed files with 207 additions and 84 deletions

View File

@ -12,10 +12,12 @@ import (
"github.com/influxdata/telegraf/testutil"
)
const servicePort = "3000"
func launchTestServer(t *testing.T) testutil.Container {
container := testutil.Container{
Image: "aerospike:ce-6.0.0.1",
ExposedPorts: []string{"3000"},
ExposedPorts: []string{servicePort},
WaitingFor: wait.ForLog("migrations: complete"),
}
err := container.Start()
@ -35,7 +37,7 @@ func TestAerospikeStatisticsIntegration(t *testing.T) {
}()
a := &Aerospike{
Servers: []string{fmt.Sprintf("%s:%s", container.Address, container.Port)},
Servers: []string{fmt.Sprintf("%s:%s", container.Address, container.Ports[servicePort])},
}
var acc testutil.Accumulator
@ -65,7 +67,7 @@ func TestAerospikeStatisticsPartialErrIntegration(t *testing.T) {
a := &Aerospike{
Servers: []string{
fmt.Sprintf("%s:%s", container.Address, container.Port),
fmt.Sprintf("%s:%s", container.Address, container.Ports[servicePort]),
testutil.GetLocalHost() + ":9999",
},
}
@ -94,7 +96,7 @@ func TestSelectNamespacesIntegration(t *testing.T) {
// Select nonexistent namespace
a := &Aerospike{
Servers: []string{fmt.Sprintf("%s:%s", container.Address, container.Port)},
Servers: []string{fmt.Sprintf("%s:%s", container.Address, container.Ports[servicePort])},
Namespaces: []string{"notTest"},
}
@ -133,7 +135,7 @@ func TestDisableQueryNamespacesIntegration(t *testing.T) {
a := &Aerospike{
Servers: []string{
fmt.Sprintf("%s:%s", container.Address, container.Port),
fmt.Sprintf("%s:%s", container.Address, container.Ports[servicePort]),
},
DisableQueryNamespaces: true,
}
@ -163,7 +165,7 @@ func TestQuerySetsIntegration(t *testing.T) {
require.NoError(t, container.Terminate(), "terminating container failed")
}()
portInt, err := strconv.Atoi(container.Port)
portInt, err := strconv.Atoi(container.Ports[servicePort])
require.NoError(t, err)
// create a set
@ -192,7 +194,7 @@ func TestQuerySetsIntegration(t *testing.T) {
a := &Aerospike{
Servers: []string{
fmt.Sprintf("%s:%s", container.Address, container.Port),
fmt.Sprintf("%s:%s", container.Address, container.Ports[servicePort]),
},
QuerySets: true,
DisableQueryNamespaces: true,
@ -220,7 +222,7 @@ func TestSelectQuerySetsIntegration(t *testing.T) {
require.NoError(t, container.Terminate(), "terminating container failed")
}()
portInt, err := strconv.Atoi(container.Port)
portInt, err := strconv.Atoi(container.Ports[servicePort])
require.NoError(t, err)
// create a set
@ -249,7 +251,7 @@ func TestSelectQuerySetsIntegration(t *testing.T) {
a := &Aerospike{
Servers: []string{
fmt.Sprintf("%s:%s", container.Address, container.Port),
fmt.Sprintf("%s:%s", container.Address, container.Ports[servicePort]),
},
QuerySets: true,
Sets: []string{"test/foo"},
@ -280,7 +282,7 @@ func TestDisableTTLHistogramIntegration(t *testing.T) {
a := &Aerospike{
Servers: []string{
fmt.Sprintf("%s:%s", container.Address, container.Port),
fmt.Sprintf("%s:%s", container.Address, container.Ports[servicePort]),
},
QuerySets: true,
EnableTTLHistogram: false,
@ -307,7 +309,7 @@ func TestDisableObjectSizeLinearHistogramIntegration(t *testing.T) {
a := &Aerospike{
Servers: []string{
fmt.Sprintf("%s:%s", container.Address, container.Port),
fmt.Sprintf("%s:%s", container.Address, container.Ports[servicePort]),
},
QuerySets: true,
EnableObjectSizeLinearHistogram: false,

View File

@ -6,6 +6,7 @@ import (
"strings"
"testing"
"github.com/docker/go-connections/nat"
"github.com/stretchr/testify/require"
"github.com/testcontainers/testcontainers-go/wait"
@ -17,10 +18,11 @@ func TestMemcachedGeneratesMetricsIntegration(t *testing.T) {
t.Skip("Skipping integration test in short mode")
}
servicePort := "11211"
container := testutil.Container{
Image: "memcached",
ExposedPorts: []string{"11211"},
WaitingFor: wait.ForListeningPort("11211/tcp"),
ExposedPorts: []string{servicePort},
WaitingFor: wait.ForListeningPort(nat.Port(servicePort)),
}
err := container.Start()
require.NoError(t, err, "failed to start container")
@ -29,7 +31,7 @@ func TestMemcachedGeneratesMetricsIntegration(t *testing.T) {
}()
m := &Memcached{
Servers: []string{fmt.Sprintf("%s:%s", container.Address, container.Port)},
Servers: []string{fmt.Sprintf("%s:%s", container.Address, container.Ports[servicePort])},
}
var acc testutil.Accumulator

View File

@ -5,12 +5,15 @@ import (
"testing"
"github.com/DATA-DOG/go-sqlmock"
"github.com/docker/go-connections/nat"
"github.com/stretchr/testify/require"
"github.com/testcontainers/testcontainers-go/wait"
"github.com/influxdata/telegraf/testutil"
)
const servicePort = "3306"
func TestMysqlDefaultsToLocalIntegration(t *testing.T) {
if testing.Short() {
t.Skip("Skipping integration test in short mode")
@ -21,8 +24,8 @@ func TestMysqlDefaultsToLocalIntegration(t *testing.T) {
Env: map[string]string{
"MYSQL_ALLOW_EMPTY_PASSWORD": "yes",
},
ExposedPorts: []string{"3306"},
WaitingFor: wait.ForListeningPort("3306"),
ExposedPorts: []string{servicePort},
WaitingFor: wait.ForListeningPort(nat.Port(servicePort)),
}
err := container.Start()
@ -32,7 +35,7 @@ func TestMysqlDefaultsToLocalIntegration(t *testing.T) {
}()
m := &Mysql{
Servers: []string{fmt.Sprintf("root@tcp(%s:%s)/", container.Address, container.Port)},
Servers: []string{fmt.Sprintf("root@tcp(%s:%s)/", container.Address, container.Ports[servicePort])},
}
var acc testutil.Accumulator
@ -55,8 +58,8 @@ func TestMysqlMultipleInstancesIntegration(t *testing.T) {
Env: map[string]string{
"MYSQL_ALLOW_EMPTY_PASSWORD": "yes",
},
ExposedPorts: []string{"3306"},
WaitingFor: wait.ForListeningPort("3306/tcp"),
ExposedPorts: []string{servicePort},
WaitingFor: wait.ForListeningPort(nat.Port(servicePort)),
}
err := container.Start()
@ -65,7 +68,7 @@ func TestMysqlMultipleInstancesIntegration(t *testing.T) {
require.NoError(t, container.Terminate(), "terminating container failed")
}()
testServer := fmt.Sprintf("root@tcp(%s:%s)/?tls=false", container.Address, container.Port)
testServer := fmt.Sprintf("root@tcp(%s:%s)/?tls=false", container.Address, container.Ports[servicePort])
m := &Mysql{
Servers: []string{testServer},
IntervalSlow: "30s",

View File

@ -6,6 +6,7 @@ import (
"testing"
"time"
"github.com/docker/go-connections/nat"
"github.com/stretchr/testify/require"
"github.com/testcontainers/testcontainers-go/wait"
@ -22,6 +23,8 @@ type OPCTags struct {
Want interface{}
}
const servicePort = "4840"
func TestGetDataBadNodeContainerIntegration(t *testing.T) {
if testing.Short() {
t.Skip("Skipping integration test in short mode")
@ -29,8 +32,8 @@ func TestGetDataBadNodeContainerIntegration(t *testing.T) {
container := testutil.Container{
Image: "open62541/open62541",
ExposedPorts: []string{"4840"},
WaitingFor: wait.ForListeningPort("4840/tcp"),
ExposedPorts: []string{servicePort},
WaitingFor: wait.ForListeningPort(nat.Port(servicePort)),
}
err := container.Start()
require.NoError(t, err, "failed to start container")
@ -46,7 +49,7 @@ func TestGetDataBadNodeContainerIntegration(t *testing.T) {
var o OpcUA
o.MetricName = "testing"
o.Endpoint = fmt.Sprintf("opc.tcp://%s:%s", container.Address, container.Port)
o.Endpoint = fmt.Sprintf("opc.tcp://%s:%s", container.Address, container.Ports[servicePort])
fmt.Println(o.Endpoint)
o.AuthMethod = "Anonymous"
o.ConnectTimeout = config.Duration(10 * time.Second)
@ -82,8 +85,8 @@ func TestClient1Integration(t *testing.T) {
container := testutil.Container{
Image: "open62541/open62541",
ExposedPorts: []string{"4840"},
WaitingFor: wait.ForListeningPort("4840/tcp"),
ExposedPorts: []string{servicePort},
WaitingFor: wait.ForListeningPort(nat.Port(servicePort)),
}
err := container.Start()
require.NoError(t, err, "failed to start container")
@ -101,7 +104,7 @@ func TestClient1Integration(t *testing.T) {
var o OpcUA
o.MetricName = "testing"
o.Endpoint = fmt.Sprintf("opc.tcp://%s:%s", container.Address, container.Port)
o.Endpoint = fmt.Sprintf("opc.tcp://%s:%s", container.Address, container.Ports[servicePort])
o.AuthMethod = "Anonymous"
o.ConnectTimeout = config.Duration(10 * time.Second)
o.RequestTimeout = config.Duration(1 * time.Second)

View File

@ -5,6 +5,7 @@ import (
"strconv"
"testing"
"github.com/docker/go-connections/nat"
"github.com/go-ldap/ldap/v3"
"github.com/stretchr/testify/require"
"github.com/testcontainers/testcontainers-go/wait"
@ -12,6 +13,11 @@ import (
"github.com/influxdata/telegraf/testutil"
)
const (
servicePort = "1389"
servicePortSecure = "1636"
)
func TestOpenldapMockResult(t *testing.T) {
var acc testutil.Accumulator
@ -59,12 +65,15 @@ func TestOpenldapGeneratesMetricsIntegration(t *testing.T) {
container := testutil.Container{
Image: "bitnami/openldap",
ExposedPorts: []string{"1389"},
ExposedPorts: []string{servicePort},
Env: map[string]string{
"LDAP_ADMIN_USERNAME": "manager",
"LDAP_ADMIN_PASSWORD": "secret",
},
WaitingFor: wait.ForLog("Starting slapd"),
WaitingFor: wait.ForAll(
wait.ForLog("Starting slapd"),
wait.ForListeningPort(nat.Port(servicePort)),
),
}
err := container.Start()
require.NoError(t, err, "failed to start container")
@ -72,7 +81,7 @@ func TestOpenldapGeneratesMetricsIntegration(t *testing.T) {
require.NoError(t, container.Terminate(), "terminating container failed")
}()
port, err := strconv.Atoi(container.Port)
port, err := strconv.Atoi(container.Ports[servicePort])
require.NoError(t, err)
o := &Openldap{
@ -104,7 +113,7 @@ func TestOpenldapStartTLSIntegration(t *testing.T) {
container := testutil.Container{
Image: "bitnami/openldap",
ExposedPorts: []string{"1389", "1636"},
ExposedPorts: []string{servicePort},
Env: map[string]string{
"LDAP_ADMIN_USERNAME": "manager",
"LDAP_ADMIN_PASSWORD": "secret",
@ -118,7 +127,10 @@ func TestOpenldapStartTLSIntegration(t *testing.T) {
"/server.crt": tlsCert,
"/server.key": tlsKey,
},
WaitingFor: wait.ForListeningPort("1389/tcp"),
WaitingFor: wait.ForAll(
wait.ForLog("Starting slapd"),
wait.ForListeningPort(nat.Port(servicePort)),
),
}
err = container.Start()
require.NoError(t, err, "failed to start container")
@ -126,7 +138,7 @@ func TestOpenldapStartTLSIntegration(t *testing.T) {
require.NoError(t, container.Terminate(), "terminating container failed")
}()
port, err := strconv.Atoi(container.Port)
port, err := strconv.Atoi(container.Ports[servicePort])
require.NoError(t, err)
cert, err := filepath.Abs(pki.ClientCertPath())
@ -164,7 +176,7 @@ func TestOpenldapLDAPSIntegration(t *testing.T) {
container := testutil.Container{
Image: "bitnami/openldap",
ExposedPorts: []string{"1636"},
ExposedPorts: []string{servicePortSecure},
Env: map[string]string{
"LDAP_ADMIN_USERNAME": "manager",
"LDAP_ADMIN_PASSWORD": "secret",
@ -178,7 +190,10 @@ func TestOpenldapLDAPSIntegration(t *testing.T) {
"/server.crt": tlsCert,
"/server.key": tlsKey,
},
WaitingFor: wait.ForListeningPort("1636/tcp"),
WaitingFor: wait.ForAll(
wait.ForLog("Starting slapd"),
wait.ForListeningPort(nat.Port(servicePortSecure)),
),
}
err = container.Start()
require.NoError(t, err, "failed to start container")
@ -186,7 +201,7 @@ func TestOpenldapLDAPSIntegration(t *testing.T) {
require.NoError(t, container.Terminate(), "terminating container failed")
}()
port, err := strconv.Atoi(container.Port)
port, err := strconv.Atoi(container.Ports[servicePortSecure])
require.NoError(t, err)
o := &Openldap{
@ -219,7 +234,7 @@ func TestOpenldapInvalidSSLIntegration(t *testing.T) {
container := testutil.Container{
Image: "bitnami/openldap",
ExposedPorts: []string{"1636"},
ExposedPorts: []string{servicePortSecure},
Env: map[string]string{
"LDAP_ADMIN_USERNAME": "manager",
"LDAP_ADMIN_PASSWORD": "secret",
@ -233,7 +248,10 @@ func TestOpenldapInvalidSSLIntegration(t *testing.T) {
"/server.crt": tlsCert,
"/server.key": tlsKey,
},
WaitingFor: wait.ForListeningPort("1636/tcp"),
WaitingFor: wait.ForAll(
wait.ForLog("Starting slapd"),
wait.ForListeningPort(nat.Port(servicePortSecure)),
),
}
err = container.Start()
require.NoError(t, err, "failed to start container")
@ -241,7 +259,7 @@ func TestOpenldapInvalidSSLIntegration(t *testing.T) {
require.NoError(t, container.Terminate(), "terminating container failed")
}()
port, err := strconv.Atoi(container.Port)
port, err := strconv.Atoi(container.Ports[servicePortSecure])
require.NoError(t, err)
o := &Openldap{
@ -265,12 +283,15 @@ func TestOpenldapBindIntegration(t *testing.T) {
container := testutil.Container{
Image: "bitnami/openldap",
ExposedPorts: []string{"1389"},
ExposedPorts: []string{servicePort},
Env: map[string]string{
"LDAP_ADMIN_USERNAME": "manager",
"LDAP_ADMIN_PASSWORD": "secret",
},
WaitingFor: wait.ForListeningPort("1389/tcp"),
WaitingFor: wait.ForAll(
wait.ForLog("Starting slapd"),
wait.ForListeningPort(nat.Port(servicePort)),
),
}
err := container.Start()
require.NoError(t, err, "failed to start container")
@ -278,7 +299,7 @@ func TestOpenldapBindIntegration(t *testing.T) {
require.NoError(t, container.Terminate(), "terminating container failed")
}()
port, err := strconv.Atoi(container.Port)
port, err := strconv.Atoi(container.Ports[servicePort])
require.NoError(t, err)
o := &Openldap{
@ -314,12 +335,15 @@ func TestOpenldapReverseMetricsIntegration(t *testing.T) {
container := testutil.Container{
Image: "bitnami/openldap",
ExposedPorts: []string{"1389"},
ExposedPorts: []string{servicePort},
Env: map[string]string{
"LDAP_ADMIN_USERNAME": "manager",
"LDAP_ADMIN_PASSWORD": "secret",
},
WaitingFor: wait.ForListeningPort("1389/tcp"),
WaitingFor: wait.ForAll(
wait.ForLog("Starting slapd"),
wait.ForListeningPort(nat.Port(servicePort)),
),
}
err := container.Start()
require.NoError(t, err, "failed to start container")
@ -327,7 +351,7 @@ func TestOpenldapReverseMetricsIntegration(t *testing.T) {
require.NoError(t, container.Terminate(), "terminating container failed")
}()
port, err := strconv.Atoi(container.Port)
port, err := strconv.Atoi(container.Ports[servicePort])
require.NoError(t, err)
o := &Openldap{

View File

@ -4,6 +4,7 @@ import (
"fmt"
"testing"
"github.com/docker/go-connections/nat"
"github.com/stretchr/testify/require"
"github.com/testcontainers/testcontainers-go/wait"
@ -16,9 +17,12 @@ func TestPgBouncerGeneratesMetricsIntegration(t *testing.T) {
t.Skip("Skipping integration test in short mode")
}
postgresServicePort := "5432"
pgBouncerServicePort := "6432"
backend := testutil.Container{
Image: "postgres:alpine",
ExposedPorts: []string{"5432"},
ExposedPorts: []string{postgresServicePort},
Env: map[string]string{
"POSTGRES_HOST_AUTH_METHOD": "trust",
},
@ -32,12 +36,12 @@ func TestPgBouncerGeneratesMetricsIntegration(t *testing.T) {
container := testutil.Container{
Image: "z9pascal/pgbouncer-container:1.17.0-latest",
ExposedPorts: []string{"6432"},
ExposedPorts: []string{pgBouncerServicePort},
Env: map[string]string{
"PG_ENV_POSTGRESQL_USER": "pgbouncer",
"PG_ENV_POSTGRESQL_PASS": "pgbouncer",
},
WaitingFor: wait.ForListeningPort("6432"),
WaitingFor: wait.ForListeningPort(nat.Port(pgBouncerServicePort)),
}
err = container.Start()
require.NoError(t, err, "failed to start container")
@ -50,7 +54,7 @@ func TestPgBouncerGeneratesMetricsIntegration(t *testing.T) {
Address: fmt.Sprintf(
"host=%s user=pgbouncer password=pgbouncer dbname=pgbouncer port=%s sslmode=disable",
container.Address,
container.Port,
container.Ports[pgBouncerServicePort],
),
IsPgBouncer: true,
},

View File

@ -7,6 +7,7 @@ import (
"testing"
"time"
"github.com/docker/go-connections/nat"
"github.com/go-redis/redis"
"github.com/stretchr/testify/require"
"github.com/testcontainers/testcontainers-go/wait"
@ -38,10 +39,11 @@ func TestRedisConnectIntegration(t *testing.T) {
t.Skip("Skipping integration test in short mode")
}
servicePort := "6379"
container := testutil.Container{
Image: "redis:alpine",
ExposedPorts: []string{"6379"},
WaitingFor: wait.ForListeningPort("6379/tcp"),
ExposedPorts: []string{servicePort},
WaitingFor: wait.ForListeningPort(nat.Port(servicePort)),
}
err := container.Start()
require.NoError(t, err, "failed to start container")
@ -49,7 +51,7 @@ func TestRedisConnectIntegration(t *testing.T) {
require.NoError(t, container.Terminate(), "terminating container failed")
}()
addr := fmt.Sprintf("%s:%s", container.Address, container.Port)
addr := fmt.Sprintf("%s:%s", container.Address, container.Ports[servicePort])
r := &Redis{
Log: testutil.Logger{},

View File

@ -8,6 +8,7 @@ import (
"testing"
"time"
"github.com/docker/go-connections/nat"
"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/testutil"
"github.com/testcontainers/testcontainers-go/wait"
@ -22,10 +23,11 @@ func TestRedisSentinelConnect(t *testing.T) {
t.Skip("Skipping integration test in short mode")
}
servicePort := "6379"
container := testutil.Container{
Image: "redis:alpine",
ExposedPorts: []string{"6379"},
WaitingFor: wait.ForListeningPort("6379/tcp"),
ExposedPorts: []string{servicePort},
WaitingFor: wait.ForListeningPort(nat.Port(servicePort)),
}
err := container.Start()
require.NoError(t, err, "failed to start container")
@ -33,7 +35,7 @@ func TestRedisSentinelConnect(t *testing.T) {
require.NoError(t, container.Terminate(), "terminating container failed")
}()
addr := fmt.Sprintf("tcp://%s:%s", container.Address, container.Port)
addr := fmt.Sprintf("tcp://%s:%s", container.Address, container.Ports[servicePort])
r := &RedisSentinel{
Servers: []string{addr},

View File

@ -4,6 +4,7 @@ import (
"fmt"
"testing"
"github.com/docker/go-connections/nat"
"github.com/stretchr/testify/require"
"github.com/testcontainers/testcontainers-go/wait"
@ -15,13 +16,14 @@ func TestZookeeperGeneratesMetricsIntegration(t *testing.T) {
t.Skip("Skipping integration test in short mode")
}
servicePort := "2181"
container := testutil.Container{
Image: "zookeeper",
ExposedPorts: []string{"2181"},
ExposedPorts: []string{servicePort},
Env: map[string]string{
"ZOO_4LW_COMMANDS_WHITELIST": "mntr",
},
WaitingFor: wait.ForListeningPort("2181"),
WaitingFor: wait.ForListeningPort(nat.Port(servicePort)),
}
err := container.Start()
require.NoError(t, err, "failed to start container")
@ -31,7 +33,7 @@ func TestZookeeperGeneratesMetricsIntegration(t *testing.T) {
z := &Zookeeper{
Servers: []string{
fmt.Sprintf("%s:%s", container.Address, container.Port),
fmt.Sprintf("%s:%s", container.Address, container.Ports[servicePort]),
},
}

View File

@ -7,6 +7,7 @@ import (
"testing"
"time"
"github.com/docker/go-connections/nat"
"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/config"
"github.com/influxdata/telegraf/metric"
@ -15,15 +16,20 @@ import (
"github.com/testcontainers/testcontainers-go/wait"
)
const servicePort = "5432"
func createTestContainer(t *testing.T) testutil.Container {
container := testutil.Container{
Image: "crate",
ExposedPorts: []string{"5432"},
ExposedPorts: []string{servicePort},
Entrypoint: []string{
"/docker-entrypoint.sh",
"-Cdiscovery.type=single-node",
},
WaitingFor: wait.ForListeningPort("5432/tcp"),
WaitingFor: wait.ForAll(
wait.ForListeningPort(nat.Port(servicePort)),
wait.ForLog("recovered [0] indices into cluster_state"),
),
}
err := container.Start()
require.NoError(t, err, "failed to start container")
@ -40,8 +46,9 @@ func TestConnectAndWriteIntegration(t *testing.T) {
defer func() {
require.NoError(t, container.Terminate(), "terminating container failed")
}()
url := fmt.Sprintf("postgres://crate@%s:%s/test", container.Address, container.Port)
url := fmt.Sprintf("postgres://crate@%s:%s/test", container.Address, container.Ports[servicePort])
fmt.Println(url)
table := "testing"
db, err := sql.Open("pgx", url)
require.NoError(t, err)
@ -151,7 +158,7 @@ func Test_escapeValueIntegration(t *testing.T) {
defer func() {
require.NoError(t, container.Terminate(), "terminating container failed")
}()
url := fmt.Sprintf("postgres://crate@%s:%s/test", container.Address, container.Port)
url := fmt.Sprintf("postgres://crate@%s:%s/test", container.Address, container.Ports[servicePort])
db, err := sql.Open("pgx", url)
require.NoError(t, err)

View File

@ -4,6 +4,7 @@ import (
"fmt"
"testing"
"github.com/docker/go-connections/nat"
"github.com/influxdata/telegraf/plugins/serializers"
"github.com/influxdata/telegraf/testutil"
"github.com/testcontainers/testcontainers-go/wait"
@ -16,10 +17,11 @@ func TestConnectAndWriteIntegration(t *testing.T) {
t.Skip("Skipping integration test in short mode")
}
servicePort := "1883"
container := testutil.Container{
Image: "ncarlier/mqtt",
ExposedPorts: []string{"1883"},
WaitingFor: wait.ForListeningPort("1883/tcp"),
ExposedPorts: []string{servicePort},
WaitingFor: wait.ForListeningPort(nat.Port(servicePort)),
}
err := container.Start()
require.NoError(t, err, "failed to start container")
@ -27,7 +29,7 @@ func TestConnectAndWriteIntegration(t *testing.T) {
require.NoError(t, container.Terminate(), "terminating container failed")
}()
var url = fmt.Sprintf("%s:%s", container.Address, container.Port)
var url = fmt.Sprintf("%s:%s", container.Address, container.Ports[servicePort])
s, _ := serializers.NewInfluxSerializer()
m := &MQTT{
Servers: []string{url},

View File

@ -15,9 +15,10 @@ func TestConnectAndWriteIntegration(t *testing.T) {
t.Skip("Skipping integration test in short mode")
}
servicePort := "4222"
container := testutil.Container{
Image: "nats",
ExposedPorts: []string{"4222"},
ExposedPorts: []string{servicePort},
WaitingFor: wait.ForLog("Server is ready"),
}
err := container.Start()
@ -26,7 +27,7 @@ func TestConnectAndWriteIntegration(t *testing.T) {
require.NoError(t, container.Terminate(), "terminating container failed")
}()
server := []string{fmt.Sprintf("nats://%s:%s", container.Address, container.Port)}
server := []string{fmt.Sprintf("nats://%s:%s", container.Address, container.Ports[servicePort])}
s, _ := serializers.NewInfluxSerializer()
n := &NATS{
Servers: server,

View File

@ -4,6 +4,7 @@ import (
"fmt"
"testing"
"github.com/docker/go-connections/nat"
"github.com/influxdata/telegraf/plugins/serializers"
"github.com/influxdata/telegraf/testutil"
"github.com/stretchr/testify/require"
@ -15,11 +16,12 @@ func TestConnectAndWriteIntegration(t *testing.T) {
t.Skip("Skipping integration test in short mode")
}
servicePort := "4150"
container := testutil.Container{
Image: "nsqio/nsq",
ExposedPorts: []string{"4150"},
ExposedPorts: []string{servicePort},
Entrypoint: []string{"/nsqd"},
WaitingFor: wait.ForListeningPort("4150/tcp"),
WaitingFor: wait.ForListeningPort(nat.Port(servicePort)),
}
err := container.Start()
require.NoError(t, err, "failed to start container")
@ -27,7 +29,7 @@ func TestConnectAndWriteIntegration(t *testing.T) {
require.NoError(t, container.Terminate(), "terminating container failed")
}()
server := []string{fmt.Sprintf("%s:%s", container.Address, container.Port)}
server := []string{fmt.Sprintf("%s:%s", container.Address, container.Ports[servicePort])}
s, _ := serializers.NewInfluxSerializer()
n := &NSQ{
Server: server[0],

View File

@ -6,6 +6,7 @@ package testutil
import (
"context"
"fmt"
"strings"
"github.com/docker/go-connections/nat"
"github.com/testcontainers/testcontainers-go"
@ -13,15 +14,17 @@ import (
)
type Container struct {
Image string
BindMounts map[string]string
Entrypoint []string
Env map[string]string
ExposedPorts []string
BindMounts map[string]string
Image string
Name string
Networks []string
WaitingFor wait.Strategy
Address string
Port string
Ports map[string]string
container testcontainers.Container
ctx context.Context
@ -32,11 +35,13 @@ func (c *Container) Start() error {
req := testcontainers.GenericContainerRequest{
ContainerRequest: testcontainers.ContainerRequest{
Image: c.Image,
Env: c.Env,
ExposedPorts: c.ExposedPorts,
BindMounts: c.BindMounts,
Entrypoint: c.Entrypoint,
Env: c.Env,
ExposedPorts: c.ExposedPorts,
Image: c.Image,
Name: c.Name,
Networks: c.Networks,
WaitingFor: c.WaitingFor,
},
Started: true,
@ -48,19 +53,44 @@ func (c *Container) Start() error {
}
c.container = container
c.Address, err = c.container.Host(c.ctx)
c.Address = "localhost"
err = c.LookupMappedPorts()
if err != nil {
return fmt.Errorf("container host address failed: %s", err)
_ = c.Terminate()
return fmt.Errorf("port lookup failed: %s", err)
}
// assume the first port is the one the test will connect to
// additional ports can be used for the waiting for section
if len(c.ExposedPorts) > 0 {
p, err := c.container.MappedPort(c.ctx, nat.Port(c.ExposedPorts[0]))
if err != nil {
return fmt.Errorf("container host port failed: %s", err)
return nil
}
// create a lookup table of exposed ports to mapped ports
func (c *Container) LookupMappedPorts() error {
if len(c.ExposedPorts) == 0 {
return nil
}
if len(c.Ports) == 0 {
c.Ports = make(map[string]string)
}
for _, port := range c.ExposedPorts {
// strip off leading host port: 80:8080 -> 8080
if strings.Contains(port, ":") {
port = strings.Split(port, ":")[1]
}
c.Port = p.Port()
// strip off the transport: 80/tcp -> 80
if strings.Contains(port, "/") {
port = strings.Split(port, "/")[0]
}
p, err := c.container.MappedPort(c.ctx, nat.Port(port))
if err != nil {
return fmt.Errorf("failed to find '%s' - %s", port, err)
}
fmt.Printf("mapped container port '%s' to host port '%s'\n", port, p.Port())
c.Ports[port] = p.Port()
}
return nil

View File

@ -22,6 +22,43 @@ func TestEmptyContainer(t *testing.T) {
require.NoError(t, err)
}
func TestMappedPortLookup(t *testing.T) {
if testing.Short() {
t.Skip("Skipping integration test in short mode")
}
cases := []struct {
name string
port string
expected string
}{
{"random", "80", "80"},
{"only 80", "80:80", "80"},
{"only 80", "80:80/tcp", "80"},
{"only 8080", "8080:80", "8080"},
{"only 8080", "8080:80/tcp", "8080"},
}
for _, tc := range cases {
container := Container{
Image: "nginx:stable-alpine",
ExposedPorts: []string{tc.port},
}
err := container.Start()
require.NoError(t, err)
if tc.name == "random" {
require.NotEqual(t, tc.expected, container.Ports["80"])
} else {
require.Equal(t, tc.expected, container.Ports["80"])
}
err = container.Terminate()
require.NoError(t, err)
}
}
func TestBadImageName(t *testing.T) {
if testing.Short() {
t.Skip("Skipping integration test in short mode")