Skip to content

Commit

Permalink
Avoid modifying original config argument
Browse files Browse the repository at this point in the history
  • Loading branch information
nemesifier committed Dec 2, 2015
1 parent 69197ed commit 0005186
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
4 changes: 3 additions & 1 deletion netjsonconfig/backends/openwrt/openwrt.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import time
import tarfile
from io import BytesIO
from copy import deepcopy

from jsonschema import validate
from jsonschema.exceptions import ValidationError as JsonSchemaError
Expand Down Expand Up @@ -32,7 +33,8 @@ def __init__(self, config, templates=[]):
as a base for the main config, defaults to empty list
:raises TypeError: raised if config is not dict or templates is not a list
"""
config = self._load(config)
# perform deepcopy to avoid modifying the original config argument
config = deepcopy(self._load(config))
# allow omitting NetJSON type
if 'type' not in config:
config.update({'type': 'DeviceConfiguration'})
Expand Down
7 changes: 7 additions & 0 deletions tests/openwrt/test_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,15 @@ class TestBackend(unittest.TestCase, _TabsMixin):
"""
tests for backends.openwrt.OpenWrt
"""
def test_config_copy(self):
config = {'interfaces': []}
o = OpenWrt(config)
o.validate()
self.assertDictEqual(config, {'interfaces': []})

def test_json_method(self):
config = {
"type": "DeviceConfiguration",
"interfaces": [
{
"name": "lo",
Expand Down

0 comments on commit 0005186

Please sign in to comment.