Skip to content

Commit

Permalink
Assertion Error for datetime format of 'last sync time'
Browse files Browse the repository at this point in the history
change to sca-only orgs, entitlement orgs are failing

assert capsule sync task(s) when invoked, and sync status
  • Loading branch information
damoore044 committed Mar 7, 2024
1 parent 22180e7 commit a64fe70
Show file tree
Hide file tree
Showing 2 changed files with 205 additions and 104 deletions.
79 changes: 70 additions & 9 deletions robottelo/host_helpers/capsule_mixins.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from datetime import datetime
from datetime import datetime, timedelta
import time

from dateutil.parser import parse

from robottelo.constants import PUPPET_CAPSULE_INSTALLER, PUPPET_COMMON_INSTALLER_OPTS
from robottelo.logging import logger
from robottelo.utils.installer import InstallerCommand
Expand Down Expand Up @@ -61,26 +63,85 @@ def wait_for_tasks(
raise AssertionError(f"No task was found using query '{search_query}'")
return tasks

def wait_for_sync(self, timeout=600, start_time=None):
"""Wait for capsule sync to finish and assert the sync task succeeded"""
# Assert that a task to sync lifecycle environment to the capsule
# is started (or finished already)
def wait_for_sync(self, timeout=600, start_time=None, return_result=False):
"""Wait for capsule sync to finish and assert the sync task succeeded.
Assert that a task to sync lifecycle environment to the capsule
is started (or finished already).
Assert all sync task(s) succeed, and update capsule's last sync time.
Assert last_sync_time is on or newer than start time.
After polling active tasks, Assert the last_sync_time is updated.
Assert the last task :id of any active_task(s), is the same :id for capsule's final_sync_task.
:param start_time: datetime, that sync should occur on or after.
:return: if return_result = True:
:type list: [boolean, dict]
:value: [if active sync found, updated sync status]
"""
# query capsule sync status to catch any quick sync task(s) in progress
sync_status = self.nailgun_capsule.content_get_sync(timeout=timeout, synchronous=True)
active_tasks = sync_status['active_sync_tasks']
active_sync = True if len(active_tasks) else False

if start_time is None:
start_time = datetime.utcnow().replace(microsecond=0)
# 1s margin of safety for rounding
start_time = (
(start_time - timedelta(seconds=1))
.replace(microsecond=0)
.strftime('%Y-%m-%d %H:%M:%S UTC')
)
logger.info(f"Waiting for capsule {self.hostname} sync to finish ...")
sync_status = self.nailgun_capsule.content_get_sync()
logger.info(f"Active tasks {sync_status['active_sync_tasks']}")
# found some active sync task(s), or an updated last_sync_time
assert (
len(sync_status['active_sync_tasks'])
or datetime.strptime(sync_status['last_sync_time'], '%Y-%m-%d %H:%M:%S UTC')
>= start_time
), (
f'No active sync task(s) were found for capsule: {self.hostname},'
f' capsule `last_sync time`: {sync_status["last_sync_time"]} is not on or newer than the `start_time`: {start_time}.'
f' an ongoing sync task or recent sync time for this capsule could not be found.'
)

# Wait till capsule sync finishes and assert the sync task succeeded
for task in sync_status['active_sync_tasks']:
# for any ongoing sync(s), polled task(s) must succeed
for task in active_tasks:
self.satellite.api.ForemanTask(id=task['id']).poll(timeout=timeout)
sync_status = self.nailgun_capsule.content_get_sync()
logger.info(f"Active sync task :id: {task['id']} succeeded.")

# get updated capsule sync status after polling any task(s)
sync_status = self.nailgun_capsule.content_get_sync(timeout=timeout, synchronous=True)
logger.info(f"Querying updated sync status from capsule {self.hostname}.")
# final sync task end time, is the same as capsule's last_sync_time
assert parse(sync_status['last_sync_time']) == parse(
sync_status['last_sync_task']['ended_at']
)
logger.info(
f"Final sync task for the capsule ended at: {sync_status['last_sync_task']['ended_at']}."
)
# check timedelta, that total time is not < 0s (last sync not before start time), and took less than timeout.
assert (
timedelta(seconds=0)
<= parse(sync_status['last_sync_time']) - parse(start_time)
<= timedelta(seconds=timeout)
), (
f'Capsule: {self.hostname},\n last_sync_time: {sync_status["last_sync_time"]} was before the start time: {start_time},'
f' or duration exceeded timeout: {timeout}s.'
)
if active_sync:
# if we found in-progress task(s), last one was the final sync task for capsule
assert active_tasks[-1]['id'] == sync_status['last_sync_task']['id']
logger.info(
f"Active sync task :id {active_tasks[-1]['id']}, was the final capsule task."
)

# no failed or active sync task(s) remaining
assert len(sync_status['last_failed_sync_tasks']) == 0
assert len(sync_status['active_sync_tasks']) == 0

# return [if an active sync was encountered, updated sync status]
# :type list: [boolean, dict]
if return_result:
return [active_sync, sync_status]

def get_published_repo_url(self, org, prod, repo, lce=None, cv=None):
"""Forms url of a repo or CV published on a Satellite or Capsule.
Expand Down
Loading

0 comments on commit a64fe70

Please sign in to comment.