Skip to content

Commit

Permalink
[fix] OpenWISP1: Added fallback value for hostname in _get_install_co…
Browse files Browse the repository at this point in the history
…ntext
  • Loading branch information
pandafy committed Dec 3, 2024
1 parent 076aa2f commit ef36af0
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
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

0 comments on commit ef36af0

Please sign in to comment.