Files
Redflag/aggregator-agent/internal/system/machine_id_fallback_test.go
jpetree331 db67049e92 fix(identity): D-1 machine ID deduplication fixes
- Remove unhashed 'unknown-' fallback from registration (F-D1-1)
  Registration aborts if GetMachineID() fails (no bad data)
- Add POST /admin/agents/:id/rebind-machine-id endpoint (F-D1-2)
  Admin can update stored machine ID after hardware change
- Delete dead example_integration.go with wrong usage (F-D1-3)
- Remove redundant Windows machineid.ID() retry (F-D1-4)
- Replace fmt.Printf with log.Printf in client.go (F-D1-5)

Operator note: agents registered with 'unknown-' machine IDs
must be rebound before upgrading. See D1_Fix_Implementation.md.

All tests pass. No regressions.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-29 09:53:43 -04:00

108 lines
3.2 KiB
Go

package system
// machine_id_fallback_test.go — Tests for machine ID registration fallback.
//
// F-D1-1 FIXED: Registration no longer uses unhashed "unknown-" fallback.
// GetMachineID() is trusted as canonical; registration aborts if it fails.
import (
"encoding/hex"
"os"
"path/filepath"
"strings"
"testing"
)
func TestRegistrationFallbackIsNotHashed(t *testing.T) {
// POST-FIX: "unknown-" fallback is gone from main.go.
mainPath := filepath.Join("..", "..", "cmd", "agent", "main.go")
content, err := os.ReadFile(mainPath)
if err != nil {
t.Fatalf("failed to read main.go: %v", err)
}
src := string(content)
if strings.Contains(src, `"unknown-"`) {
t.Error("[ERROR] [agent] [system] F-D1-1 NOT FIXED: 'unknown-' fallback still present")
}
t.Log("[INFO] [agent] [system] F-D1-1 FIXED: unhashed fallback removed from registration")
}
func TestRegistrationFallbackUsesCanonicalFunction(t *testing.T) {
// POST-FIX: Registration uses system.GetMachineID() with no inline fallback.
mainPath := filepath.Join("..", "..", "cmd", "agent", "main.go")
content, err := os.ReadFile(mainPath)
if err != nil {
t.Fatalf("failed to read main.go: %v", err)
}
src := string(content)
if strings.Contains(src, `"unknown-"`) {
t.Errorf("[ERROR] [agent] [system] registration still uses 'unknown-' fallback.\n" +
"F-D1-1: must use canonical GetMachineID() or abort.")
}
if !strings.Contains(src, "system.GetMachineID()") {
t.Error("[ERROR] [agent] [system] registration doesn't call system.GetMachineID()")
}
t.Log("[INFO] [agent] [system] F-D1-1 FIXED: registration uses canonical function only")
}
func TestMachineIDIsAlways64HexChars(t *testing.T) {
id, err := GetMachineID()
if err != nil {
t.Fatalf("GetMachineID failed: %v", err)
}
if len(id) != 64 {
t.Errorf("[ERROR] [agent] [system] machine ID is %d chars, expected 64", len(id))
}
if _, err := hex.DecodeString(id); err != nil {
t.Errorf("[ERROR] [agent] [system] machine ID is not valid hex: %v", err)
}
// Idempotency check
id2, _ := GetMachineID()
if id != id2 {
t.Error("[ERROR] [agent] [system] GetMachineID not idempotent")
}
t.Logf("[INFO] [agent] [system] canonical machine ID: %s... (64 hex chars)", id[:16])
}
func TestRegistrationAndRuntimeUseSameCodePath(t *testing.T) {
// POST-FIX: Both paths call system.GetMachineID(), no divergent fallback.
mainPath := filepath.Join("..", "..", "cmd", "agent", "main.go")
mainContent, err := os.ReadFile(mainPath)
if err != nil {
t.Fatalf("failed to read main.go: %v", err)
}
clientPath := filepath.Join("..", "client", "client.go")
clientContent, err := os.ReadFile(clientPath)
if err != nil {
t.Fatalf("failed to read client.go: %v", err)
}
mainSrc := string(mainContent)
clientSrc := string(clientContent)
if !strings.Contains(mainSrc, "system.GetMachineID()") {
t.Error("[ERROR] [agent] [system] main.go doesn't call system.GetMachineID()")
}
if !strings.Contains(clientSrc, "system.GetMachineID()") {
t.Error("[ERROR] [agent] [system] client.go doesn't call system.GetMachineID()")
}
if strings.Contains(mainSrc, `"unknown-"`) {
t.Errorf("[ERROR] [agent] [system] main.go has divergent 'unknown-' fallback")
}
t.Log("[INFO] [agent] [system] F-D1-1 FIXED: both paths use canonical GetMachineID()")
}