test: add generic test-container code for re-use, migrate mysql (#11157)
This commit is contained in:
parent
61126dcbba
commit
db3beab38b
|
|
@ -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:
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
@ -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)
|
||||
|
|
@ -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)
|
||||
|
|
@ -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)
|
||||
|
|
@ -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)
|
||||
|
|
|
|||
2
go.mod
2
go.mod
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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"
|
||||
)
|
||||
|
|
@ -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)
|
||||
|
||||
|
|
@ -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",
|
||||
|
|
@ -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"))
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
|
|
@ -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)
|
||||
}
|
||||
Loading…
Reference in New Issue