2019-09-11 17:39:28 +08:00
|
|
|
from time import sleep
|
|
|
|
|
|
|
|
from envs.monkey_zoo.blackbox.utils.test_timer import TestTimer
|
2019-09-17 14:17:29 +08:00
|
|
|
from envs.monkey_zoo.blackbox.log_handlers.test_logs_handler import TestLogsHandler
|
2019-09-11 17:39:28 +08:00
|
|
|
|
2019-09-13 21:12:58 +08:00
|
|
|
MAX_TIME_FOR_MONKEYS_TO_DIE = 5*60
|
|
|
|
WAIT_TIME_BETWEEN_REQUESTS = 10
|
2019-09-24 20:38:16 +08:00
|
|
|
TIME_FOR_MONKEY_PROCESS_TO_FINISH = 40
|
2019-09-11 17:39:28 +08:00
|
|
|
|
|
|
|
|
|
|
|
class BasicTest(object):
|
|
|
|
|
2019-09-17 14:17:29 +08:00
|
|
|
def __init__(self, name, island_client, config_parser, analyzers, timeout):
|
2019-09-11 17:39:28 +08:00
|
|
|
self.name = name
|
|
|
|
self.island_client = island_client
|
2019-09-17 14:17:29 +08:00
|
|
|
self.config_parser = config_parser
|
2019-09-11 17:39:28 +08:00
|
|
|
self.analyzers = analyzers
|
|
|
|
self.timeout = timeout
|
|
|
|
|
|
|
|
def run(self):
|
2019-09-17 14:17:29 +08:00
|
|
|
self.island_client.import_config(self.config_parser.config_raw)
|
|
|
|
self.print_test_starting_info()
|
2019-09-11 17:39:28 +08:00
|
|
|
try:
|
|
|
|
self.island_client.run_monkey_local()
|
|
|
|
self.test_until_timeout()
|
|
|
|
finally:
|
|
|
|
self.island_client.kill_all_monkeys()
|
2019-09-13 21:12:58 +08:00
|
|
|
self.wait_until_monkeys_die()
|
2019-09-17 14:17:29 +08:00
|
|
|
self.wait_for_monkey_process_to_finish()
|
|
|
|
self.parse_logs()
|
2019-09-11 17:39:28 +08:00
|
|
|
self.island_client.reset_env()
|
|
|
|
|
2019-09-17 14:17:29 +08:00
|
|
|
def print_test_starting_info(self):
|
|
|
|
print("Started {} test".format(self.name))
|
|
|
|
print("Machines participating in test:")
|
|
|
|
for target_ip in self.config_parser.get_ips_of_targets():
|
|
|
|
print(" "+target_ip)
|
|
|
|
print("")
|
|
|
|
|
2019-09-11 17:39:28 +08:00
|
|
|
def test_until_timeout(self):
|
|
|
|
timer = TestTimer(self.timeout)
|
|
|
|
while not timer.timed_out():
|
|
|
|
if self.all_analyzers_pass():
|
|
|
|
self.log_success(timer)
|
|
|
|
return
|
|
|
|
self.log_failure(timer)
|
|
|
|
assert False
|
|
|
|
|
|
|
|
def log_success(self, timer):
|
|
|
|
print(self.get_analyzer_logs())
|
|
|
|
print("{} test passed, time taken: {:.1f} seconds.".format(self.name, timer.get_time_taken()))
|
|
|
|
|
|
|
|
def log_failure(self, timer):
|
|
|
|
print(self.get_analyzer_logs())
|
|
|
|
print("{} test failed because of timeout. Time taken: {:.1f} seconds.".format(self.name,
|
|
|
|
timer.get_time_taken()))
|
|
|
|
|
|
|
|
def all_analyzers_pass(self):
|
|
|
|
for analyzer in self.analyzers:
|
|
|
|
if not analyzer.analyze_test_results():
|
|
|
|
return False
|
|
|
|
return True
|
|
|
|
|
|
|
|
def get_analyzer_logs(self):
|
|
|
|
log = ""
|
|
|
|
for analyzer in self.analyzers:
|
|
|
|
log += "\n"+analyzer.log.get_contents()
|
|
|
|
return log
|
2019-09-13 21:12:58 +08:00
|
|
|
|
|
|
|
def wait_until_monkeys_die(self):
|
|
|
|
time_passed = 0
|
|
|
|
while not self.island_client.is_all_monkeys_dead() and time_passed < MAX_TIME_FOR_MONKEYS_TO_DIE:
|
|
|
|
sleep(WAIT_TIME_BETWEEN_REQUESTS)
|
|
|
|
time_passed += WAIT_TIME_BETWEEN_REQUESTS
|
|
|
|
if time_passed > MAX_TIME_FOR_MONKEYS_TO_DIE:
|
|
|
|
print("Some monkeys didn't die after the test, passing")
|
|
|
|
assert False
|
2019-09-17 14:17:29 +08:00
|
|
|
|
|
|
|
def parse_logs(self):
|
|
|
|
print("\nParsing test logs:")
|
|
|
|
TestLogsHandler(self.name, self.island_client).parse_test_logs()
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def wait_for_monkey_process_to_finish():
|
|
|
|
sleep(TIME_FOR_MONKEY_PROCESS_TO_FINISH)
|