Skip to content

Commit 626d43f

Browse files
committed
Verify the systemd-resolved stub resolver is being used by default
Note that this entails installing dnsutils in the Molecule prepare stage so that dig is available when the Molecule tests are run.
1 parent 9a69c22 commit 626d43f

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

molecule/default/prepare.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,14 @@
99
# situation.
1010
- name: Unmount /etc/resolv.conf
1111
ansible.builtin.import_playbook: unmount.yml
12+
13+
# We require dig for one of our Molecule tests
14+
- name: Install dig
15+
hosts: all
16+
become: true
17+
become_method: ansible.builtin.sudo
18+
tasks:
19+
- name: Install dig
20+
ansible.builtin.package:
21+
name:
22+
- dnsutils

molecule/default/tests/test_default.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22

33
# Standard Python Libraries
44
import os
5+
import re
56

67
# Third-Party Libraries
8+
import pytest
79
import testinfra.utils.ansible_runner
810

911
testinfra_hosts = testinfra.utils.ansible_runner.AnsibleRunner(
@@ -51,3 +53,29 @@ def test_services(host):
5153
# assert s.exists, "systemd-resolved service does not exist."
5254
assert s.is_enabled, "systemd-resolved service is not enabled."
5355
assert s.is_running, "systemd-resolved service is not running."
56+
57+
58+
@pytest.mark.parametrize(
59+
"dig_command",
60+
[
61+
"www.yahoo.com",
62+
"AAAA www.yahoo.com",
63+
],
64+
)
65+
def test_dns_resolution(host, dig_command):
66+
"""Verify that the systemd-resolved resolver is being used by default."""
67+
cmd = host.run(f"dig {dig_command}")
68+
assert cmd.rc == 0, f"Command dig {dig_command} did not exit successfully."
69+
# AL2023 is funky. /run/systemd/resolve/stub-resolv.conf is
70+
# itself a symlink to /run/systemd/resolve/resolv.conf, which
71+
# points directly to the nameserver obtained from DNS. I don't
72+
# know why it does this, but our testing must work around it.
73+
if host.system_info.distribution in ["amzn"]:
74+
pass
75+
else:
76+
# Verify that the dig result came from the systemd-resolved
77+
# service.
78+
assert (
79+
re.search(r"^;; SERVER: 127\.0\.0\.53#53", cmd.stdout, re.MULTILINE)
80+
is not None
81+
), f"Command dig {dig_command} did not return a results from 127.0.0.53."

0 commit comments

Comments
 (0)