Skip to content

Commit 4ebbc8a

Browse files
[fix] Ignored duplicate list elements when merging templates #197
Detect identical elements present in both lists to avoid adding the duplicate to the result. This is needed because some templates may share one or multiple common files and these do not not have to be duplicated. Fixes #197 Co-authored-by: Federico Capoano <f.capoano@openwisp.io>
1 parent e696759 commit 4ebbc8a

3 files changed

Lines changed: 55 additions & 0 deletions

File tree

netjsonconfig/utils.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,13 @@ def merge_list(list1, list2, identifiers=None):
5555
for el in list_:
5656
# merge by internal python id by default
5757
key = id(el)
58+
# Detect identical elements present in both lists
59+
# avoid adding the duplicate to the result.
60+
# This is needed because some templates may share
61+
# one or multiple common files and these do not
62+
# not have to be duplicated.
63+
if counter == 2 and el in dict_map['list1'].values():
64+
continue
5865
# if el is a dict, merge by keys specified in ``identifiers``
5966
if isinstance(el, dict):
6067
for id_key in identifiers:

tests/openvpn/test_backend.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -886,3 +886,42 @@ def test_tls_auth_key_present(self):
886886
)
887887
client = OpenVpn(client_config)
888888
self.assertEqual(client.render(), self._openvpn_client_tls_auth_render)
889+
890+
def test_ca_same_file_path_for_same_device(self):
891+
conf = {
892+
"ca": "/etc/x509/ca.pem",
893+
"cert": "cert.pem",
894+
"dev": "tap0",
895+
"dev_type": "tap",
896+
"dh": "dh.pem",
897+
"key": "key.pem",
898+
"mode": "server",
899+
"name": "test-1",
900+
"proto": "udp",
901+
"tls_server": True,
902+
}
903+
ca_file = {"path": "/etc/x509/ca.pem", "mode": "0644", "contents": "testing!"}
904+
template1 = {
905+
"openvpn": [{**conf}],
906+
"files": [ca_file],
907+
}
908+
template2 = {
909+
"openvpn": [
910+
{
911+
**conf,
912+
"name": "test-2",
913+
"cert": "cert2.pem",
914+
}
915+
],
916+
"files": [ca_file],
917+
}
918+
client = OpenVpn(
919+
{
920+
"openvpn": [conf],
921+
},
922+
templates=[template1, template2],
923+
)
924+
try:
925+
client.render()
926+
except ValidationError:
927+
self.fail('ValidationError raised!')

tests/test_utils.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,3 +138,12 @@ def test_merge_list_config_value(self):
138138

139139
def test_get_copy_default(self):
140140
self.assertEqual('test', get_copy({}, key='hello', default='test'))
141+
142+
def test_merge_list_skip_duplicates(self):
143+
conf1 = [{"mode": "0644", "contents": "test"}]
144+
conf2 = [
145+
{"mode": "0644", "contents": "test"},
146+
[{"mode": "0644", "contents": "test2"}],
147+
]
148+
result = merge_list(conf1, conf2)
149+
self.assertEqual(result, conf2)

0 commit comments

Comments
 (0)