diff --git a/netjsonconfig/backends/openwisp/openwisp.py b/netjsonconfig/backends/openwisp/openwisp.py index 2e67f0e32..dca27b6d0 100644 --- a/netjsonconfig/backends/openwisp/openwisp.py +++ b/netjsonconfig/backends/openwisp/openwisp.py @@ -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 diff --git a/tests/openwisp/test_backend.py b/tests/openwisp/test_backend.py index 923eb0dc3..5e334b2a1 100644 --- a/tests/openwisp/test_backend.py +++ b/tests/openwisp/test_backend.py @@ -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 @@ -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)