From 526baa94256ac2ae50edcdbd262c589a18bcf5d8 Mon Sep 17 00:00:00 2001 From: Arne Luenser Date: Thu, 9 Mar 2023 13:13:18 +0100 Subject: [PATCH] fix: don't reuse ports in courier/SMTP tests --- courier/courier_test.go | 6 ++---- courier/smtp_test.go | 2 +- x/mailhog.go | 19 +++++++++++++++++-- 3 files changed, 20 insertions(+), 7 deletions(-) diff --git a/courier/courier_test.go b/courier/courier_test.go index 15c5ec783474..fefcafc7ef10 100644 --- a/courier/courier_test.go +++ b/courier/courier_test.go @@ -7,11 +7,9 @@ import ( "testing" "github.com/ory/kratos/x" - dhelper "github.com/ory/x/sqlcon/dockertest" ) func TestMain(m *testing.M) { - atexit := dhelper.NewOnExit() - atexit.Add(x.CleanUpTestSMTP) - atexit.Exit(m.Run()) + m.Run() + x.CleanUpTestSMTP() } diff --git a/courier/smtp_test.go b/courier/smtp_test.go index fd936c5799cc..c30d865b88ec 100644 --- a/courier/smtp_test.go +++ b/courier/smtp_test.go @@ -172,7 +172,7 @@ func TestQueueEmail(t *testing.T) { } if total := gjson.GetBytes(body, "total").Int(); total != 3 { - return errors.Errorf("expected to have delivered at least 3 messages but got count %d with body: %s", total, body) + return errors.Errorf("expected to have delivered exactly 3 messages but got count %d with body: %s", total, body) } return nil diff --git a/x/mailhog.go b/x/mailhog.go index 88e0286e0d2a..8a54f5a35ed4 100644 --- a/x/mailhog.go +++ b/x/mailhog.go @@ -11,9 +11,11 @@ import ( "time" "github.com/cenkalti/backoff" + "github.com/phayes/freeport" "github.com/pkg/errors" "github.com/ory/dockertest/v3" + "github.com/ory/dockertest/v3/docker" ) var ( @@ -41,6 +43,15 @@ func RunTestSMTP() (smtp, api string, err error) { if err != nil { return "", "", err } + if err := pool.Client.Ping(); err != nil { + return "", "", err + } + + ports, err := freeport.GetFreePorts(2) + if err != nil { + return "", "", err + } + smtpPort, apiPort := ports[0], ports[1] resource, err := pool. RunWithOptions(&dockertest.RunOptions{ @@ -56,6 +67,10 @@ func RunTestSMTP() (smtp, api string, err error) { "-jim-linkspeed-min=1250", "-jim-linkspeed-max=12500", }, + PortBindings: map[docker.Port][]docker.PortBinding{ + "8025/tcp": {{HostPort: fmt.Sprintf("%d/tcp", apiPort)}}, + "1025/tcp": {{HostPort: fmt.Sprintf("%d/tcp", smtpPort)}}, + }, }) if err != nil { return "", "", err @@ -64,8 +79,8 @@ func RunTestSMTP() (smtp, api string, err error) { resources = append(resources, resource) resourceMux.Unlock() - smtp = fmt.Sprintf("smtp://test:test@127.0.0.1:%s/?disable_starttls=true", resource.GetPort("1025/tcp")) - api = fmt.Sprintf("http://127.0.0.1:%s", resource.GetPort("8025/tcp")) + smtp = fmt.Sprintf("smtp://test:test@127.0.0.1:%d/?disable_starttls=true", smtpPort) + api = fmt.Sprintf("http://127.0.0.1:%d", apiPort) if err := backoff.Retry(func() error { res, err := http.Get(api + "/api/v2/messages") if err != nil {