From 07bc76e1acc3d789c8fcae0f8acf0c5c62cf4771 Mon Sep 17 00:00:00 2001 From: Steven Hartland Date: Sat, 20 Jul 2024 11:53:07 +0100 Subject: [PATCH 1/2] fix(inbucket): expose POP3 and wait for all ports Expose the POP3 port and wait for all ports which fixes the race condition in the tests. --- modules/inbucket/go.mod | 5 ++++ modules/inbucket/go.sum | 9 ++++++ modules/inbucket/inbucket.go | 8 ++++-- modules/inbucket/inbucket_test.go | 46 +++++++++---------------------- 4 files changed, 33 insertions(+), 35 deletions(-) diff --git a/modules/inbucket/go.mod b/modules/inbucket/go.mod index f6f7ba6f96..89851c357e 100644 --- a/modules/inbucket/go.mod +++ b/modules/inbucket/go.mod @@ -4,6 +4,7 @@ go 1.21 require ( github.com/inbucket/inbucket v2.0.0+incompatible + github.com/stretchr/testify v1.9.0 github.com/testcontainers/testcontainers-go v0.32.0 ) @@ -16,6 +17,7 @@ require ( github.com/containerd/log v0.1.0 // indirect github.com/containerd/platforms v0.2.1 // indirect github.com/cpuguy83/dockercfg v0.3.1 // indirect + github.com/davecgh/go-spew v1.1.1 // indirect github.com/distribution/reference v0.6.0 // indirect github.com/docker/docker v27.1.0+incompatible // indirect github.com/docker/go-connections v0.5.0 // indirect @@ -28,6 +30,7 @@ require ( github.com/google/uuid v1.6.0 // indirect github.com/jhillyerd/inbucket v2.0.0+incompatible // indirect github.com/klauspost/compress v1.17.4 // indirect + github.com/kr/text v0.2.0 // indirect github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 // indirect github.com/magiconair/properties v1.8.7 // indirect github.com/moby/docker-image-spec v1.3.1 // indirect @@ -39,6 +42,7 @@ require ( github.com/opencontainers/go-digest v1.0.0 // indirect github.com/opencontainers/image-spec v1.1.0 // indirect github.com/pkg/errors v0.9.1 // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c // indirect github.com/shirou/gopsutil/v3 v3.23.12 // indirect github.com/shoenig/go-m1cpu v0.1.6 // indirect @@ -55,6 +59,7 @@ require ( golang.org/x/sys v0.21.0 // indirect google.golang.org/genproto/googleapis/api v0.0.0-20240318140521-94a12d6c2237 // indirect google.golang.org/genproto/googleapis/rpc v0.0.0-20240318140521-94a12d6c2237 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect ) replace github.com/testcontainers/testcontainers-go => ../.. diff --git a/modules/inbucket/go.sum b/modules/inbucket/go.sum index 0f998df7df..8236abef2f 100644 --- a/modules/inbucket/go.sum +++ b/modules/inbucket/go.sum @@ -16,6 +16,7 @@ github.com/containerd/platforms v0.2.1 h1:zvwtM3rz2YHPQsF2CHYM8+KtB5dvhISiXh5ZpS github.com/containerd/platforms v0.2.1/go.mod h1:XHCb+2/hzowdiut9rkudds9bE5yJ7npe7dG/wG+uFPw= github.com/cpuguy83/dockercfg v0.3.1 h1:/FpZ+JaygUR/lZP2NlFI2DVfrOEMAIKP5wWEJdoYe9E= github.com/cpuguy83/dockercfg v0.3.1/go.mod h1:sugsbF4//dDlL/i+S+rtpIWp+5h0BHJHfjj5/jFyUJc= +github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= github.com/creack/pty v1.1.18 h1:n56/Zwd5o6whRC5PMGretI4IdRLlmBXYNjScPaBgsbY= github.com/creack/pty v1.1.18/go.mod h1:MOBLtS5ELjhRRrroQr9kyvTxUAFNvYEK993ew/Vr4O4= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= @@ -56,6 +57,10 @@ github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck= github.com/klauspost/compress v1.17.4 h1:Ej5ixsIri7BrIjBkRZLTo6ghwrEtHFk7ijlczPW4fZ4= github.com/klauspost/compress v1.17.4/go.mod h1:/dCuZOvVtNoHsyb+cuJD3itjs3NbnF6KH9zAO4BDxPM= +github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0= +github.com/kr/pretty v0.3.0/go.mod h1:640gp4NfQd8pI5XOwp5fnNeVWj67G7CFk/SaSQn7NBk= +github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= +github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 h1:6E+4a0GO5zZEnZ81pIr0yLvtUWk2if982qA3F3QD6H4= github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0/go.mod h1:zJYVVT2jmtg6P3p1VtQj7WsuWi/y4VnjVBn7F8KPB3I= github.com/magiconair/properties v1.8.7 h1:IeQXZAiQcpL9mgcAe1Nu6cX9LLw6ExEHKjN0VQdvPDY= @@ -82,6 +87,8 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c h1:ncq/mPwQF4JjgDlrVEn3C11VoGHZN7m8qihwgMEtzYw= github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c/go.mod h1:OmDBASR4679mdNQnz2pUhc2G8CO2JrUAVFDRBDP/hJE= +github.com/rogpeppe/go-internal v1.8.1 h1:geMPLpDpQOgVyCg5z5GoRwLHepNdb71NXb67XFkP+Eg= +github.com/rogpeppe/go-internal v1.8.1/go.mod h1:JeRgkft04UBgHMgCIwADu4Pn6Mtm5d4nPKWu0nJ5d+o= github.com/shirou/gopsutil/v3 v3.23.12 h1:z90NtUkp3bMtmICZKpC4+WaknU1eXtp5vtbQ11DgpE4= github.com/shirou/gopsutil/v3 v3.23.12/go.mod h1:1FrWgea594Jp7qmjHUUPlJDTPgcsb9mGnXDxavtikzM= github.com/shoenig/go-m1cpu v0.1.6 h1:nxdKQNcEB6vzgA2E2bvzKIYRuNj7XNJ4S/aRSwKzFtM= @@ -176,6 +183,8 @@ google.golang.org/grpc v1.64.1/go.mod h1:hiQF4LFZelK2WKaP6W0L92zGHtiQdZxk8CrSdvy google.golang.org/protobuf v1.33.0 h1:uNO2rsAINq/JlFpSdYEKIZ0uKD/R9cpdv0T+yoGwGmI= google.golang.org/protobuf v1.33.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= +gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/modules/inbucket/inbucket.go b/modules/inbucket/inbucket.go index 214e1c619c..579e0aa661 100644 --- a/modules/inbucket/inbucket.go +++ b/modules/inbucket/inbucket.go @@ -57,8 +57,12 @@ func RunContainer(ctx context.Context, opts ...testcontainers.ContainerCustomize func Run(ctx context.Context, img string, opts ...testcontainers.ContainerCustomizer) (*InbucketContainer, error) { req := testcontainers.ContainerRequest{ Image: img, - ExposedPorts: []string{"2500/tcp", "9000/tcp"}, - WaitingFor: wait.ForLog("SMTP listening on tcp4"), + ExposedPorts: []string{"2500/tcp", "9000/tcp", "1100/tcp"}, + WaitingFor: wait.ForAll( + wait.NewHostPortStrategy("2500/tcp"), + wait.NewHostPortStrategy("9000/tcp"), + wait.NewHostPortStrategy("1100/tcp"), + ), } genericContainerReq := testcontainers.GenericContainerRequest{ diff --git a/modules/inbucket/inbucket_test.go b/modules/inbucket/inbucket_test.go index 8c2c479aed..eb6dc3c493 100644 --- a/modules/inbucket/inbucket_test.go +++ b/modules/inbucket/inbucket_test.go @@ -6,65 +6,45 @@ import ( "testing" "github.com/inbucket/inbucket/pkg/rest/client" + "github.com/stretchr/testify/require" ) func TestInbucket(t *testing.T) { ctx := context.Background() container, err := Run(ctx, "inbucket/inbucket:sha-2d409bb") - if err != nil { - t.Fatal(err) - } + require.NoError(t, err) // Clean up the container after the test is complete t.Cleanup(func() { - if err := container.Terminate(ctx); err != nil { - t.Fatalf("failed to terminate container: %s", err) - } + err := container.Terminate(ctx) + require.NoError(t, err) }) // smtpConnection { smtpUrl, err := container.SmtpConnection(ctx) // } - if err != nil { - t.Fatal(err) - } + require.NoError(t, err) // webInterface { webInterfaceUrl, err := container.WebInterface(ctx) // } - if err != nil { - t.Fatal(err) - } - + require.NoError(t, err) restClient, err := client.New(webInterfaceUrl) - if err != nil { - t.Fatal(err) - } + require.NoError(t, err) headers, err := restClient.ListMailbox("to@example.org") - if err != nil { - t.Fatal(err) - } - if len(headers) != 0 { - t.Fatalf("expected 0 messages, got %d", len(headers)) - } + require.NoError(t, err) + require.Empty(t, headers) msg := []byte("To: to@example.org\r\n" + "Subject: Testcontainers test!\r\n" + "\r\n" + "This is a Testcontainers test.\r\n") - if err = smtp.SendMail(smtpUrl, nil, "from@example.org", []string{"to@example.org"}, msg); err != nil { - t.Fatal(err) - } + err = smtp.SendMail(smtpUrl, nil, "from@example.org", []string{"to@example.org"}, msg) + require.NoError(t, err) headers, err = restClient.ListMailbox("to@example.org") - if err != nil { - t.Fatal(err) - } - - if len(headers) != 1 { - t.Fatalf("expected 1 message, got %d", len(headers)) - } - // perform assertions + require.NoError(t, err) + require.Len(t, headers, 1) } From d229f43bd999887f3ca0b710ed8722e2928c40c9 Mon Sep 17 00:00:00 2001 From: Steven Hartland Date: Wed, 7 Aug 2024 22:59:27 +0100 Subject: [PATCH 2/2] chore: use listen port instead of host port MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Use ForListeningPort instead of NewHostPortStrategy as it's more readable. Co-authored-by: Manuel de la Peña --- modules/inbucket/inbucket.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/modules/inbucket/inbucket.go b/modules/inbucket/inbucket.go index 579e0aa661..beae784557 100644 --- a/modules/inbucket/inbucket.go +++ b/modules/inbucket/inbucket.go @@ -59,9 +59,9 @@ func Run(ctx context.Context, img string, opts ...testcontainers.ContainerCustom Image: img, ExposedPorts: []string{"2500/tcp", "9000/tcp", "1100/tcp"}, WaitingFor: wait.ForAll( - wait.NewHostPortStrategy("2500/tcp"), - wait.NewHostPortStrategy("9000/tcp"), - wait.NewHostPortStrategy("1100/tcp"), + wait.ForListeningPort("2500/tcp"), + wait.ForListeningPort("9000/tcp"), + wait.ForListeningPort("1100/tcp"), ), }