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: update Docker labels for containers #813

Merged
merged 1 commit into from
Feb 9, 2023
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
5 changes: 4 additions & 1 deletion docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import (
specs "github.com/opencontainers/image-spec/specs-go/v1"

tcexec "github.com/testcontainers/testcontainers-go/exec"
"github.com/testcontainers/testcontainers-go/internal"
"github.com/testcontainers/testcontainers-go/internal/testcontainersdocker"
"github.com/testcontainers/testcontainers-go/internal/testcontainerssession"
"github.com/testcontainers/testcontainers-go/wait"
Expand Down Expand Up @@ -1464,7 +1465,9 @@ func (p *DockerProvider) getDefaultNetwork(ctx context.Context, cli client.APICl
Driver: Bridge,
Attachable: true,
Labels: map[string]string{
TestcontainerLabel: "true",
TestcontainerLabel: "true",
testcontainersdocker.LabelLang: "go",
testcontainersdocker.LabelVersion: internal.Version,
},
})

Expand Down
5 changes: 3 additions & 2 deletions docker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"github.com/docker/docker/api/types/filters"
"github.com/docker/docker/client"

"github.com/testcontainers/testcontainers-go/internal/testcontainersdocker"
"github.com/testcontainers/testcontainers-go/wait"
)

Expand Down Expand Up @@ -378,7 +379,7 @@ func TestContainerStartsWithoutTheReaper(t *testing.T) {
terminateContainerOnEnd(t, ctx, container)

resp, err := client.ContainerList(ctx, types.ContainerListOptions{
Filters: filters.NewArgs(filters.Arg("label", fmt.Sprintf("%s=%s", TestcontainerLabelSessionID, container.SessionID()))),
Filters: filters.NewArgs(filters.Arg("label", fmt.Sprintf("%s=%s", testcontainersdocker.LabelSessionID, container.SessionID()))),
mdelapenya marked this conversation as resolved.
Show resolved Hide resolved
})
if err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -408,7 +409,7 @@ func TestContainerStartsWithTheReaper(t *testing.T) {
if err != nil {
t.Fatal(err)
}
filtersJSON := fmt.Sprintf(`{"label":{"%s":true}}`, TestcontainerLabelIsReaper)
filtersJSON := fmt.Sprintf(`{"label":{"%s":true}}`, testcontainersdocker.LabelReaper)
f, err := filters.FromJSON(filtersJSON)
if err != nil {
t.Fatal(err)
Expand Down
9 changes: 9 additions & 0 deletions internal/testcontainersdocker/labels.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package testcontainersdocker

const (
LabelBase = "org.testcontainers"
LabelLang = LabelBase + ".lang"
LabelReaper = LabelBase + ".reaper"
LabelSessionID = LabelBase + ".sessionId"
LabelVersion = LabelBase + ".version"
)
3 changes: 3 additions & 0 deletions internal/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package internal

const Version = "0.18.0"
20 changes: 14 additions & 6 deletions reaper.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,18 @@ import (

"github.com/docker/go-connections/nat"

"github.com/testcontainers/testcontainers-go/internal"
"github.com/testcontainers/testcontainers-go/internal/testcontainersdocker"
"github.com/testcontainers/testcontainers-go/wait"
)

const (
TestcontainerLabel = "org.testcontainers.golang"
// Deprecated: it has been replaced by the internal testcontainersdocker.LabelLang
TestcontainerLabel = "org.testcontainers.golang"
// Deprecated: it has been replaced by the internal testcontainersdocker.LabelSessionID
TestcontainerLabelSessionID = TestcontainerLabel + ".sessionId"
TestcontainerLabelIsReaper = TestcontainerLabel + ".reaper"
// Deprecated: it has been replaced by the internal testcontainersdocker.LabelReaper
TestcontainerLabelIsReaper = TestcontainerLabel + ".reaper"

ReaperDefaultImage = "docker.io/testcontainers/ryuk:0.3.4"
)
Expand Down Expand Up @@ -88,7 +92,8 @@ func newReaper(ctx context.Context, sessionID string, provider ReaperProvider, o
ExposedPorts: []string{string(listeningPort)},
NetworkMode: Bridge,
Labels: map[string]string{
TestcontainerLabelIsReaper: "true",
TestcontainerLabelIsReaper: "true",
testcontainersdocker.LabelReaper: "true",
},
SkipReaper: true,
RegistryCred: reaperOpts.RegistryCredentials,
Expand All @@ -103,7 +108,7 @@ func newReaper(ctx context.Context, sessionID string, provider ReaperProvider, o

// include reaper-specific labels to the reaper container
for k, v := range reaper.Labels() {
if k == TestcontainerLabelSessionID {
if k == TestcontainerLabelSessionID || k == testcontainersdocker.LabelSessionID {
continue
}
req.Labels[k] = v
Expand Down Expand Up @@ -191,8 +196,11 @@ func (r *Reaper) Connect() (chan bool, error) {
// Labels returns the container labels to use so that this Reaper cleans them up
func (r *Reaper) Labels() map[string]string {
return map[string]string{
TestcontainerLabel: "true",
TestcontainerLabelSessionID: r.SessionID,
TestcontainerLabel: "true",
TestcontainerLabelSessionID: r.SessionID,
testcontainersdocker.LabelLang: "go",
testcontainersdocker.LabelVersion: internal.Version,
testcontainersdocker.LabelSessionID: r.SessionID,
}
}

Expand Down
8 changes: 6 additions & 2 deletions reaper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/docker/go-connections/nat"
"github.com/stretchr/testify/assert"
"github.com/testcontainers/testcontainers-go/internal"
"github.com/testcontainers/testcontainers-go/internal/testcontainersdocker"
"github.com/testcontainers/testcontainers-go/wait"
)
Expand Down Expand Up @@ -37,8 +38,11 @@ func createContainerRequest(customize func(ContainerRequest) ContainerRequest) C
ReaperImage: "reaperImage",
ExposedPorts: []string{"8080/tcp"},
Labels: map[string]string{
TestcontainerLabel: "true",
TestcontainerLabelIsReaper: "true",
TestcontainerLabel: "true",
TestcontainerLabelIsReaper: "true",
testcontainersdocker.LabelReaper: "true",
testcontainersdocker.LabelLang: "go",
testcontainersdocker.LabelVersion: internal.Version,
},
SkipReaper: true,
Mounts: Mounts(BindMount("/var/run/docker.sock", "/var/run/docker.sock")),
Expand Down