/
test_bin.py
130 lines (116 loc) · 4.79 KB
/
test_bin.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import json
import os
import subprocess
import tarfile
import unittest
from netjsonconfig.utils import _TabsMixin
class TestBin(unittest.TestCase, _TabsMixin):
"""
tests for netjsonconfig command line tool
"""
@classmethod
def tearDownClass(self):
os.remove('test.tar.gz')
def test_file_not_found(self):
with self.assertRaises(subprocess.CalledProcessError):
subprocess.check_output("netjsonconfig -c WRONG -b openwrt -m generate", shell=True)
def test_invalid_netjson(self):
command = '''netjsonconfig -c '{ "interfaces":["w"] }' -b openwrt -m render'''
with self.assertRaises(subprocess.CalledProcessError):
subprocess.check_output(command, shell=True)
def test_invalid_netjson_verbose(self):
command = '''netjsonconfig -c '{ "interfaces":["w"] }' -b openwrt -m render --verbose'''
with self.assertRaises(subprocess.CalledProcessError):
subprocess.check_output(command, shell=True)
def test_empty_netjson(self):
output = subprocess.check_output("netjsonconfig -c '{}' -b openwrt -m render", shell=True)
self.assertEqual(output.decode(), '')
def test_templates(self):
config = json.dumps({
'general': {'hostname': 'template_test'}
})
template1 = json.dumps({
'interfaces': [
{
"name": "eth0",
"type": "ethernet",
"addresses": [
{
"proto": "dhcp",
"family": "ipv4"
}
]
}
]
})
template2 = json.dumps({
'interfaces': [
{
"name": "wlan0",
"type": "wireless",
"addresses": [
{
"proto": "dhcp",
"family": "ipv6"
}
]
}
]
})
command = """netjsonconfig --config '{0}' -b openwrt -m render --templates '{1}' '{2}'"""
command = command.format(config, template1, template2)
output = subprocess.check_output(command, shell=True).decode()
self.assertIn("hostname 'template_test'", output)
self.assertIn("interface 'eth0'", output)
self.assertIn("interface 'wlan0'", output)
def test_invalid_template(self):
command = "netjsonconfig -c '{}' -b openwrt -t WRONG -m render"
try:
subprocess.check_output(command, shell=True)
except subprocess.CalledProcessError as e:
self.assertIn('"WRONG": file not found', e.output.decode())
else:
self.fail('subprocess.CalledProcessError not raised')
def test_invalid_arguments(self):
command = "netjsonconfig -c '{}' -b openwrt -m render -a WRONG"
try:
subprocess.check_output(command, shell=True)
except subprocess.CalledProcessError as e:
self.assertIn('--arg option expects', e.output.decode())
else:
self.fail('subprocess.CalledProcessError not raised')
def test_arg_exception(self):
command = "netjsonconfig -c '{}' -b openwrt -m write"
try:
subprocess.check_output(command, shell=True)
except subprocess.CalledProcessError as e:
self.assertIn('write()', e.output.decode())
else:
self.fail('subprocess.CalledProcessError not raised')
def test_valid_arg(self):
config = json.dumps({
'general': {'hostname': 'template_test'},
'files': [
{
'path': '/etc/test.txt',
'contents': 'test_valid_arg'
}
]
})
command = "netjsonconfig --config '{0}' -b openwrt -m render -a files=False".format(config)
output = subprocess.check_output(command, shell=True).decode()
self.assertNotIn('test.txt', output)
self.assertNotIn('test_valid_arg', output)
def test_generate_redirection(self):
config = """'{"general": { "hostname": "example" }}'"""
command = """netjsonconfig -c %s -b openwrt -m generate > test.tar.gz""" % config
subprocess.check_output(command, shell=True)
tar = tarfile.open('test.tar.gz', 'r')
self.assertEqual(len(tar.getmembers()), 1)
tar.close()
def test_context(self):
config = json.dumps({'general': {'description': '{{ DESC }}'}})
command = "export DESC=testdesc; netjsonconfig --config '{0}' -b openwrt -m render".format(config)
output = subprocess.check_output(command, shell=True).decode()
self.assertNotIn('{{ DESC }}', output)
self.assertIn('testdesc', output)