| 1 | package parser |
| 2 | |
| 3 | import ( |
| 4 | "testing" |
| 5 | "time" |
| 6 | ) |
| 7 | |
| 8 | func TestParseStatus(t *testing.T) { |
| 9 | sample := ` |
| 10 | BIRD 2.17.1 ready. |
| 11 | BIRD 2.17.1 |
| 12 | Router ID is 10.72.0.164 |
| 13 | Hostname is store-blob-pp014 |
| 14 | Current server time is 2025-08-21 10:27:57.219 |
| 15 | Last reboot on 2025-07-31 14:23:03.232 |
| 16 | Last reconfiguration on 2025-08-07 09:53:19.531 |
| 17 | Daemon is up and running |
| 18 | ` |
| 19 | |
| 20 | s := ParseStatus([]byte(sample)) |
| 21 | |
| 22 | if s.Version != "2.17.1" { |
| 23 | t.Errorf("expected Version=2.17.1, got %q", s.Version) |
| 24 | } |
| 25 | if s.RouterID != "10.72.0.164" { |
| 26 | t.Errorf("expected RouterID=10.72.0.164, got %q", s.RouterID) |
| 27 | } |
| 28 | |
| 29 | expectedServerTime := time.Date(2025, 8, 21, 10, 27, 57, 219000000, time.UTC) |
| 30 | if !s.ServerTime.Equal(expectedServerTime) { |
| 31 | t.Errorf("expected ServerTime=%v, got %v", expectedServerTime, s.ServerTime) |
| 32 | } |
| 33 | |
| 34 | expectedLastReboot := time.Date(2025, 7, 31, 14, 23, 3, 232000000, time.UTC) |
| 35 | if !s.LastReboot.Equal(expectedLastReboot) { |
| 36 | t.Errorf("expected LastReboot=%v, got %v", expectedLastReboot, s.LastReboot) |
| 37 | } |
| 38 | |
| 39 | expectedLastReconfig := time.Date(2025, 8, 7, 9, 53, 19, 531000000, time.UTC) |
| 40 | if !s.LastReconfig.Equal(expectedLastReconfig) { |
| 41 | t.Errorf("expected LastReconfig=%v, got %v", expectedLastReconfig, s.LastReconfig) |
| 42 | } |
| 43 | |
| 44 | if !s.DaemonUp { |
| 45 | t.Errorf("expected DaemonUp=true, got false") |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | func TestParseStatusMissingFields(t *testing.T) { |
| 50 | sample := ` |
| 51 | BIRD 2.17.1 |
| 52 | Daemon is down |
| 53 | ` |
| 54 | |
| 55 | s := ParseStatus([]byte(sample)) |
| 56 | |
| 57 | if s.Version != "2.17.1" { |
| 58 | t.Errorf("expected Version=2.17.1, got %q", s.Version) |
| 59 | } |
| 60 | if s.DaemonUp { |
| 61 | t.Errorf("expected DaemonUp=false, got true") |
| 62 | } |
| 63 | |
| 64 | // Other fields should be zero-values |
| 65 | if !s.ServerTime.IsZero() { |
| 66 | t.Errorf("expected ServerTime zero, got %v", s.ServerTime) |
| 67 | } |
| 68 | if !s.LastReboot.IsZero() { |
| 69 | t.Errorf("expected LastReboot zero, got %v", s.LastReboot) |
| 70 | } |
| 71 | if !s.LastReconfig.IsZero() { |
| 72 | t.Errorf("expected LastReconfig zero, got %v", s.LastReconfig) |
| 73 | } |
| 74 | if s.RouterID != "" { |
| 75 | t.Errorf("expected RouterID empty, got %q", s.RouterID) |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | func TestParseStatusMalformedTime(t *testing.T) { |
| 80 | sample := ` |
| 81 | BIRD 2.17.1 |
| 82 | Current server time is not-a-time |
| 83 | Daemon is up |
| 84 | ` |
| 85 | |
| 86 | s := ParseStatus([]byte(sample)) |
| 87 | |
| 88 | if !s.DaemonUp { |
| 89 | t.Errorf("expected DaemonUp=true, got false") |
| 90 | } |
| 91 | if !s.ServerTime.IsZero() { |
| 92 | t.Errorf("expected ServerTime zero due to parse error, got %v", s.ServerTime) |
| 93 | } |
| 94 | } |