Skip to content

Commit c3f34ca

Browse files
pandafynemesifier
authored andcommitted
[fix] OpenWISP1: Added fallback value for hostname in _get_install_context
1 parent 1509340 commit c3f34ca

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

netjsonconfig/backends/openwisp/openwisp.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ def _get_install_context(self):
7979
break
8080
# return context
8181
return dict(
82-
hostname=config['general']['hostname'], # hostname is required
82+
hostname=config.get('general', {}).get('hostname', 'OpenWISP1'),
8383
l2vpn=l2vpn,
8484
bridges=bridges,
8585
radios=config.get('radios', []), # radios might be empty

tests/openwisp/test_backend.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from copy import deepcopy
44
from hashlib import md5
55
from time import sleep
6+
from unittest.mock import patch
67

78
from netjsonconfig import OpenWisp
89
from netjsonconfig.exceptions import ValidationError
@@ -123,6 +124,17 @@ def test_hostname_required(self):
123124
with self.assertRaises(ValidationError):
124125
o.validate()
125126

127+
@patch.object(OpenWisp, 'validate')
128+
def test_fallback_hostname(self, *args):
129+
config = deepcopy(self.config)
130+
del config['general']['hostname']
131+
o = OpenWisp(config)
132+
tar = tarfile.open(fileobj=o.generate(), mode='r')
133+
install = tar.getmember('install.sh')
134+
contents = tar.extractfile(install).read().decode()
135+
self.assertIn('echo "Changing hostname"', contents)
136+
self.assertIn('echo "OpenWISP1" > /proc/sys/kernel/hostname', contents)
137+
126138
def test_install_script(self):
127139
config = deepcopy(self.config)
128140
o = OpenWisp(config)

0 commit comments

Comments
 (0)