| 1 | from datetime import datetime |
| 2 | |
| 3 | |
| 4 | def test_hwclock_rate(command): |
| 5 | """Test that the hardware clock rate is not too inaccurate.""" |
| 6 | result = command.run_check("hwclock -c | head -n 3") |
| 7 | hw_time, sys_time, freq_offset_ppm, tick = result[-1].strip().split() |
| 8 | assert abs(int(freq_offset_ppm)) < 1000 |
| 9 | |
| 10 | |
| 11 | def test_hwclock_value(command): |
| 12 | """Test that the hardware clock has the correct time. |
| 13 | |
| 14 | If the time is wrong, it is set once and tested again. |
| 15 | """ |
| 16 | |
| 17 | def get_time(): |
| 18 | result = command.run_check("hwclock --utc --show")[0].strip() |
| 19 | return datetime.strptime(result, "%Y-%m-%d %H:%M:%S.%f+0:00") |
| 20 | |
| 21 | def set_time(time): |
| 22 | time = time.strftime("%Y-%m-%d %H:%M:%S.%f+0:00") |
| 23 | command.run_check(f'hwclock --utc --set --date "{time}"') |
| 24 | |
| 25 | offset = abs((get_time() - datetime.utcnow()).total_seconds()) |
| 26 | if offset > 60: |
| 27 | set_time(datetime.utcnow()) |
| 28 | offset = abs((get_time() - datetime.utcnow()).total_seconds()) |
| 29 | assert offset < 60 |