From 36e3db7c6cbde85379e410ab838c538df7a77344 Mon Sep 17 00:00:00 2001 From: krakenhavoc Date: Wed, 18 Mar 2026 14:02:26 +0000 Subject: [PATCH] fix: lint and tests --- .pre-commit-config.yaml | 14 +++++++++ cmd/probe/main.go | 4 ++- internal/config/config_test.go | 44 +++++++++++++++++++++------- internal/health/health.go | 6 ++-- internal/health/health_test.go | 12 ++++++-- internal/reporter/reporter_test.go | 10 +++---- internal/scanner/scanner_test.go | 4 +-- internal/scheduler/scheduler_test.go | 8 ++--- internal/state/state_test.go | 4 ++- 9 files changed, 76 insertions(+), 30 deletions(-) create mode 100644 .pre-commit-config.yaml diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 0000000..79c5993 --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1,14 @@ +repos: + - repo: https://github.com/golangci/golangci-lint + rev: v1.64.8 + hooks: + - id: golangci-lint + + - repo: local + hooks: + - id: go-test + name: go test + entry: go test ./... -race -count=1 + language: system + pass_filenames: false + types: [go] diff --git a/cmd/probe/main.go b/cmd/probe/main.go index 6897c22..0fb87ab 100644 --- a/cmd/probe/main.go +++ b/cmd/probe/main.go @@ -93,7 +93,9 @@ func main() { if healthSrv != nil { shutdownCtx, shutdownCancel := context.WithTimeout(context.Background(), 5*1e9) // 5s defer shutdownCancel() - healthSrv.Shutdown(shutdownCtx) + if err := healthSrv.Shutdown(shutdownCtx); err != nil { + logger.Error("health server shutdown error", "error", err) + } } logger.Info("krakenkey-probe stopped") diff --git a/internal/config/config_test.go b/internal/config/config_test.go index 5d7e50e..294608b 100644 --- a/internal/config/config_test.go +++ b/internal/config/config_test.go @@ -164,7 +164,9 @@ endpoints: - host: "example.com" port: 443 ` - os.WriteFile(cfgPath, []byte(yaml), 0o644) + if err := os.WriteFile(cfgPath, []byte(yaml), 0o644); err != nil { + t.Fatal(err) + } _, err := Load(cfgPath) if err == nil { @@ -183,7 +185,9 @@ endpoints: - host: "example.com" port: 443 ` - os.WriteFile(cfgPath, []byte(yaml), 0o644) + if err := os.WriteFile(cfgPath, []byte(yaml), 0o644); err != nil { + t.Fatal(err) + } _, err := Load(cfgPath) if err == nil { @@ -200,7 +204,9 @@ api: key: "kk_testkey" endpoints: [] ` - os.WriteFile(cfgPath, []byte(yaml), 0o644) + if err := os.WriteFile(cfgPath, []byte(yaml), 0o644); err != nil { + t.Fatal(err) + } _, err := Load(cfgPath) if err == nil { @@ -221,7 +227,9 @@ endpoints: - host: "example.com" port: 443 ` - os.WriteFile(cfgPath, []byte(yaml), 0o644) + if err := os.WriteFile(cfgPath, []byte(yaml), 0o644); err != nil { + t.Fatal(err) + } _, err := Load(cfgPath) if err == nil { @@ -242,7 +250,9 @@ endpoints: - host: "example.com" port: 443 ` - os.WriteFile(cfgPath, []byte(yaml), 0o644) + if err := os.WriteFile(cfgPath, []byte(yaml), 0o644); err != nil { + t.Fatal(err) + } _, err := Load(cfgPath) if err == nil { @@ -261,7 +271,9 @@ endpoints: - host: "example.com" port: 0 ` - os.WriteFile(cfgPath, []byte(yaml), 0o644) + if err := os.WriteFile(cfgPath, []byte(yaml), 0o644); err != nil { + t.Fatal(err) + } _, err := Load(cfgPath) if err == nil { @@ -282,7 +294,9 @@ endpoints: - host: "example.com" port: 443 ` - os.WriteFile(cfgPath, []byte(yaml), 0o644) + if err := os.WriteFile(cfgPath, []byte(yaml), 0o644); err != nil { + t.Fatal(err) + } _, err := Load(cfgPath) if err == nil { @@ -301,7 +315,9 @@ probe: mode: "hosted" id: "some-uuid" ` - os.WriteFile(cfgPath, []byte(yaml), 0o644) + if err := os.WriteFile(cfgPath, []byte(yaml), 0o644); err != nil { + t.Fatal(err) + } _, err := Load(cfgPath) if err == nil { @@ -320,7 +336,9 @@ probe: mode: "hosted" region: "us-east-1" ` - os.WriteFile(cfgPath, []byte(yaml), 0o644) + if err := os.WriteFile(cfgPath, []byte(yaml), 0o644); err != nil { + t.Fatal(err) + } _, err := Load(cfgPath) if err == nil { @@ -340,7 +358,9 @@ probe: id: "some-uuid" region: "us-east-1" ` - os.WriteFile(cfgPath, []byte(yaml), 0o644) + if err := os.WriteFile(cfgPath, []byte(yaml), 0o644); err != nil { + t.Fatal(err) + } cfg, err := Load(cfgPath) if err != nil { @@ -367,7 +387,9 @@ logging: level: "trace" format: "json" ` - os.WriteFile(cfgPath, []byte(yaml), 0o644) + if err := os.WriteFile(cfgPath, []byte(yaml), 0o644); err != nil { + t.Fatal(err) + } _, err := Load(cfgPath) if err == nil { diff --git a/internal/health/health.go b/internal/health/health.go index a193d56..c59473d 100644 --- a/internal/health/health.go +++ b/internal/health/health.go @@ -87,7 +87,7 @@ func (s *Server) handleHealthz(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/json") w.WriteHeader(http.StatusOK) - json.NewEncoder(w).Encode(status) + _ = json.NewEncoder(w).Encode(status) } func (s *Server) handleReadyz(w http.ResponseWriter, r *http.Request) { @@ -99,10 +99,10 @@ func (s *Server) handleReadyz(w http.ResponseWriter, r *http.Request) { if !ready { w.WriteHeader(http.StatusServiceUnavailable) - json.NewEncoder(w).Encode(map[string]string{"status": "not ready"}) + _ = json.NewEncoder(w).Encode(map[string]string{"status": "not ready"}) return } w.WriteHeader(http.StatusOK) - json.NewEncoder(w).Encode(map[string]string{"status": "ready"}) + _ = json.NewEncoder(w).Encode(map[string]string{"status": "ready"}) } diff --git a/internal/health/health_test.go b/internal/health/health_test.go index 9d5c995..359543d 100644 --- a/internal/health/health_test.go +++ b/internal/health/health_test.go @@ -56,7 +56,9 @@ func TestHealthzWithScanTimes(t *testing.T) { s.handleHealthz(rec, req) var status Status - json.NewDecoder(rec.Body).Decode(&status) + if err := json.NewDecoder(rec.Body).Decode(&status); err != nil { + t.Fatalf("decode error: %v", err) + } if status.LastScan != "2026-03-15T12:00:00Z" { t.Errorf("lastScan = %q, want %q", status.LastScan, "2026-03-15T12:00:00Z") @@ -79,7 +81,9 @@ func TestReadyzNotReady(t *testing.T) { } var body map[string]string - json.NewDecoder(rec.Body).Decode(&body) + if err := json.NewDecoder(rec.Body).Decode(&body); err != nil { + t.Fatalf("decode error: %v", err) + } if body["status"] != "not ready" { t.Errorf("status = %q, want %q", body["status"], "not ready") } @@ -99,7 +103,9 @@ func TestReadyzAfterReady(t *testing.T) { } var body map[string]string - json.NewDecoder(rec.Body).Decode(&body) + if err := json.NewDecoder(rec.Body).Decode(&body); err != nil { + t.Fatalf("decode error: %v", err) + } if body["status"] != "ready" { t.Errorf("status = %q, want %q", body["status"], "ready") } diff --git a/internal/reporter/reporter_test.go b/internal/reporter/reporter_test.go index b16cd22..16a648f 100644 --- a/internal/reporter/reporter_test.go +++ b/internal/reporter/reporter_test.go @@ -32,7 +32,7 @@ func TestRegisterSuccess(t *testing.T) { t.Error("User-Agent is empty") } - json.NewDecoder(r.Body).Decode(&received) + _ = json.NewDecoder(r.Body).Decode(&received) w.WriteHeader(http.StatusOK) })) defer srv.Close() @@ -69,7 +69,7 @@ func TestReportSuccess(t *testing.T) { if r.URL.Path != "/probes/report" { t.Errorf("path = %s, want /probes/report", r.URL.Path) } - json.NewDecoder(r.Body).Decode(&received) + _ = json.NewDecoder(r.Body).Decode(&received) w.WriteHeader(http.StatusOK) })) defer srv.Close() @@ -116,7 +116,7 @@ func TestReportSuccess(t *testing.T) { func TestReport401(t *testing.T) { srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusUnauthorized) - w.Write([]byte(`{"error":"invalid key"}`)) + _, _ = w.Write([]byte(`{"error":"invalid key"}`)) })) defer srv.Close() @@ -205,7 +205,7 @@ func TestFetchHostedConfig(t *testing.T) { }, Interval: "5m", } - json.NewEncoder(w).Encode(cfg) + _ = json.NewEncoder(w).Encode(cfg) })) defer srv.Close() @@ -242,5 +242,5 @@ func TestUserAgent(t *testing.T) { defer srv.Close() rep := New(srv.URL, "kk_testkey", "1.2.3", "linux", "amd64") - rep.Register(context.Background(), ProbeInfo{}) + _ = rep.Register(context.Background(), ProbeInfo{}) } diff --git a/internal/scanner/scanner_test.go b/internal/scanner/scanner_test.go index e85886d..0df3f96 100644 --- a/internal/scanner/scanner_test.go +++ b/internal/scanner/scanner_test.go @@ -76,7 +76,7 @@ func newTestTLSServer(t *testing.T, opts ...func(*x509.Certificate)) (net.Listen go func(c net.Conn) { defer c.Close() tlsConn := c.(*tls.Conn) - tlsConn.Handshake() + _ = tlsConn.Handshake() }(conn) } }() @@ -227,7 +227,7 @@ func TestScanEndpointTimeout(t *testing.T) { // Hold connection open, never respond go func(c net.Conn) { buf := make([]byte, 1024) - c.Read(buf) // block until closed + _, _ = c.Read(buf) // block until closed c.Close() }(conn) } diff --git a/internal/scheduler/scheduler_test.go b/internal/scheduler/scheduler_test.go index 63d70df..37db9e0 100644 --- a/internal/scheduler/scheduler_test.go +++ b/internal/scheduler/scheduler_test.go @@ -36,7 +36,7 @@ func TestRunCycleSelfHosted(t *testing.T) { } if r.URL.Path == "/probes/report" { var report reporter.ScanReport - json.NewDecoder(r.Body).Decode(&report) + _ = json.NewDecoder(r.Body).Decode(&report) if len(report.Results) > 0 { reportReceived.Store(true) } @@ -87,7 +87,7 @@ func TestRunCycleHostedMode(t *testing.T) { }, "interval": "5m", } - json.NewEncoder(w).Encode(cfg) + _ = json.NewEncoder(w).Encode(cfg) return } if r.URL.Path == "/probes/report" { @@ -205,11 +205,11 @@ func TestRunSetsReady(t *testing.T) { mux := http.NewServeMux() mux.HandleFunc("/readyz", func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusOK) - json.NewEncoder(w).Encode(map[string]string{"status": "ready"}) + _ = json.NewEncoder(w).Encode(map[string]string{"status": "ready"}) }) mux.ServeHTTP(rec, req) - json.NewDecoder(rec.Body).Decode(&body) + _ = json.NewDecoder(rec.Body).Decode(&body) if body["status"] != "ready" { t.Errorf("expected ready status, got %q", body["status"]) } diff --git a/internal/state/state_test.go b/internal/state/state_test.go index 75315fb..513bc7b 100644 --- a/internal/state/state_test.go +++ b/internal/state/state_test.go @@ -57,7 +57,9 @@ func TestLoadOrCreateReusesExistingID(t *testing.T) { RegisteredAt: "2026-01-01T00:00:00Z", } data, _ := json.Marshal(existing) - os.WriteFile(stateFile, data, 0o644) + if err := os.WriteFile(stateFile, data, 0o644); err != nil { + t.Fatal(err) + } cfg := &config.Config{ Probe: config.ProbeConfig{