def test_process_log_taskid(p, q): set_cwd(tempfile.mkdtemp()) cuckoo_create() init_console_logging(logging.DEBUG) init_logfile("process-p0.json") def log_something(target, copy_path, task): logger("test message", action="hello.world", status="success") q.side_effect = log_something process_task({ "id": 12345, "category": "url", "target": "http://google.com/", "package": "ie", "options": {}, "custom": None, }) for line in open(cwd("log", "process-p0.json"), "rb"): obj = json.loads(line) if obj["action"] == "hello.world": assert obj["task_id"] == 12345 break else: raise
def test_process_json_logging(): set_cwd(tempfile.mkdtemp()) cuckoo_create() index_yara() init_logfile("process-p0.json") def process_tasks(instance, maxcount): logger("foo bar", action="hello.world", status="success") with mock.patch("cuckoo.main.Database") as p0: with mock.patch("cuckoo.main.process_tasks") as p1: with mock.patch("time.time") as p2: p1.side_effect = process_tasks p2.return_value = 1484232003 main.main( ("--cwd", cwd(), "process", "p0"), standalone_mode=False ) assert json.load(open(cwd("log", "process-p0.json"), "rb")) == { "asctime": mock.ANY, "action": "hello.world", "level": "info", "message": "foo bar", "status": "success", "task_id": None, "time": 1484232003, }
def process(ctx, instance, report, maxcount): """Process raw task data into reports.""" init_console_logging(level=ctx.parent.level) if instance: init_logfile("process-%s.json" % instance) Database().connect() # Load additional Signatures. load_signatures() # Initialize all modules & Yara rules. init_modules() init_yara(False) try: # Regenerate one or more reports. if report: process_task_range(report) elif not instance: print ctx.get_help(), "\n" sys.exit("In automated mode an instance name is required!") else: log.info( "Initialized instance=%s, ready to process some tasks", instance ) process_tasks(instance, maxcount) except KeyboardInterrupt: print(red("Aborting (re-)processing of your analyses.."))
def test_process_json_logging(): set_cwd(tempfile.mkdtemp()) cuckoo_create() init_yara() init_logfile("process-p0.json") def process_tasks(instance, maxcount): logger("foo bar", action="hello.world", status="success") with mock.patch("cuckoo.main.Database"): with mock.patch("cuckoo.main.process_tasks") as p1: with mock.patch("time.time") as p2: p1.side_effect = process_tasks p2.return_value = 1484232003 main.main( ("--cwd", cwd(), "process", "p0"), standalone_mode=False ) assert json.load(open(cwd("log", "process-p0.json"), "rb")) == { "asctime": mock.ANY, "action": "hello.world", "level": "info", "message": "foo bar", "status": "success", "task_id": None, "time": 1484232003, }
def process(ctx, instance, report, maxcount): """Process raw task data into reports.""" init_console_logging(level=ctx.parent.level) if instance: pidfile = Pidfile(instance) if pidfile.exists(): log.error(red( "Cuckoo process instance '%s' already exists. PID: %s\n" ), instance, pidfile.pid) sys.exit(1) pidfile.create() init_logfile("process-%s.json" % instance) Database().connect() # Load additional Signatures. load_signatures() try: # Initialize all modules & Yara rules. init_modules() init_yara() except CuckooCriticalError as e: message = red("{0}: {1}".format(e.__class__.__name__, e)) if len(log.handlers): log.critical(message) else: sys.stderr.write("{0}\n".format(message)) sys.exit(1) try: # Regenerate one or more reports. if report: process_task_range(report) elif not instance: print ctx.get_help(), "\n" sys.exit("In automated mode an instance name is required!") else: log.info( "Initialized instance=%s, ready to process some tasks", instance ) process_tasks(instance, maxcount) except KeyboardInterrupt: print(red("Aborting (re-)processing of your analyses..")) if instance: Pidfile(instance).remove()
def test_logger(): set_cwd(tempfile.mkdtemp()) cuckoo_create() init_logfile("cuckoo.json") with mock.patch("time.time") as p: p.return_value = 1484232001 logger("test %s", "message", action="a", status="b") assert json.load(open(cwd("log", "cuckoo.json"), "rb")) == { "asctime": mock.ANY, "action": "a", "level": "info", "message": "test message", "status": "b", "task_id": None, "time": 1484232001, }
def test_process_log_taskid(): set_cwd(tempfile.mkdtemp()) cuckoo_create() db.connect() init_console_logging(logging.DEBUG) init_logfile("process-p0.json") task = Task() id = submit_task.add_url("http://google.com/", package="ie") task.load_from_db(id) task.process = mock.MagicMock() process_task(task) for line in open(cwd("log", "process-p0.json"), "rb"): obj = json.loads(line) if obj["action"] == "task.report": assert obj["task_id"] == task.id break else: raise
def test_logging(): set_cwd(tempfile.mkdtemp()) cuckoo_create() init_logfile("cuckoo.json") with mock.patch("time.time") as p: p.return_value = 1484232002 log = logging.getLogger("test.module") log.warning("test %s", "message2", extra={ "action": "a", "status": "b", }) assert json.load(open(cwd("log", "cuckoo.json"), "rb")) == { "asctime": mock.ANY, "action": "a", "level": "warning", "message": "test message2", "status": "b", "task_id": None, "time": 1484232002, }