From 39d99af2b432a08315e05b0dc683f91866eb474c Mon Sep 17 00:00:00 2001 From: Steve Shaw Date: Mon, 29 Mar 2021 22:58:56 -0400 Subject: [PATCH] Fixing bird2.x status parsing --- bird/parser.go | 6 +++--- bird/parser_test.go | 46 +++++++++++++++++++++++++++++++++++++++++++++ test/status1.sample | 7 +++++++ test/status2.sample | 7 +++++++ 4 files changed, 63 insertions(+), 3 deletions(-) create mode 100644 test/status1.sample create mode 100644 test/status2.sample diff --git a/bird/parser.go b/bird/parser.go index d64797e..48fdc84 100644 --- a/bird/parser.go +++ b/bird/parser.go @@ -62,9 +62,9 @@ func init() { regex.status.startLine = regexp.MustCompile(`^BIRD\s([0-9\.]+)\s*$`) regex.status.routerID = regexp.MustCompile(`^Router\sID\sis\s([0-9\.]+)\s*$`) - regex.status.currentServer = regexp.MustCompile(`^Current\sserver\stime\sis\s([0-9\-]+\s[0-9\:]+)\s*$`) - regex.status.lastReboot = regexp.MustCompile(`^Last\sreboot\son\s([0-9\-]+\s[0-9\:]+)\s*$`) - regex.status.lastReconfig = regexp.MustCompile(`^Last\sreconfiguration\son\s([0-9\-]+\s[0-9\:]+)\s*$`) + regex.status.currentServer = regexp.MustCompile(`^Current\sserver\stime\sis\s([0-9\-]+\s[0-9\:\.]+)\s*$`) + regex.status.lastReboot = regexp.MustCompile(`^Last\sreboot\son\s([0-9\-]+\s[0-9\:\.]+)\s*$`) + regex.status.lastReconfig = regexp.MustCompile(`^Last\sreconfiguration\son\s([0-9\-]+\s[0-9\:\.]+)\s*$`) regex.symbols.keyRx = regexp.MustCompile(`^([^\s]+)\s+(.+)\s*$`) diff --git a/bird/parser_test.go b/bird/parser_test.go index 3272430..6645842 100644 --- a/bird/parser_test.go +++ b/bird/parser_test.go @@ -15,6 +15,52 @@ func openFile(filename string) (*os.File, error) { return os.Open(sample) } +func TestParseStatus(t *testing.T) { + tests := []struct { + file string + expected Parsed + }{ + // Test for bird1.x status + { + "status1.sample", + Parsed{ + "current_server": "2021-03-30 02:28:45", + "last_reboot": "2021-03-30 02:28:19", + "last_reconfig": "2021-03-30 02:28:19", + "message": "Daemon is up and running", + "router_id": "172.25.3.2", + "version": "1.6.6", + }, + }, + // Test for bird2.x status + { + "status2.sample", + Parsed{ + "current_server": "2021-03-30 02:23:32.330", + "last_reboot": "2021-03-30 01:58:07.850", + "last_reconfig": "2021-03-30 01:58:07.850", + "message": "Daemon is up and running", + "router_id": "172.25.3.2", + "version": "2.0.7", + }, + }, + } + + for _, test := range tests { + f, err := openFile(test.file) + if err != nil { + t.Error(err) + } + s := parseStatus(f) + f.Close() + status := s["status"].(Parsed) + if !reflect.DeepEqual(status, test.expected) { + t.Error("Parse status: ", status, "expected: ", test.expected) + } + + } +} + func TestParseBgpRoutes(t *testing.T) { inputs := []string{ diff --git a/test/status1.sample b/test/status1.sample new file mode 100644 index 0000000..5d8c9c2 --- /dev/null +++ b/test/status1.sample @@ -0,0 +1,7 @@ +BIRD 1.6.6 ready. +BIRD 1.6.6 +Router ID is 172.25.3.2 +Current server time is 2021-03-30 02:28:45 +Last reboot on 2021-03-30 02:28:19 +Last reconfiguration on 2021-03-30 02:28:19 +Daemon is up and running diff --git a/test/status2.sample b/test/status2.sample new file mode 100644 index 0000000..c3545af --- /dev/null +++ b/test/status2.sample @@ -0,0 +1,7 @@ +BIRD 2.0.7 ready. +BIRD 2.0.7 +Router ID is 172.25.3.2 +Current server time is 2021-03-30 02:23:32.330 +Last reboot on 2021-03-30 01:58:07.850 +Last reconfiguration on 2021-03-30 01:58:07.850 +Daemon is up and running