Implement export to Reanimator format for asset tracking integration. Features: - New API endpoint: GET /api/export/reanimator - Web UI button "Экспорт Reanimator" in Configuration tab - Auto-detect CPU manufacturer (Intel/AMD/ARM/Ampere) - Generate PCIe serial numbers if missing - Merge GPUs and NetworkAdapters into pcie_devices - Filter components without serial numbers - RFC3339 timestamp format - Full compliance with Reanimator specification Changes: - Add reanimator_models.go: data models for Reanimator format - Add reanimator_converter.go: conversion functions - Add reanimator_converter_test.go: unit tests - Add reanimator_integration_test.go: integration tests - Update handlers.go: add handleExportReanimator - Update server.go: register /api/export/reanimator route - Update index.html: add export button - Update CLAUDE.md: document export behavior - Add REANIMATOR_EXPORT.md: implementation summary Tests: All tests passing (15+ new tests) Format spec: example/docs/INTEGRATION_GUIDE.md Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
378 lines
8.0 KiB
Go
378 lines
8.0 KiB
Go
package exporter
|
|
|
|
import (
|
|
"testing"
|
|
"time"
|
|
|
|
"git.mchus.pro/mchus/logpile/internal/models"
|
|
)
|
|
|
|
func TestConvertToReanimator(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
input *models.AnalysisResult
|
|
wantErr bool
|
|
errMsg string
|
|
}{
|
|
{
|
|
name: "nil result",
|
|
input: nil,
|
|
wantErr: true,
|
|
errMsg: "no data available",
|
|
},
|
|
{
|
|
name: "no hardware",
|
|
input: &models.AnalysisResult{
|
|
Filename: "test.json",
|
|
},
|
|
wantErr: true,
|
|
errMsg: "no hardware data available",
|
|
},
|
|
{
|
|
name: "no board serial",
|
|
input: &models.AnalysisResult{
|
|
Filename: "test.json",
|
|
Hardware: &models.HardwareConfig{
|
|
BoardInfo: models.BoardInfo{},
|
|
},
|
|
},
|
|
wantErr: true,
|
|
errMsg: "board serial_number is required",
|
|
},
|
|
{
|
|
name: "valid minimal data",
|
|
input: &models.AnalysisResult{
|
|
Filename: "test.json",
|
|
SourceType: "api",
|
|
Protocol: "redfish",
|
|
TargetHost: "10.10.10.10",
|
|
CollectedAt: time.Date(2026, 2, 10, 15, 30, 0, 0, time.UTC),
|
|
Hardware: &models.HardwareConfig{
|
|
BoardInfo: models.BoardInfo{
|
|
Manufacturer: "Supermicro",
|
|
ProductName: "X12DPG-QT6",
|
|
SerialNumber: "TEST123",
|
|
},
|
|
},
|
|
},
|
|
wantErr: false,
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
result, err := ConvertToReanimator(tt.input)
|
|
if tt.wantErr {
|
|
if err == nil {
|
|
t.Errorf("expected error containing %q, got nil", tt.errMsg)
|
|
}
|
|
return
|
|
}
|
|
if err != nil {
|
|
t.Errorf("unexpected error: %v", err)
|
|
return
|
|
}
|
|
if result == nil {
|
|
t.Error("expected non-nil result")
|
|
return
|
|
}
|
|
if result.Hardware.Board.SerialNumber != tt.input.Hardware.BoardInfo.SerialNumber {
|
|
t.Errorf("board serial mismatch: got %q, want %q",
|
|
result.Hardware.Board.SerialNumber,
|
|
tt.input.Hardware.BoardInfo.SerialNumber)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestInferCPUManufacturer(t *testing.T) {
|
|
tests := []struct {
|
|
model string
|
|
want string
|
|
}{
|
|
{"INTEL(R) XEON(R) GOLD 6530", "Intel"},
|
|
{"Intel Core i9-12900K", "Intel"},
|
|
{"AMD EPYC 7763", "AMD"},
|
|
{"AMD Ryzen 9 5950X", "AMD"},
|
|
{"ARM Cortex-A78", "ARM"},
|
|
{"Ampere Altra Max", "Ampere"},
|
|
{"Unknown CPU Model", ""},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.model, func(t *testing.T) {
|
|
got := inferCPUManufacturer(tt.model)
|
|
if got != tt.want {
|
|
t.Errorf("inferCPUManufacturer(%q) = %q, want %q", tt.model, got, tt.want)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestGeneratePCIeSerialNumber(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
boardSerial string
|
|
slot string
|
|
bdf string
|
|
want string
|
|
}{
|
|
{
|
|
name: "with slot",
|
|
boardSerial: "TEST123",
|
|
slot: "PCIeCard1",
|
|
bdf: "0000:18:00.0",
|
|
want: "TEST123-PCIE-PCIeCard1",
|
|
},
|
|
{
|
|
name: "without slot, with bdf",
|
|
boardSerial: "TEST123",
|
|
slot: "",
|
|
bdf: "0000:18:00.0",
|
|
want: "TEST123-PCIE-0000-18-00-0",
|
|
},
|
|
{
|
|
name: "without slot and bdf",
|
|
boardSerial: "TEST123",
|
|
slot: "",
|
|
bdf: "",
|
|
want: "TEST123-PCIE-UNKNOWN",
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
got := generatePCIeSerialNumber(tt.boardSerial, tt.slot, tt.bdf)
|
|
if got != tt.want {
|
|
t.Errorf("generatePCIeSerialNumber() = %q, want %q", got, tt.want)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestInferStorageStatus(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
stor models.Storage
|
|
want string
|
|
}{
|
|
{
|
|
name: "present",
|
|
stor: models.Storage{
|
|
Present: true,
|
|
},
|
|
want: "OK",
|
|
},
|
|
{
|
|
name: "not present",
|
|
stor: models.Storage{
|
|
Present: false,
|
|
},
|
|
want: "Empty",
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
got := inferStorageStatus(tt.stor)
|
|
if got != tt.want {
|
|
t.Errorf("inferStorageStatus() = %q, want %q", got, tt.want)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestConvertCPUs(t *testing.T) {
|
|
cpus := []models.CPU{
|
|
{
|
|
Socket: 0,
|
|
Model: "INTEL(R) XEON(R) GOLD 6530",
|
|
Cores: 32,
|
|
Threads: 64,
|
|
FrequencyMHz: 2100,
|
|
MaxFreqMHz: 4000,
|
|
},
|
|
{
|
|
Socket: 1,
|
|
Model: "AMD EPYC 7763",
|
|
Cores: 64,
|
|
Threads: 128,
|
|
FrequencyMHz: 2450,
|
|
MaxFreqMHz: 3500,
|
|
},
|
|
}
|
|
|
|
result := convertCPUs(cpus)
|
|
|
|
if len(result) != 2 {
|
|
t.Fatalf("expected 2 CPUs, got %d", len(result))
|
|
}
|
|
|
|
if result[0].Manufacturer != "Intel" {
|
|
t.Errorf("expected Intel manufacturer for first CPU, got %q", result[0].Manufacturer)
|
|
}
|
|
|
|
if result[1].Manufacturer != "AMD" {
|
|
t.Errorf("expected AMD manufacturer for second CPU, got %q", result[1].Manufacturer)
|
|
}
|
|
|
|
if result[0].Status != "OK" {
|
|
t.Errorf("expected OK status, got %q", result[0].Status)
|
|
}
|
|
}
|
|
|
|
func TestConvertMemory(t *testing.T) {
|
|
memory := []models.MemoryDIMM{
|
|
{
|
|
Slot: "CPU0_C0D0",
|
|
Present: true,
|
|
SizeMB: 32768,
|
|
Type: "DDR5",
|
|
SerialNumber: "TEST-MEM-001",
|
|
Status: "OK",
|
|
},
|
|
{
|
|
Slot: "CPU0_C1D0",
|
|
Present: false,
|
|
},
|
|
}
|
|
|
|
result := convertMemory(memory)
|
|
|
|
if len(result) != 2 {
|
|
t.Fatalf("expected 2 memory modules, got %d", len(result))
|
|
}
|
|
|
|
if result[0].Status != "OK" {
|
|
t.Errorf("expected OK status for first module, got %q", result[0].Status)
|
|
}
|
|
|
|
if result[1].Status != "Empty" {
|
|
t.Errorf("expected Empty status for second module, got %q", result[1].Status)
|
|
}
|
|
}
|
|
|
|
func TestConvertStorage(t *testing.T) {
|
|
storage := []models.Storage{
|
|
{
|
|
Slot: "OB01",
|
|
Type: "NVMe",
|
|
Model: "INTEL SSDPF2KX076T1",
|
|
SerialNumber: "BTAX41900GF87P6DGN",
|
|
Present: true,
|
|
},
|
|
{
|
|
Slot: "OB02",
|
|
Type: "NVMe",
|
|
Model: "INTEL SSDPF2KX076T1",
|
|
SerialNumber: "", // No serial - should be skipped
|
|
Present: true,
|
|
},
|
|
}
|
|
|
|
result := convertStorage(storage)
|
|
|
|
if len(result) != 1 {
|
|
t.Fatalf("expected 1 storage device (skipped one without serial), got %d", len(result))
|
|
}
|
|
|
|
if result[0].Status != "OK" {
|
|
t.Errorf("expected OK status, got %q", result[0].Status)
|
|
}
|
|
}
|
|
|
|
func TestConvertPCIeDevices(t *testing.T) {
|
|
hw := &models.HardwareConfig{
|
|
PCIeDevices: []models.PCIeDevice{
|
|
{
|
|
Slot: "PCIeCard1",
|
|
VendorID: 32902,
|
|
DeviceID: 2912,
|
|
BDF: "0000:18:00.0",
|
|
DeviceClass: "MassStorageController",
|
|
Manufacturer: "Intel",
|
|
PartNumber: "RSP3DD080F",
|
|
SerialNumber: "RAID-001",
|
|
},
|
|
{
|
|
Slot: "PCIeCard2",
|
|
DeviceClass: "NetworkController",
|
|
Manufacturer: "Mellanox",
|
|
SerialNumber: "", // Should be generated
|
|
},
|
|
},
|
|
GPUs: []models.GPU{
|
|
{
|
|
Slot: "GPU1",
|
|
Model: "NVIDIA A100",
|
|
Manufacturer: "NVIDIA",
|
|
SerialNumber: "GPU-001",
|
|
Status: "OK",
|
|
},
|
|
},
|
|
NetworkAdapters: []models.NetworkAdapter{
|
|
{
|
|
Slot: "NIC1",
|
|
Model: "ConnectX-6",
|
|
Vendor: "Mellanox",
|
|
Present: true,
|
|
SerialNumber: "NIC-001",
|
|
},
|
|
},
|
|
}
|
|
|
|
boardSerial := "TEST123"
|
|
result := convertPCIeDevices(hw, boardSerial)
|
|
|
|
// Should have: 2 PCIe devices + 1 GPU + 1 NIC = 4 total
|
|
if len(result) != 4 {
|
|
t.Fatalf("expected 4 PCIe devices total, got %d", len(result))
|
|
}
|
|
|
|
// Check that serial was generated for second PCIe device
|
|
if result[1].SerialNumber != "TEST123-PCIE-PCIeCard2" {
|
|
t.Errorf("expected generated serial TEST123-PCIE-PCIeCard2, got %q", result[1].SerialNumber)
|
|
}
|
|
|
|
// Check GPU was included
|
|
foundGPU := false
|
|
for _, dev := range result {
|
|
if dev.SerialNumber == "GPU-001" {
|
|
foundGPU = true
|
|
break
|
|
}
|
|
}
|
|
if !foundGPU {
|
|
t.Error("expected GPU to be included in PCIe devices")
|
|
}
|
|
}
|
|
|
|
func TestConvertPowerSupplies(t *testing.T) {
|
|
psus := []models.PSU{
|
|
{
|
|
Slot: "0",
|
|
Present: true,
|
|
Model: "GW-CRPS3000LW",
|
|
Vendor: "Great Wall",
|
|
WattageW: 3000,
|
|
SerialNumber: "PSU-001",
|
|
Status: "OK",
|
|
},
|
|
{
|
|
Slot: "1",
|
|
Present: false,
|
|
SerialNumber: "", // Not present, should be skipped
|
|
},
|
|
}
|
|
|
|
result := convertPowerSupplies(psus)
|
|
|
|
if len(result) != 1 {
|
|
t.Fatalf("expected 1 PSU (skipped empty), got %d", len(result))
|
|
}
|
|
|
|
if result[0].Status != "OK" {
|
|
t.Errorf("expected OK status, got %q", result[0].Status)
|
|
}
|
|
}
|