def test__requires_input_file(self): parser = ArgumentParser() add_arguments(parser) args = parser.parse_args([]) with ExpectedException(ActionScriptError, '.*Required argument: interface.*'): run(args)
def test__allows_file_input(self): with NamedTemporaryFile('wb') as f: parser = ArgumentParser() add_arguments(parser) f.write(BEACON_PCAP) f.flush() args = parser.parse_args(['--input-file', f.name]) output = io.StringIO() run(args, output=output)
def test__allows_pipe_input(self): parser = ArgumentParser() add_arguments(parser) args = parser.parse_args(['--input-file', '-']) output = io.StringIO() stdin = self.patch(beaconing_module.sys, 'stdin') stdin.return_value.fileno = Mock() fstat = self.patch(beaconing_module.os, 'fstat') fstat.return_value.st_mode = None stat = self.patch(beaconing_module.stat, 'S_ISFIFO') stat.return_value = True stdin_buffer = io.BytesIO(BEACON_PCAP) run(args, output=output, stdin_buffer=stdin_buffer)
def test__raises_systemexit_observe_beaconing_return_code(self): parser = ArgumentParser() add_arguments(parser) args = parser.parse_args(['eth0']) popen = self.patch(beaconing_module.subprocess, 'Popen') popen.return_value.poll = Mock() popen.return_value.poll.return_value = None popen.return_value.stdout = io.BytesIO(BEACON_PCAP) output = io.StringIO() observe_beaconing_packets = self.patch(beaconing_module, 'observe_beaconing_packets') observe_beaconing_packets.return_value = 37 with ExpectedException(SystemExit, '.*37.*'): run(args, output=output)
def test__checks_for_pipe(self): parser = ArgumentParser() add_arguments(parser) args = parser.parse_args(['--input-file', '-']) output = io.StringIO() stdin = self.patch(beaconing_module.sys, 'stdin') stdin.return_value.fileno = Mock() fstat = self.patch(beaconing_module.os, 'fstat') fstat.return_value.st_mode = None stat = self.patch(beaconing_module.stat, 'S_ISFIFO') stat.return_value = False with ExpectedException(ActionScriptError, 'Expected stdin to be a pipe'): run(args, output=output)
def test_checks_for_pipe(self): parser = ArgumentParser() add_arguments(parser) args = parser.parse_args(["--input-file", "-"]) output = io.StringIO() stdin = self.patch(beaconing_module.sys, "stdin") stdin.return_value.fileno = Mock() fstat = self.patch(beaconing_module.os, "fstat") fstat.return_value.st_mode = None stat = self.patch(beaconing_module.stat, "S_ISFIFO") stat.return_value = False with ExpectedException(ActionScriptError, "Expected stdin to be a pipe"): run(args, output=output)
def test__calls_subprocess_for_interface_sudo(self): parser = ArgumentParser() add_arguments(parser) args = parser.parse_args(['eth0']) popen = self.patch(beaconing_module.subprocess, 'Popen') popen.return_value.poll = Mock() popen.return_value.poll.return_value = None popen.return_value.stdout = io.BytesIO(BEACON_PCAP) output = io.StringIO() run(args, output=output) self.assertThat( popen, MockCalledOnceWith( ['sudo', '-n', '/usr/lib/maas/maas-beacon-monitor', 'eth0'], stdin=subprocess.DEVNULL, stdout=subprocess.PIPE))
def test_raises_systemexit_poll_result(self): parser = ArgumentParser() add_arguments(parser) args = parser.parse_args(["eth0"]) popen = self.patch(beaconing_module.subprocess, "Popen") popen.return_value.poll = Mock() popen.return_value.poll.return_value = None popen.return_value.stdout = io.BytesIO(BEACON_PCAP) output = io.StringIO() observe_beaconing_packets = self.patch(beaconing_module, "observe_beaconing_packets") observe_beaconing_packets.return_value = None popen.return_value.poll = Mock() popen.return_value.poll.return_value = 42 with ExpectedException(SystemExit, ".*42.*"): run(args, output=output)
def test__calls_subprocess_for_interface(self): parser = ArgumentParser() add_arguments(parser) args = parser.parse_args(["eth0"]) popen = self.patch(beaconing_module.subprocess, "Popen") popen.return_value.poll = Mock() popen.return_value.poll.return_value = None popen.return_value.stdout = io.BytesIO(BEACON_PCAP) output = io.StringIO() run(args, output=output) self.assertThat( popen, MockCalledOnceWith( ["sudo", "-n", "/usr/lib/maas/beacon-monitor", "eth0"], stdin=subprocess.DEVNULL, stdout=subprocess.PIPE, ), )