Get captured packets :param expected_count: expected number of packets to capture, if None, then self.test.packet_count_for_dst_pg_idx is used to lookup the expected count :param remark: remark printed into debug logs
(
self, expected_count=None, remark=None, timeout=None, filter_out_fn=is_ipv6_misc
)
| 317 | return output |
| 318 | |
| 319 | def get_capture( |
| 320 | self, expected_count=None, remark=None, timeout=None, filter_out_fn=is_ipv6_misc |
| 321 | ): |
| 322 | """Get captured packets |
| 323 | |
| 324 | :param expected_count: expected number of packets to capture, if None, |
| 325 | then self.test.packet_count_for_dst_pg_idx is |
| 326 | used to lookup the expected count |
| 327 | :param remark: remark printed into debug logs |
| 328 | :param timeout: how long to wait for packets, needed if packet capture |
| 329 | is independent of packet generator (e.g. packets |
| 330 | generated by timers) |
| 331 | :param filter_out_fn: filter applied to each packet, packets for which |
| 332 | the filter returns True are removed from capture |
| 333 | :returns: iterable packets |
| 334 | """ |
| 335 | remaining_time = timeout |
| 336 | capture = None |
| 337 | name = self.name if remark is None else "%s (%s)" % (self.name, remark) |
| 338 | based_on = "based on provided argument" |
| 339 | if expected_count is None: |
| 340 | expected_count = self.test.get_packet_count_for_if_idx(self.sw_if_index) |
| 341 | based_on = "based on stored packet_infos" |
| 342 | if expected_count == 0: |
| 343 | raise RuntimeError(f"Expected packet count for {name} is 0!") |
| 344 | self.test.logger.debug( |
| 345 | "Expecting to capture %s (%s) packets on %s" |
| 346 | % (expected_count, based_on, name) |
| 347 | ) |
| 348 | while remaining_time is None or remaining_time > 0: |
| 349 | before = time.time() |
| 350 | capture = self._get_capture(filter_out_fn) |
| 351 | elapsed_time = time.time() - before |
| 352 | if capture: |
| 353 | if len(capture.res) == expected_count: |
| 354 | # bingo, got the packets we expected |
| 355 | return capture |
| 356 | elif len(capture.res) > expected_count: |
| 357 | self.test.logger.info( |
| 358 | ppc( |
| 359 | f"Unexpected packets captured, got {len(capture.res)}, expected {expected_count}:", |
| 360 | capture, |
| 361 | ) |
| 362 | ) |
| 363 | break |
| 364 | else: |
| 365 | self.test.logger.debug( |
| 366 | "Partial capture containing %s " |
| 367 | "packets doesn't match expected " |
| 368 | "count %s (yet?)" % (len(capture.res), expected_count) |
| 369 | ) |
| 370 | elif expected_count == 0: |
| 371 | # bingo, got None as we expected - return empty capture |
| 372 | return PacketList() |
| 373 | if remaining_time is None: |
| 374 | break |
| 375 | remaining_time -= elapsed_time |
| 376 | if capture: |