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.
This commit is contained in:
Winni Neessen 2024-11-14 10:36:24 +01:00
parent bd655b768b
commit ca3f50552e
Signed by: wneessen
GPG key ID: 385AC9889632126E
2 changed files with 32 additions and 4 deletions

View file

@ -17,6 +17,7 @@ import (
"net/mail" "net/mail"
"os" "os"
"reflect" "reflect"
"strconv"
"strings" "strings"
"sync" "sync"
"sync/atomic" "sync/atomic"
@ -34,14 +35,15 @@ const (
TestServerProto = "tcp" TestServerProto = "tcp"
// TestServerAddr is the address the simple SMTP test server listens on // TestServerAddr is the address the simple SMTP test server listens on
TestServerAddr = "127.0.0.1" 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 is a test sender email address considered valid for sending test emails.
TestSenderValid = "valid-from@domain.tld" TestSenderValid = "valid-from@domain.tld"
// TestRcptValid is a test recipient email address considered valid for sending test emails. // TestRcptValid is a test recipient email address considered valid for sending test emails.
TestRcptValid = "valid-to@domain.tld" 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. // PortAdder is an atomic counter used to increment port numbers for the test SMTP server instances.
var PortAdder atomic.Int32 var PortAdder atomic.Int32
@ -98,6 +100,18 @@ type logData struct {
Lines []logLine `json:"lines"` 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) { func TestNewClient(t *testing.T) {
t.Run("create new Client", func(t *testing.T) { t.Run("create new Client", func(t *testing.T) {
client, err := NewClient(DefaultHost) client, err := NewClient(DefaultHost)

View file

@ -30,6 +30,7 @@ import (
"io" "io"
"net" "net"
"os" "os"
"strconv"
"strings" "strings"
"sync" "sync"
"sync/atomic" "sync/atomic"
@ -46,13 +47,14 @@ const (
TestServerProto = "tcp" TestServerProto = "tcp"
// TestServerAddr is the address the simple SMTP test server listens on // TestServerAddr is the address the simple SMTP test server listens on
TestServerAddr = "127.0.0.1" 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. // PortAdder is an atomic counter used to increment port numbers for the test SMTP server instances.
var PortAdder atomic.Int32 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: // 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 \ // 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) { func TestAuth(t *testing.T) {
t.Run("Auth for all supported auth methods", func(t *testing.T) { t.Run("Auth for all supported auth methods", func(t *testing.T) {
for i, tt := range authTests { for i, tt := range authTests {