monkey/envs/monkey_zoo/blackbox/island_client/monkey_island_client.py

87 lines
2.9 KiB
Python
Raw Normal View History

2019-09-13 21:12:58 +08:00
from time import sleep
import json
2019-10-01 15:42:51 +08:00
import logging
2019-09-13 21:12:58 +08:00
from bson import json_util
from envs.monkey_zoo.blackbox.island_client.monkey_island_requests import MonkeyIslandRequests
SLEEP_BETWEEN_REQUESTS_SECONDS = 0.5
MONKEY_TEST_ENDPOINT = 'api/test/monkey'
LOG_TEST_ENDPOINT = 'api/test/log'
def avoid_race_condition(func):
sleep(SLEEP_BETWEEN_REQUESTS_SECONDS)
return func
class MonkeyIslandClient(object):
def __init__(self, server_address):
self.requests = MonkeyIslandRequests(server_address)
def get_api_status(self):
return self.requests.get("api")
@avoid_race_condition
def import_config(self, config_contents):
_ = self.requests.post("api/configuration/island", data=config_contents)
@avoid_race_condition
def run_monkey_local(self):
response = self.requests.post_json("api/local-monkey", dict_data={"action": "run"})
if MonkeyIslandClient.monkey_ran_successfully(response):
2019-10-01 15:42:51 +08:00
logging.info("Running the monkey.")
2019-09-13 21:12:58 +08:00
else:
2019-10-01 15:42:51 +08:00
logging.error("Failed to run the monkey.")
2019-09-13 21:12:58 +08:00
assert False
@staticmethod
def monkey_ran_successfully(response):
return response.ok and json.loads(response.content)['is_running']
@avoid_race_condition
def kill_all_monkeys(self):
if self.requests.get("api", {"action": "killall"}).ok:
2019-10-01 15:42:51 +08:00
logging.info("Killing all monkeys after the test.")
2019-09-13 21:12:58 +08:00
else:
2019-10-01 15:42:51 +08:00
logging.error("Failed to kill all monkeys.")
2019-09-13 21:12:58 +08:00
assert False
@avoid_race_condition
def reset_env(self):
if self.requests.get("api", {"action": "reset"}).ok:
2019-10-01 15:42:51 +08:00
logging.info("Resetting environment after the test.")
2019-09-13 21:12:58 +08:00
else:
2019-10-01 15:42:51 +08:00
logging.error("Failed to reset the environment.")
2019-09-13 21:12:58 +08:00
assert False
def find_monkeys_in_db(self, query):
2019-10-01 15:42:51 +08:00
if query is None:
raise TypeError
2019-09-13 21:12:58 +08:00
response = self.requests.get(MONKEY_TEST_ENDPOINT,
MonkeyIslandClient.form_find_query_for_request(query))
2019-10-01 15:42:51 +08:00
return MonkeyIslandClient.get_test_query_results(response)
def get_all_monkeys_from_db(self):
response = self.requests.get(MONKEY_TEST_ENDPOINT,
MonkeyIslandClient.form_find_query_for_request(None))
return MonkeyIslandClient.get_test_query_results(response)
2019-09-13 21:12:58 +08:00
def find_log_in_db(self, query):
response = self.requests.get(LOG_TEST_ENDPOINT,
MonkeyIslandClient.form_find_query_for_request(query))
2019-10-01 15:42:51 +08:00
return MonkeyIslandClient.get_test_query_results(response)
2019-09-13 21:12:58 +08:00
@staticmethod
def form_find_query_for_request(query):
return {'find_query': json_util.dumps(query)}
@staticmethod
def get_test_query_results(response):
return json.loads(response.content)['results']
def is_all_monkeys_dead(self):
query = {'dead': False}
2019-09-13 21:12:58 +08:00
return len(self.find_monkeys_in_db(query)) == 0