|
| 1 | +package agent |
| 2 | + |
| 3 | +import ( |
| 4 | + "strings" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/NexusGPU/gpu-go/internal/api" |
| 8 | + "github.com/stretchr/testify/assert" |
| 9 | +) |
| 10 | + |
| 11 | +func TestBuildMetricsLineProtocol_SingleGPU(t *testing.T) { |
| 12 | + gpuMetrics := map[string]*api.GPUMetrics{ |
| 13 | + "gpu-001": { |
| 14 | + GPUID: "gpu-001", |
| 15 | + Utilization: 75.5, |
| 16 | + VRAMUsedMb: 8192, |
| 17 | + Temperature: 72.0, |
| 18 | + PowerUsageW: 250.0, |
| 19 | + PCIeRxKB: 1024.5, |
| 20 | + PCIeTxKB: 512.3, |
| 21 | + }, |
| 22 | + } |
| 23 | + gpuConfigs := []api.GPUStatus{ |
| 24 | + {GPUID: "gpu-001", Vendor: "nvidia", Model: "RTX 4090", VRAMMb: 24576}, |
| 25 | + } |
| 26 | + workers := []api.WorkerStatus{ |
| 27 | + {WorkerID: "w-1", Status: "running", Restarts: 2, Connections: []api.ConnectionInfo{{ClientIP: "1.2.3.4"}}}, |
| 28 | + } |
| 29 | + sysMetrics := &api.SystemMetrics{CPUUsage: 45.2, MemoryUsedMb: 8000, MemoryTotalMb: 16384} |
| 30 | + ts := int64(1700000000000) |
| 31 | + |
| 32 | + result := buildMetricsLineProtocol("agent-abc", "my-host", gpuMetrics, gpuConfigs, workers, sysMetrics, ts) |
| 33 | + |
| 34 | + lines := strings.Split(result, "\n") |
| 35 | + assert.Len(t, lines, 3) |
| 36 | + |
| 37 | + // GPU line |
| 38 | + assert.Contains(t, lines[0], "gpu_metrics,agent_id=agent-abc,gpu_id=gpu-001,model=RTX\\ 4090,vendor=nvidia") |
| 39 | + assert.Contains(t, lines[0], "utilization=75.50,vram_used_mb=8192i,vram_total_mb=24576i,temperature=72.0,power_usage_w=250.0,pcie_rx_kb=1024.5,pcie_tx_kb=512.3") |
| 40 | + assert.True(t, strings.HasSuffix(lines[0], " 1700000000000")) |
| 41 | + |
| 42 | + // System line |
| 43 | + assert.Contains(t, lines[1], "system_metrics,agent_id=agent-abc,hostname=my-host") |
| 44 | + assert.Contains(t, lines[1], "cpu_usage=45.20,memory_used_mb=8000i,memory_total_mb=16384i") |
| 45 | + |
| 46 | + // Worker line |
| 47 | + assert.Contains(t, lines[2], "worker_metrics,agent_id=agent-abc,worker_id=w-1") |
| 48 | + assert.Contains(t, lines[2], "status=1i,connections=1i,restarts=2i") |
| 49 | +} |
| 50 | + |
| 51 | +func TestBuildMetricsLineProtocol_MultipleGPUs(t *testing.T) { |
| 52 | + gpuMetrics := map[string]*api.GPUMetrics{ |
| 53 | + "gpu-001": {GPUID: "gpu-001", Utilization: 50.0, VRAMUsedMb: 4000, Temperature: 65.0, PowerUsageW: 200.0}, |
| 54 | + "gpu-002": {GPUID: "gpu-002", Utilization: 80.0, VRAMUsedMb: 6000, Temperature: 70.0, PowerUsageW: 300.0}, |
| 55 | + } |
| 56 | + gpuConfigs := []api.GPUStatus{ |
| 57 | + {GPUID: "gpu-001", Vendor: "nvidia", Model: "RTX 3090", VRAMMb: 24576}, |
| 58 | + {GPUID: "gpu-002", Vendor: "nvidia", Model: "RTX 4090", VRAMMb: 24576}, |
| 59 | + } |
| 60 | + |
| 61 | + result := buildMetricsLineProtocol("agent-1", "host1", gpuMetrics, gpuConfigs, nil, nil, 1700000000000) |
| 62 | + |
| 63 | + lines := strings.Split(result, "\n") |
| 64 | + assert.Len(t, lines, 2, "should have exactly 2 GPU metric lines") |
| 65 | + |
| 66 | + // Check both GPUs are present (map iteration order is non-deterministic) |
| 67 | + combined := result |
| 68 | + assert.Contains(t, combined, "gpu_id=gpu-001") |
| 69 | + assert.Contains(t, combined, "gpu_id=gpu-002") |
| 70 | + assert.Contains(t, combined, "model=RTX\\ 3090") |
| 71 | + assert.Contains(t, combined, "model=RTX\\ 4090") |
| 72 | +} |
| 73 | + |
| 74 | +func TestBuildMetricsLineProtocol_TagEscaping(t *testing.T) { |
| 75 | + gpuMetrics := map[string]*api.GPUMetrics{ |
| 76 | + "gpu=special": {GPUID: "gpu=special", Utilization: 10.0, VRAMUsedMb: 100, Temperature: 50.0, PowerUsageW: 100.0}, |
| 77 | + } |
| 78 | + gpuConfigs := []api.GPUStatus{ |
| 79 | + {GPUID: "gpu=special", Vendor: "vendor,test", Model: "Model With Spaces", VRAMMb: 8192}, |
| 80 | + } |
| 81 | + |
| 82 | + result := buildMetricsLineProtocol("agent,id=1", "host name", gpuMetrics, gpuConfigs, nil, nil, 1700000000000) |
| 83 | + |
| 84 | + assert.Contains(t, result, "agent_id=agent\\,id\\=1") |
| 85 | + assert.Contains(t, result, "gpu_id=gpu\\=special") |
| 86 | + assert.Contains(t, result, "model=Model\\ With\\ Spaces") |
| 87 | + assert.Contains(t, result, "vendor=vendor\\,test") |
| 88 | +} |
| 89 | + |
| 90 | +func TestBuildMetricsLineProtocol_EmptyMetrics(t *testing.T) { |
| 91 | + result := buildMetricsLineProtocol("agent-1", "host1", nil, nil, nil, nil, 1700000000000) |
| 92 | + assert.Empty(t, result) |
| 93 | +} |
| 94 | + |
| 95 | +func TestBuildMetricsLineProtocol_WorkerStatusMapping(t *testing.T) { |
| 96 | + workers := []api.WorkerStatus{ |
| 97 | + {WorkerID: "w-running", Status: "running"}, |
| 98 | + {WorkerID: "w-stopped", Status: "stopped"}, |
| 99 | + {WorkerID: "w-pending", Status: "pending"}, |
| 100 | + {WorkerID: "w-stopping", Status: "stopping"}, |
| 101 | + {WorkerID: "w-unknown", Status: "weird"}, |
| 102 | + } |
| 103 | + |
| 104 | + result := buildMetricsLineProtocol("agent-1", "host1", nil, nil, workers, nil, 1700000000000) |
| 105 | + |
| 106 | + lines := strings.Split(result, "\n") |
| 107 | + assert.Len(t, lines, 5) |
| 108 | + |
| 109 | + // Verify status integer mapping |
| 110 | + assert.Contains(t, lines[0], "worker_id=w-running") |
| 111 | + assert.Contains(t, lines[0], "status=1i") |
| 112 | + |
| 113 | + assert.Contains(t, lines[1], "worker_id=w-stopped") |
| 114 | + assert.Contains(t, lines[1], "status=0i") |
| 115 | + |
| 116 | + assert.Contains(t, lines[2], "worker_id=w-pending") |
| 117 | + assert.Contains(t, lines[2], "status=2i") |
| 118 | + |
| 119 | + assert.Contains(t, lines[3], "worker_id=w-stopping") |
| 120 | + assert.Contains(t, lines[3], "status=3i") |
| 121 | + |
| 122 | + assert.Contains(t, lines[4], "worker_id=w-unknown") |
| 123 | + assert.Contains(t, lines[4], "status=-1i") |
| 124 | +} |
| 125 | + |
| 126 | +func TestBuildMetricsLineProtocol_OnlySystemMetrics(t *testing.T) { |
| 127 | + sysMetrics := &api.SystemMetrics{CPUUsage: 25.0, MemoryUsedMb: 4096, MemoryTotalMb: 8192} |
| 128 | + |
| 129 | + result := buildMetricsLineProtocol("agent-1", "host1", nil, nil, nil, sysMetrics, 1700000000000) |
| 130 | + |
| 131 | + assert.Contains(t, result, "system_metrics") |
| 132 | + assert.NotContains(t, result, "gpu_metrics") |
| 133 | + assert.NotContains(t, result, "worker_metrics") |
| 134 | +} |
| 135 | + |
| 136 | +func TestBuildMetricsLineProtocol_TimestampFormat(t *testing.T) { |
| 137 | + gpuMetrics := map[string]*api.GPUMetrics{ |
| 138 | + "gpu-001": {GPUID: "gpu-001", Utilization: 50.0, VRAMUsedMb: 4000, Temperature: 65.0, PowerUsageW: 200.0}, |
| 139 | + } |
| 140 | + gpuConfigs := []api.GPUStatus{ |
| 141 | + {GPUID: "gpu-001", Vendor: "nvidia", Model: "RTX4090", VRAMMb: 24576}, |
| 142 | + } |
| 143 | + ts := int64(1700000000123) // millisecond precision |
| 144 | + |
| 145 | + result := buildMetricsLineProtocol("agent-1", "host1", gpuMetrics, gpuConfigs, nil, nil, ts) |
| 146 | + |
| 147 | + assert.True(t, strings.HasSuffix(result, "1700000000123"), "timestamp should be in milliseconds") |
| 148 | +} |
| 149 | + |
| 150 | +func TestEscapeTagValue(t *testing.T) { |
| 151 | + tests := []struct { |
| 152 | + input string |
| 153 | + expected string |
| 154 | + }{ |
| 155 | + {"simple", "simple"}, |
| 156 | + {"has space", "has\\ space"}, |
| 157 | + {"has,comma", "has\\,comma"}, |
| 158 | + {"has=equals", "has\\=equals"}, |
| 159 | + {"all three, = mixed", "all\\ three\\,\\ \\=\\ mixed"}, |
| 160 | + {"", ""}, |
| 161 | + } |
| 162 | + for _, tc := range tests { |
| 163 | + assert.Equal(t, tc.expected, escapeTagValue(tc.input), "escapeTagValue(%q)", tc.input) |
| 164 | + } |
| 165 | +} |
| 166 | + |
| 167 | +func TestWorkerStatusToInt(t *testing.T) { |
| 168 | + assert.Equal(t, 1, workerStatusToInt("running")) |
| 169 | + assert.Equal(t, 0, workerStatusToInt("stopped")) |
| 170 | + assert.Equal(t, 2, workerStatusToInt("pending")) |
| 171 | + assert.Equal(t, 3, workerStatusToInt("stopping")) |
| 172 | + assert.Equal(t, -1, workerStatusToInt("unknown")) |
| 173 | +} |
| 174 | + |
| 175 | +func TestBuildMetricsLineProtocol_GPUConfigMismatch(t *testing.T) { |
| 176 | + // GPU has metrics but no matching config — vendor/model should be empty |
| 177 | + gpuMetrics := map[string]*api.GPUMetrics{ |
| 178 | + "gpu-orphan": {GPUID: "gpu-orphan", Utilization: 30.0, VRAMUsedMb: 1000, Temperature: 55.0, PowerUsageW: 150.0}, |
| 179 | + } |
| 180 | + |
| 181 | + result := buildMetricsLineProtocol("agent-1", "host1", gpuMetrics, nil, nil, nil, 1700000000000) |
| 182 | + |
| 183 | + assert.Contains(t, result, "gpu_id=gpu-orphan") |
| 184 | + assert.Contains(t, result, "model=,vendor=") // empty but present |
| 185 | + assert.Contains(t, result, "vram_total_mb=0i") |
| 186 | +} |
0 commit comments