Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions cloudinit/analyze/dump.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import sys
from datetime import datetime

from cloudinit import atomic_helper, subp
from cloudinit import atomic_helper, subp, util

stage_to_description = {
"finished": "finished running cloud-init",
Expand Down Expand Up @@ -44,14 +44,23 @@ def parse_timestamp(timestampstr):
dt = datetime.strptime(timestampstr, CLOUD_INIT_ASCTIME_FMT)
timestamp = dt.strftime("%s.%f")
else:
# allow date(1) to handle other formats we don't expect
# allow GNU date(1) to handle other formats we don't expect
# This may throw a ValueError if no GNU date can be found
timestamp = parse_timestamp_from_date(timestampstr)

return float(timestamp)


def parse_timestamp_from_date(timestampstr):
out, _ = subp.subp(["date", "+%s.%3N", "-d", timestampstr])
cmd = "date"
if not util.is_Linux():
if subp.which("gdate"):
cmd = "gdate"
else:
raise ValueError(
f"Unable to parse timestamp without GNU date: [{timestampstr}]"
)
out, _ = subp.subp([cmd, "+%s.%3N", "-d", timestampstr])
timestamp = out.strip()
return float(timestamp)

Expand Down
8 changes: 6 additions & 2 deletions tests/unittests/analyze/test_dump.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
parse_timestamp,
)
from cloudinit.subp import which
from cloudinit.util import write_file
from cloudinit.util import is_Linux, write_file
from tests.unittests.helpers import mock, skipIf


Expand Down Expand Up @@ -44,7 +44,11 @@ def test_parse_timestamp_handles_journalctl_format_adding_year(self):
assert float(dt.strftime("%s.%f")) == parse_timestamp(journal_stamp)

@skipIf(not which("date"), "'date' command not available.")
@pytest.mark.allow_subp_for("date")
@skipIf(
not is_Linux() and not which("gdate"),
"'GNU date' command not available.",
)
@pytest.mark.allow_subp_for("date", "gdate")
def test_parse_unexpected_timestamp_format_with_date_command(self):
"""Dump sends unexpected timestamp formats to date for processing."""
new_fmt = "%H:%M %m/%d %Y"
Expand Down