def start_packet_capture(self, band, log_path, pcap_fname): """Start packet capture for band. band = 2G starts tcpdump on 'mon0' interface. band = 5G starts tcpdump on 'mon1' interface. Args: band: '2g' or '2G' and '5g' or '5G'. log_path: test log path to save the pcap file. pcap_fname: name of the pcap file. Returns: pcap_proc: Process object of the tcpdump. """ band = band.upper() if band not in BAND_IFACE.keys() or band in self.pcap_properties: self.log.error("Invalid band or packet capture already running") return None pcap_name = '%s_%s.pcap' % (pcap_fname, band) pcap_fname = os.path.join(log_path, pcap_name) pcap_file = open(pcap_fname, 'w+b') tcpdump_cmd = 'tcpdump -i %s -w - -U 2>/dev/null' % (BAND_IFACE[band]) cmd = formatter.SshFormatter().format_command( tcpdump_cmd, None, self.ssh_settings, extra_flags={'-q': None}) pcap_proc = Process(cmd) pcap_proc.set_on_output_callback( lambda msg: pcap_file.write(msg), binary=True) pcap_proc.start() self.pcap_properties[band] = PcapProperties(pcap_proc, pcap_fname, pcap_file) return pcap_proc
def test_start_starts_listening_thread(self): """Tests that start starts the _exec_popen_loop function.""" process = Process('cmd') # Here we need the thread to start the process object. class FakeThreadImpl(FakeThread): def _on_start(self): process._process = mock.Mock() with self.patch('Thread', FakeThreadImpl): process.start() self.assertTrue(process._listening_thread.alive) self.assertEqual(process._listening_thread.target, process._exec_loop)
def test_start_raises_if_called_back_to_back(self): """Tests that start raises an exception if it has already been called prior. This is required to prevent references to processes and threads from being overwritten, potentially causing ACTS to hang.""" process = Process('cmd') # Here we need the thread to start the process object. class FakeThreadImpl(FakeThread): def _on_start(self): process._process = mock.Mock() with self.patch('Thread', FakeThreadImpl): process.start() expected_msg = 'Process has already started.' with self.assertRaisesRegex(ProcessError, expected_msg): process.start()