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

chore: use testcontainers-go instead of containertest #16264

Merged
merged 11 commits into from
Nov 15, 2022
Merged
27 changes: 19 additions & 8 deletions extension/observer/dockerobserver/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,48 +7,61 @@ require (
github.com/docker/go-connections v0.4.1-0.20210727194412-58542c764a11
github.com/open-telemetry/opentelemetry-collector-contrib/extension/observer v0.64.0
github.com/open-telemetry/opentelemetry-collector-contrib/internal/common v0.64.0
github.com/open-telemetry/opentelemetry-collector-contrib/internal/containertest v0.64.0
github.com/open-telemetry/opentelemetry-collector-contrib/internal/docker v0.64.0
github.com/stretchr/testify v1.8.1
github.com/testcontainers/testcontainers-go v0.15.0
go.opentelemetry.io/collector v0.64.2-0.20221110222631-20e3aac00413
go.uber.org/zap v1.23.0
)

require (
github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1 // indirect
github.com/Microsoft/go-winio v0.5.1 // indirect
github.com/Microsoft/go-winio v0.5.2 // indirect
github.com/Microsoft/hcsshim v0.9.4 // indirect
github.com/cenkalti/backoff/v4 v4.1.3 // indirect
github.com/containerd/cgroups v1.0.4 // indirect
github.com/containerd/containerd v1.6.8 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/docker/distribution v2.8.1+incompatible // indirect
github.com/docker/go-units v0.4.0 // indirect
github.com/docker/go-units v0.5.0 // indirect
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/gobwas/glob v0.2.3 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/knadh/koanf v1.4.4 // indirect
github.com/kr/pretty v0.3.0 // indirect
github.com/magiconair/properties v1.8.6 // indirect
github.com/mitchellh/copystructure v1.2.0 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/mitchellh/reflectwalk v1.0.2 // indirect
github.com/moby/sys/mount v0.3.3 // indirect
github.com/moby/sys/mountinfo v0.6.2 // indirect
github.com/moby/term v0.0.0-20210619224110-3f7ff695adc6 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/morikuni/aec v1.0.0 // indirect
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/opencontainers/image-spec v1.0.2 // indirect
github.com/opencontainers/image-spec v1.0.3-0.20211202183452-c5a74bcca799 // indirect
github.com/opencontainers/runc v1.1.3 // indirect
github.com/pelletier/go-toml v1.9.4 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/sirupsen/logrus v1.8.1 // indirect
go.opencensus.io v0.24.0 // indirect
go.opentelemetry.io/collector/pdata v0.64.2-0.20221110222631-20e3aac00413 // indirect
go.opentelemetry.io/otel v1.11.1 // indirect
go.opentelemetry.io/otel/metric v0.33.0 // indirect
go.opentelemetry.io/otel/trace v1.11.1 // indirect
go.uber.org/atomic v1.10.0 // indirect
go.uber.org/multierr v1.8.0 // indirect
golang.org/x/net v0.0.0-20220225172249-27dd8689420f // indirect
golang.org/x/net v0.0.0-20220617184016-355a448f1bc9 // indirect
golang.org/x/sys v0.0.0-20220919091848-fb04ddd9f9c8 // indirect
golang.org/x/text v0.4.0 // indirect
google.golang.org/genproto v0.0.0-20211208223120-3a66f561d7aa // indirect
golang.org/x/time v0.0.0-20220210224613-90d013bbcef8 // indirect
google.golang.org/genproto v0.0.0-20220617124728-180714bec0ad // indirect
google.golang.org/grpc v1.50.1 // indirect
google.golang.org/protobuf v1.28.1 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
Expand All @@ -60,7 +73,5 @@ replace github.com/open-telemetry/opentelemetry-collector-contrib/internal/docke

replace github.com/open-telemetry/opentelemetry-collector-contrib/internal/common => ../../../internal/common

replace github.com/open-telemetry/opentelemetry-collector-contrib/internal/containertest => ../../../internal/containertest

// see https://github.com/distribution/distribution/issues/3590
exclude github.com/docker/distribution v2.8.0+incompatible
817 changes: 806 additions & 11 deletions extension/observer/dockerobserver/go.sum

Large diffs are not rendered by default.

100 changes: 85 additions & 15 deletions extension/observer/dockerobserver/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,12 @@ import (

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/testcontainers/testcontainers-go"
"github.com/testcontainers/testcontainers-go/wait"
"go.opentelemetry.io/collector/component"
"go.uber.org/zap"

"github.com/open-telemetry/opentelemetry-collector-contrib/extension/observer"
"github.com/open-telemetry/opentelemetry-collector-contrib/internal/containertest"
)

type testHost struct {
Expand All @@ -46,10 +47,27 @@ func (h *testHost) ReportFatalError(err error) {
var _ component.Host = (*testHost)(nil)

func TestObserverEmitsEndpointsIntegration(t *testing.T) {
c := containertest.New(t)
image := "docker.io/library/nginx"
tag := "1.17"
cntr := c.StartImage(fmt.Sprintf("%s:%s", image, tag), containertest.WithPortReady(80))

ctx := context.Background()
req := testcontainers.ContainerRequest{
Image: fmt.Sprintf("%s:%s", image, tag),
ExposedPorts: []string{"80/tcp"},
WaitingFor: wait.ForListeningPort("80/tcp"),
SkipReaper: true, // skipping the reaper to avoid creating two endpoints
}
container, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
ContainerRequest: req,
Started: true,
})
require.Nil(t, err)
defer func() {
err := container.Terminate(ctx)
require.Nil(t, err)
}()
require.NotNil(t, container)

config := NewFactory().CreateDefaultConfig().(*Config)
config.CacheSyncInterval = 1 * time.Second
config.UseHostBindings = true
Expand All @@ -62,17 +80,34 @@ func TestObserverEmitsEndpointsIntegration(t *testing.T) {
require.Equal(t, len(endpoints), 1)
for _, e := range endpoints {
require.Equal(t, uint16(80), e.Details.Env()["alternate_port"])
require.Equal(t, string(cntr.ID), e.Details.Env()["container_id"])
require.Equal(t, container.GetContainerID(), e.Details.Env()["container_id"])
require.Equal(t, image, e.Details.Env()["image"])
require.Equal(t, tag, e.Details.Env()["tag"])
}
}

func TestObserverUpdatesEndpointsIntegration(t *testing.T) {
c := containertest.New(t)
image := "docker.io/library/nginx"
tag := "1.17"
cntr := c.StartImage(fmt.Sprintf("%s:%s", image, tag), containertest.WithPortReady(80))

ctx := context.Background()
req := testcontainers.ContainerRequest{
Image: fmt.Sprintf("%s:%s", image, tag),
ExposedPorts: []string{"80/tcp"},
WaitingFor: wait.ForListeningPort("80/tcp"),
SkipReaper: true, // skipping the reaper to avoid creating two endpoints
}
container, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
ContainerRequest: req,
Started: true,
})
require.Nil(t, err)
defer func() {
err := container.Terminate(ctx)
require.Nil(t, err)
}()
require.NotNil(t, container)

mn := &mockNotifier{endpointsMap: map[observer.EndpointID]observer.Endpoint{}}
obvs := startObserver(t, mn)
defer stopObserver(t, obvs)
Expand All @@ -81,30 +116,47 @@ func TestObserverUpdatesEndpointsIntegration(t *testing.T) {
require.Equal(t, len(endpoints), 1)
for _, e := range endpoints {
require.Equal(t, uint16(80), e.Details.Env()["port"])
require.Equal(t, string(cntr.ID), e.Details.Env()["container_id"])
require.Equal(t, container.GetContainerID(), e.Details.Env()["container_id"])
require.Equal(t, image, e.Details.Env()["image"])
require.Equal(t, tag, e.Details.Env()["tag"])
}

c.RenameContainer(cntr, "nginx-updated")
tcDockerClient, _, _, err := testcontainers.NewDockerClient()
require.Nil(t, err)

tcDockerClient.ContainerRename(context.Background(), container.GetContainerID(), "nginx-updated")

require.Eventually(t, func() bool { return mn.ChangeCount() == 1 }, 3*time.Second, 10*time.Millisecond)
require.Equal(t, 1, mn.AddCount())

endpoints = mn.EndpointsMap()
for _, e := range endpoints {
require.Equal(t, "nginx-updated", e.Details.Env()["name"])
require.Equal(t, uint16(80), e.Details.Env()["port"])
require.Equal(t, string(cntr.ID), e.Details.Env()["container_id"])
require.Equal(t, container.GetContainerID(), e.Details.Env()["container_id"])
require.Equal(t, image, e.Details.Env()["image"])
require.Equal(t, tag, e.Details.Env()["tag"])
}
}

func TestObserverRemovesEndpointsIntegration(t *testing.T) {
c := containertest.New(t)
image := "docker.io/library/nginx"
tag := "1.17"
tmpCntr := c.StartImage(fmt.Sprintf("%s:%s", image, tag), containertest.WithPortReady(80))

ctx := context.Background()
req := testcontainers.ContainerRequest{
Image: fmt.Sprintf("%s:%s", image, tag),
ExposedPorts: []string{"80/tcp"},
WaitingFor: wait.ForListeningPort("80/tcp"),
SkipReaper: true, // skipping the reaper to avoid creating two endpoints
}
container, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
ContainerRequest: req,
Started: true,
})
require.Nil(t, err)
require.NotNil(t, container)

mn := &mockNotifier{endpointsMap: map[observer.EndpointID]observer.Endpoint{}}
obvs := startObserver(t, mn)
defer stopObserver(t, obvs)
Expand All @@ -113,18 +165,36 @@ func TestObserverRemovesEndpointsIntegration(t *testing.T) {
require.Equal(t, len(endpoints), 1)
for _, e := range endpoints {
require.Equal(t, uint16(80), e.Details.Env()["port"])
require.Equal(t, string(tmpCntr.ID), e.Details.Env()["container_id"])
require.Equal(t, container.GetContainerID(), e.Details.Env()["container_id"])
require.Equal(t, image, e.Details.Env()["image"])
require.Equal(t, tag, e.Details.Env()["tag"])
}
c.RemoveContainer(tmpCntr)

err = container.Terminate(ctx)
require.Nil(t, err)

require.Eventually(t, func() bool { return mn.RemoveCount() == 1 }, 3*time.Second, 10*time.Millisecond)
require.Empty(t, mn.EndpointsMap())
}

func TestObserverExcludesImagesIntegration(t *testing.T) {
c := containertest.New(t)
c.StartImage("docker.io/library/nginx:1.17", containertest.WithPortReady(80))
ctx := context.Background()
req := testcontainers.ContainerRequest{
Image: "docker.io/library/nginx:1.17",
ExposedPorts: []string{"80/tcp"},
WaitingFor: wait.ForListeningPort("80/tcp"),
SkipReaper: true,
}
container, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
ContainerRequest: req,
Started: true,
})
require.Nil(t, err)
defer func() {
err := container.Terminate(ctx)
require.Nil(t, err)
}()
require.NotNil(t, container)

config := NewFactory().CreateDefaultConfig().(*Config)
config.ExcludedImages = []string{"*nginx*"}
Expand Down
4 changes: 0 additions & 4 deletions internal/containertest/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,10 +178,6 @@ func (c *Containers) waitForPorts(con Container) {

}

func (c *Containers) RenameContainer(container Container, name string) error {
return c.cli.ContainerRename(context.Background(), string(container.ID), name)
}

// StartImage starts a container with the given image and zero or more ContainerOptions.
func (c *Containers) StartImage(image string, opts ...Option) Container {
return c.StartImageWithEnv(image, nil, opts...)
Expand Down
28 changes: 17 additions & 11 deletions receiver/aerospikereceiver/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ go 1.18

require (
github.com/aerospike/aerospike-client-go/v6 v6.4.0
github.com/open-telemetry/opentelemetry-collector-contrib/internal/containertest v0.64.0
github.com/open-telemetry/opentelemetry-collector-contrib/internal/scrapertest v0.64.0
github.com/stretchr/testify v1.8.1
github.com/testcontainers/testcontainers-go v0.15.0
go.opentelemetry.io/collector v0.64.2-0.20221110222631-20e3aac00413
go.opentelemetry.io/collector/pdata v0.64.2-0.20221110222631-20e3aac00413
go.uber.org/multierr v1.8.0
Expand All @@ -15,29 +15,37 @@ require (

require (
github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1 // indirect
github.com/Microsoft/go-winio v0.5.1 // indirect
github.com/Microsoft/go-winio v0.5.2 // indirect
github.com/Microsoft/hcsshim v0.9.4 // indirect
github.com/cenkalti/backoff/v4 v4.1.3 // indirect
github.com/containerd/cgroups v1.0.4 // indirect
github.com/containerd/containerd v1.6.8 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/docker/distribution v2.8.1+incompatible // indirect
github.com/docker/docker v20.10.21+incompatible // indirect
github.com/docker/go-connections v0.4.0 // indirect
github.com/docker/go-units v0.4.0 // indirect
github.com/docker/go-units v0.5.0 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/knadh/koanf v1.4.4 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/magiconair/properties v1.8.6 // indirect
github.com/mitchellh/copystructure v1.2.0 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/mitchellh/reflectwalk v1.0.2 // indirect
github.com/moby/sys/mount v0.3.3 // indirect
github.com/moby/sys/mountinfo v0.6.2 // indirect
github.com/moby/term v0.0.0-20210619224110-3f7ff695adc6 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/morikuni/aec v1.0.0 // indirect
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/opencontainers/image-spec v1.0.2 // indirect
github.com/pelletier/go-toml v1.8.1 // indirect
github.com/opencontainers/image-spec v1.0.3-0.20211202183452-c5a74bcca799 // indirect
github.com/opencontainers/runc v1.1.3 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/rogpeppe/go-internal v1.8.1 // indirect
github.com/sirupsen/logrus v1.8.1 // indirect
github.com/stretchr/objx v0.5.0 // indirect
github.com/yuin/gopher-lua v0.0.0-20220504180219-658193537a64 // indirect
Expand All @@ -46,16 +54,14 @@ require (
go.opentelemetry.io/otel/metric v0.33.0 // indirect
go.opentelemetry.io/otel/trace v1.11.1 // indirect
go.uber.org/atomic v1.10.0 // indirect
golang.org/x/net v0.0.0-20220225172249-27dd8689420f // indirect
golang.org/x/net v0.0.0-20220617184016-355a448f1bc9 // indirect
golang.org/x/sync v0.0.0-20220907140024-f12130a52804 // indirect
golang.org/x/sys v0.0.0-20220919091848-fb04ddd9f9c8 // indirect
golang.org/x/text v0.4.0 // indirect
google.golang.org/genproto v0.0.0-20211208223120-3a66f561d7aa // indirect
google.golang.org/genproto v0.0.0-20220617124728-180714bec0ad // indirect
google.golang.org/grpc v1.50.1 // indirect
google.golang.org/protobuf v1.28.1 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

replace github.com/open-telemetry/opentelemetry-collector-contrib/internal/scrapertest => ../../internal/scrapertest

replace github.com/open-telemetry/opentelemetry-collector-contrib/internal/containertest => ../../internal/containertest
Loading