2019-09-13 21:12:58 +08:00
|
|
|
import json
|
2019-10-01 15:42:51 +08:00
|
|
|
import logging
|
2020-04-24 18:19:07 +08:00
|
|
|
from time import sleep
|
|
|
|
|
2019-09-13 21:12:58 +08:00
|
|
|
from bson import json_util
|
|
|
|
|
2020-10-07 15:32:33 +08:00
|
|
|
from envs.monkey_zoo.blackbox.island_client.monkey_island_requests import MonkeyIslandRequests
|
2019-09-13 21:12:58 +08:00
|
|
|
|
|
|
|
SLEEP_BETWEEN_REQUESTS_SECONDS = 0.5
|
|
|
|
MONKEY_TEST_ENDPOINT = 'api/test/monkey'
|
|
|
|
LOG_TEST_ENDPOINT = 'api/test/log'
|
2019-10-01 21:11:53 +08:00
|
|
|
LOGGER = logging.getLogger(__name__)
|
2019-09-13 21:12:58 +08:00
|
|
|
|
|
|
|
|
|
|
|
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")
|
|
|
|
|
2021-03-02 21:14:33 +08:00
|
|
|
def get_config(self):
|
|
|
|
return json.loads(self.requests.get("api/configuration/island").content)
|
|
|
|
|
2019-09-13 21:12:58 +08:00
|
|
|
@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):
|
2020-04-16 21:39:10 +08:00
|
|
|
response = self.requests.post_json("api/local-monkey", data={"action": "run"})
|
2019-09-13 21:12:58 +08:00
|
|
|
if MonkeyIslandClient.monkey_ran_successfully(response):
|
2019-10-01 21:11:53 +08:00
|
|
|
LOGGER.info("Running the monkey.")
|
2019-09-13 21:12:58 +08:00
|
|
|
else:
|
2019-10-01 21:11:53 +08:00
|
|
|
LOGGER.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 21:11:53 +08:00
|
|
|
LOGGER.info("Killing all monkeys after the test.")
|
2019-09-13 21:12:58 +08:00
|
|
|
else:
|
2019-10-01 21:11:53 +08:00
|
|
|
LOGGER.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 21:11:53 +08:00
|
|
|
LOGGER.info("Resetting environment after the test.")
|
2019-09-13 21:12:58 +08:00
|
|
|
else:
|
2019-10-01 21:11:53 +08:00
|
|
|
LOGGER.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):
|
2019-09-17 14:17:29 +08:00
|
|
|
query = {'dead': False}
|
2019-09-13 21:12:58 +08:00
|
|
|
return len(self.find_monkeys_in_db(query)) == 0
|
2020-02-23 20:02:18 +08:00
|
|
|
|
|
|
|
def clear_caches(self):
|
|
|
|
"""
|
|
|
|
Tries to clear caches.
|
|
|
|
:raises: If error (by error code), raises the error
|
|
|
|
:return: The response
|
|
|
|
"""
|
2020-02-23 21:24:44 +08:00
|
|
|
response = self.requests.get("api/test/clear_caches")
|
2020-02-23 20:02:18 +08:00
|
|
|
response.raise_for_status()
|
|
|
|
return response
|