@@ -16,6 +16,8 @@ import (
1616 "github.com/EvalOps/keep/agent/internal/posture"
1717)
1818
19+ const testDeviceID = "test-device"
20+
1921// mockPostureCollector implements the posture.Collector interface for testing
2022type mockPostureCollector struct {
2123 postureData * posture.DevicePosture
@@ -41,7 +43,7 @@ func (e *mockError) Error() string {
4143func TestService_New (t * testing.T ) {
4244 t .Run ("creates service with valid config" , func (t * testing.T ) {
4345 config := & Config {
44- DeviceID : "test-device" ,
46+ DeviceID : testDeviceID ,
4547 InventoryURL : "http://localhost:8081" ,
4648 AttestURL : "http://localhost:8443" ,
4749 KeyPath : "/tmp/test.key" ,
@@ -84,7 +86,7 @@ func TestService_initialRegistration(t *testing.T) {
8486 return
8587 }
8688
87- if req .ID != "test-device" {
89+ if req .ID != testDeviceID {
8890 t .Errorf ("Expected device ID 'test-device', got %s" , req .ID )
8991 }
9092
@@ -97,7 +99,9 @@ func TestService_initialRegistration(t *testing.T) {
9799 }
98100
99101 w .WriteHeader (http .StatusOK )
100- json .NewEncoder (w ).Encode (map [string ]string {"status" : "ok" })
102+ if err := json .NewEncoder (w ).Encode (map [string ]string {"status" : "ok" }); err != nil {
103+ t .Fatalf ("Failed to encode response: %v" , err )
104+ }
101105 } else {
102106 http .Error (w , "not found" , http .StatusNotFound )
103107 }
@@ -106,7 +110,7 @@ func TestService_initialRegistration(t *testing.T) {
106110
107111 // Create test config
108112 config := & Config {
109- DeviceID : "test-device" ,
113+ DeviceID : testDeviceID ,
110114 InventoryURL : mockInventory .URL ,
111115 AttestURL : "http://localhost:8443" ,
112116 KeyPath : filepath .Join (tmpDir , "test.key" ),
@@ -142,7 +146,7 @@ func TestService_initialRegistration(t *testing.T) {
142146
143147 t .Run ("handles posture collection failure" , func (t * testing.T ) {
144148 config := & Config {
145- DeviceID : "test-device" ,
149+ DeviceID : testDeviceID ,
146150 InventoryURL : "http://localhost:8081" ,
147151 KeyPath : filepath .Join (tmpDir , "test.key" ),
148152 }
@@ -180,7 +184,7 @@ func TestService_initialRegistration(t *testing.T) {
180184 defer mockInventory .Close ()
181185
182186 config := & Config {
183- DeviceID : "test-device" ,
187+ DeviceID : testDeviceID ,
184188 InventoryURL : mockInventory .URL ,
185189 KeyPath : filepath .Join (tmpDir , "test.key" ),
186190 }
@@ -234,15 +238,17 @@ func TestService_updatePosture(t *testing.T) {
234238 }
235239
236240 w .WriteHeader (http .StatusOK )
237- json .NewEncoder (w ).Encode (map [string ]string {"status" : "updated" })
241+ if err := json .NewEncoder (w ).Encode (map [string ]string {"status" : "updated" }); err != nil {
242+ t .Fatalf ("Failed to encode response: %v" , err )
243+ }
238244 } else {
239245 http .Error (w , "not found" , http .StatusNotFound )
240246 }
241247 }))
242248 defer mockInventory .Close ()
243249
244250 config := & Config {
245- DeviceID : "test-device" ,
251+ DeviceID : testDeviceID ,
246252 InventoryURL : mockInventory .URL ,
247253 }
248254
@@ -269,7 +275,7 @@ func TestService_updatePosture(t *testing.T) {
269275
270276 t .Run ("handles posture collection failure" , func (t * testing.T ) {
271277 config := & Config {
272- DeviceID : "test-device" ,
278+ DeviceID : testDeviceID ,
273279 InventoryURL : "http://localhost:8081" ,
274280 }
275281
@@ -316,11 +322,15 @@ func TestService_obtainCertificate(t *testing.T) {
316322 response := certResponse {
317323 Certificate : "-----BEGIN CERTIFICATE-----\n MOCK_CERT_DATA\n -----END CERTIFICATE-----" ,
318324 }
319- json .NewEncoder (w ).Encode (response )
325+ if err := json .NewEncoder (w ).Encode (response ); err != nil {
326+ t .Fatalf ("Failed to encode certificate response: %v" , err )
327+ }
320328 } else if r .URL .Path == "/v1/certs/ca" && r .Method == http .MethodGet {
321329 // Return mock CA certificate
322330 w .Header ().Set ("Content-Type" , "application/x-pem-file" )
323- w .Write ([]byte ("-----BEGIN CERTIFICATE-----\n MOCK_CA_DATA\n -----END CERTIFICATE-----" ))
331+ if _ , err := w .Write ([]byte ("-----BEGIN CERTIFICATE-----\n MOCK_CA_DATA\n -----END CERTIFICATE-----" )); err != nil {
332+ t .Fatalf ("Failed to write CA certificate: %v" , err )
333+ }
324334 } else {
325335 http .Error (w , "not found" , http .StatusNotFound )
326336 }
@@ -432,7 +442,7 @@ func TestService_removePIDFile(t *testing.T) {
432442 pidFile := filepath .Join (tmpDir , "test.pid" )
433443
434444 // Create PID file
435- err := os .WriteFile (pidFile , []byte ("12345\n " ), 0644 )
445+ err := os .WriteFile (pidFile , []byte ("12345\n " ), 0o600 )
436446 if err != nil {
437447 t .Fatalf ("Failed to create test PID file: %v" , err )
438448 }
0 commit comments