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

[fix] OpenWISP1: Added fallback value for hostname in _get_install_context #328

Merged
merged 1 commit into from
Dec 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion netjsonconfig/backends/openwisp/openwisp.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ def _get_install_context(self):
break
# return context
return dict(
hostname=config['general']['hostname'], # hostname is required
hostname=config.get('general', {}).get('hostname', 'OpenWISP1'),
l2vpn=l2vpn,
bridges=bridges,
radios=config.get('radios', []), # radios might be empty
Expand Down
12 changes: 12 additions & 0 deletions tests/openwisp/test_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from copy import deepcopy
from hashlib import md5
from time import sleep
from unittest.mock import patch

from netjsonconfig import OpenWisp
from netjsonconfig.exceptions import ValidationError
Expand Down Expand Up @@ -123,6 +124,17 @@ def test_hostname_required(self):
with self.assertRaises(ValidationError):
o.validate()

@patch.object(OpenWisp, 'validate')
def test_fallback_hostname(self, *args):
config = deepcopy(self.config)
del config['general']['hostname']
o = OpenWisp(config)
tar = tarfile.open(fileobj=o.generate(), mode='r')
install = tar.getmember('install.sh')
contents = tar.extractfile(install).read().decode()
self.assertIn('echo "Changing hostname"', contents)
self.assertIn('echo "OpenWISP1" > /proc/sys/kernel/hostname', contents)

def test_install_script(self):
config = deepcopy(self.config)
o = OpenWisp(config)
Expand Down
Loading