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

ansible-test - Avoid use of deprecated utcnow #80750

Merged
merged 2 commits into from May 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions changelogs/fragments/ansible-test-utcnow.yml
@@ -0,0 +1,2 @@
minor_changes:
- ansible-test - Use ``datetime.datetime.now`` with ``tz`` specified instead of ``datetime.datetime.utcnow``.
6 changes: 5 additions & 1 deletion lib/ansible/utils/_junit_xml.py
Expand Up @@ -144,6 +144,10 @@ class TestSuite:
system_out: str | None = None
system_err: str | None = None

def __post_init__(self):
if self.timestamp and self.timestamp.tzinfo != datetime.timezone.utc:
raise ValueError(f'timestamp.tzinfo must be {datetime.timezone.utc!r}')

@property
def disabled(self) -> int:
"""The number of disabled test cases."""
Expand Down Expand Up @@ -187,7 +191,7 @@ def get_attributes(self) -> dict[str, str]:
skipped=self.skipped,
tests=self.tests,
time=self.time,
timestamp=self.timestamp.isoformat(timespec='seconds') if self.timestamp else None,
timestamp=self.timestamp.replace(tzinfo=None).isoformat(timespec='seconds') if self.timestamp else None,
)

def get_xml_element(self) -> ET.Element:
Expand Down
2 changes: 1 addition & 1 deletion test/lib/ansible_test/_internal/commands/env/__init__.py
Expand Up @@ -85,7 +85,7 @@ def show_dump_env(args: EnvConfig) -> None:
),
git=get_ci_provider().get_git_details(args),
platform=dict(
datetime=datetime.datetime.utcnow().strftime('%Y-%m-%dT%H:%M:%SZ'),
datetime=datetime.datetime.now(tz=datetime.timezone.utc).strftime('%Y-%m-%dT%H:%M:%SZ'),
platform=platform.platform(),
uname=platform.uname(),
),
Expand Down
Expand Up @@ -566,7 +566,7 @@ def command_integration_filtered(
coverage_manager.teardown()

result_name = '%s-%s.json' % (
args.command, re.sub(r'[^0-9]', '-', str(datetime.datetime.utcnow().replace(microsecond=0))))
args.command, re.sub(r'[^0-9]', '-', str(datetime.datetime.now(tz=datetime.timezone.utc).replace(microsecond=0, tzinfo=None))))

data = dict(
targets=results,
Expand Down
Expand Up @@ -170,7 +170,7 @@ def cloud_init(args: IntegrationConfig, targets: tuple[IntegrationTarget, ...])

if not args.explain and results:
result_name = '%s-%s.json' % (
args.command, re.sub(r'[^0-9]', '-', str(datetime.datetime.utcnow().replace(microsecond=0))))
args.command, re.sub(r'[^0-9]', '-', str(datetime.datetime.now(tz=datetime.timezone.utc).replace(microsecond=0, tzinfo=None))))

data = dict(
clouds=results,
Expand Down
8 changes: 4 additions & 4 deletions test/lib/ansible_test/_internal/commands/sanity/pylint.py
Expand Up @@ -156,19 +156,19 @@ def context_filter(path_to_filter: str) -> bool:
except CollectionDetailError as ex:
display.warning('Skipping pylint collection version checks since collection detail loading failed: %s' % ex.reason)

test_start = datetime.datetime.utcnow()
test_start = datetime.datetime.now(tz=datetime.timezone.utc)

for context, context_paths in sorted(contexts):
if not context_paths:
continue

context_start = datetime.datetime.utcnow()
context_start = datetime.datetime.now(tz=datetime.timezone.utc)
messages += self.pylint(args, context, context_paths, plugin_dir, plugin_names, python, collection_detail)
context_end = datetime.datetime.utcnow()
context_end = datetime.datetime.now(tz=datetime.timezone.utc)

context_times.append('%s: %d (%s)' % (context, len(context_paths), context_end - context_start))

test_end = datetime.datetime.utcnow()
test_end = datetime.datetime.now(tz=datetime.timezone.utc)

for context_time in context_times:
display.info(context_time, verbosity=4)
Expand Down
6 changes: 2 additions & 4 deletions test/lib/ansible_test/_internal/test.py
Expand Up @@ -114,7 +114,7 @@ def save_junit(self, args: TestConfig, test_case: junit_xml.TestCase) -> None:
junit_xml.TestSuite(
name='ansible-test',
cases=[test_case],
timestamp=datetime.datetime.utcnow(),
timestamp=datetime.datetime.now(tz=datetime.timezone.utc),
),
],
)
Expand Down Expand Up @@ -153,13 +153,11 @@ def write(self, args: TestConfig) -> None:

output += '\n\nConsult the console log for additional details on where the timeout occurred.'

timestamp = datetime.datetime.utcnow()

suites = junit_xml.TestSuites(
suites=[
junit_xml.TestSuite(
name='ansible-test',
timestamp=timestamp,
timestamp=datetime.datetime.now(tz=datetime.timezone.utc),
cases=[
junit_xml.TestCase(
name='timeout',
Expand Down