101 lines
2.6 KiB
Go
101 lines
2.6 KiB
Go
package schema
|
|
|
|
import (
|
|
"encoding/json"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestHardwareSnapshotMarshalsNewContractFields(t *testing.T) {
|
|
week := "2024-W07"
|
|
eventTime := "2026-03-15T14:03:11Z"
|
|
message := "Correctable ECC error threshold exceeded"
|
|
|
|
payload := HardwareIngestRequest{
|
|
CollectedAt: "2026-03-15T15:00:00Z",
|
|
Hardware: HardwareSnapshot{
|
|
Board: HardwareBoard{SerialNumber: "SRV-001"},
|
|
CPUs: []HardwareCPU{
|
|
{
|
|
HardwareComponentStatus: HardwareComponentStatus{
|
|
ManufacturedYearWeek: &week,
|
|
},
|
|
},
|
|
},
|
|
EventLogs: []HardwareEventLog{
|
|
{
|
|
Source: "bmc",
|
|
EventTime: &eventTime,
|
|
Message: message,
|
|
},
|
|
},
|
|
},
|
|
}
|
|
|
|
data, err := json.Marshal(payload)
|
|
if err != nil {
|
|
t.Fatalf("marshal: %v", err)
|
|
}
|
|
text := string(data)
|
|
if !strings.Contains(text, `"manufactured_year_week":"2024-W07"`) {
|
|
t.Fatalf("missing manufactured_year_week: %s", text)
|
|
}
|
|
if !strings.Contains(text, `"event_logs":[{"source":"bmc","event_time":"2026-03-15T14:03:11Z","message":"Correctable ECC error threshold exceeded"}]`) {
|
|
t.Fatalf("missing event_logs payload: %s", text)
|
|
}
|
|
}
|
|
|
|
func TestHardwareSnapshotMarshalsStorageTelemetryFields(t *testing.T) {
|
|
powerOnHours := int64(12450)
|
|
writtenBytes := int64(9876543210)
|
|
readBytes := int64(1234567890)
|
|
lifeRemainingPct := 91.0
|
|
logicalBlockSizeBytes := int64(512)
|
|
physicalBlockSizeBytes := int64(4096)
|
|
metadataBytesPerBlock := int64(8)
|
|
|
|
payload := HardwareIngestRequest{
|
|
CollectedAt: "2026-03-15T15:00:00Z",
|
|
Hardware: HardwareSnapshot{
|
|
Board: HardwareBoard{SerialNumber: "SRV-001"},
|
|
Storage: []HardwareStorage{
|
|
{
|
|
SerialNumber: stringPtr("DISK-001"),
|
|
Model: stringPtr("TestDisk"),
|
|
LogicalBlockSizeBytes: &logicalBlockSizeBytes,
|
|
PhysicalBlockSizeBytes: &physicalBlockSizeBytes,
|
|
MetadataBytesPerBlock: &metadataBytesPerBlock,
|
|
PowerOnHours: &powerOnHours,
|
|
WrittenBytes: &writtenBytes,
|
|
ReadBytes: &readBytes,
|
|
LifeRemainingPct: &lifeRemainingPct,
|
|
},
|
|
},
|
|
},
|
|
}
|
|
|
|
data, err := json.Marshal(payload)
|
|
if err != nil {
|
|
t.Fatalf("marshal: %v", err)
|
|
}
|
|
text := string(data)
|
|
for _, needle := range []string{
|
|
`"storage":[{`,
|
|
`"logical_block_size_bytes":512`,
|
|
`"physical_block_size_bytes":4096`,
|
|
`"metadata_bytes_per_block":8`,
|
|
`"power_on_hours":12450`,
|
|
`"written_bytes":9876543210`,
|
|
`"read_bytes":1234567890`,
|
|
`"life_remaining_pct":91`,
|
|
} {
|
|
if !strings.Contains(text, needle) {
|
|
t.Fatalf("missing %q in payload: %s", needle, text)
|
|
}
|
|
}
|
|
}
|
|
|
|
func stringPtr(v string) *string {
|
|
return &v
|
|
}
|