Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix execute strategy and add try except #15

Open
wants to merge 2 commits into
base: students
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions UPISAS/strategy.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,17 @@ def monitor(self, endpoint_suffix="monitor", with_validation=True):
return True

def execute(self, adaptation, endpoint_suffix="execute", with_validation=True):
if with_validation:
validate_schema(adaptation, self.knowledge.execute_schema)
# * Uncomment if you want to validate adaptation input against adaptation_options_schema
# if with_validation:
# validate_schema(adaptation, self.knowledge.adaptation_options_schema)
url = '/'.join([self.exemplar.base_endpoint, endpoint_suffix])
response = requests.put(url, json=adaptation)
print("[Execute]\tposted configuration: " + str(adaptation))
if response.status_code == 404:
logging.error("Cannot execute adaptation on remote system, check that the execute endpoint exists.")
raise EndpointNotReachable
if with_validation:
validate_schema(response.json(), self.knowledge.execute_schema)
return True

def get_adaptation_options(self, endpoint_suffix: "API Endpoint" = "adaptation_options", with_validation=True):
Expand Down
17 changes: 13 additions & 4 deletions UPISAS/tests/your_exemplar/test_your_exemplar_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
from UPISAS import get_response_for_get_request
from UPISAS.exemplars.your_exemplar import YourExemplar
from UPISAS.strategies.empty_strategy import EmptyStrategy
from UPISAS.exceptions import ServerNotReachable

from requests.exceptions import RequestException

class TestYourExemplarInterface(unittest.TestCase):

Expand Down Expand Up @@ -69,10 +71,17 @@ def _start_server_and_wait_until_is_up(self, base_endpoint="http://localhost:300
while True:
time.sleep(1)
print("trying to connect...")
response = get_response_for_get_request(base_endpoint)
print(response.status_code)
if response.status_code < 400:
return
try:
response = get_response_for_get_request(base_endpoint)
print(response.status_code)
if response.status_code < 400:
return
except RequestException as e:
print(e)
print("retrying connection")
except ServerNotReachable as e:
print(e)
print("retrying connection")


if __name__ == '__main__':
Expand Down