Skip to content

Commit bcdcd99

Browse files
committed
fixing lint errors
1 parent 961954a commit bcdcd99

File tree

2 files changed

+28
-26
lines changed

2 files changed

+28
-26
lines changed

internal/collector/nginxossreceiver/internal/scraper/stubstatus/stub_status_scraper_tls_test.go

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,11 @@ func TestStubStatusScraperTLS(t *testing.T) {
4646
IsCA: true,
4747
}
4848

49-
caPrivKey, err := rsa.GenerateKey(rand.Reader, 2048)
50-
require.NoError(t, err)
49+
caPrivKey, caPrivKeyErr := rsa.GenerateKey(rand.Reader, 2048)
50+
require.NoError(t, caPrivKeyErr)
5151

52-
caBytes, err := x509.CreateCertificate(rand.Reader, ca, ca, &caPrivKey.PublicKey, caPrivKey)
53-
require.NoError(t, err)
52+
caBytes, caBytesErr := x509.CreateCertificate(rand.Reader, ca, ca, &caPrivKey.PublicKey, caPrivKey)
53+
require.NoError(t, caBytesErr)
5454

5555
// Create a test server certificate signed by the CA
5656
cert := &x509.Certificate{
@@ -67,23 +67,24 @@ func TestStubStatusScraperTLS(t *testing.T) {
6767
DNSNames: []string{"localhost"},
6868
}
6969

70-
certPrivKey, err := rsa.GenerateKey(rand.Reader, 2048)
71-
require.NoError(t, err)
70+
certPrivKey, certPrivKeyErr := rsa.GenerateKey(rand.Reader, 2048)
71+
require.NoError(t, certPrivKeyErr)
7272

73-
certBytes, err := x509.CreateCertificate(rand.Reader, cert, ca, &certPrivKey.PublicKey, caPrivKey)
74-
require.NoError(t, err)
73+
certBytes, certBytesErr := x509.CreateCertificate(rand.Reader, cert, ca, &certPrivKey.PublicKey, caPrivKey)
74+
require.NoError(t, certBytesErr)
7575

7676
// Create a temporary directory for test files
7777
tempDir := t.TempDir()
7878

7979
// Save CA certificate to a file
8080
caFile := filepath.Join(tempDir, "ca.crt")
8181
caPEM := pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: caBytes})
82-
err = os.WriteFile(caFile, caPEM, 0o600)
83-
require.NoError(t, err)
82+
writeErr := os.WriteFile(caFile, caPEM, 0o600)
83+
require.NoError(t, writeErr)
8484

8585
// Create a TLS config for the server
8686
serverTLSConfig := &tls.Config{
87+
MinVersion: tls.VersionTLS13,
8788
Certificates: []tls.Certificate{
8889
{
8990
Certificate: [][]byte{certBytes},
@@ -101,6 +102,7 @@ server accepts handled requests
101102
16630948 16630946 31070465
102103
Reading: 6 Writing: 179 Waiting: 106
103104
`))
105+
104106
return
105107
}
106108
rw.WriteHeader(http.StatusNotFound)
@@ -129,14 +131,14 @@ Reading: 6 Writing: 179 Waiting: 106
129131
}
130132

131133
func TestStubStatusScraperUnixSocket(t *testing.T) {
132-
tempDir, err := os.MkdirTemp("", "TestStubStatusScraperUnixSocket")
133-
require.NoError(t, err)
134-
t.Cleanup(func() { os.RemoveAll(tempDir) })
135-
socketPath := filepath.Join(tempDir, "nginx.sock")
134+
// Use a shorter path for the socket to avoid path length issues
135+
socketPath := filepath.Join(os.TempDir(), "test-nginx.sock")
136+
// Clean up the socket file after the test
137+
t.Cleanup(func() { os.Remove(socketPath) })
136138

137139
// Create a Unix domain socket listener
138-
listener, err := net.Listen("unix", socketPath)
139-
require.NoError(t, err)
140+
listener, listenErr := net.Listen("unix", socketPath)
141+
require.NoError(t, listenErr)
140142
defer listener.Close()
141143

142144
// Start a simple HTTP server on the Unix socket
@@ -149,6 +151,7 @@ server accepts handled requests
149151
16630948 16630946 31070465
150152
Reading: 6 Writing: 179 Waiting: 106
151153
`))
154+
152155
return
153156
}
154157
rw.WriteHeader(http.StatusNotFound)

internal/resource/resource_service_test.go

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"errors"
1111
"fmt"
1212
"os"
13+
"path/filepath"
1314
"testing"
1415

1516
"github.com/nginx/agent/v3/internal/model"
@@ -240,13 +241,11 @@ func TestResourceService_GetResource(t *testing.T) {
240241

241242
func TestResourceService_createPlusClient(t *testing.T) {
242243
// Create a temporary file for testing CA certificate
243-
tempCAFile, err := os.CreateTemp("", "test-ca.crt")
244-
require.NoError(t, err)
245-
defer os.Remove(tempCAFile.Name())
244+
tempDir := t.TempDir()
245+
caFile := filepath.Join(tempDir, "test-ca.crt")
246246

247-
_, err = tempCAFile.Write([]byte("-----BEGIN CERTIFICATE-----\nMII...\n-----END CERTIFICATE-----"))
247+
err := os.WriteFile(caFile, []byte("-----BEGIN CERTIFICATE-----\nMII...\n-----END CERTIFICATE-----"), 0o600)
248248
require.NoError(t, err)
249-
tempCAFile.Close()
250249

251250
instanceWithAPI := protos.NginxPlusInstance([]string{})
252251
instanceWithAPI.InstanceRuntime.GetNginxPlusRuntimeInfo().PlusApi = &v1.APIDetails{
@@ -264,7 +263,7 @@ func TestResourceService_createPlusClient(t *testing.T) {
264263
instanceWithCACert.InstanceRuntime.GetNginxPlusRuntimeInfo().PlusApi = &v1.APIDetails{
265264
Location: "/api",
266265
Listen: "localhost:443",
267-
Ca: tempCAFile.Name(),
266+
Ca: caFile,
268267
}
269268

270269
ctx := context.Background()
@@ -303,12 +302,12 @@ func TestResourceService_createPlusClient(t *testing.T) {
303302
protos.NginxPlusInstance([]string{}),
304303
}
305304

306-
_, err := resourceService.createPlusClient(test.instance)
305+
_, clientErr := resourceService.createPlusClient(test.instance)
307306
if test.err != nil {
308-
assert.Error(tt, err)
309-
assert.Contains(tt, err.Error(), test.err.Error())
307+
require.Error(tt, clientErr)
308+
assert.Contains(tt, clientErr.Error(), test.err.Error())
310309
} else {
311-
assert.NoError(tt, err)
310+
require.NoError(tt, clientErr)
312311
// For the CA cert test, we can't easily verify the internal http.Client configuration
313312
// without exporting it or adding test hooks, so we'll just verify no error is returned
314313
}

0 commit comments

Comments
 (0)