package probes import ( "strings" "testing" ) // TestParseNetDev_RealSample exercises parseNetDev against a synthetic // /proc/net/dev fixture with the full 16-column layout. Confirms the // loopback interface is dropped, headers are skipped, and each of the // six curated counters lands in the right field. func TestParseNetDev_RealSample(t *testing.T) { // Columns after "iface:": // 0 rx_bytes 1 rx_packets 2 rx_errs 3 rx_drop // 4 fifo 5 frame 6 compressed 7 multicast // 8 tx_bytes 9 tx_packets 10 tx_errs 11 tx_drop // 12 fifo 13 colls 14 carrier 15 compressed fixture := `Inter-| Receive | Transmit face |bytes packets errs drop fifo frame compressed multicast|bytes packets errs drop fifo colls carrier compressed lo: 1000000 10000 0 0 0 0 0 0 1000000 10000 0 0 0 0 0 0 eth0: 50000000 100000 7 12 0 0 0 0 40000000 90000 3 5 0 0 0 0 eth1: 12345 200 0 0 0 0 0 0 54321 180 0 0 0 0 0 0 ` snaps := parseNetDev(strings.NewReader(fixture)) if len(snaps) != 2 { t.Fatalf("got %d snapshots, want 2 (lo should be dropped)", len(snaps)) } byIface := map[string]NetDevSnapshot{} for _, s := range snaps { byIface[s.Iface] = s } eth0, ok := byIface["eth0"] if !ok { t.Fatalf("eth0 missing from parsed snapshots") } if eth0.RxBytes != 50000000 { t.Errorf("eth0 RxBytes=%d, want 50000000", eth0.RxBytes) } if eth0.RxErrs != 7 { t.Errorf("eth0 RxErrs=%d, want 7", eth0.RxErrs) } if eth0.RxDrop != 12 { t.Errorf("eth0 RxDrop=%d, want 12", eth0.RxDrop) } if eth0.TxBytes != 40000000 { t.Errorf("eth0 TxBytes=%d, want 40000000", eth0.TxBytes) } if eth0.TxErrs != 3 { t.Errorf("eth0 TxErrs=%d, want 3", eth0.TxErrs) } if eth0.TxDrop != 5 { t.Errorf("eth0 TxDrop=%d, want 5", eth0.TxDrop) } if _, ok := byIface["lo"]; ok { t.Errorf("lo should have been filtered out") } } // TestParseNetDev_Empty: an empty reader returns no snapshots, not a // crash. Callers treat nil as "no data" and skip the delta step. func TestParseNetDev_Empty(t *testing.T) { snaps := parseNetDev(strings.NewReader("")) if len(snaps) != 0 { t.Errorf("got %d snapshots from empty reader, want 0", len(snaps)) } } // TestParseNetDev_MalformedRow skips rows that don't have the expected // 16 columns rather than panicking. A truncated line shouldn't hide the // good rows that follow. func TestParseNetDev_MalformedRow(t *testing.T) { fixture := `header line 1 header line 2 bad0: 123 456 eth0: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 ` snaps := parseNetDev(strings.NewReader(fixture)) if len(snaps) != 1 { t.Fatalf("got %d snapshots, want 1 (bad0 should be dropped)", len(snaps)) } if snaps[0].Iface != "eth0" { t.Errorf("got iface=%q, want eth0", snaps[0].Iface) } }