Skip to content

Commit

Permalink
test: add generic test-container code for re-use, migrate mysql (#11157)
Browse files Browse the repository at this point in the history
  • Loading branch information
powersj authored and MyaLongmire committed Jul 6, 2022
1 parent a0da3e3 commit f321c7f
Show file tree
Hide file tree
Showing 6 changed files with 154 additions and 14 deletions.
9 changes: 0 additions & 9 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,6 @@ services:
- xpack.security.enabled=false
ports:
- "9200:9200"
mysql:
image: mysql
environment:
- MYSQL_ALLOW_EMPTY_PASSWORD=yes
ports:
- "3306:3306"
# removes warning "mbind operation not permitted" enables you to see the docker logs
cap_add:
- SYS_NICE # CAP_SYS_NICE
memcached:
image: memcached
ports:
Expand Down
9 changes: 9 additions & 0 deletions docs/LICENSE_OF_DEPENDENCIES.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@ following works:
- github.com/Azure/azure-storage-blob-go [MIT License](https://github.com/Azure/azure-storage-blob-go/blob/master/LICENSE)
- github.com/Azure/azure-storage-queue-go [MIT License](https://github.com/Azure/azure-storage-queue-go/blob/master/LICENSE)
- github.com/Azure/go-amqp [MIT License](https://github.com/Azure/go-amqp/blob/master/LICENSE)
- github.com/Azure/go-ansiterm [MIT License](https://github.com/Azure/go-ansiterm/blob/master/LICENSE)
- github.com/Azure/go-autorest [Apache License 2.0](https://github.com/Azure/go-autorest/blob/master/LICENSE)
- github.com/Azure/go-ntlmssp [MIT License](https://github.com/Azure/go-ntlmssp/blob/master/LICENSE)
- github.com/ClickHouse/clickhouse-go [MIT License](https://github.com/ClickHouse/clickhouse-go/blob/master/LICENSE)
- github.com/Mellanox/rdmamap [Apache License 2.0](https://github.com/Mellanox/rdmamap/blob/master/LICENSE)
- github.com/Microsoft/go-winio [MIT License](https://github.com/Microsoft/go-winio/blob/master/LICENSE)
- github.com/Microsoft/hcsshim [MIT License](https://github.com/microsoft/hcsshim/blob/master/LICENSE)
- github.com/Shopify/sarama [MIT License](https://github.com/Shopify/sarama/blob/master/LICENSE)
- github.com/aerospike/aerospike-client-go [Apache License 2.0](https://github.com/aerospike/aerospike-client-go/blob/master/LICENSE)
- github.com/alecthomas/participle [MIT License](https://github.com/alecthomas/participle/blob/master/COPYING)
Expand Down Expand Up @@ -175,6 +177,7 @@ following works:
- github.com/klauspost/compress [BSD 3-Clause Clear License](https://github.com/klauspost/compress/blob/master/LICENSE)
- github.com/kylelemons/godebug [Apache License 2.0](https://github.com/kylelemons/godebug/blob/master/LICENSE)
- github.com/leodido/ragel-machinery [MIT License](https://github.com/leodido/ragel-machinery/blob/develop/LICENSE)
- github.com/magiconair/properties [BSD 2-Clause "Simplified" License](https://github.com/magiconair/properties/blob/main/LICENSE.md)
- github.com/mailru/easyjson [MIT License](https://github.com/mailru/easyjson/blob/master/LICENSE)
- github.com/mattn/go-colorable [MIT License](https://github.com/mattn/go-colorable/blob/master/LICENSE)
- github.com/mattn/go-ieproxy [MIT License](https://github.com/mattn/go-ieproxy/blob/master/LICENSE)
Expand All @@ -190,8 +193,12 @@ following works:
- github.com/mitchellh/go-homedir [MIT License](https://github.com/mitchellh/go-homedir/blob/master/LICENSE)
- github.com/mitchellh/mapstructure [MIT License](https://github.com/mitchellh/mapstructure/blob/master/LICENSE)
- github.com/moby/ipvs [Apache License 2.0](https://github.com/moby/ipvs/blob/master/LICENSE)
- github.com/moby/sys/mount [Apache License 2.0](https://github.com/moby/sys/blob/main/LICENSE)
- github.com/moby/sys/mountinfo [Apache License 2.0](https://github.com/moby/sys/blob/main/LICENSE)
- github.com/moby/term [Apache License 2.0](https://github.com/moby/term/blob/master/LICENSE)
- github.com/modern-go/concurrent [Apache License 2.0](https://github.com/modern-go/concurrent/blob/master/LICENSE)
- github.com/modern-go/reflect2 [Apache License 2.0](https://github.com/modern-go/reflect2/blob/master/LICENSE)
- github.com/morikuni/aec [MIT License](https://github.com/morikuni/aec/blob/master/LICENSE)
- github.com/multiplay/go-ts3 [BSD 2-Clause "Simplified" License](https://github.com/multiplay/go-ts3/blob/master/LICENSE)
- github.com/naoina/go-stringutil [MIT License](https://github.com/naoina/go-stringutil/blob/master/LICENSE)
- github.com/nats-io/jwt [Apache License 2.0](https://github.com/nats-io/jwt/blob/master/LICENSE)
Expand All @@ -205,6 +212,7 @@ following works:
- github.com/openconfig/gnmi [Apache License 2.0](https://github.com/openconfig/gnmi/blob/master/LICENSE)
- github.com/opencontainers/go-digest [Apache License 2.0](https://github.com/opencontainers/go-digest/blob/master/LICENSE)
- github.com/opencontainers/image-spec [Apache License 2.0](https://github.com/opencontainers/image-spec/blob/master/LICENSE)
- github.com/opencontainers/runc [Apache License 2.0](https://github.com/opencontainers/runc/blob/main/LICENSE)
- github.com/opentracing/opentracing-go [Apache License 2.0](https://github.com/opentracing/opentracing-go/blob/master/LICENSE)
- github.com/pborman/ansi [BSD 3-Clause "New" or "Revised" License](https://github.com/pborman/ansi/blob/master/LICENSE)
- github.com/philhofer/fwd [MIT License](https://github.com/philhofer/fwd/blob/master/LICENSE.md)
Expand Down Expand Up @@ -238,6 +246,7 @@ following works:
- github.com/streadway/amqp [BSD 2-Clause "Simplified" License](https://github.com/streadway/amqp/blob/master/LICENSE)
- github.com/stretchr/objx [MIT License](https://github.com/stretchr/objx/blob/master/LICENSE)
- github.com/stretchr/testify [MIT License](https://github.com/stretchr/testify/blob/master/LICENSE)
- github.com/testcontainers/testcontainers-go [MIT License](https://github.com/testcontainers/testcontainers-go/blob/main/LICENSE)
- github.com/tidwall/gjson [MIT License](https://github.com/tidwall/gjson/blob/master/LICENSE)
- github.com/tidwall/match [MIT License](https://github.com/tidwall/match/blob/master/LICENSE)
- github.com/tidwall/pretty [MIT License](https://github.com/tidwall/pretty/blob/master/LICENSE)
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/devigned/tab v0.1.1 // indirect
github.com/docker/distribution v2.7.1+incompatible // indirect
github.com/docker/go-connections v0.4.0 // indirect
github.com/docker/go-connections v0.4.0
github.com/docker/go-units v0.4.0 // indirect
github.com/eapache/go-resiliency v1.2.0 // indirect
github.com/eapache/go-xerial-snappy v0.0.0-20180814174437-776d5712da21 // indirect
Expand Down
40 changes: 36 additions & 4 deletions plugins/inputs/mysql/mysql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/DATA-DOG/go-sqlmock"
"github.com/stretchr/testify/require"
"github.com/testcontainers/testcontainers-go/wait"

"github.com/influxdata/telegraf/testutil"
)
Expand All @@ -15,12 +16,27 @@ func TestMysqlDefaultsToLocalIntegration(t *testing.T) {
t.Skip("Skipping integration test in short mode")
}

container := testutil.Container{
Image: "mysql",
Env: map[string]string{
"MYSQL_ALLOW_EMPTY_PASSWORD": "yes",
},
ExposedPorts: []string{"3306"},
WaitingFor: wait.ForListeningPort("3306"),
}

err := container.Start()
require.NoError(t, err, "failed to start container")
defer func() {
require.NoError(t, container.Terminate(), "terminating container failed")
}()

m := &Mysql{
Servers: []string{fmt.Sprintf("root@tcp(%s:3306)/", testutil.GetLocalHost())},
Servers: []string{fmt.Sprintf("root@tcp(%s:%s)/", container.Address, container.Port)},
}

var acc testutil.Accumulator
err := m.Gather(&acc)
err = m.Gather(&acc)
require.NoError(t, err)
require.Empty(t, acc.Errors)

Expand All @@ -33,7 +49,23 @@ func TestMysqlMultipleInstancesIntegration(t *testing.T) {
if testing.Short() {
t.Skip("Skipping integration test in short mode")
}
testServer := "root@tcp(127.0.0.1:3306)/?tls=false"

container := testutil.Container{
Image: "mysql",
Env: map[string]string{
"MYSQL_ALLOW_EMPTY_PASSWORD": "yes",
},
ExposedPorts: []string{"3306"},
WaitingFor: wait.ForListeningPort("3306/tcp"),
}

err := container.Start()
require.NoError(t, err, "failed to start container")
defer func() {
require.NoError(t, container.Terminate(), "terminating container failed")
}()

testServer := fmt.Sprintf("root@tcp(%s:%s)/?tls=false", container.Address, container.Port)
m := &Mysql{
Servers: []string{testServer},
IntervalSlow: "30s",
Expand All @@ -42,7 +74,7 @@ func TestMysqlMultipleInstancesIntegration(t *testing.T) {
}

var acc, acc2 testutil.Accumulator
err := m.Gather(&acc)
err = m.Gather(&acc)
require.NoError(t, err)
require.Empty(t, acc.Errors)
require.True(t, acc.HasMeasurement("mysql"))
Expand Down
72 changes: 72 additions & 0 deletions testutil/container.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
//go:build !freebsd
// +build !freebsd

package testutil

import (
"context"
"fmt"

"github.com/docker/go-connections/nat"
"github.com/testcontainers/testcontainers-go"
"github.com/testcontainers/testcontainers-go/wait"
)

type Container struct {
Image string
Env map[string]string
ExposedPorts []string
WaitingFor wait.Strategy

Address string
Port string

container testcontainers.Container
ctx context.Context
}

func (c *Container) Start() error {
c.ctx = context.Background()

req := testcontainers.GenericContainerRequest{
ContainerRequest: testcontainers.ContainerRequest{
Image: c.Image,
Env: c.Env,
ExposedPorts: c.ExposedPorts,
WaitingFor: c.WaitingFor,
},
Started: true,
}

container, err := testcontainers.GenericContainer(c.ctx, req)
if err != nil {
return fmt.Errorf("container failed to start: %s", err)
}
c.container = container

c.Address, err = c.container.Host(c.ctx)
if err != nil {
return fmt.Errorf("container host address 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)
}
c.Port = p.Port()
}

return nil
}

func (c *Container) Terminate() error {
err := c.container.Terminate(c.ctx)
if err != nil {
return fmt.Errorf("failed to terminate the container: %s", err)
}

return nil
}
36 changes: 36 additions & 0 deletions testutil/container_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package testutil

import (
"testing"

"github.com/stretchr/testify/require"
)

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

container := Container{
Image: "docksal/empty",
}

err := container.Start()
require.NoError(t, err)

err = container.Terminate()
require.NoError(t, err)
}

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

container := Container{
Image: "fAk3-n4mE",
}

err := container.Start()
require.Error(t, err)
}

0 comments on commit f321c7f

Please sign in to comment.