forked from saschpe/libvirt-hook-qemu
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_hook.py
executable file
·111 lines (87 loc) · 3.17 KB
/
test_hook.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
#!/usr/bin/python3
"""
Libvirt port-forwarding hook unit tests.
0.0.3:
======
* Uses in source JSON config
* Mocking of the iptables call
* Patching of the IPTABLES_BINARY environment variable to avoid sudo.
"""
import json
import imp
import unittest
from unittest import mock
from unittest.mock import patch
from hookjsonconf import HookConfig
with patch.dict('os.environ', values={'IPTABLES_BINARY': 'iptables'}, clear=True):
from hooks import IPTABLES_BINARY, ctrl_network, ctrl_machine, logged_call
TEST_CONFIG = """
{
"debug": false,
"machines": {
"test": {
"private_ip": "192.168.122.2",
"port_map": [
["2222", "22"],
["8002", "80"]
]
}
},
"networks": {
"default": "192.168.122.0/24"
},
"public_ip": "192.168.0.166"
}
"""
def dummy_func(args, config):
pass
class QemuTestCase(unittest.TestCase):
config = None
def __init__(self, *args, **kwargs):
super(QemuTestCase, self).__init__(*args, **kwargs)
self.config = None
try:
# Import configuration
json_config = HookConfig()
self.config = HookConfig(TEST_CONFIG).config
except FileNotFoundError:
print('No config.json found, terminating.')
except json.JSONDecodeError as jde:
print('Error loading configuration file: {} in {} line {} char {}'.format(
jde.msg, jde.doc, jde.lineno, jde.colno))
def test_config(self):
self.assertEqual(self.config['debug'], False)
self.assertEqual(self.config['machines'], {
'test': {
'private_ip': '192.168.122.2',
'port_map': [['2222', '22'], ['8002', '80']]
}
})
self.assertEqual(self.config['networks'], {
'default': '192.168.122.0/24'
})
self.assertEqual(self.config['public_ip'], '192.168.0.166')
@mock.patch('hooks.logged_call', side_effect=dummy_func)
def test_network_plugged(self, logged_call_function):
cmd = ctrl_network('plugged', 'default', self.config)
self.assertEqual(cmd, [
IPTABLES_BINARY + ' -I FORWARD -m state -d 192.168.122.0/24 --state NEW,RELATED,ESTABLISHED -j ACCEPT'])
@mock.patch('hooks.logged_call', side_effect=dummy_func)
def test_network_unplugged(self, logged_call_function):
cmd = ctrl_network('unplugged', 'default', self.config)
self.assertEqual(cmd, [
IPTABLES_BINARY + ' -D FORWARD -m state -d 192.168.122.0/24 --state NEW,RELATED,ESTABLISHED -j ACCEPT'])
@mock.patch('hooks.logged_call', side_effect=dummy_func)
def test_machine_start(self, logged_call_function):
ctrl_machine('start', 'test', self.config)
pass
@mock.patch('hooks.logged_call', side_effect=dummy_func)
def test_machine_stopped(self, logged_call_function):
ctrl_machine('stopped', 'test', self.config)
pass
@mock.patch('hooks.logged_call', side_effect=dummy_func)
def test_reconnect(self, logged_call_function):
ctrl_machine('reconnect', 'test', self.config)
pass
if __name__ == '__main__':
unittest.main()