Skip to content

Commit

Permalink
fix: don't reuse ports in courier/SMTP tests (#3156)
Browse files Browse the repository at this point in the history
  • Loading branch information
alnr committed Mar 9, 2023
1 parent cca36f8 commit e260fcf
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 7 deletions.
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

0 comments on commit e260fcf

Please sign in to comment.