Skip to content

Commit 1dc1fbe

Browse files
Use constants consistently in test files
- Replace hardcoded 'test-device' string literals with existing testDeviceCN and testDeviceID constants - Improve test maintainability by using centralized test constants - Ensure consistent test data across PKI and service test suites - Address goconst linting issues in test files Files updated: - agent/internal/service/service_test.go: Use testDeviceID constant - pkg/pki/device_test.go: Add and use testDeviceCN constant - pkg/pki/ca_test.go: Use existing testDeviceCN constant Co-authored-by: Amp <[email protected]> Amp-Thread-ID: https://ampcode.com/threads/T-5be4213f-26eb-400c-bb7b-d4c79b7ee6fe
1 parent 7ad717c commit 1dc1fbe

File tree

4 files changed

+7
-7
lines changed

4 files changed

+7
-7
lines changed

agent/internal/service/service_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ func TestService_obtainCertificate(t *testing.T) {
313313
return
314314
}
315315

316-
if req["device_id"] != "test-device" {
316+
if req["device_id"] != testDeviceID {
317317
t.Errorf("Expected device ID 'test-device', got %s", req["device_id"])
318318
}
319319

@@ -341,7 +341,7 @@ func TestService_obtainCertificate(t *testing.T) {
341341
defer mockAuthz.Close()
342342

343343
config := &Config{
344-
DeviceID: "test-device",
344+
DeviceID: testDeviceID,
345345
AttestURL: mockAuthz.URL,
346346
CertPath: filepath.Join(tmpDir, "device.crt"),
347347
CAPath: filepath.Join(tmpDir, "ca.pem"),
@@ -384,7 +384,7 @@ func TestService_obtainCertificate(t *testing.T) {
384384
defer mockAuthz.Close()
385385

386386
config := &Config{
387-
DeviceID: "test-device",
387+
DeviceID: testDeviceID,
388388
AttestURL: mockAuthz.URL,
389389
CertPath: filepath.Join(tmpDir, "device.crt"),
390390
CAPath: filepath.Join(tmpDir, "ca.pem"),

lint_priority1.json

Lines changed: 0 additions & 1 deletion
This file was deleted.

pkg/pki/ca_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ func TestCertificateAuthority_IssueCertificate(t *testing.T) {
224224
})
225225

226226
t.Run("uses default TTL when zero", func(t *testing.T) {
227-
subject := pkix.Name{CommonName: "test-device"}
227+
subject := pkix.Name{CommonName: testDeviceCN}
228228

229229
certPEM, err := ca.IssueCertificate(subject, nil, nil, 0, &priv.PublicKey)
230230
if err != nil {
@@ -245,7 +245,7 @@ func TestCertificateAuthority_IssueCertificate(t *testing.T) {
245245
})
246246

247247
t.Run("handles invalid URI", func(t *testing.T) {
248-
subject := pkix.Name{CommonName: "test-device"}
248+
subject := pkix.Name{CommonName: testDeviceCN}
249249
invalidURIs := []string{"not://a valid uri with spaces"}
250250

251251
_, err := ca.IssueCertificate(subject, invalidURIs, nil, time.Hour, &priv.PublicKey)

pkg/pki/device_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ const (
1717
testKeyName = "test.key"
1818
benchKeyName = "bench.key"
1919
msgGenerateKeyFail = "Failed to generate key: %v"
20+
testDeviceCN = "test-device"
2021
)
2122

2223
func TestGenerateSigningKey(t *testing.T) {
@@ -441,7 +442,7 @@ func TestCertificateExpiry(t *testing.T) {
441442

442443
// Issue a certificate with known TTL
443444
ttl := 2 * time.Hour
444-
subject := pkix.Name{CommonName: "test-device"}
445+
subject := pkix.Name{CommonName: testDeviceCN}
445446
certPEM, err := ca.IssueCertificate(subject, nil, nil, ttl, &priv.PublicKey)
446447
if err != nil {
447448
t.Fatalf("Failed to issue certificate: %v", err)

0 commit comments

Comments
 (0)