Skip to content

Commit

Permalink
[fix] Deduplicate files
Browse files Browse the repository at this point in the history
If a file is present in multiple places (templates, configuration)
The last definition wins.

eg:

- the configuration overrides the template
- the second template overrides the first
  • Loading branch information
nemesifier committed May 27, 2020
1 parent 5182887 commit 092e231
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
10 changes: 10 additions & 0 deletions netjsonconfig/backends/base/backend.py
Expand Up @@ -107,6 +107,15 @@ def _render_files(self):
output += file_output
return output

def _deduplicate_files(self):
files = self.config.get('files', [])
if not files:
return
files_dict = OrderedDict()
for file in files:
files_dict[file['path']] = file
self.config['files'] = list(files_dict.values())

def validate(self):
try:
Draft4Validator(self.schema, format_checker=draft4_format_checker).validate(self.config)
Expand All @@ -125,6 +134,7 @@ def render(self, files=True):
# convert NetJSON config to intermediate data structure
if self.intermediate_data is None:
self.to_intermediate()
self._deduplicate_files()
# support multiple renderers
renderers = getattr(self, 'renderers', None) or [self.renderer]
# convert intermediate data structure to native configuration
Expand Down
35 changes: 35 additions & 0 deletions tests/openwrt/test_backend.py
Expand Up @@ -408,3 +408,38 @@ def test_value_error(self):
OpenWrt(templates=[])
with self.assertRaises(ValueError):
OpenWrt(context=[])

def test_override_file(self):
o = OpenWrt({
"files": [
{
"path": "/etc/crontabs/root",
"mode": "0644",
"contents": "*/5 * * * * /command1\n*/5 * * * * /command2"
}
]
}, templates=[
{
"files": [
{
"path": "/etc/crontabs/root",
"mode": "0644",
"contents": "*/5 * * * * /command1"
}
]
}
])
expected = """
# ---------- files ---------- #
# path: /etc/crontabs/root
# mode: 0644
*/5 * * * * /command1
*/5 * * * * /command2
"""
self.assertEqual(o.render(), expected)
# ensure the additional files are there present in the tar.gz archive
tar = tarfile.open(fileobj=o.generate(), mode='r')
self.assertEqual(len(tar.getmembers()), 1)

0 comments on commit 092e231

Please sign in to comment.