Permalink
Browse files

[OpenWrt] Avoid error with "sysupgrade -r"

  • Loading branch information...
nemesisdesign committed Oct 22, 2015
1 parent 30034a6 commit 82cc5e5a9bf8284b0902d1affbaf83077a2d0c1f
Showing with 12 additions and 7 deletions.
  1. +7 −2 netjsonconfig/backends/openwrt/openwrt.py
  2. +5 −5 tests/openwrt/test_backend.py
@@ -141,16 +141,21 @@ def generate(self, name='openwrt-config'):
package_name = lines[0]
text_contents = '\n'.join(lines[2:])
byte_contents = BytesIO(text_contents.encode('utf8'))
info = tarfile.TarInfo(name='/etc/config/{0}'.format(package_name))
info = tarfile.TarInfo(name='etc/config/{0}'.format(package_name))
info.size = len(text_contents)
tar.addfile(tarinfo=info, fileobj=byte_contents)
# insert additional files
for file_item in self.config.get('files', []):
contents = file_item['contents']
path = file_item['path']
# join lines if contents is a list
if isinstance(contents, list):
contents = '\n'.join(contents)
# remove leading slashes from path
if path.startswith('/'):
path = path[1:]
byte_contents = BytesIO(contents.encode('utf8'))
info = tarfile.TarInfo(name=file_item['path'])
info = tarfile.TarInfo(name=path)
info.size = len(contents)
tar.addfile(tarinfo=info, fileobj=byte_contents)
# close archive
@@ -142,7 +142,7 @@ def test_generate(self):
tar = tarfile.open('openwrt-config.tar.gz', 'r:gz')
self.assertEqual(len(tar.getmembers()), 2)
# network
network = tar.getmember('/etc/config/network')
network = tar.getmember('etc/config/network')
contents = tar.extractfile(network).read().decode()
expected = self._tabs("""config interface 'wlan0'
option ifname 'wlan0'
@@ -152,7 +152,7 @@ def test_generate(self):
""")
self.assertEqual(contents, expected)
# wireless
wireless = tar.getmember('/etc/config/wireless')
wireless = tar.getmember('etc/config/wireless')
contents = tar.extractfile(wireless).read().decode()
expected = self._tabs("""config wifi-device 'radio0'
option channel '3'
@@ -281,11 +281,11 @@ def test_file_inclusion(self):
tar = tarfile.open('openwrt-config.tar.gz', 'r:gz')
self.assertEqual(len(tar.getmembers()), 2)
# first file
crontab = tar.getmember('/etc/crontabs/root')
crontab = tar.getmember('etc/crontabs/root')
contents = tar.extractfile(crontab).read().decode()
self.assertEqual(contents, o.config['files'][0]['contents'])
# second file
dummy = tar.getmember('/etc/dummy.conf')
dummy = tar.getmember('etc/dummy.conf')
contents = tar.extractfile(dummy).read().decode()
self.assertEqual(contents, o.config['files'][1]['contents'])
# close and delete tar.gz file
@@ -309,7 +309,7 @@ def test_file_inclusion_list_contents(self):
tar = tarfile.open('openwrt-config.tar.gz', 'r:gz')
self.assertEqual(len(tar.getmembers()), 1)
# check file
crontab = tar.getmember('/root/.ssh/authorized_keys')
crontab = tar.getmember('root/.ssh/authorized_keys')
contents = tar.extractfile(crontab).read().decode()
self.assertEqual(contents, '\n'.join(o.config['files'][0]['contents']))
# close and delete tar.gz file

0 comments on commit 82cc5e5

Please sign in to comment.