Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include old resolv.conf contents without nameservers #10731

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions localstack-core/localstack/dns/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -809,8 +809,14 @@ def add_resolv_entry(file_path: Path | str = Path("/etc/resolv.conf")):
try:
with file_path.open("r+") as outfile:
PREVIOUS_RESOLV_CONF_FILE = outfile.read()
previous_resolv_conf_without_nameservers = [
line
for line in PREVIOUS_RESOLV_CONF_FILE.splitlines()
if not line.startswith("nameserver")
]
outfile.seek(0)
outfile.write(content)
outfile.write("\n".join(previous_resolv_conf_without_nameservers))
outfile.truncate()
except Exception:
LOG.warning(
Expand Down
27 changes: 27 additions & 0 deletions tests/unit/test_dns_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,33 @@ def test_resolv_conf_overwriting(self, tmp_path: Path, monkeypatch):

assert "nameserver 127.0.0.1" in new_contents.splitlines()

def test_exising_resolv_conf_contents(self, tmp_path: Path, monkeypatch):
from localstack.dns import server

monkeypatch.setattr(server, "in_docker", lambda: True)

file = tmp_path.joinpath("resolv.conf")
with file.open("w") as outfile:
print(
"nameserver 127.0.0.11\n"
"search default.svc.cluster.local svc.cluster.local cluster.local\n"
"options ndots:5",
file=outfile,
)

add_resolv_entry(file)

with file.open() as infile:
new_contents = infile.read()

lines = new_contents.splitlines()
assert "nameserver 127.0.0.1" in lines
dfangl marked this conversation as resolved.
Show resolved Hide resolved
assert "search default.svc.cluster.local svc.cluster.local cluster.local" in lines
assert "options ndots:5" in lines

# check the previous value is _not_ in the file
assert "nameserver 127.0.0.11" not in lines

def test_no_resolv_conf_overwriting_on_host(self, tmp_path: Path, monkeypatch):
from localstack.dns import server

Expand Down