Skip to content

Commit

Permalink
Enhance CI tests for chrony role
Browse files Browse the repository at this point in the history
Signed-off-by: Sebastian Neumann <[email protected]>
  • Loading branch information
sbstnnmnn committed Sep 19, 2024
1 parent 152f3b8 commit 4674ded
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 0 deletions.
18 changes: 18 additions & 0 deletions molecule/delegated/prepare/chrony.yml
Original file line number Diff line number Diff line change
@@ -1 +1,19 @@
---
- name: Ensure EPEL repository
ansible.builtin.include_role:
name: osism.commons.repository
when: ansible_os_family == "RedHat"

- name: Install netcat for Debian/Ubuntu
become: true
ansible.builtin.apt:
name: netcat
state: present
when: ansible_os_family == "Debian"

- name: Install netcat for RedHat/CentOS
become: true
ansible.builtin.dnf:
name: netcat
state: present
when: ansible_os_family == "RedHat"
52 changes: 52 additions & 0 deletions molecule/delegated/tests/chrony/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,55 @@ def test_configfile(host):
assert f.exists
assert not f.is_directory
assert f.mode == 0o644

chrony_servers = get_variable(host, "chrony_servers")
content = f.content_string

for server in chrony_servers:
assert f"server {server}" in content


def test_chrony_servers_reachable(host):
"""Check if chrony servers are reachable"""

chrony_servers = get_variable(host, "chrony_servers")
assert isinstance(chrony_servers, list)
assert len(chrony_servers) > 0

for server in chrony_servers:
# Resolve the hostname to an IP address
cmd_resolve = host.run(f"dig +short {server}")
assert cmd_resolve.rc == 0
ip_addresses = cmd_resolve.stdout.strip().split("\n")

assert len(ip_addresses) > 0

# Check if at least one IP address is reachable
reachable = False
for ip in ip_addresses:
cmd_ping = host.run(f"ping -c 1 -W 2 {ip}")
if cmd_ping.rc == 0:
reachable = True
break

assert reachable

# Check if the NTP port is open
cmd_ntp = host.run(f"nc -zu -w 2 {server} 123")
assert cmd_ntp.rc == 0


def test_chrony_status(host):
"""Check chrony status and sources."""

cmd = host.run("chronyc sources")
assert cmd.rc == 0

# Check if there are any reachable sources
assert "^*" in cmd.stdout or "^+" in cmd.stdout

cmd = host.run("chronyc tracking")
assert cmd.rc == 0

# Check if system clock is synchronized
assert "Leap status : Normal" in cmd.stdout

0 comments on commit 4674ded

Please sign in to comment.