Skip to content

Commit

Permalink
[tests] Added tests zt openwrt multiple servers configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
Aryamanz29 committed Aug 4, 2023
1 parent 254de1a commit 266e22b
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 11 deletions.
2 changes: 1 addition & 1 deletion netjsonconfig/backends/openwrt/converters/zerotier.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def __intermediate_vpn(self, vpn):
def __netjson_vpn(self, vpn):
vpn['id'] = vpn.pop('join')
vpn['name'] = vpn.pop('.name')
# 'enabled' defaults to False in OpenWRT
# 'disabled' defaults to False in OpenWRT
vpn['disabled'] = vpn.pop('enabled', '0') == '0'
del vpn['.type']
return super().__netjson_vpn(vpn)
8 changes: 7 additions & 1 deletion netjsonconfig/backends/zerotier/zerotier.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,13 @@ class ZeroTier(BaseVpnBackend):
config_suffix = config_suffix

@classmethod
def auto_client(cls, name='', nwid=None, identity_secret=None, disabled=False):
def auto_client(
cls,
name='ow_zt',
nwid=None,
identity_secret='{{zt_identity_secret}}',
disabled=False,
):
nwid = nwid or ['']
return {
'id': nwid,
Expand Down
11 changes: 7 additions & 4 deletions tests/openwrt/test_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -550,7 +550,12 @@ def test_zerotier_auto_client(self):
with self.subTest('No arguments provided'):
expected = {
'zerotier': [
{'name': '', 'id': [''], 'secret': None, 'disabled': False}
{
'name': 'ow_zt',
'id': [''],
'secret': '{{zt_identity_secret}}',
'disabled': False,
}
]
}
self.assertDictEqual(OpenWrt.zerotier_auto_client(), expected)
Expand All @@ -560,16 +565,14 @@ def test_zerotier_auto_client(self):
{
'name': 'ow_zt',
'id': ['9536600adf654321'],
'secret': 'test_secret',
'secret': '{{zt_identity_secret}}',
'disabled': False,
}
]
}
self.assertDictEqual(
OpenWrt.zerotier_auto_client(
name='ow_zt',
nwid=['9536600adf654321'],
identity_secret='test_secret',
),
expected,
)
74 changes: 69 additions & 5 deletions tests/openwrt/test_zerotier.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,81 @@

class TestZeroTier(unittest.TestCase, _TabsMixin):
maxDiff = None
_TEST_CONFIG = {
# This configuration is used when we want to join multiple networks
# and the ZT service is running on a single default port 9993
_TEST_SAME_NAME_MULTIPLE_CONFIG = {
"zerotier": [
{
"id": ["9536600adf654321", "9536600adf654322"],
"name": "ow_zt",
"id": ["9536600adf654321", "9536600adf654322"],
},
]
}
# This ZT configuration is used when ZT services
# are configured to run on multiple ports, e.g., 9993 and 9994.
# For more information, refer to:
# https://docs.zerotier.com/zerotier/zerotier.conf/#local-configuration-options
_TEST_DIFF_NAME_MULTIPLE_CONFIG = {
"zerotier": [
{
"name": "ow_zt1",
"id": ["9536600adf654321"],
},
{
"name": "ow_zt2",
"id": ["9536600adf654322"],
},
]
}

def test_zt_multiple_render_diff_name(self):
o = OpenWrt(self._TEST_DIFF_NAME_MULTIPLE_CONFIG)
expected = self._tabs(
"""package zerotier
config zerotier 'ow_zt1'
option enabled '1'
list join '9536600adf654321'
config zerotier 'ow_zt2'
option enabled '1'
list join '9536600adf654322'
"""
)
self.assertEqual(o.render(), expected)

def test_zt_mutiple_parse_diff_name(self):
native = self._tabs(
"""package zerotier
config zerotier 'ow_zt1'
option enabled '1'
list join '9536600adf654321'
config zerotier 'ow_zt2'
option enabled '1'
list join '9536600adf654322'
"""
)
expected = {
"zerotier": [
{
"id": ["9536600adf654321"],
"name": "ow_zt1",
"disabled": False,
},
{
"id": ["9536600adf654322"],
"name": "ow_zt2",
"disabled": False,
},
]
}
o = OpenWrt(native=native)
self.assertEqual(o.config, expected)

def test_render_zerotier(self):
o = OpenWrt(self._TEST_CONFIG)
def test_zt_multiple_render_same_name(self):
o = OpenWrt(self._TEST_SAME_NAME_MULTIPLE_CONFIG)
expected = self._tabs(
"""package zerotier
Expand All @@ -28,7 +92,7 @@ def test_render_zerotier(self):
)
self.assertEqual(o.render(), expected)

def test_parse_zerotier(self):
def test_zt_mutiple_parse_same_name(self):
native = self._tabs(
"""package zerotier
Expand Down

0 comments on commit 266e22b

Please sign in to comment.