From ca3f50552e283eb2e7b040d2038b7157b7ba11b0 Mon Sep 17 00:00:00 2001 From: Winni Neessen Date: Thu, 14 Nov 2024 10:36:24 +0100 Subject: [PATCH] Allow configuration of test server port via environment variable Moved TestServerPortBase initialization to use an environment variable `TEST_BASEPORT` if provided. This adjustment helps in specifying custom base ports for running tests, ensuring better flexibility in different testing environments. --- client_test.go | 18 ++++++++++++++++-- smtp/smtp_test.go | 18 ++++++++++++++++-- 2 files changed, 32 insertions(+), 4 deletions(-) diff --git a/client_test.go b/client_test.go index d1afc86..2ac8811 100644 --- a/client_test.go +++ b/client_test.go @@ -17,6 +17,7 @@ import ( "net/mail" "os" "reflect" + "strconv" "strings" "sync" "sync/atomic" @@ -34,14 +35,15 @@ const ( TestServerProto = "tcp" // TestServerAddr is the address the simple SMTP test server listens on TestServerAddr = "127.0.0.1" - // TestServerPortBase is the base port for the simple SMTP test server - TestServerPortBase = 30025 // TestSenderValid is a test sender email address considered valid for sending test emails. TestSenderValid = "valid-from@domain.tld" // TestRcptValid is a test recipient email address considered valid for sending test emails. TestRcptValid = "valid-to@domain.tld" ) +// TestServerPortBase is the base port for the simple SMTP test server +var TestServerPortBase int32 = 30025 + // PortAdder is an atomic counter used to increment port numbers for the test SMTP server instances. var PortAdder atomic.Int32 @@ -98,6 +100,18 @@ type logData struct { Lines []logLine `json:"lines"` } +func init() { + testPort := os.Getenv("TEST_BASEPORT") + if testPort == "" { + return + } + if port, err := strconv.Atoi(testPort); err == nil { + if port <= 65000 && port > 1023 { + TestServerPortBase = int32(port) + } + } +} + func TestNewClient(t *testing.T) { t.Run("create new Client", func(t *testing.T) { client, err := NewClient(DefaultHost) diff --git a/smtp/smtp_test.go b/smtp/smtp_test.go index 1b616fc..764d65b 100644 --- a/smtp/smtp_test.go +++ b/smtp/smtp_test.go @@ -30,6 +30,7 @@ import ( "io" "net" "os" + "strconv" "strings" "sync" "sync/atomic" @@ -46,13 +47,14 @@ const ( TestServerProto = "tcp" // TestServerAddr is the address the simple SMTP test server listens on TestServerAddr = "127.0.0.1" - // TestServerPortBase is the base port for the simple SMTP test server - TestServerPortBase = 30025 ) // PortAdder is an atomic counter used to increment port numbers for the test SMTP server instances. var PortAdder atomic.Int32 +// TestServerPortBase is the base port for the simple SMTP test server +var TestServerPortBase int32 = 30025 + // localhostCert is a PEM-encoded TLS cert generated from src/crypto/tls: // // go run generate_cert.go --rsa-bits 1024 --host 127.0.0.1,::1,example.com \ @@ -231,6 +233,18 @@ var authTests = []authTest{ }, } +func init() { + testPort := os.Getenv("TEST_BASEPORT") + if testPort == "" { + return + } + if port, err := strconv.Atoi(testPort); err == nil { + if port <= 65000 && port > 1023 { + TestServerPortBase = int32(port) + } + } +} + func TestAuth(t *testing.T) { t.Run("Auth for all supported auth methods", func(t *testing.T) { for i, tt := range authTests {