Skip to content

Commit

Permalink
refactor(update-server): Don't add extra newlines to /etc/machine-inf…
Browse files Browse the repository at this point in the history
…o on update (#4671)
  • Loading branch information
SyntaxColoring authored and sfoster1 committed Jan 21, 2020
1 parent af819a1 commit b867f96
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 5 deletions.
21 changes: 16 additions & 5 deletions update-server/otupdate/buildroot/name_management.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,15 +182,26 @@ def _update_pretty_hostname(new_val: str):
except OSError:
LOG.exception("Couldn't read /etc/machine-info")
contents = ''
new_contents = ''
for line in contents.split('\n'):
if not line.startswith('PRETTY_HOSTNAME'):
new_contents += f'{line}\n'
new_contents += f'PRETTY_HOSTNAME={new_val}\n'
new_contents = _rewrite_machine_info(contents, new_val)
with open('/etc/machine-info', 'w') as emi:
emi.write(new_contents)


def _rewrite_machine_info(
current_machine_info_contents: str, new_pretty_hostname: str) -> str:
"""
Return current_machine_info_contents - the full contents of
/etc/machine-info - with the PRETTY_HOSTNAME=... line rewritten to refer
to new_pretty_hostname.
"""
current_lines = current_machine_info_contents.splitlines()
preserved_lines = [
l for l in current_lines if not l.startswith('PRETTY_HOSTNAME')]
new_lines = preserved_lines + [f'PRETTY_HOSTNAME={new_pretty_hostname}']
new_contents = '\n'.join(new_lines) + '\n'
return new_contents


def get_name(default: str = 'no name set'):
""" Get the currently-configured name of the machine """
try:
Expand Down
42 changes: 42 additions & 0 deletions update-server/tests/buildroot/test_name_management.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from otupdate.buildroot import name_management
import pytest
from collections import Counter


async def test_name_endpoint(test_cli, monkeypatch):
Expand Down Expand Up @@ -41,3 +43,43 @@ async def fake_name(name):
health = await test_cli.get('/server/update/health')
health_body = await health.json()
assert health_body['name'] == to_set + to_set


machine_info_examples = [
'',
'FOO=foo',
'PRETTY_HOSTNAME=initial_pretty_hostname',
'FOO=foo\nPRETTY_HOSTNAME=initial_pretty_hostname\nBAR=bar'
]


@pytest.mark.parametrize('initial_contents', machine_info_examples)
def test_rewrite_machine_info_updates_pretty_hostname(initial_contents):
rewrite = name_management._rewrite_machine_info(
initial_contents, 'new_pretty_hostname')
assert 'PRETTY_HOSTNAME=new_pretty_hostname' in rewrite.splitlines(), \
'new PRETTY_HOSTNAME should be present.'
assert rewrite.count('PRETTY_HOSTNAME') == 1, \
'Old PRETTY_HOSTNAME should be deleted.'


@pytest.mark.parametrize('initial_contents', machine_info_examples)
def test_rewrite_machine_info_preserves_other_lines(initial_contents):
intitial_lines = Counter(initial_contents.splitlines())
rewrite_string = name_management._rewrite_machine_info(
initial_contents, 'new_pretty_hostname')
rewrite_lines = Counter(rewrite_string.splitlines())
lost_lines = intitial_lines - rewrite_lines
for line in lost_lines:
# Lines are only allowed to be "lost" in the rewrite if they were an
# old PRETTY_HOSTNAME assignment, or were blank.
assert line.startswith('PRETTY_HOSTNAME=') or line == ''


@pytest.mark.parametrize('initial_contents', machine_info_examples)
def test_rewrite_machine_info_is_idempotent(initial_contents):
first_rewrite = name_management._rewrite_machine_info(
initial_contents, 'new_pretty_hostname')
second_rewrite = name_management._rewrite_machine_info(
first_rewrite, 'new_pretty_hostname')
assert second_rewrite == first_rewrite

0 comments on commit b867f96

Please sign in to comment.