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

fix: don't reuse ports in courier/SMTP tests #3156

Merged
merged 1 commit into from
Mar 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
6 changes: 2 additions & 4 deletions courier/courier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
2 changes: 1 addition & 1 deletion courier/smtp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 17 additions & 2 deletions x/mailhog.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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{
Expand All @@ -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
Expand All @@ -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 {
Expand Down