Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: refactor testcontainer port lookup #11198

Merged
merged 3 commits into from
May 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 13 additions & 11 deletions plugins/inputs/aerospike/aerospike_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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
Expand Down Expand Up @@ -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",
},
}
Expand Down Expand Up @@ -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"},
}

Expand Down Expand Up @@ -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,
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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"},
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down
8 changes: 5 additions & 3 deletions plugins/inputs/memcached/memcached_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"strings"
"testing"

"github.com/docker/go-connections/nat"
"github.com/stretchr/testify/require"
"github.com/testcontainers/testcontainers-go/wait"

Expand All @@ -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")
Expand All @@ -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
Expand Down
15 changes: 9 additions & 6 deletions plugins/inputs/mysql/mysql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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()
Expand All @@ -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
Expand All @@ -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()
Expand All @@ -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",
Expand Down
15 changes: 9 additions & 6 deletions plugins/inputs/opcua/opcua_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"testing"
"time"

"github.com/docker/go-connections/nat"
"github.com/stretchr/testify/require"
"github.com/testcontainers/testcontainers-go/wait"

Expand All @@ -22,15 +23,17 @@ type OPCTags struct {
Want interface{}
}

const servicePort = "4840"

func TestGetDataBadNodeContainerIntegration(t *testing.T) {
if testing.Short() {
t.Skip("Skipping integration test in short mode")
}

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")
Expand All @@ -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)
Expand Down Expand Up @@ -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")
Expand All @@ -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)
Expand Down
Loading