fix errcheck lint violations in test files

This commit is contained in:
Graham McIntire 2026-02-11 10:48:41 -06:00
parent a36a2b7fd7
commit 5ad95fac42
No known key found for this signature in database
5 changed files with 64 additions and 65 deletions

View file

@ -9,14 +9,13 @@ func TestEnvOrDefault(t *testing.T) {
key := "TOWEROPS_TEST_ENV_OR_DEFAULT" key := "TOWEROPS_TEST_ENV_OR_DEFAULT"
// Unset case // Unset case
os.Unsetenv(key) _ = os.Unsetenv(key)
if got := envOrDefault(key, "fallback"); got != "fallback" { if got := envOrDefault(key, "fallback"); got != "fallback" {
t.Errorf("unset: got %q, want %q", got, "fallback") t.Errorf("unset: got %q, want %q", got, "fallback")
} }
// Set case // Set case
os.Setenv(key, "custom") t.Setenv(key, "custom")
defer os.Unsetenv(key)
if got := envOrDefault(key, "fallback"); got != "custom" { if got := envOrDefault(key, "fallback"); got != "custom" {
t.Errorf("set: got %q, want %q", got, "custom") t.Errorf("set: got %q, want %q", got, "custom")
} }

View file

@ -262,7 +262,7 @@ func TestExecute(t *testing.T) {
// Server goroutine: read command, write response // Server goroutine: read command, write response
go func() { go func() {
defer serverW.Close() defer func() { _ = serverW.Close() }()
sc := &mikrotikClient{conn: &readWriteCloser{r: serverR, w: serverW}} sc := &mikrotikClient{conn: &readWriteCloser{r: serverR, w: serverW}}
// Read the command sentence // Read the command sentence
_, _ = sc.readSentence() _, _ = sc.readSentence()
@ -294,7 +294,7 @@ func TestExecuteWithArgs(t *testing.T) {
var receivedWords []string var receivedWords []string
go func() { go func() {
defer serverW.Close() defer func() { _ = serverW.Close() }()
sc := &mikrotikClient{conn: &readWriteCloser{r: serverR, w: serverW}} sc := &mikrotikClient{conn: &readWriteCloser{r: serverR, w: serverW}}
receivedWords, _ = sc.readSentence() receivedWords, _ = sc.readSentence()
_ = sc.writeSentence([]string{"!done"}) _ = sc.writeSentence([]string{"!done"})
@ -342,7 +342,7 @@ func TestMikrotikClose(t *testing.T) {
done := make(chan struct{}) done := make(chan struct{})
go func() { go func() {
defer close(done) defer close(done)
defer serverW.Close() defer func() { _ = serverW.Close() }()
sc := &mikrotikClient{conn: &readWriteCloser{r: serverR, w: serverW}} sc := &mikrotikClient{conn: &readWriteCloser{r: serverR, w: serverW}}
receivedWords, _ = sc.readSentence() receivedWords, _ = sc.readSentence()
_ = sc.writeSentence([]string{"!fatal"}) _ = sc.writeSentence([]string{"!fatal"})
@ -362,14 +362,14 @@ func TestMikrotikConnect(t *testing.T) {
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
defer ln.Close() defer func() { _ = ln.Close() }()
go func() { go func() {
conn, err := ln.Accept() conn, err := ln.Accept()
if err != nil { if err != nil {
return return
} }
defer conn.Close() defer func() { _ = conn.Close() }()
sc := &mikrotikClient{conn: conn} sc := &mikrotikClient{conn: conn}
// Read the /login command // Read the /login command
_, _ = sc.readSentence() _, _ = sc.readSentence()
@ -383,7 +383,7 @@ func TestMikrotikConnect(t *testing.T) {
_, port, _ := net.SplitHostPort(ln.Addr().String()) _, port, _ := net.SplitHostPort(ln.Addr().String())
var portNum uint32 var portNum uint32
fmt.Sscanf(port, "%d", &portNum) _, _ = fmt.Sscanf(port, "%d", &portNum)
client, err := mikrotikConnect("127.0.0.1", portNum, "admin", "pass", false) client, err := mikrotikConnect("127.0.0.1", portNum, "admin", "pass", false)
if err != nil { if err != nil {
@ -397,14 +397,14 @@ func TestMikrotikConnectAuthError(t *testing.T) {
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
defer ln.Close() defer func() { _ = ln.Close() }()
go func() { go func() {
conn, err := ln.Accept() conn, err := ln.Accept()
if err != nil { if err != nil {
return return
} }
defer conn.Close() defer func() { _ = conn.Close() }()
sc := &mikrotikClient{conn: conn} sc := &mikrotikClient{conn: conn}
_, _ = sc.readSentence() _, _ = sc.readSentence()
// Respond with trap error + done // Respond with trap error + done
@ -414,7 +414,7 @@ func TestMikrotikConnectAuthError(t *testing.T) {
_, port, _ := net.SplitHostPort(ln.Addr().String()) _, port, _ := net.SplitHostPort(ln.Addr().String())
var portNum uint32 var portNum uint32
fmt.Sscanf(port, "%d", &portNum) _, _ = fmt.Sscanf(port, "%d", &portNum)
_, err = mikrotikConnect("127.0.0.1", portNum, "admin", "wrong", false) _, err = mikrotikConnect("127.0.0.1", portNum, "admin", "wrong", false)
if err == nil { if err == nil {
@ -427,14 +427,14 @@ func TestMikrotikConnectFatalError(t *testing.T) {
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
defer ln.Close() defer func() { _ = ln.Close() }()
go func() { go func() {
conn, err := ln.Accept() conn, err := ln.Accept()
if err != nil { if err != nil {
return return
} }
defer conn.Close() defer func() { _ = conn.Close() }()
sc := &mikrotikClient{conn: conn} sc := &mikrotikClient{conn: conn}
_, _ = sc.readSentence() _, _ = sc.readSentence()
// Respond with fatal error // Respond with fatal error
@ -443,7 +443,7 @@ func TestMikrotikConnectFatalError(t *testing.T) {
_, port, _ := net.SplitHostPort(ln.Addr().String()) _, port, _ := net.SplitHostPort(ln.Addr().String())
var portNum uint32 var portNum uint32
fmt.Sscanf(port, "%d", &portNum) _, _ = fmt.Sscanf(port, "%d", &portNum)
_, err = mikrotikConnect("127.0.0.1", portNum, "admin", "pass", false) _, err = mikrotikConnect("127.0.0.1", portNum, "admin", "pass", false)
if err == nil { if err == nil {
@ -485,11 +485,11 @@ func TestReadResponseEmptySentence(t *testing.T) {
func TestReadSentenceWithNetConn(t *testing.T) { func TestReadSentenceWithNetConn(t *testing.T) {
// Test readSentence with a real net.Conn to trigger SetReadDeadline path // Test readSentence with a real net.Conn to trigger SetReadDeadline path
server, client := net.Pipe() server, client := net.Pipe()
defer server.Close() defer func() { _ = server.Close() }()
defer client.Close() defer func() { _ = client.Close() }()
go func() { go func() {
server.Write(encodeSentence([]string{"!done"})) _, _ = server.Write(encodeSentence([]string{"!done"}))
}() }()
c := &mikrotikClient{conn: client} c := &mikrotikClient{conn: client}

View file

@ -278,17 +278,17 @@ func TestExecuteMikrotikBackupDialError(t *testing.T) {
func TestExecuteMikrotikBackupSuccess(t *testing.T) { func TestExecuteMikrotikBackupSuccess(t *testing.T) {
addr, cleanup := startTestSSHServer(t, func(ch ssh.Channel) { addr, cleanup := startTestSSHServer(t, func(ch ssh.Channel) {
ch.Write([]byte("# RouterOS config\n/ip address\nadd address=10.0.0.1/24\n")) _, _ = ch.Write([]byte("# RouterOS config\n/ip address\nadd address=10.0.0.1/24\n"))
ch.CloseWrite() _ = ch.CloseWrite()
// Send exit-status 0 // Send exit-status 0
ch.SendRequest("exit-status", false, ssh.Marshal(struct{ Status uint32 }{0})) _, _ = ch.SendRequest("exit-status", false, ssh.Marshal(struct{ Status uint32 }{0}))
ch.Close() _ = ch.Close()
}) })
defer cleanup() defer cleanup()
_, port, _ := net.SplitHostPort(addr) _, port, _ := net.SplitHostPort(addr)
var portNum uint16 var portNum uint16
fmt.Sscanf(port, "%d", &portNum) _, _ = fmt.Sscanf(port, "%d", &portNum)
config, err := executeMikrotikBackup("127.0.0.1", portNum, "admin", "pass") config, err := executeMikrotikBackup("127.0.0.1", portNum, "admin", "pass")
if err != nil { if err != nil {
@ -302,14 +302,14 @@ func TestExecuteMikrotikBackupSuccess(t *testing.T) {
func TestExecuteMikrotikBackupCommandError(t *testing.T) { func TestExecuteMikrotikBackupCommandError(t *testing.T) {
addr, cleanup := startTestSSHServer(t, func(ch ssh.Channel) { addr, cleanup := startTestSSHServer(t, func(ch ssh.Channel) {
// Send exit-status 1 with no output (simulates command failure) // Send exit-status 1 with no output (simulates command failure)
ch.SendRequest("exit-status", false, ssh.Marshal(struct{ Status uint32 }{1})) _, _ = ch.SendRequest("exit-status", false, ssh.Marshal(struct{ Status uint32 }{1}))
ch.Close() _ = ch.Close()
}) })
defer cleanup() defer cleanup()
_, port, _ := net.SplitHostPort(addr) _, port, _ := net.SplitHostPort(addr)
var portNum uint16 var portNum uint16
fmt.Sscanf(port, "%d", &portNum) _, _ = fmt.Sscanf(port, "%d", &portNum)
_, err := executeMikrotikBackup("127.0.0.1", portNum, "admin", "pass") _, err := executeMikrotikBackup("127.0.0.1", portNum, "admin", "pass")
if err == nil { if err == nil {
@ -320,16 +320,16 @@ func TestExecuteMikrotikBackupCommandError(t *testing.T) {
func TestExecuteMikrotikBackupWithOutput(t *testing.T) { func TestExecuteMikrotikBackupWithOutput(t *testing.T) {
// MikroTik SSH returns output even with non-zero exit code // MikroTik SSH returns output even with non-zero exit code
addr, cleanup := startTestSSHServer(t, func(ch ssh.Channel) { addr, cleanup := startTestSSHServer(t, func(ch ssh.Channel) {
ch.Write([]byte("# partial config\n")) _, _ = ch.Write([]byte("# partial config\n"))
ch.CloseWrite() _ = ch.CloseWrite()
ch.SendRequest("exit-status", false, ssh.Marshal(struct{ Status uint32 }{1})) _, _ = ch.SendRequest("exit-status", false, ssh.Marshal(struct{ Status uint32 }{1}))
ch.Close() _ = ch.Close()
}) })
defer cleanup() defer cleanup()
_, port, _ := net.SplitHostPort(addr) _, port, _ := net.SplitHostPort(addr)
var portNum uint16 var portNum uint16
fmt.Sscanf(port, "%d", &portNum) _, _ = fmt.Sscanf(port, "%d", &portNum)
config, err := executeMikrotikBackup("127.0.0.1", portNum, "admin", "pass") config, err := executeMikrotikBackup("127.0.0.1", portNum, "admin", "pass")
if err != nil { if err != nil {
@ -362,31 +362,31 @@ func TestExecuteMikrotikBackupSessionError(t *testing.T) {
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
defer ln.Close() defer func() { _ = ln.Close() }()
go func() { go func() {
conn, err := ln.Accept() conn, err := ln.Accept()
if err != nil { if err != nil {
return return
} }
defer conn.Close() defer func() { _ = conn.Close() }()
sconn, chans, reqs, err := ssh.NewServerConn(conn, config) sconn, chans, reqs, err := ssh.NewServerConn(conn, config)
if err != nil { if err != nil {
return return
} }
defer sconn.Close() defer func() { _ = sconn.Close() }()
go ssh.DiscardRequests(reqs) go ssh.DiscardRequests(reqs)
// Reject all channel requests to trigger NewSession error // Reject all channel requests to trigger NewSession error
for newChannel := range chans { for newChannel := range chans {
newChannel.Reject(ssh.Prohibited, "no sessions allowed") _ = newChannel.Reject(ssh.Prohibited, "no sessions allowed")
} }
}() }()
_, port, _ := net.SplitHostPort(ln.Addr().String()) _, port, _ := net.SplitHostPort(ln.Addr().String())
var portNum uint16 var portNum uint16
fmt.Sscanf(port, "%d", &portNum) _, _ = fmt.Sscanf(port, "%d", &portNum)
_, err = executeMikrotikBackup("127.0.0.1", portNum, "admin", "pass") _, err = executeMikrotikBackup("127.0.0.1", portNum, "admin", "pass")
if err == nil { if err == nil {
@ -427,18 +427,18 @@ func startTestSSHServer(t *testing.T, handler func(ch ssh.Channel)) (string, fun
if err != nil { if err != nil {
return return
} }
defer conn.Close() defer func() { _ = conn.Close() }()
sconn, chans, reqs, err := ssh.NewServerConn(conn, config) sconn, chans, reqs, err := ssh.NewServerConn(conn, config)
if err != nil { if err != nil {
return return
} }
defer sconn.Close() defer func() { _ = sconn.Close() }()
go ssh.DiscardRequests(reqs) go ssh.DiscardRequests(reqs)
for newChannel := range chans { for newChannel := range chans {
if newChannel.ChannelType() != "session" { if newChannel.ChannelType() != "session" {
newChannel.Reject(ssh.UnknownChannelType, "unknown channel type") _ = newChannel.Reject(ssh.UnknownChannelType, "unknown channel type")
continue continue
} }
ch, requests, err := newChannel.Accept() ch, requests, err := newChannel.Accept()
@ -448,17 +448,17 @@ func startTestSSHServer(t *testing.T, handler func(ch ssh.Channel)) (string, fun
go func() { go func() {
for req := range requests { for req := range requests {
if req.Type == "exec" { if req.Type == "exec" {
req.Reply(true, nil) _ = req.Reply(true, nil)
handler(ch) handler(ch)
return return
} }
req.Reply(false, nil) _ = req.Reply(false, nil)
} }
}() }()
} }
}() }()
return ln.Addr().String(), func() { ln.Close() } return ln.Addr().String(), func() { _ = ln.Close() }
} }
// mockMikrotikResponse pairs a response with an optional error for mock execute calls. // mockMikrotikResponse pairs a response with an optional error for mock execute calls.

View file

@ -49,7 +49,7 @@ func TestSelfUpdateReadBodyError(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Length", "99999") w.Header().Set("Content-Length", "99999")
w.WriteHeader(http.StatusOK) w.WriteHeader(http.StatusOK)
w.Write([]byte("partial")) _, _ = w.Write([]byte("partial"))
// Connection closes without sending the full body // Connection closes without sending the full body
if f, ok := w.(http.Flusher); ok { if f, ok := w.(http.Flusher); ok {
f.Flush() f.Flush()
@ -71,7 +71,7 @@ func TestSelfUpdateOsExecutableError(t *testing.T) {
} }
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("binary data")) _, _ = w.Write([]byte("binary data"))
})) }))
defer srv.Close() defer srv.Close()
@ -97,7 +97,7 @@ func TestSelfUpdateWriteFileError(t *testing.T) {
} }
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("binary data")) _, _ = w.Write([]byte("binary data"))
})) }))
defer srv.Close() defer srv.Close()
@ -126,7 +126,7 @@ func TestSelfUpdateRenameError(t *testing.T) {
} }
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("binary data")) _, _ = w.Write([]byte("binary data"))
})) }))
defer srv.Close() defer srv.Close()

View file

@ -415,25 +415,25 @@ func TestWSDial(t *testing.T) {
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
defer ln.Close() defer func() { _ = ln.Close() }()
go func() { go func() {
conn, err := ln.Accept() conn, err := ln.Accept()
if err != nil { if err != nil {
return return
} }
defer conn.Close() defer func() { _ = conn.Close() }()
buf := make([]byte, 4096) buf := make([]byte, 4096)
n, _ := conn.Read(buf) n, _ := conn.Read(buf)
_ = string(buf[:n]) // Read the request _ = string(buf[:n]) // Read the request
resp := "HTTP/1.1 101 Switching Protocols\r\nUpgrade: websocket\r\nConnection: Upgrade\r\n\r\n" resp := "HTTP/1.1 101 Switching Protocols\r\nUpgrade: websocket\r\nConnection: Upgrade\r\n\r\n"
conn.Write([]byte(resp)) _, _ = conn.Write([]byte(resp))
// Keep connection open briefly for the test // Keep connection open briefly for the test
buf2 := make([]byte, 1) buf2 := make([]byte, 1)
conn.Read(buf2) _, _ = conn.Read(buf2)
}() }()
addr := ln.Addr().String() addr := ln.Addr().String()
@ -450,20 +450,20 @@ func TestWSDialHandshakeFailed(t *testing.T) {
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
defer ln.Close() defer func() { _ = ln.Close() }()
go func() { go func() {
conn, err := ln.Accept() conn, err := ln.Accept()
if err != nil { if err != nil {
return return
} }
defer conn.Close() defer func() { _ = conn.Close() }()
buf := make([]byte, 4096) buf := make([]byte, 4096)
conn.Read(buf) _, _ = conn.Read(buf)
resp := "HTTP/1.1 403 Forbidden\r\nContent-Length: 0\r\n\r\n" resp := "HTTP/1.1 403 Forbidden\r\nContent-Length: 0\r\n\r\n"
conn.Write([]byte(resp)) _, _ = conn.Write([]byte(resp))
}() }()
addr := ln.Addr().String() addr := ln.Addr().String()
@ -482,15 +482,15 @@ func TestWSDialReadHandshakeError(t *testing.T) {
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
defer ln.Close() defer func() { _ = ln.Close() }()
go func() { go func() {
conn, _ := ln.Accept() conn, _ := ln.Accept()
if conn != nil { if conn != nil {
// Read the request then close immediately // Read the request then close immediately
buf := make([]byte, 4096) buf := make([]byte, 4096)
conn.Read(buf) _, _ = conn.Read(buf)
conn.Close() _ = conn.Close()
} }
}() }()
@ -513,7 +513,7 @@ func TestWSDialGenerateKeyError(t *testing.T) {
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
defer ln.Close() defer func() { _ = ln.Close() }()
go func() { go func() {
conn, _ := ln.Accept() conn, _ := ln.Accept()
@ -539,7 +539,7 @@ func TestWSDialWriteHandshakeError(t *testing.T) {
netDial = func(network, addr string) (net.Conn, error) { netDial = func(network, addr string) (net.Conn, error) {
// Return a connection whose Write always fails // Return a connection whose Write always fails
client, _ := net.Pipe() client, _ := net.Pipe()
client.Close() // Close immediately so Write fails _ = client.Close() // Close immediately so Write fails
return client, nil return client, nil
} }
@ -583,16 +583,16 @@ func TestWSDialRealHTTPServer(t *testing.T) {
return return
} }
conn, brw, _ := hj.Hijack() conn, brw, _ := hj.Hijack()
defer conn.Close() defer func() { _ = conn.Close() }()
brw.WriteString("HTTP/1.1 101 Switching Protocols\r\nUpgrade: websocket\r\nConnection: Upgrade\r\n\r\n") _, _ = brw.WriteString("HTTP/1.1 101 Switching Protocols\r\nUpgrade: websocket\r\nConnection: Upgrade\r\n\r\n")
brw.Flush() _ = brw.Flush()
// Keep alive briefly // Keep alive briefly
buf := make([]byte, 1) buf := make([]byte, 1)
conn.Read(buf) _, _ = conn.Read(buf)
}) })
ln, _ := net.Listen("tcp", "127.0.0.1:0") ln, _ := net.Listen("tcp", "127.0.0.1:0")
defer ln.Close() defer func() { _ = ln.Close() }()
go http.Serve(ln, mux) go func() { _ = http.Serve(ln, mux) }()
addr := ln.Addr().String() addr := ln.Addr().String()
ws, err := WSDial("ws://" + addr + "/ws") ws, err := WSDial("ws://" + addr + "/ws")