Skip to content

Commit 7d94d77

Browse files
committed
Linters clean-up, fix DNS mock leak in integration tests
1 parent 5189bb8 commit 7d94d77

File tree

14 files changed

+93
-40
lines changed

14 files changed

+93
-40
lines changed

framework/module/lifetime.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,9 @@ func (lt *LifetimeTracker) StartAll() error {
6060
}
6161

6262
if err := entry.mod.Start(); err != nil {
63-
lt.StopAll()
63+
if err := lt.StopAll(); err != nil {
64+
lt.logger.Error("StopAll failed after Start fail", err)
65+
}
6466
return fmt.Errorf("failed to start module %v: %w",
6567
entry.mod.InstanceName(), err)
6668
}

framework/resource/netresource/listen.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ func CloseUnusedListeners() error {
1616
return tracker.CloseUnused()
1717
}
1818

19-
func CloseAllListeners() {
20-
tracker.Close()
19+
func CloseAllListeners() error {
20+
return tracker.Close()
2121
}
2222

2323
func ResetListenersUsage() {

framework/resource/netresource/tracker.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,14 +67,22 @@ func (lt *ListenerTracker) ResetUsage() {
6767
}
6868

6969
func (lt *ListenerTracker) CloseUnused() error {
70-
lt.tcp.CloseUnused(func(key string) bool { return true })
71-
lt.unix.CloseUnused(func(key string) bool { return true })
70+
if err := lt.tcp.CloseUnused(func(key string) bool { return true }); err != nil {
71+
lt.logger.Error("CloseUnused for TCP failed", err)
72+
}
73+
if err := lt.unix.CloseUnused(func(key string) bool { return true }); err != nil {
74+
lt.logger.Error("CloseUnused for Unix failed", err)
75+
}
7276
return nil
7377
}
7478

7579
func (lt *ListenerTracker) Close() error {
76-
lt.tcp.Close()
77-
lt.unix.Close()
80+
if err := lt.tcp.Close(); err != nil {
81+
lt.logger.Error("Close for TCP failed", err)
82+
}
83+
if err := lt.unix.Close(); err != nil {
84+
lt.logger.Error("Close for Unix failed", err)
85+
}
7886
return nil
7987
}
8088

go.mod

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ require (
5252
github.com/minio/minio-go/v7 v7.0.84
5353
github.com/netauth/netauth v0.6.2
5454
github.com/prometheus/client_golang v1.20.5
55+
github.com/stretchr/testify v1.10.0
5556
github.com/urfave/cli/v2 v2.27.5
5657
go.uber.org/zap v1.27.0
5758
golang.org/x/crypto v0.32.0
@@ -87,6 +88,7 @@ require (
8788
github.com/caddyserver/zerossl v0.1.3 // indirect
8889
github.com/cespare/xxhash/v2 v2.3.0 // indirect
8990
github.com/cpuguy83/go-md2man/v2 v2.0.6 // indirect
91+
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
9092
github.com/digitalocean/godo v1.134.0 // indirect
9193
github.com/dustin/go-humanize v1.0.1 // indirect
9294
github.com/fatih/color v1.18.0 // indirect
@@ -122,6 +124,7 @@ require (
122124
github.com/pelletier/go-toml/v2 v2.2.3 // indirect
123125
github.com/pierrec/lz4 v2.6.1+incompatible // indirect
124126
github.com/pkg/errors v0.9.1 // indirect
127+
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
125128
github.com/prometheus/client_model v0.6.1 // indirect
126129
github.com/prometheus/common v0.62.0 // indirect
127130
github.com/prometheus/procfs v0.15.1 // indirect

internal/cli/ctl/moduleinit.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,21 @@ package ctl
2121
import (
2222
"errors"
2323
"fmt"
24-
"io"
2524
"os"
2625

2726
"github.com/foxcpp/maddy"
2827
"github.com/foxcpp/maddy/framework/container"
28+
"github.com/foxcpp/maddy/framework/log"
2929
"github.com/foxcpp/maddy/framework/module"
3030
"github.com/foxcpp/maddy/internal/updatepipe"
3131
"github.com/urfave/cli/v2"
3232
)
3333

34-
func closeIfNeeded(i interface{}) {
35-
if c, ok := i.(io.Closer); ok {
36-
c.Close()
34+
func closeIfNeeded(i any) {
35+
if c, ok := i.(module.LifetimeModule); ok {
36+
if err := c.Stop(); err != nil {
37+
log.DefaultLogger.Error("failed to stop module", err)
38+
}
3739
}
3840
}
3941

internal/endpoint/imap/imap.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,9 @@ func (endp *Endpoint) Start() error {
167167
}
168168

169169
if err := endp.setupListeners(endp.endpoints); err != nil {
170-
endp.Stop()
170+
if err := endp.Stop(); err != nil {
171+
endp.Log.Error("failed to stop after setupListeners error", err)
172+
}
171173
return err
172174
}
173175
return nil

internal/endpoint/openmetrics/om.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,9 @@ func (e *Endpoint) Start() error {
8585
for _, endp := range e.endpoints {
8686
l, err := netresource.Listen(endp.Network(), endp.Address())
8787
if err != nil {
88-
e.Stop()
88+
if err := e.Stop(); err != nil {
89+
90+
}
8991
return fmt.Errorf("%s: %v", modName, err)
9092
}
9193

internal/endpoint/smtp/smtp.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,9 @@ func (endp *Endpoint) setConfig(cfg *config.Map) error {
318318

319319
func (endp *Endpoint) Start() error {
320320
if err := endp.setupListeners(endp.endpoints); err != nil {
321-
endp.Stop()
321+
if err := endp.Stop(); err != nil {
322+
endp.Log.Error("failed to Stop after setupListeners fail", err)
323+
}
322324
return err
323325
}
324326
return nil
@@ -426,7 +428,9 @@ func (endp *Endpoint) Stop() error {
426428
ctx, cancel := context.WithTimeout(context.Background(), endp.shutdownTimeout)
427429
defer cancel()
428430

429-
endp.serv.Shutdown(ctx)
431+
if err := endp.serv.Shutdown(ctx); err != nil {
432+
return err
433+
}
430434

431435
endp.listenersWg.Wait()
432436

internal/endpoint/smtp/smtp_test.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import (
3737
"github.com/foxcpp/maddy/internal/auth"
3838
"github.com/foxcpp/maddy/internal/msgpipeline"
3939
"github.com/foxcpp/maddy/internal/testutils"
40+
"github.com/stretchr/testify/assert"
4041
)
4142

4243
var testPort string
@@ -146,7 +147,9 @@ func submitMsgOpts(t *testing.T, cl *smtp.Client, from string, rcpts []string, o
146147
func TestSMTPDelivery(t *testing.T) {
147148
tgt := testutils.Target{}
148149
endp := testEndpoint(t, "smtp", nil, &tgt, nil, nil)
149-
defer endp.Stop()
150+
defer func() {
151+
assert.NoError(t, endp.Stop())
152+
}()
150153

151154
cl, err := smtp.Dial("127.0.0.1:" + testPort)
152155
if err != nil {

internal/table/file_test.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626

2727
"github.com/foxcpp/maddy/framework/config"
2828
"github.com/foxcpp/maddy/internal/testutils"
29+
"github.com/stretchr/testify/assert"
2930
)
3031

3132
func TestReadFile(t *testing.T) {
@@ -107,7 +108,9 @@ func TestFileReload(t *testing.T) {
107108
t.Fatal(err)
108109
}
109110
m.log = testutils.Logger(t, "file_map")
110-
defer m.Stop()
111+
defer func() {
112+
assert.NoError(t, m.Stop())
113+
}()
111114

112115
if err := mod.Configure([]string{f.Name()}, &config.Map{Block: config.Node{}}); err != nil {
113116
t.Fatal(err)
@@ -147,10 +150,10 @@ func TestFileReload_Broken(t *testing.T) {
147150
}
148151
defer os.Remove(f.Name())
149152
if _, err := f.WriteString(file); err != nil {
150-
f.Close()
153+
assert.NoError(t, f.Close())
151154
t.Fatal(err)
152155
}
153-
f.Close()
156+
assert.NoError(t, f.Close())
154157

155158
mod, err := NewFile("", "")
156159
if err != nil {

0 commit comments

Comments
 (0)