Skip to content

Commit 0c8bef5

Browse files
author
Ritwick DSouza
committed
[raspbian] Added tests for generate and write methods
1 parent 019b02e commit 0c8bef5

1 file changed

Lines changed: 67 additions & 0 deletions

File tree

tests/raspbian/test_backend.py

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import os
2+
import tarfile
3+
import unittest
4+
5+
from netjsonconfig import Raspbian
6+
from netjsonconfig.utils import _TabsMixin
7+
8+
class TestBackend(unittest.TestCase, _TabsMixin):
9+
10+
def test_generate(self):
11+
o = Raspbian({
12+
"interfaces": [
13+
{
14+
"name": "eth0",
15+
"type": "ethernet",
16+
"addresses": [
17+
{
18+
"address": "192.168.1.1",
19+
"mask": 24,
20+
"proto": "static",
21+
"family": "ipv4"
22+
}
23+
]
24+
}
25+
],
26+
"dns_servers": [
27+
"10.11.12.13",
28+
"8.8.8.8"
29+
],
30+
"dns_search": [
31+
"netjson.org",
32+
"openwisp.org"
33+
]
34+
})
35+
tar = tarfile.open(fileobj=o.generate(), mode='r')
36+
self.assertEqual(len(tar.getmembers()), 2)
37+
38+
interface = tar.getmember('/etc/network/interfaces')
39+
contents = tar.extractfile(interface).read().decode()
40+
expected = self._tabs('''auto eth0
41+
iface eth0 inet static
42+
address 192.168.1.1
43+
netmask 255.255.255.0
44+
45+
''')
46+
self.assertEqual(contents, expected)
47+
48+
resolv = tar.getmember('/etc/resolv.conf')
49+
contents = tar.extractfile(resolv).read().decode()
50+
expected = self._tabs('''nameserver 10.11.12.13
51+
nameserver 8.8.8.8
52+
search netjson.org
53+
search openwisp.org
54+
''')
55+
self.assertEqual(contents, expected)
56+
57+
def test_write(self):
58+
o = Raspbian({
59+
"general": {
60+
"hostname": "test"
61+
}
62+
})
63+
o.write(name='test', path='/tmp')
64+
tar = tarfile.open('/tmp/test.tar.gz', mode='r')
65+
self.assertEqual(len(tar.getmembers()), 1)
66+
tar.close()
67+
os.remove('/tmp/test.tar.gz')

0 commit comments

Comments
 (0)