def test_output_data_to_file(self): configfile = os.path.join( FILE_DIR, "..", "..", "sample_eventgen_conf", "medium_test", "eventgen.conf.fileoutput", ) testargs = ["eventgen", "generate", configfile] with patch.object(sys, "argv", testargs): pargs = parse_args() assert pargs.subcommand == "generate" assert pargs.configfile == configfile eventgen = EventGenerator(args=pargs) eventgen.start() file_output_path = os.path.abspath( os.path.join(FILE_DIR, "..", "..", "..", "test_file_output.result") ) assert os.path.isfile(file_output_path) with open(file_output_path, "r") as outfile: line_count = 1 for output_line in outfile: if not output_line or line_count == 6: break assert "WINDBAG Event {} of 5".format(line_count) in output_line line_count += 1 # tear down if os.path.isfile(file_output_path): os.remove(file_output_path)
def test_output_data_to_syslog(self): configfile = "tests/sample_eventgen_conf/medium_test/eventgen.conf.syslogoutput" testargs = ["eventgen", "generate", configfile] with patch.object(sys, 'argv', testargs): with patch('logging.getLogger') as mock_log: pargs = parse_args() assert pargs.subcommand == 'generate' assert pargs.configfile == configfile eventgen = EventGenerator(args=pargs) sample = MagicMock() sample.name = 'test' sample.syslogDestinationHost = '127.0.0.1' sample.syslogDestinationPort = 9999 syslogoutput = SyslogOutOutputPlugin(sample) eventgen.start() for i in xrange(1, 6): appearance = False for logger_call in syslogoutput._l.info.call_args_list: if "WINDBAG Event {} of 5".format(i) in str( logger_call): appearance = True if not appearance: assert False
def test_jinja_generator_to_file(self): configfile = "tests/sample_eventgen_conf/jinja/eventgen.conf.jinja_basic" testargs = ["eventgen", "generate", configfile] file_output_path = os.path.abspath(os.path.join(FILE_DIR, '..', '..', '..', OUTPUT_FILE)) # remove the result file if it exists if os.path.exists(file_output_path): os.remove(file_output_path) with patch.object(sys, 'argv', testargs): pargs = parse_args() assert pargs.subcommand == 'generate' assert pargs.configfile == configfile eventgen = EventGenerator(args=pargs) eventgen.start() assert os.path.isfile(file_output_path) with open(file_output_path, 'r') as outfile: line_count = 1 for output_line in outfile: if not output_line or line_count == 11: break assert "I like little windbags" in output_line assert "Im at: {0} out of: 10".format(line_count) in output_line line_count += 1
def test_jinja_generator_to_file(self): configfile = "tests/sample_eventgen_conf/jinja/eventgen.conf.jinja_basic" testargs = ["eventgen", "generate", configfile] file_output_path = os.path.abspath( os.path.join(FILE_DIR, '..', '..', '..', OUTPUT_FILE)) # remove the result file if it exists if os.path.exists(file_output_path): os.remove(file_output_path) with patch.object(sys, 'argv', testargs): pargs = parse_args() assert pargs.subcommand == 'generate' assert pargs.configfile == configfile eventgen = EventGenerator(args=pargs) eventgen.start() assert os.path.isfile(file_output_path) with open(file_output_path, 'r') as outfile: line_count = 1 for output_line in outfile: if not output_line or line_count == 11: break assert "I like little windbags" in output_line assert "Im at: {0} out of: 10".format( line_count) in output_line line_count += 1
def test_output_data_to_scs(self): configfile = "tests/sample_eventgen_conf/medium_test/eventgen.conf.scsoutput" testargs = ["eventgen", "generate", configfile] with patch.object(sys, "argv", testargs): pargs = parse_args() assert pargs.subcommand == "generate" assert pargs.configfile == configfile eventgen = EventGenerator(args=pargs) with patch("requests_futures.sessions.FuturesSession.post"): sample = MagicMock() scsoutput = SCSOutputPlugin(sample) eventgen.start() scsoutput.session.post.assert_called() assert scsoutput.session.post.call_count == 1
def test_output_data_to_file(self): configfile = "tests/sample_eventgen_conf/medium_test/eventgen.conf.fileoutput" testargs = ["eventgen", "generate", configfile] with patch.object(sys, 'argv', testargs): pargs = parse_args() assert pargs.subcommand == 'generate' assert pargs.configfile == configfile eventgen = EventGenerator(args=pargs) eventgen.start() file_output_path = os.path.abspath(os.path.join(FILE_DIR, '..', '..', '..', 'test_file_output.result')) assert os.path.isfile(file_output_path) with open(file_output_path, 'r') as outfile: line_count = 1 for output_line in outfile: if not output_line or line_count == 6: break assert "WINDBAG Event {} of 5".format(line_count) in output_line line_count += 1
def test_output_data_to_udp_port(self): configfile = "tests/sample_eventgen_conf/medium_test/eventgen.conf.udpoutput" testargs = ["eventgen", "generate", configfile] with patch.object(sys, 'argv', testargs): pargs = parse_args() assert pargs.subcommand == 'generate' assert pargs.configfile == configfile eventgen = EventGenerator(args=pargs) with patch('socket.socket') as mock_requests: sample = MagicMock() udpoutput = UdpOutputPlugin(sample) mock_requests.sendto = MagicMock() mock_requests.connect = MagicMock() post_resp = MagicMock() post_resp.raise_for_status = MagicMock() mock_requests.post.return_value = MagicMock() eventgen.start() assert not udpoutput.s.connect.called assert udpoutput.s.sendto.call_count == 5
def test_output_data_to_tcp_port(self): configfile = "tests/sample_eventgen_conf/medium_test/eventgen.conf.tcpoutput" testargs = ["eventgen", "generate", configfile] with patch.object(sys, "argv", testargs): pargs = parse_args() assert pargs.subcommand == "generate" assert pargs.configfile == configfile eventgen = EventGenerator(args=pargs) with patch("socket.socket") as mock_requests: sample = MagicMock() tcpoutput = TcpOutputPlugin(sample) mock_requests.send = MagicMock() mock_requests.connect = MagicMock() post_resp = MagicMock() post_resp.raise_for_status = MagicMock() mock_requests.post.return_value = MagicMock() mock_requests.connect.return_value = True eventgen.start() tcpoutput.s.connect.assert_called_with(("127.0.0.1", 9999)) assert tcpoutput.s.send.call_count == 5
def test_output_data_to_file(self): configfile = "tests/sample_eventgen_conf/medium_test/eventgen.conf.fileoutput" testargs = ["eventgen", "generate", configfile] with patch.object(sys, 'argv', testargs): pargs = parse_args() assert pargs.subcommand == 'generate' assert pargs.configfile == configfile eventgen = EventGenerator(args=pargs) eventgen.start() file_output_path = os.path.abspath( os.path.join(FILE_DIR, '..', '..', '..', 'test_file_output.result')) assert os.path.isfile(file_output_path) f = open(file_output_path, 'r') line_count = 1 for output_line in f: if not output_line or line_count == 6: break assert "WINDBAG Event {} of 5".format(line_count) in output_line line_count += 1
def test_output_data_to_syslog(self): configfile = "tests/sample_eventgen_conf/medium_test/eventgen.conf.syslogoutput" testargs = ["eventgen", "generate", configfile] with patch.object(sys, 'argv', testargs): with patch('logging.getLogger'): pargs = parse_args() assert pargs.subcommand == 'generate' assert pargs.configfile == configfile eventgen = EventGenerator(args=pargs) sample = MagicMock() sample.name = 'test' sample.syslogDestinationHost = '127.0.0.1' sample.syslogDestinationPort = 9999 syslogoutput = SyslogOutOutputPlugin(sample) eventgen.start() for i in xrange(1, 6): appearance = False for logger_call in syslogoutput._l.info.call_args_list: if "WINDBAG Event {} of 5".format(i) in str(logger_call): appearance = True if not appearance: assert False