Skip to content

Commit 1839f5e

Browse files
committed
CI do not assume test ansible controller has configured timezone
Failed CI job https://github.com/ScaleComputing/HyperCoreAnsibleCollection/actions/runs/4696220997/jobs/8326124886#step:8:49 The timezone from https://ipapi.co/timezone is correct one, it is based on public IP address/geolocation. But out test ansible controller is not configured with timezone. UTC is just OK for servers. We can convert unix epoch to local time with python and use pytz for that. pytz is used only for testing. The timezone from ipapi.co is not needed anymore. Signed-off-by: Justin Cinkelj <justin.cinkelj@xlab.si>
1 parent 5bfa3cb commit 1839f5e

File tree

1 file changed

+65
-27
lines changed
  • tests/integration/targets/role_check_local_time/tasks

1 file changed

+65
-27
lines changed

tests/integration/targets/role_check_local_time/tasks/main.yml

Lines changed: 65 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,15 @@
55
SC_PASSWORD: "{{ sc_config[sc_host].sc_password }}"
66
SC_TIMEOUT: "{{ sc_timeout }}"
77

8-
block:
9-
- name: Get Timezone
10-
ansible.builtin.uri:
11-
url: https://ipapi.co/timezone
12-
return_content: true
13-
register: response
8+
vars:
9+
# Test with timezone far away from UTC, to make difference local vs UTC obvious.
10+
test_timezone: "America/New_York"
11+
# test_timezone: "Europe/Ljubljana" # almost same as UTC
1412

13+
block:
1514
- name: Show Timezone
1615
ansible.builtin.debug:
17-
msg: Timezone is {{ response.content }}
16+
msg: Timezone is {{ test_timezone }}
1817

1918
# ansible_date_time is incremented for a few second for each next task.
2019
# Store value, so we have a stable value.
@@ -24,34 +23,73 @@
2423

2524
- name: Show current time
2625
ansible.builtin.debug:
27-
msg: current_date_time={{ current_date_time }} TZ={{ current_date_time.tz }} hour={{ current_date_time.hour }}
26+
msg: current_date_time={{ current_date_time }} TZ={{ current_date_time.tz }} hour={{ current_date_time.hour }} epoch_int={{ current_date_time.epoch_int }}
27+
# current_date_time.tz might not contain correct timezone.
2828

29-
# The scheduled integration tests run near 3.00 UTC (5:00 CEST).
30-
# ansible_date_time.hour is reported in local timezone.
31-
# It should be safe to add/subtract 1 hour to prevent occasional test failures,
32-
# if scheduled test is run near x:59:59.
29+
# Ansible ansible_date_time.hour should be in local timezone,
30+
# but required local timezone to be configured on OS level.
31+
# Convert epoch from ansible_date_time to local time.
32+
# We will create a dedicated venv, then we can use pytz
33+
# Our CI image has python3 on path
34+
- name: Create python venv with pytz
35+
ansible.builtin.shell: |
36+
python3 -m venv /tmp/venv-pytz
37+
/tmp/venv-pytz/bin/pip install pytz
3338
34-
# Manual testing - can be run at arbitrary time, can always be a problem. So extra assert
35-
- name: Check "current_date_time.hour +/- 1" is still a valid hour
36-
ansible.builtin.assert:
37-
that:
38-
- 1 <= (current_date_time.hour | int)
39-
- (current_date_time.hour | int) <= 22
39+
- name: Convert UTC timestamp to localtime, +/- 1 hour
40+
ansible.builtin.shell: |
41+
#!/tmp/venv-pytz/bin/python3
42+
import json
43+
import sys
44+
from datetime import datetime
45+
import pytz
46+
47+
# timezone = 'America/New_York'
48+
# unix_timestamp = 1681452184
49+
timezone, unix_timestamp = sys.stdin.read().split()
50+
unix_timestamp = int(unix_timestamp)
51+
local_tz = pytz.timezone(timezone)
52+
53+
# m1/p1 suffix - minus/plus 1 hour
54+
dt = datetime.fromtimestamp(unix_timestamp, pytz.utc)
55+
dtm1 = datetime.fromtimestamp(unix_timestamp - 3600, pytz.utc)
56+
dtp1 = datetime.fromtimestamp(unix_timestamp + 3600, pytz.utc)
57+
local_dt = dt.astimezone(local_tz)
58+
local_dtm1 = dtm1.astimezone(local_tz)
59+
local_dtp1 = dtp1.astimezone(local_tz)
60+
61+
ret = dict(
62+
hour=local_dt.hour,
63+
hour_m1=local_dtm1.hour,
64+
hour_p1=local_dtp1.hour,
65+
)
66+
print(json.dumps(ret))
67+
args:
68+
# /usr/bin/python3 - fedora
69+
# /usr/local/bin/python - python:3.10-slim-buster docker image
70+
executable: /tmp/venv-pytz/bin/python3
71+
stdin: "{{ test_timezone }} {{ current_date_time.epoch_int }}"
72+
changed_when: false
73+
register: local_time_result
74+
75+
- name: Set fact local_time
76+
ansible.builtin.set_fact:
77+
local_time: "{{ local_time_result.stdout | from_json }}"
4078

4179
# ------------------------------------------------------------------------------
4280
- name: Check that local time meets required time interval
4381
ansible.builtin.include_role:
4482
name: scale_computing.hypercore.check_local_time
4583
vars:
46-
time_zone: "{{ response.content }}" # ansible_date_time.tz returns CEST which is not a valid tz for env var TZ
47-
time_interval: "{{ (current_date_time.hour | int) - 1 }}:00-{{ (current_date_time.hour | int) + 1 }}:59"
48-
84+
time_zone: "{{ test_timezone }}" # ansible_date_time.tz returns CEST which is not a valid tz for env var TZ
85+
time_interval: "{{ local_time.hour_m1 }}:00-{{ local_time.hour_p1 }}:59"
86+
4987
- name: Check local_time_msg for passed case
5088
ansible.builtin.assert:
5189
that:
5290
- >-
53-
'Local time for time zone {{ response.content }} is in required time interval
54-
{{ (current_date_time.hour | int) - 1 }}:00-{{ (current_date_time.hour | int) + 1 }}:59' in local_time_msg
91+
'Local time for time zone {{ test_timezone }} is in required time interval
92+
{{ local_time.hour_m1 }}:00-{{ local_time.hour_p1 }}:59' in local_time_msg
5593
5694
# ------------------------------------------------------------------------------
5795
- name: Check that local time doesn't meet required time interval
@@ -60,12 +98,12 @@
6098
apply:
6199
ignore_errors: True
62100
vars:
63-
time_zone: "{{ response.content }}"
64-
time_interval: "{{ (current_date_time.hour | int) - 1 }}:00-{{ (current_date_time.hour | int) - 1 }}:01"
101+
time_zone: "{{ test_timezone }}"
102+
time_interval: "{{ local_time.hour_m1 }}:00-{{ local_time.hour_m1 }}:01"
65103

66104
- name: Check local_time_msg for failed case
67105
ansible.builtin.assert:
68106
that:
69107
- >-
70-
'Local time for time zone {{ response.content }} is not in required time interval
71-
{{ (current_date_time.hour | int) - 1 }}:00-{{ (current_date_time.hour | int) - 1 }}:01' in local_time_msg
108+
'Local time for time zone {{ test_timezone }} is not in required time interval
109+
{{ local_time.hour_m1 }}:00-{{ local_time.hour_m1 }}:01' in local_time_msg

0 commit comments

Comments
 (0)