-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest.py
executable file
·178 lines (152 loc) · 7.3 KB
/
test.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#!/usr/bin/env python
import unittest
import mock
import app
# Make sure we always return a fresh copy of these values. The tests
# modify the values and we don't want those to leak between tests.
def all_params():
return {'local_interface': 'p9p1',
'network_cidr': '10.0.0.0/24',
'node_count': '25',
'undercloud_hostname': 'uc-prod.tripleo.org',
'local_ip': '10.0.0.10/24',
'local_mtu': '9000',
'dhcp_start': '10.0.0.20',
'dhcp_end': '10.0.0.60',
'inspection_start': '10.0.0.100',
'inspection_end': '10.0.0.130',
'network_gateway': '10.0.0.254',
'undercloud_public_host': '10.0.0.11',
'undercloud_admin_host': '10.0.0.12',
'undercloud_service_certificate':
'/etc/pki/instack-certs/undercloud.pem',
'generate_service_certificate': 'True',
}
class TestProcessRequest(unittest.TestCase):
def setUp(self):
self.mock_request = mock.Mock()
self.mock_request.params = {}
def _test_params(self):
t, params = app.process_request(self.mock_request)
self.assertEqual('templates/ucw.jinja2', t.filename)
return params
def _assert_overwritten_params(self, params):
self.assertEqual('eth1', params['local_interface'])
self.assertEqual('192.168.24.0/24', params['network_cidr'])
self.assertEqual('2', params['node_count'])
self.assertEqual('192.168.24.1/24', params['local_ip'])
self.assertEqual('192.168.24.4', params['dhcp_start'])
self.assertEqual('192.168.24.15', params['dhcp_end'])
self.assertEqual('192.168.24.16', params['inspection_start'])
self.assertEqual('192.168.24.17', params['inspection_end'])
self.assertEqual('192.168.24.1', params['network_gateway'])
self.assertEqual('192.168.24.2', params['undercloud_public_host'])
self.assertEqual('192.168.24.3', params['undercloud_admin_host'])
def _assert_kept_params(self, params):
self.assertEqual('9000', params['local_mtu'])
self.assertEqual('uc-prod.tripleo.org', params['undercloud_hostname'])
self.assertEqual('/etc/pki/instack-certs/undercloud.pem',
params['undercloud_service_certificate'])
self.assertEqual('True', params['generate_service_certificate'])
def _assert_defaults(self, params):
self._assert_overwritten_params(params)
self.assertEqual('1500', params['local_mtu'])
self.assertEqual('undercloud.localdomain',
params['undercloud_hostname'])
self.assertEqual('', params['undercloud_service_certificate'])
self.assertEqual('', params['error'])
def test_process_request(self):
params = self._test_params()
self._assert_defaults(params)
def _assert_request_params(self, params):
for key in self.mock_request.params.keys():
self.assertEqual(self.mock_request.params[key],
params[key])
self.assertEqual('', params['error'])
def test_basic_input(self):
self.mock_request.params = {'local_interface': 'p9p1',
'network_cidr': '10.0.0.0/24',
'node_count': '25',
}
params = self._test_params()
self._assert_request_params(params)
def test_bogus_key_ignored(self):
self.mock_request.params = {'foo': 'bar'}
params = self._test_params()
self.assertNotIn('foo', params)
def test_advanced_input(self):
self.mock_request.params = all_params()
params = self._test_params()
self._assert_request_params(params)
def test_generate_advanced(self):
self.mock_request.params = all_params()
self.mock_request.params.update({'local_interface': 'eth1',
'network_cidr': '192.168.24.0/24',
'node_count': '2',
'genadv': 'Generate Advanced',
})
params = self._test_params()
self._assert_overwritten_params(params)
self._assert_kept_params(params)
def test_insufficient_ips(self):
self.mock_request.params = {'local_interface': 'p9p1',
'network_cidr': '10.0.0.0/24',
'node_count': '250',
}
params = self._test_params()
self.assertEqual('Insufficient addresses available in provisioning '
'CIDR', params['error'])
def test_invalid_configuration(self):
self.mock_request.params = all_params()
self.mock_request.params['dhcp_start'] = '10.0.0.70'
params = self._test_params()
self.assertEqual('Invalid dhcp range specified, dhcp_start "%s" does '
'not come before dhcp_end "%s"' %
(params['dhcp_start'], params['dhcp_end']),
params['error'])
@mock.patch('netaddr.IPNetwork')
def test_unexpected_exception(self, mock_ipn):
mock_ipn.side_effect = Exception('Unexpected')
params = self._test_params()
self.assertEqual('Unexpected', params['error'])
def test_generate(self):
self.mock_request.params = all_params()
self.mock_request.params['generate'] = 'Generate Configuration'
t, params = app.process_request(self.mock_request)
self.assertEqual('templates/generate.jinja2', t.filename)
def test_view(self):
response = app.ucw(self.mock_request)
self.assertIn("<td>Provisioning Interface</td> <td><input type='text' "
"name='local_interface' value=\"eth1\"></td>",
response.body)
def test_gen_cert(self):
self.mock_request.params = all_params()
self.mock_request.params['generate_service_certificate'] = 'True'
response = app.ucw(self.mock_request)
self.assertIn("<td>Generate SSL Certificate<sup>N</sup></td> "
"<td><input type='checkbox' "
"name='generate_service_certificate' "
"value='True' checked",
response.body)
def test_no_gen_cert(self):
response = app.ucw(self.mock_request)
self.assertIn("<td>Generate SSL Certificate<sup>N</sup></td> "
"<td><input type='checkbox' "
"name='generate_service_certificate' "
"value='True'",
response.body)
def test_scheduler_attempts(self):
self.mock_request.params = {'node_count': '20'}
params = self._test_params()
self.assertEqual('20', params['scheduler_max_attempts'])
def test_scheduler_attempts_min(self):
self.mock_request.params = {'node_count': '2'}
params = self._test_params()
self.assertEqual('10', params['scheduler_max_attempts'])
def test_ipv6(self):
self.mock_request.params = {'node_count': '2',
'network_cidr': '2000::1/64'}
params = self._test_params()
self.assertEqual('2000::1', params['network_gateway'])
if __name__ == '__main__':
unittest.main()