utils: test: account for last cmd format change in test-aa-notify

The "last" command, which was supplied by util-linux in older Ubuntu
versions, is now supplied by wtmpdb in Oracular and Plucky. Unfortunately,
this changed the output format and broke our column based parsing.

While the wtmpdb upstream has added json support at
https://github.com/thkukuk/wtmpdb/issues/20, we cannot use it because
we need to support systems that do not have this new feature added.

Signed-off-by: Ryan Lee <ryan.lee@canonical.com>
This commit is contained in:
Ryan Lee 2025-01-27 10:15:45 -08:00
parent c81eacacac
commit afd6aa0581

View file

@ -143,11 +143,21 @@ Feb 4 13:40:38 XPS-13-9370 kernel: [128552.880347] audit: type=1400 audit({epoc
return_code, output = cmd(['last', username, '--fullnames', '--time-format', 'iso'])
output = output.split('\n')[0] # the first line is enough
# example of output:
# example of output (util-linux last command):
# ubuntu tty7 :0 2024-01-05T14:29:11-03:00 gone - no logout
# example of output (wtmpdb last command, local login):
# ubuntu tty7 2025-01-15T09:32:49-0800 - still logged in
# example of output (wtmpdb last command, remote login)
# ubuntu tty7 192.168.122.1 2024-01-05T14:29:11-03:00 gone - no logout
if output.startswith(username):
last_login = output.split()[3]
last_login_epoch = datetime.fromisoformat(last_login).timestamp()
# Check both possible columns for the date
try:
last_login = output.split()[3]
last_login_epoch = datetime.fromisoformat(last_login).timestamp()
except (IndexError, ValueError):
last_login = output.split()[2]
last_login_epoch = datetime.fromisoformat(last_login).timestamp()
# add 60 seconds to the epoch so that the time in the logs are AFTER login time
last_login_contents = cls.create_logfile_contents(last_login_epoch + 60)
file_last_login.write(last_login_contents)