-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtesthelpers_test.go
More file actions
396 lines (331 loc) · 9.59 KB
/
testhelpers_test.go
File metadata and controls
396 lines (331 loc) · 9.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
package mailpitclient
import (
"context"
"log"
"net"
"net/http"
"os"
"path/filepath"
"strconv"
"sync"
"testing"
"time"
"github.com/testcontainers/testcontainers-go"
"github.com/testcontainers/testcontainers-go/wait"
)
// SMTPContainerPool manages a pool of SMTP containers
type SMTPContainerPool struct {
available chan testcontainers.Container
containers []testcontainers.Container
maxSize int
created int
mu sync.RWMutex
}
var (
smtpContainerPool *SMTPContainerPool
smtpPoolMu sync.Mutex
)
type SMTPConfig struct {
Host string
Username string
Password string
AuthType string
Encryption string
Port uint16
}
// TestSMTP holds the test SMTP resources
type TestSMTP struct {
Container testcontainers.Container
MailpitClient Client
SMTPPort string
APIPort string
Host string
SMTPConfig SMTPConfig
}
// GetTestSMTP returns a configured SMTP test environment with mailpit container.
// It uses a singleton container for efficiency and proper resource management.
func GetTestSMTP(tb testing.TB) *TestSMTP {
tb.Helper()
ctx := tb.Context()
// Use pooled container for parallel testing support
container := getSMTPContainerFromPool(tb)
tb.Cleanup(func() {
releaseSMTPContainerToPool(container)
})
// Get the mapped ports
smtpPort, err := container.MappedPort(ctx, "1025")
if err != nil {
tb.Fatalf("Failed to get SMTP port: %v", err)
}
apiPort, err := container.MappedPort(ctx, "8025")
if err != nil {
tb.Fatalf("Failed to get API port: %v", err)
}
// Get the container host
host, err := container.Host(ctx)
if err != nil {
tb.Fatalf("Failed to get container host: %v", err)
}
// Create SMTP service configuration
smtpConfig := SMTPConfig{
Host: host,
Port: uint16(smtpPort.Int()),
Username: "",
Password: "",
AuthType: "PLAIN",
Encryption: "starttls",
}
// Create mailpit client
mailpitConfig := &Config{
BaseURL: "http://" + net.JoinHostPort(host, apiPort.Port()),
APIPath: "/api/v1",
HTTPClient: &http.Client{Timeout: 10 * time.Second},
}
mailpitClient, err := NewClient(mailpitConfig)
if err != nil {
tb.Fatalf("Failed to create mailpit client: %v", err)
}
// Setup per-test cleanup
tb.Cleanup(func() {
if mailpitClient != nil {
if err = mailpitClient.Close(); err != nil {
tb.Errorf("Failed to close mailpit client: %v", err)
}
}
})
return &TestSMTP{
Container: container,
SMTPConfig: smtpConfig,
MailpitClient: mailpitClient,
SMTPPort: smtpPort.Port(),
APIPort: apiPort.Port(),
Host: host,
}
}
// initSMTPContainerPool initializes the SMTP container pool structure (lazy creation)
func initSMTPContainerPool(tb testing.TB) {
tb.Helper()
poolSize := 5
// Fixed pool size for SMTP containers (can be configurable)
if envPoolSize := os.Getenv("TEST_SMTP_POOL_SIZE"); envPoolSize != "" {
if size, err := strconv.Atoi(envPoolSize); err == nil && size > 0 {
poolSize = size
}
}
smtpContainerPool = &SMTPContainerPool{
containers: make([]testcontainers.Container, 0, poolSize),
available: make(chan testcontainers.Container, poolSize),
maxSize: poolSize,
created: 0,
}
}
// getSMTPContainerFromPool gets a container from the pool, creating one lazily if needed
func getSMTPContainerFromPool(tb testing.TB) testcontainers.Container {
tb.Helper()
smtpPoolMu.Lock()
defer smtpPoolMu.Unlock()
if smtpContainerPool == nil {
initSMTPContainerPool(tb)
}
// Try to get an available container first (non-blocking)
select {
case c := <-smtpContainerPool.available:
return c
default:
// No available containers, try to create one if we haven't reached the limit
}
// Check if we can create a new container (within bounds)
smtpContainerPool.mu.Lock()
canCreate := smtpContainerPool.created < smtpContainerPool.maxSize
if canCreate {
smtpContainerPool.created++
}
smtpContainerPool.mu.Unlock()
if canCreate {
// Create a new container lazily
ctx := tb.Context()
// Get project root and certificates directory
certsPath := filepath.Join(ProjectRootDir(tb), "certs")
// Create mailpit container request
req := testcontainers.ContainerRequest{
Image: "axllent/mailpit:latest",
ExposedPorts: []string{"1025/tcp", "8025/tcp"},
WaitingFor: wait.ForAll(
wait.ForListeningPort("1025/tcp"),
wait.ForListeningPort("8025/tcp"),
wait.ForHTTP("/api/v1/info").WithPort("8025/tcp").WithStartupTimeout(30*time.Second),
),
Env: map[string]string{
"MP_SMTP_TLS_CERT": "/certs/smtp.crt",
"MP_SMTP_TLS_KEY": "/certs/smtp.key",
"MP_SMTP_REQUIRE_STARTTLS": "false", // Allow both TLS and non-TLS connections
"MP_ENABLE_SPAMASSASSIN": "true",
"MP_SMTP_AUTH_ACCEPT_ANY": "1",
"MP_SMTP_AUTH_ALLOW_INSECURE": "1",
"MP_SMTP_8BITMIME": "1", // Enable 8BITMIME support
},
Files: []testcontainers.ContainerFile{
{
HostFilePath: filepath.Join(certsPath, "smtp.crt"),
ContainerFilePath: "/certs/smtp.crt",
},
{
HostFilePath: filepath.Join(certsPath, "smtp.key"),
ContainerFilePath: "/certs/smtp.key",
},
},
}
// Start the container
container, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
ContainerRequest: req,
Started: true,
})
if err != nil {
// Decrement counter on failure
smtpContainerPool.mu.Lock()
smtpContainerPool.created--
smtpContainerPool.mu.Unlock()
tb.Fatalf("Failed to start mailpit container: %v", err)
}
smtpContainerPool.mu.Lock()
smtpContainerPool.containers = append(smtpContainerPool.containers, container)
smtpContainerPool.mu.Unlock()
return container
}
// Wait for an available container (blocking)
select {
case cont := <-smtpContainerPool.available:
return cont
case <-tb.Context().Done():
tb.Fatalf("Test context cancelled while waiting for SMTP container: %v", tb.Context().Err())
}
return nil
}
// releaseSMTPContainerToPool returns a container to the pool
func releaseSMTPContainerToPool(container testcontainers.Container) {
if smtpContainerPool != nil {
smtpContainerPool.available <- container
}
}
// ClearMessages is a helper function to clear all messages from mailpit
func (ts *TestSMTP) ClearMessages(tb testing.TB) {
tb.Helper()
err := ts.MailpitClient.DeleteAllMessages(tb.Context())
if err != nil {
tb.Fatalf("Failed to clear messages: %v", err)
}
}
// GetMessages is a helper function to retrieve all messages from mailpit
func (ts *TestSMTP) GetMessages(tb testing.TB) []Message {
tb.Helper()
resp, err := ts.MailpitClient.ListMessages(tb.Context(), nil)
if err != nil {
tb.Fatalf("Failed to get messages from mailpit API: %v", err)
}
return resp.Messages
}
// WaitForMessages waits for the expected number of messages to arrive
func (ts *TestSMTP) WaitForMessages(tb testing.TB, expectedCount int, timeout time.Duration) []Message {
tb.Helper()
ctx, cancel := context.WithTimeout(tb.Context(), timeout)
defer cancel()
ticker := time.NewTicker(500 * time.Millisecond)
defer ticker.Stop()
for {
select {
case <-ctx.Done():
tb.Fatalf("Timeout waiting for %d messages", expectedCount)
return nil
case <-ticker.C:
messages := ts.GetMessages(tb)
if len(messages) >= expectedCount {
return messages
}
}
}
}
// cleanupSMTPContainerPool terminates all containers in the pool
func cleanupSMTPContainerPool() {
if smtpContainerPool == nil {
return
}
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
// Close the available channel to prevent new acquisitions
close(smtpContainerPool.available)
// Terminate all containers
for _, c := range smtpContainerPool.containers {
go func(container testcontainers.Container) {
if err := container.Terminate(ctx); err != nil {
log.Printf("Failed to terminate container: %v", err)
}
}(c)
}
smtpContainerPool = nil
}
// CleanupSMTPContainers should be called in TestMain to cleanup shared SMTP resources
func CleanupSMTPContainers() {
smtpPoolMu.Lock()
defer smtpPoolMu.Unlock()
cleanupSMTPContainerPool()
}
const gomod = "go.mod"
var (
projectRootDirCache = make(map[string]string, 10)
projectRootDirCacheMu sync.RWMutex
)
func ProjectRootDir(tb testing.TB) string {
tb.Helper()
originalWorkingDir := WorkingDir(tb)
workingDir := originalWorkingDir
projectRootDirCacheMu.RLock()
if dir, ok := projectRootDirCache[originalWorkingDir]; ok {
projectRootDirCacheMu.RUnlock()
return dir
}
projectRootDirCacheMu.RUnlock()
for entries, err := os.ReadDir(workingDir); err == nil; {
for _, entry := range entries {
if entry.Name() == gomod {
projectRootDirCacheMu.Lock()
projectRootDirCache[originalWorkingDir] = workingDir
projectRootDirCacheMu.Unlock()
return workingDir
}
}
if workingDir == "/" {
tb.Error("got to FS Root, file not found")
tb.FailNow()
}
workingDir, err = GetAbsolutePath(filepath.Join(workingDir, ".."))
if err != nil {
tb.Errorf("failed to get absolute path from %s", filepath.Join(workingDir, ".."))
tb.FailNow()
}
entries, err = os.ReadDir(workingDir)
}
tb.Errorf("%s not found", gomod)
tb.FailNow()
return ""
}
func WorkingDir(tb testing.TB) string {
tb.Helper()
wd, err := os.Getwd()
if err != nil {
tb.Error(err)
tb.FailNow()
}
return wd
}
// GetAbsolutePath Returns absolute path string for a given directory and error if directory doesent exist
func GetAbsolutePath(path string) (string, error) {
var err error
if !filepath.IsAbs(path) {
path, err = filepath.Abs(path)
if err != nil {
return "", err
}
return path, nil
}
return path, err
}