| 1 | package parser |
| 2 | |
| 3 | import ( |
| 4 | "testing" |
| 5 | "time" |
| 6 | |
| 7 | "github.com/czerwonk/bird_exporter/protocol" |
| 8 | "github.com/czerwonk/testutils/assert" |
| 9 | ) |
| 10 | |
| 11 | func TestEstablishedBgpOldTimeFormat(t *testing.T) { |
| 12 | overrideNowFunc(func() time.Time { |
| 13 | return time.Date(2018, 1, 1, 2, 0, 0, 0, time.UTC) |
| 14 | }) |
| 15 | |
| 16 | data := "foo BGP master up 1514768400 Established\ntest\nbar\n Routes: 12 imported, 1 filtered, 34 exported, 100 preferred\nxxx" |
| 17 | p := ParseProtocols([]byte(data), "4") |
| 18 | |
| 19 | x := p[0] |
| 20 | assert.StringEqual("name", "foo", x.Name, t) |
| 21 | assert.IntEqual("proto", int(protocol.BGP), int(x.Proto), t) |
| 22 | assert.IntEqual("established", 1, x.Up, t) |
| 23 | assert.Int64Equal("imported", 12, x.Imported, t) |
| 24 | assert.Int64Equal("exported", 34, x.Exported, t) |
| 25 | assert.Int64Equal("filtered", 1, x.Filtered, t) |
| 26 | assert.Int64Equal("preferred", 100, x.Preferred, t) |
| 27 | assert.StringEqual("ipVersion", "4", x.IPVersion, t) |
| 28 | assert.Int64Equal("uptime", 3600, int64(x.Uptime), t) |
| 29 | } |
| 30 | |
| 31 | func TestEstablishedBgpCurrentTimeFormat(t *testing.T) { |
| 32 | data := "foo BGP master up 00:01:00 Established\ntest\nbar\n Routes: 12 imported, 1 filtered, 34 exported, 100 preferred\nxxx" |
| 33 | p := ParseProtocols([]byte(data), "4") |
| 34 | assert.IntEqual("protocols", 1, len(p), t) |
| 35 | |
| 36 | x := p[0] |
| 37 | assert.StringEqual("name", "foo", x.Name, t) |
| 38 | assert.IntEqual("proto", int(protocol.BGP), int(x.Proto), t) |
| 39 | assert.IntEqual("established", 1, x.Up, t) |
| 40 | assert.Int64Equal("imported", 12, x.Imported, t) |
| 41 | assert.Int64Equal("exported", 34, x.Exported, t) |
| 42 | assert.Int64Equal("filtered", 1, x.Filtered, t) |
| 43 | assert.Int64Equal("preferred", 100, x.Preferred, t) |
| 44 | assert.StringEqual("ipVersion", "4", x.IPVersion, t) |
| 45 | assert.IntEqual("uptime", 60, x.Uptime, t) |
| 46 | } |
| 47 | |
| 48 | func TestEstablishedBgpIsoLongTimeFormat(t *testing.T) { |
| 49 | overrideNowFunc(func() time.Time { |
| 50 | return time.Date(2018, 1, 1, 2, 0, 0, 0, time.Local) |
| 51 | }) |
| 52 | |
| 53 | data := "foo BGP master up 2018-01-01 01:00:00 Established\ntest\nbar\n Routes: 12 imported, 1 filtered, 34 exported, 100 preferred\nxxx" |
| 54 | p := ParseProtocols([]byte(data), "4") |
| 55 | |
| 56 | assert.IntEqual("protocols", 1, len(p), t) |
| 57 | |
| 58 | x := p[0] |
| 59 | assert.StringEqual("name", "foo", x.Name, t) |
| 60 | assert.IntEqual("proto", int(protocol.BGP), int(x.Proto), t) |
| 61 | assert.IntEqual("established", 1, x.Up, t) |
| 62 | assert.Int64Equal("imported", 12, x.Imported, t) |
| 63 | assert.Int64Equal("exported", 34, x.Exported, t) |
| 64 | assert.Int64Equal("filtered", 1, x.Filtered, t) |
| 65 | assert.Int64Equal("preferred", 100, x.Preferred, t) |
| 66 | assert.StringEqual("ipVersion", "4", x.IPVersion, t) |
| 67 | assert.Int64Equal("uptime", 3600, int64(x.Uptime), t) |
| 68 | } |
| 69 | |
| 70 | func TestIpv6BGP(t *testing.T) { |
| 71 | data := "foo BGP master up 00:01:00 Established\ntest\nbar\n Routes: 12 imported, 1 filtered, 34 exported, 100 preferred\nxxx" |
| 72 | p := ParseProtocols([]byte(data), "6") |
| 73 | assert.IntEqual("protocols", 1, len(p), t) |
| 74 | |
| 75 | x := p[0] |
| 76 | assert.StringEqual("ipVersion", "6", x.IPVersion, t) |
| 77 | } |
| 78 | |
| 79 | func TestActiveBGP(t *testing.T) { |
| 80 | data := "bar BGP master start 2016-01-01 Active\ntest\nbar" |
| 81 | p := ParseProtocols([]byte(data), "4") |
| 82 | assert.IntEqual("protocols", 1, len(p), t) |
| 83 | |
| 84 | x := p[0] |
| 85 | assert.StringEqual("name", "bar", x.Name, t) |
| 86 | assert.IntEqual("proto", int(protocol.BGP), int(x.Proto), t) |
| 87 | assert.IntEqual("established", 0, x.Up, t) |
| 88 | assert.IntEqual("imported", 0, int(x.Imported), t) |
| 89 | assert.IntEqual("exported", 0, int(x.Exported), t) |
| 90 | assert.StringEqual("ipVersion", "4", x.IPVersion, t) |
| 91 | assert.IntEqual("uptime", 0, int(x.Uptime), t) |
| 92 | } |
| 93 | |
| 94 | func Test2BGPSessions(t *testing.T) { |
| 95 | data := "foo BGP master up 00:01:00 Established\ntest\n Routes: 12 imported, 1 filtered, 34 exported, 100 preferred\nbar BGP master start 2016-01-01 Active\nxxx" |
| 96 | p := ParseProtocols([]byte(data), "4") |
| 97 | assert.IntEqual("protocols", 2, len(p), t) |
| 98 | } |
| 99 | |
| 100 | func TestUpdateAndWithdrawCounts(t *testing.T) { |
| 101 | data := "foo BGP master up 00:01:00 Established\ntest\n" + |
| 102 | " Routes: 12 imported, 1 filtered, 34 exported, 100 preferred\n" + |
| 103 | " Route change stats: received rejected filtered ignored accepted\n" + |
| 104 | " Import updates: 1 2 3 4 5\n" + |
| 105 | " Import withdraws: 6 7 8 9 10\n" + |
| 106 | " Export updates: 11 12 13 14 15\n" + |
| 107 | " Export withdraws: 16 17 18 19 ---" |
| 108 | p := ParseProtocols([]byte(data), "4") |
| 109 | x := p[0] |
| 110 | |
| 111 | assert.Int64Equal("import updates received", 1, x.ImportUpdates.Received, t) |
| 112 | assert.Int64Equal("import updates rejected", 2, x.ImportUpdates.Rejected, t) |
| 113 | assert.Int64Equal("import updates filtered", 3, x.ImportUpdates.Filtered, t) |
| 114 | assert.Int64Equal("import updates ignored", 4, x.ImportUpdates.Ignored, t) |
| 115 | assert.Int64Equal("import updates accepted", 5, x.ImportUpdates.Accepted, t) |
| 116 | assert.Int64Equal("import withdraws received", 6, x.ImportWithdraws.Received, t) |
| 117 | assert.Int64Equal("import withdraws rejected", 7, x.ImportWithdraws.Rejected, t) |
| 118 | assert.Int64Equal("import withdraws filtered", 8, x.ImportWithdraws.Filtered, t) |
| 119 | assert.Int64Equal("import withdraws ignored", 9, x.ImportWithdraws.Ignored, t) |
| 120 | assert.Int64Equal("import withdraws accepted", 10, x.ImportWithdraws.Accepted, t) |
| 121 | assert.Int64Equal("export updates received", 11, x.ExportUpdates.Received, t) |
| 122 | assert.Int64Equal("export updates rejected", 12, x.ExportUpdates.Rejected, t) |
| 123 | assert.Int64Equal("export updates filtered", 13, x.ExportUpdates.Filtered, t) |
| 124 | assert.Int64Equal("export updates ignored", 14, x.ExportUpdates.Ignored, t) |
| 125 | assert.Int64Equal("export updates accepted", 15, x.ExportUpdates.Accepted, t) |
| 126 | assert.Int64Equal("export withdraws received", 16, x.ExportWithdraws.Received, t) |
| 127 | assert.Int64Equal("export withdraws rejected", 17, x.ExportWithdraws.Rejected, t) |
| 128 | assert.Int64Equal("export withdraws filtered", 18, x.ExportWithdraws.Filtered, t) |
| 129 | assert.Int64Equal("export withdraws ignored", 19, x.ExportWithdraws.Ignored, t) |
| 130 | assert.Int64Equal("export withdraws accepted", 0, x.ExportWithdraws.Accepted, t) |
| 131 | } |
| 132 | |
| 133 | func TestWithBird2(t *testing.T) { |
| 134 | data := "Name Proto Table State Since Info\n" + |
| 135 | "bgp1 BGP master up 1494926415\n" + |
| 136 | " Channel ipv6\n" + |
| 137 | " Routes: 1 imported, 2 filtered, 3 exported, 4 preferred\n" + |
| 138 | " Input filter: none\n" + |
| 139 | " Output filter: all\n" + |
| 140 | "\n" + |
| 141 | "direct1 Direct --- up 1513027903\n" + |
| 142 | " Channel ipv4\n" + |
| 143 | " State: UP\n" + |
| 144 | " Table: master4\n" + |
| 145 | " Preference: 240\n" + |
| 146 | " Input filter: ACCEPT\n" + |
| 147 | " Output filter: REJECT\n" + |
| 148 | " Routes: 12 imported, 1 filtered, 34 exported, 100 preferred\n" + |
| 149 | " Route change stats: received rejected filtered ignored accepted\n" + |
| 150 | " Import updates: 1 2 3 4 5\n" + |
| 151 | " Import withdraws: 6 7 8 9 10\n" + |
| 152 | " Export updates: 11 12 13 14 15\n" + |
| 153 | " Export withdraws: 16 17 18 19 ---\n" + |
| 154 | " Channel ipv6\n" + |
| 155 | " State: UP\n" + |
| 156 | " Table: master6\n" + |
| 157 | " Preference: 240\n" + |
| 158 | " Input filter: ACCEPT\n" + |
| 159 | " Output filter: REJECT\n" + |
| 160 | " Routes: 3 imported, 7 filtered, 5 exported, 13 preferred\n" + |
| 161 | " Route change stats: received rejected filtered ignored accepted\n" + |
| 162 | " Import updates: 20 21 22 23 24\n" + |
| 163 | " Import withdraws: 25 26 27 28 29\n" + |
| 164 | " Export updates: 30 31 32 33 34\n" + |
| 165 | " Export withdraws: 35 36 37 38 ---\n" + |
| 166 | "\n" + |
| 167 | "ospf1 OSPF master up 1494926415\n" + |
| 168 | " Channel ipv4\n" + |
| 169 | " Routes: 4 imported, 3 filtered, 2 exported, 1 preferred\n" + |
| 170 | "\n" |
| 171 | |
| 172 | p := ParseProtocols([]byte(data), "") |
| 173 | assert.IntEqual("protocols", 4, len(p), t) |
| 174 | |
| 175 | x := p[0] |
| 176 | assert.StringEqual("BGP ipv6 name", "bgp1", x.Name, t) |
| 177 | assert.IntEqual("BGP ipv6 proto", int(protocol.BGP), int(x.Proto), t) |
| 178 | assert.StringEqual("BGP ipv6 ip version", "6", x.IPVersion, t) |
| 179 | assert.Int64Equal("BGP ipv6 imported", 1, x.Imported, t) |
| 180 | assert.Int64Equal("BGP ipv6 exported", 3, x.Exported, t) |
| 181 | assert.Int64Equal("BGP ipv6 filtered", 2, x.Filtered, t) |
| 182 | assert.Int64Equal("BGP ipv6 preferred", 4, x.Preferred, t) |
| 183 | assert.StringEqual("BGP import filter", "none", x.ImportFilter, t) |
| 184 | assert.StringEqual("BGP export filter", "all", x.ExportFilter, t) |
| 185 | |
| 186 | x = p[1] |
| 187 | assert.StringEqual("Direct ipv4 name", "direct1", x.Name, t) |
| 188 | assert.IntEqual("Direct ipv4 proto", int(protocol.Direct), int(x.Proto), t) |
| 189 | assert.StringEqual("Direct ipv4 ip version", "4", x.IPVersion, t) |
| 190 | assert.Int64Equal("Direct ipv4 imported", 12, x.Imported, t) |
| 191 | assert.Int64Equal("Direct ipv4 exported", 34, x.Exported, t) |
| 192 | assert.Int64Equal("Direct ipv4 filtered", 1, x.Filtered, t) |
| 193 | assert.Int64Equal("Direct ipv4 preferred", 100, x.Preferred, t) |
| 194 | assert.Int64Equal("Direct ipv4 import updates received", 1, x.ImportUpdates.Received, t) |
| 195 | assert.Int64Equal("Direct ipv4 import updates rejected", 2, x.ImportUpdates.Rejected, t) |
| 196 | assert.Int64Equal("Direct ipv4 import updates filtered", 3, x.ImportUpdates.Filtered, t) |
| 197 | assert.Int64Equal("Direct ipv4 import updates ignored", 4, x.ImportUpdates.Ignored, t) |
| 198 | assert.Int64Equal("Direct ipv4 import updates accepted", 5, x.ImportUpdates.Accepted, t) |
| 199 | assert.Int64Equal("Direct ipv4 import withdraws received", 6, x.ImportWithdraws.Received, t) |
| 200 | assert.Int64Equal("Direct ipv4 import withdraws rejected", 7, x.ImportWithdraws.Rejected, t) |
| 201 | assert.Int64Equal("Direct ipv4 import withdraws filtered", 8, x.ImportWithdraws.Filtered, t) |
| 202 | assert.Int64Equal("Direct ipv4 import withdraws ignored", 9, x.ImportWithdraws.Ignored, t) |
| 203 | assert.Int64Equal("Direct ipv4 import withdraws accepted", 10, x.ImportWithdraws.Accepted, t) |
| 204 | assert.Int64Equal("Direct ipv4 export updates received", 11, x.ExportUpdates.Received, t) |
| 205 | assert.Int64Equal("Direct ipv4 export updates rejected", 12, x.ExportUpdates.Rejected, t) |
| 206 | assert.Int64Equal("Direct ipv4 export updates filtered", 13, x.ExportUpdates.Filtered, t) |
| 207 | assert.Int64Equal("Direct ipv4 export updates ignored", 14, x.ExportUpdates.Ignored, t) |
| 208 | assert.Int64Equal("Direct ipv4 export updates accepted", 15, x.ExportUpdates.Accepted, t) |
| 209 | assert.Int64Equal("Direct ipv4 export withdraws received", 16, x.ExportWithdraws.Received, t) |
| 210 | assert.Int64Equal("Direct ipv4 export withdraws rejected", 17, x.ExportWithdraws.Rejected, t) |
| 211 | assert.Int64Equal("Direct ipv4 export withdraws filtered", 18, x.ExportWithdraws.Filtered, t) |
| 212 | assert.Int64Equal("Direct ipv4 export withdraws ignored", 19, x.ExportWithdraws.Ignored, t) |
| 213 | assert.Int64Equal("Direct ipv4 export withdraws accepted", 0, x.ExportWithdraws.Accepted, t) |
| 214 | |
| 215 | x = p[2] |
| 216 | assert.StringEqual("Direct ipv6 name", "direct1", x.Name, t) |
| 217 | assert.IntEqual("Direct ipv6 proto", int(protocol.Direct), int(x.Proto), t) |
| 218 | assert.StringEqual("Direct ipv6 ip version", "6", x.IPVersion, t) |
| 219 | assert.Int64Equal("Direct ipv6 imported", 3, x.Imported, t) |
| 220 | assert.Int64Equal("Direct ipv6 exported", 5, x.Exported, t) |
| 221 | assert.Int64Equal("Direct ipv6 filtered", 7, x.Filtered, t) |
| 222 | assert.Int64Equal("Direct ipv6 preferred", 13, x.Preferred, t) |
| 223 | assert.Int64Equal("Direct ipv6 import updates received", 20, x.ImportUpdates.Received, t) |
| 224 | assert.Int64Equal("Direct ipv6 import updates rejected", 21, x.ImportUpdates.Rejected, t) |
| 225 | assert.Int64Equal("Direct ipv6 import updates filtered", 22, x.ImportUpdates.Filtered, t) |
| 226 | assert.Int64Equal("Direct ipv6 import updates ignored", 23, x.ImportUpdates.Ignored, t) |
| 227 | assert.Int64Equal("Direct ipv6 import updates accepted", 24, x.ImportUpdates.Accepted, t) |
| 228 | assert.Int64Equal("Direct ipv6 import withdraws received", 25, x.ImportWithdraws.Received, t) |
| 229 | assert.Int64Equal("Direct ipv6 import withdraws rejected", 26, x.ImportWithdraws.Rejected, t) |
| 230 | assert.Int64Equal("Direct ipv6 import withdraws filtered", 27, x.ImportWithdraws.Filtered, t) |
| 231 | assert.Int64Equal("Direct ipv6 import withdraws ignored", 28, x.ImportWithdraws.Ignored, t) |
| 232 | assert.Int64Equal("Direct ipv6 import withdraws accepted", 29, x.ImportWithdraws.Accepted, t) |
| 233 | assert.Int64Equal("Direct ipv6 export updates received", 30, x.ExportUpdates.Received, t) |
| 234 | assert.Int64Equal("Direct ipv6 export updates rejected", 31, x.ExportUpdates.Rejected, t) |
| 235 | assert.Int64Equal("Direct ipv6 export updates filtered", 32, x.ExportUpdates.Filtered, t) |
| 236 | assert.Int64Equal("Direct ipv6 export updates ignored", 33, x.ExportUpdates.Ignored, t) |
| 237 | assert.Int64Equal("Direct ipv6 export updates accepted", 34, x.ExportUpdates.Accepted, t) |
| 238 | assert.Int64Equal("Direct ipv6 export withdraws received", 35, x.ExportWithdraws.Received, t) |
| 239 | assert.Int64Equal("Direct ipv6 export withdraws rejected", 36, x.ExportWithdraws.Rejected, t) |
| 240 | assert.Int64Equal("Direct ipv6 export withdraws filtered", 37, x.ExportWithdraws.Filtered, t) |
| 241 | assert.Int64Equal("Direct ipv6 export withdraws ignored", 38, x.ExportWithdraws.Ignored, t) |
| 242 | assert.Int64Equal("Direct ipv6 export withdraws accepted", 0, x.ExportWithdraws.Accepted, t) |
| 243 | |
| 244 | x = p[3] |
| 245 | assert.StringEqual("OSPF ipv4 name", "ospf1", x.Name, t) |
| 246 | assert.IntEqual("OSPF ipv4 proto", int(protocol.OSPF), int(x.Proto), t) |
| 247 | assert.StringEqual("OSPF ipv4 ip version", "4", x.IPVersion, t) |
| 248 | assert.Int64Equal("OSPF ipv4 imported", 4, x.Imported, t) |
| 249 | assert.Int64Equal("OSPF ipv4 exported", 2, x.Exported, t) |
| 250 | assert.Int64Equal("OSPF ipv4 filtered", 3, x.Filtered, t) |
| 251 | assert.Int64Equal("OSPF ipv4 preferred", 1, x.Preferred, t) |
| 252 | } |
| 253 | |
| 254 | func TestOSPFOldTimeFormat(t *testing.T) { |
| 255 | data := "ospf1 OSPF master up 1481973060 Running\ntest\nbar\n Routes: 12 imported, 34 exported, 100 preferred\nxxx" |
| 256 | p := ParseProtocols([]byte(data), "4") |
| 257 | assert.IntEqual("protocols", 1, len(p), t) |
| 258 | |
| 259 | x := p[0] |
| 260 | assert.StringEqual("name", "ospf1", x.Name, t) |
| 261 | assert.IntEqual("proto", int(protocol.OSPF), int(x.Proto), t) |
| 262 | assert.IntEqual("up", 1, x.Up, t) |
| 263 | assert.Int64Equal("imported", 12, x.Imported, t) |
| 264 | assert.Int64Equal("exported", 34, x.Exported, t) |
| 265 | assert.Int64Equal("preferred", 100, x.Preferred, t) |
| 266 | assert.StringEqual("ipVersion", "4", x.IPVersion, t) |
| 267 | } |
| 268 | |
| 269 | func TestOSPFCurrentTimeFormat(t *testing.T) { |
| 270 | data := "ospf1 OSPF master up 00:01:00 Running\ntest\nbar\n Routes: 12 imported, 34 exported, 100 preferred\nxxx" |
| 271 | p := ParseProtocols([]byte(data), "4") |
| 272 | assert.IntEqual("protocols", 1, len(p), t) |
| 273 | |
| 274 | x := p[0] |
| 275 | assert.StringEqual("name", "ospf1", x.Name, t) |
| 276 | assert.IntEqual("proto", int(protocol.OSPF), int(x.Proto), t) |
| 277 | assert.IntEqual("up", 1, x.Up, t) |
| 278 | assert.Int64Equal("imported", 12, x.Imported, t) |
| 279 | assert.Int64Equal("exported", 34, x.Exported, t) |
| 280 | assert.Int64Equal("preferred", 100, x.Preferred, t) |
| 281 | assert.StringEqual("ipVersion", "4", x.IPVersion, t) |
| 282 | assert.IntEqual("uptime", 60, x.Uptime, t) |
| 283 | } |
| 284 | |
| 285 | func TestRPKIUp(t *testing.T) { |
| 286 | data := "rpki1 RPKI --- up 2021-12-31 13:04:29 Established" |
| 287 | p := ParseProtocols([]byte(data), "4") |
| 288 | assert.IntEqual("protocols", 1, len(p), t) |
| 289 | |
| 290 | x := p[0] |
| 291 | assert.StringEqual("name", "rpki1", x.Name, t) |
| 292 | assert.IntEqual("proto", int(protocol.RPKI), int(x.Proto), t) |
| 293 | assert.StringEqual("state", "Established", x.State, t) |
| 294 | assert.IntEqual("up", 1, x.Up, t) |
| 295 | } |