Skip to content

Commit

Permalink
Refactoring of the FakeSiteAdapter
Browse files Browse the repository at this point in the history
  • Loading branch information
giffels committed Aug 8, 2023
1 parent 2d2b1e4 commit 3d58383
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 83 deletions.
60 changes: 14 additions & 46 deletions tardis/adapters/sites/fakesite.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,6 @@ def __init__(self, machine_type: str, site_name: str) -> None:
key_translator = StaticMapping(
remote_resource_uuid="remote_resource_uuid",
resource_status="resource_status",
created="created",
updated="updated",
resource_boot_time="resource_boot_time",
)

self.handle_response = partial(
Expand All @@ -34,72 +31,43 @@ def __init__(self, machine_type: str, site_name: str) -> None:
translator_functions=StaticMapping(),
)

self._stopped_n_terminated_resources = {}

async def deploy_resource(
self, resource_attributes: AttributeDict
) -> AttributeDict:
await asyncio.sleep(self._api_response_delay.get_value())
now = datetime.now()
response = AttributeDict(
remote_resource_uuid=uuid4().hex,
resource_status=ResourceStatus.Booting,
created=now,
updated=now,
resource_boot_time=self._resource_boot_time.get_value(),
)
return self.handle_response(response)

def get_resource_boot_time(self, resource_attributes: AttributeDict) -> float:
try:
return resource_attributes.resource_boot_time
except AttributeError:
# In case tardis is restarted, resource_boot_time is not set, so re-set
resource_boot_time = resource_attributes[
"resource_boot_time"
] = self._resource_boot_time.get_value()
return resource_boot_time

async def resource_status(
self, resource_attributes: AttributeDict
) -> AttributeDict:
await asyncio.sleep(self._api_response_delay.get_value())
try: # check if resource has been stopped or terminated
resource_status = self._stopped_n_terminated_resources[
resource_attributes.drone_uuid
]
except KeyError:
pass
else:
return self.handle_response(AttributeDict(resource_status=resource_status))

created_time = resource_attributes.created

resource_boot_time = self.get_resource_boot_time(resource_attributes)
# check if resource is already running
if (datetime.now() - created_time) > timedelta(seconds=resource_boot_time):
resource_boot_time = self._resource_boot_time.get_value()
# check if resource should already run
if (datetime.now() - created_time) > timedelta(
seconds=resource_boot_time
) and resource_attributes.resource_status is ResourceStatus.Booting:
return self.handle_response(
AttributeDict(resource_status=ResourceStatus.Running)
)
return self.handle_response(resource_attributes)
return AttributeDict() # do not change anything

async def stop_resource(self, resource_attributes: AttributeDict):
async def stop_resource(self, resource_attributes: AttributeDict) -> None:
await asyncio.sleep(self._api_response_delay.get_value())
self._stopped_n_terminated_resources[
resource_attributes.drone_uuid
] = ResourceStatus.Stopped
return self.handle_response(
AttributeDict(resource_status=ResourceStatus.Stopped)
)
# update resource status manually to ResourceStatus.Stopped, so that
# the life cycle comes to an end.
resource_attributes.resource_status = ResourceStatus.Stopped

async def terminate_resource(self, resource_attributes: AttributeDict):
async def terminate_resource(self, resource_attributes: AttributeDict) -> None:
await asyncio.sleep(self._api_response_delay.get_value())
self._stopped_n_terminated_resources[
resource_attributes.drone_uuid
] = ResourceStatus.Deleted
return self.handle_response(
AttributeDict(resource_status=ResourceStatus.Deleted)
)
# update resource status manually to ResourceStatus.Deleted, so that
# the life cycle is ended.
resource_attributes.resource_status = ResourceStatus.Deleted

@contextmanager
def handle_exceptions(self) -> None:
Expand Down
100 changes: 63 additions & 37 deletions tests/adapters_t/sites_t/test_fakesite.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,6 @@ def machine_type_configuration(self):
def test_deploy_resource(self):
response = run_async(self.adapter.deploy_resource, AttributeDict())
self.assertEqual(response.resource_status, ResourceStatus.Booting)
self.assertFalse(response.created - datetime.now() > timedelta(seconds=1))
self.assertFalse(response.updated - datetime.now() > timedelta(seconds=1))

def test_machine_meta_data(self):
self.assertEqual(
Expand All @@ -59,52 +57,80 @@ def test_site_name(self):
self.assertEqual(self.adapter.site_name, "TestSite")

def test_resource_status(self):
# test tardis restart, where resource_boot_time is not set
response = run_async(
self.adapter.resource_status,
AttributeDict(
created=datetime.now(),
resource_status=ResourceStatus.Booting,
drone_uuid="test-123",
# Booting resource, where boot time is not yet over
resource_attributes = AttributeDict(
created=datetime.now(),
resource_status=ResourceStatus.Booting,
drone_uuid="test-123",
)
self.assertEqual(
AttributeDict(), # no update require, resource still in Booting state
run_async(
self.adapter.resource_status,
resource_attributes,
),
)
self.assertEqual(response.resource_status, ResourceStatus.Booting)

deploy_response = run_async(self.adapter.deploy_resource, AttributeDict())
deploy_response.update(AttributeDict(drone_uuid="test-123"))
response = run_async(self.adapter.resource_status, deploy_response)
self.assertEqual(response.resource_status, ResourceStatus.Booting)
# Booting resource, where boot time is already over
created_timestamp = datetime.now() - timedelta(seconds=100)
resource_attributes.update(AttributeDict(created=created_timestamp))

past_timestamp = datetime.now() - timedelta(seconds=100)
deploy_response.update(
AttributeDict(created=past_timestamp, drone_uuid="test-123")
self.assertDictEqual(
AttributeDict(resource_status=ResourceStatus.Running),
run_async(self.adapter.resource_status, resource_attributes),
)
response = run_async(self.adapter.resource_status, deploy_response)
self.assertEqual(response.resource_status, ResourceStatus.Running)

# test stopped resources
response.update(AttributeDict(drone_uuid="test-123"))
response = run_async(self.adapter.stop_resource, response)
self.assertEqual(response.resource_status, ResourceStatus.Stopped)
# test resource status of stop resources
resource_attributes.update(
AttributeDict(resource_status=ResourceStatus.Stopped)
)
self.assertEqual(
AttributeDict(),
run_async(self.adapter.resource_status, resource_attributes),
)
self.assertDictEqual(
AttributeDict(
created=created_timestamp,
resource_status=ResourceStatus.Stopped,
drone_uuid="test-123",
),
resource_attributes,
)

# test terminated resources
response.update(AttributeDict(drone_uuid="test-123"))
response = run_async(self.adapter.terminate_resource, response)
self.assertEqual(response.resource_status, ResourceStatus.Deleted)
# test resource status of terminated resources
resource_attributes.update(
AttributeDict(resource_status=ResourceStatus.Deleted)
)
self.assertEqual(
AttributeDict(),
run_async(self.adapter.resource_status, resource_attributes),
)
self.assertDictEqual(
AttributeDict(
created=created_timestamp,
resource_status=ResourceStatus.Deleted,
drone_uuid="test-123",
),
resource_attributes,
)

def test_stop_resource(self):
deploy_response = run_async(self.adapter.deploy_resource, AttributeDict())
deploy_response.update(AttributeDict(drone_uuid="test-123"))
run_async(self.adapter.stop_resource, deploy_response)
response = run_async(self.adapter.resource_status, deploy_response)
self.assertEqual(response.resource_status, ResourceStatus.Stopped)
resource_attributes = AttributeDict(
drone_uuid="test-123",
created=datetime.now(),
resource_status=ResourceStatus.Running,
)
run_async(self.adapter.stop_resource, resource_attributes)
self.assertEqual(resource_attributes.resource_status, ResourceStatus.Stopped)

def test_terminate_resource(self):
deploy_response = run_async(self.adapter.deploy_resource, AttributeDict())
deploy_response.update(AttributeDict(drone_uuid="test-123"))
run_async(self.adapter.terminate_resource, deploy_response)
response = run_async(self.adapter.resource_status, deploy_response)
self.assertEqual(response.resource_status, ResourceStatus.Deleted)
resource_attributes = AttributeDict(
drone_uuid="test-123",
created=datetime.now(),
resource_status=ResourceStatus.Running,
)
run_async(self.adapter.terminate_resource, resource_attributes)
self.assertEqual(resource_attributes.resource_status, ResourceStatus.Deleted)

def test_exception_handling(self):
def test_exception_handling(raise_it, catch_it):
Expand Down

0 comments on commit 3d58383

Please sign in to comment.