24
class FakeCreateProcess(object):
25
class FakeStdin(object):
29
def __init__(self, returncode):
30
self.returncode = returncode
31
self.stdin = self.FakeStdin()
33
def communicate(self, process_input=None):
37
28
class AgentUtilsExecuteTest(base.BaseTestCase):
39
30
super(AgentUtilsExecuteTest, self).setUp()
40
self.root_helper = "echo"
41
31
self.test_file = self.get_temp_file_path('test_execute.tmp')
42
32
open(self.test_file, 'w').close()
43
self.mock_popen_p = mock.patch("subprocess.Popen.communicate")
44
self.mock_popen = self.mock_popen_p.start()
33
self.process = mock.patch('eventlet.green.subprocess.Popen').start()
34
self.process.return_value.returncode = 0
35
self.mock_popen = self.process.return_value.communicate
46
37
def test_without_helper(self):
47
38
expected = "%s\n" % self.test_file
89
80
self.assertEqual(result, expected)
91
82
def test_return_code_log_error_raise_runtime(self):
92
with mock.patch.object(utils, 'create_process') as create_process:
93
create_process.return_value = FakeCreateProcess(1), 'ls'
94
with mock.patch.object(utils, 'LOG') as log:
95
self.assertRaises(RuntimeError, utils.execute,
97
self.assertTrue(log.error.called)
83
self.mock_popen.return_value = ('', '')
84
self.process.return_value.returncode = 1
85
with mock.patch.object(utils, 'LOG') as log:
86
self.assertRaises(RuntimeError, utils.execute,
88
self.assertTrue(log.error.called)
99
90
def test_return_code_log_error_no_raise_runtime(self):
100
with mock.patch.object(utils, 'create_process') as create_process:
101
create_process.return_value = FakeCreateProcess(1), 'ls'
102
with mock.patch.object(utils, 'LOG') as log:
103
utils.execute(['ls'], check_exit_code=False)
104
self.assertTrue(log.error.called)
91
self.mock_popen.return_value = ('', '')
92
self.process.return_value.returncode = 1
93
with mock.patch.object(utils, 'LOG') as log:
94
utils.execute(['ls'], check_exit_code=False)
95
self.assertTrue(log.error.called)
106
97
def test_return_code_log_debug(self):
107
with mock.patch.object(utils, 'create_process') as create_process:
108
create_process.return_value = FakeCreateProcess(0), 'ls'
109
with mock.patch.object(utils, 'LOG') as log:
110
utils.execute(['ls'])
111
self.assertTrue(log.debug.called)
98
self.mock_popen.return_value = ('', '')
99
with mock.patch.object(utils, 'LOG') as log:
100
utils.execute(['ls'])
101
self.assertTrue(log.debug.called)
113
103
def test_return_code_raise_runtime_do_not_log_fail_as_error(self):
114
with mock.patch.object(utils, 'create_process') as create_process:
115
create_process.return_value = FakeCreateProcess(1), 'ls'
116
with mock.patch.object(utils, 'LOG') as log:
117
self.assertRaises(RuntimeError, utils.execute,
118
['ls'], log_fail_as_error=False)
119
self.assertTrue(log.debug.called)
104
self.mock_popen.return_value = ('', '')
105
self.process.return_value.returncode = 1
106
with mock.patch.object(utils, 'LOG') as log:
107
self.assertRaises(RuntimeError, utils.execute,
108
['ls'], log_fail_as_error=False)
109
self.assertFalse(log.error.called)
122
112
class AgentUtilsGetInterfaceMAC(base.BaseTestCase):
182
172
mock_pid = object()
183
173
with mock.patch.object(utils, 'find_child_pids',
184
174
side_effect=_find_child_pids):
185
actual = utils.get_root_helper_child_pid(mock_pid, root_helper)
175
actual = utils.get_root_helper_child_pid(mock_pid, run_as_root)
186
176
if expected is _marker:
187
177
expected = str(mock_pid)
188
178
self.assertEqual(expected, actual)
190
def test_returns_process_pid_without_root_helper(self):
180
def test_returns_process_pid_not_root(self):
191
181
self._test_get_root_helper_child_pid()
193
def test_returns_child_pid_with_root_helper(self):
183
def test_returns_child_pid_as_root(self):
194
184
self._test_get_root_helper_child_pid(expected='2', pids=['1', '2'],
197
def test_returns_last_child_pid_with_root_helper(self):
187
def test_returns_last_child_pid_as_root(self):
198
188
self._test_get_root_helper_child_pid(expected='3',
199
189
pids=['1', '2', '3'],
202
def test_returns_none_with_root_helper(self):
203
self._test_get_root_helper_child_pid(expected=None, root_helper='a')
192
def test_returns_none_as_root(self):
193
self._test_get_root_helper_child_pid(expected=None, run_as_root=True)
196
class TestPathUtilities(base.BaseTestCase):
197
def test_remove_abs_path(self):
198
self.assertEqual(['ping', '8.8.8.8'],
199
utils.remove_abs_path(['/usr/bin/ping', '8.8.8.8']))
201
def test_cmdlines_are_equal(self):
202
self.assertTrue(utils.cmdlines_are_equal(
204
['/usr/bin/ping', '8.8.8.8']))
206
def test_cmdlines_are_equal_different_commands(self):
207
self.assertFalse(utils.cmdlines_are_equal(
209
['/usr/bin/ping6', '8.8.8.8']))
212
class TestBaseOSUtils(base.BaseTestCase):
213
@mock.patch.object(os.path, 'isdir', return_value=False)
214
@mock.patch.object(os, 'makedirs')
215
def test_ensure_dir_not_exist(self, makedirs, isdir):
216
utils.ensure_dir('/the')
217
isdir.assert_called_once_with('/the')
218
makedirs.assert_called_once_with('/the', 0o755)
220
@mock.patch.object(os.path, 'isdir', return_value=True)
221
@mock.patch.object(os, 'makedirs')
222
def test_ensure_dir_exist(self, makedirs, isdir):
223
utils.ensure_dir('/the')
224
isdir.assert_called_once_with('/the')
225
self.assertFalse(makedirs.called)
228
class TestUnixDomainHttpConnection(base.BaseTestCase):
229
def test_connect(self):
230
with mock.patch.object(utils, 'cfg') as cfg:
231
cfg.CONF.metadata_proxy_socket = '/the/path'
232
with mock.patch('socket.socket') as socket_create:
233
conn = utils.UnixDomainHTTPConnection('169.254.169.254',
237
socket_create.assert_has_calls([
238
mock.call(socket.AF_UNIX, socket.SOCK_STREAM),
239
mock.call().settimeout(3),
240
mock.call().connect('/the/path')]
242
self.assertEqual(conn.timeout, 3)
245
class TestUnixDomainHttpProtocol(base.BaseTestCase):
246
def test_init_empty_client(self):
247
u = utils.UnixDomainHttpProtocol(mock.Mock(), '', mock.Mock())
248
self.assertEqual(u.client_address, ('<local>', 0))
250
def test_init_with_client(self):
251
u = utils.UnixDomainHttpProtocol(mock.Mock(), 'foo', mock.Mock())
252
self.assertEqual(u.client_address, 'foo')
255
class TestUnixDomainWSGIServer(base.BaseTestCase):
257
super(TestUnixDomainWSGIServer, self).setUp()
258
self.eventlet_p = mock.patch.object(utils, 'eventlet')
259
self.eventlet = self.eventlet_p.start()
260
self.server = utils.UnixDomainWSGIServer('test')
262
def test_start(self):
263
mock_app = mock.Mock()
264
with mock.patch.object(self.server, '_launch') as launcher:
265
self.server.start(mock_app, '/the/path', workers=5, backlog=128)
266
self.eventlet.assert_has_calls([
269
family=socket.AF_UNIX,
273
launcher.assert_called_once_with(mock_app, workers=5)
276
self.server._run('app', 'sock')
278
self.eventlet.wsgi.server.assert_called_once_with(
281
protocol=utils.UnixDomainHttpProtocol,
283
max_size=self.server.num_threads