From 244e6eead8a71aeb99a65d5c6f1488eda9124c87 Mon Sep 17 00:00:00 2001 From: Thomas Bechtold Date: Wed, 11 Sep 2019 10:11:13 +0200 Subject: [PATCH] Use sys.executable instead of 'python' In a python3 only environment, 'python' might not be available (only 'python3'). So instead of hardcoding 'python' as executable, use the executable that is used to run the tests itself which is sys.executable . Change-Id: Iae44aa2f1c336a0f5501312af05b0133193c5462 --- neutron/agent/linux/daemon.py | 2 +- .../agent/linux/test_process_monitor.py | 3 +- .../functional/cmd/test_netns_cleanup.py | 8 +++-- .../unit/agent/common/test_async_process.py | 3 +- neutron/tests/unit/agent/linux/test_daemon.py | 29 ++++++++++--------- 5 files changed, 26 insertions(+), 19 deletions(-) diff --git a/neutron/agent/linux/daemon.py b/neutron/agent/linux/daemon.py index 97194c3a007..0b52c3f980b 100644 --- a/neutron/agent/linux/daemon.py +++ b/neutron/agent/linux/daemon.py @@ -170,7 +170,7 @@ class Daemon(object): Usage: subclass the Daemon class and override the run() method """ def __init__(self, pidfile, stdin=DEVNULL, stdout=DEVNULL, - stderr=DEVNULL, procname='python', uuid=None, + stderr=DEVNULL, procname=sys.executable, uuid=None, user=None, group=None): """Note: pidfile may be None.""" self.stdin = stdin diff --git a/neutron/tests/functional/agent/linux/test_process_monitor.py b/neutron/tests/functional/agent/linux/test_process_monitor.py index 92d683c693d..cd8e82e5de2 100644 --- a/neutron/tests/functional/agent/linux/test_process_monitor.py +++ b/neutron/tests/functional/agent/linux/test_process_monitor.py @@ -13,6 +13,7 @@ # under the License. import os +import sys from oslo_config import cfg from six import moves @@ -48,7 +49,7 @@ class BaseTestProcessMonitor(base.BaseLoggingTestCase): def _make_cmdline_callback(self, uuid): def _cmdline_callback(pidfile): - cmdline = ["python", simple_daemon.__file__, + cmdline = [sys.executable, simple_daemon.__file__, "--uuid=%s" % uuid, "--pid_file=%s" % pidfile] return cmdline diff --git a/neutron/tests/functional/cmd/test_netns_cleanup.py b/neutron/tests/functional/cmd/test_netns_cleanup.py index 495b98989ea..9026e60bb5e 100644 --- a/neutron/tests/functional/cmd/test_netns_cleanup.py +++ b/neutron/tests/functional/cmd/test_netns_cleanup.py @@ -14,6 +14,7 @@ # under the License. import os +import sys import eventlet import mock @@ -110,19 +111,20 @@ class NetnsCleanupTest(base.BaseSudoTestCase): to test the cleanup functionality which will issue a SIGKILL to all remaining processes after the SIGTERM attempt """ - commands = [['python', process_spawn.__file__, + python_exec = os.path.basename(sys.executable) + commands = [[python_exec, process_spawn.__file__, '-n', NUM_SUBPROCESSES, '-f', n_const.IPv4, '-p', n_const.PROTO_NAME_TCP, '--noignore_sigterm', '--parent_listen'], - ['python', process_spawn.__file__, + [python_exec, process_spawn.__file__, '-n', NUM_SUBPROCESSES, '-f', process_spawn.UNIX_FAMILY, '-p', n_const.PROTO_NAME_TCP, '--noignore_sigterm', '--noparent_listen'], - ['python', process_spawn.__file__, + [python_exec, process_spawn.__file__, '-n', NUM_SUBPROCESSES, '-f', n_const.IPv4, '-p', n_const.PROTO_NAME_UDP, diff --git a/neutron/tests/unit/agent/common/test_async_process.py b/neutron/tests/unit/agent/common/test_async_process.py index 8db088399c2..cba2b8e8b15 100644 --- a/neutron/tests/unit/agent/common/test_async_process.py +++ b/neutron/tests/unit/agent/common/test_async_process.py @@ -13,6 +13,7 @@ # under the License. import signal +import sys import eventlet.event from eventlet.green import subprocess @@ -319,7 +320,7 @@ class TestFailingAsyncProcess(base.BaseTestCase): def setUp(self): super(TestFailingAsyncProcess, self).setUp() path = self.get_temp_file_path('async.tmp', self.get_new_temp_dir()) - self.process = async_process.AsyncProcess(['python3', + self.process = async_process.AsyncProcess([sys.executable, failing_process.__file__, path], respawn_interval=0) diff --git a/neutron/tests/unit/agent/linux/test_daemon.py b/neutron/tests/unit/agent/linux/test_daemon.py index f6ef0d68efb..7c0bb9caff0 100644 --- a/neutron/tests/unit/agent/linux/test_daemon.py +++ b/neutron/tests/unit/agent/linux/test_daemon.py @@ -153,7 +153,7 @@ class TestPidfile(base.BaseTestCase): self.os.O_CREAT = os.O_CREAT self.os.O_RDWR = os.O_RDWR - daemon.Pidfile('thefile', 'python') + daemon.Pidfile('thefile', sys.executable) self.os.open.assert_called_once_with('thefile', os.O_CREAT | os.O_RDWR) self.fcntl.flock.assert_called_once_with(FAKE_FD, self.fcntl.LOCK_EX | self.fcntl.LOCK_NB) @@ -163,14 +163,14 @@ class TestPidfile(base.BaseTestCase): with mock.patch.object(daemon.sys, 'stderr'): with testtools.ExpectedException(SystemExit): - daemon.Pidfile('thefile', 'python') + daemon.Pidfile('thefile', sys.executable) sys.assert_has_calls([ mock.call.stderr.write(mock.ANY), mock.call.exit(1)] ) def test_unlock(self): - p = daemon.Pidfile('thefile', 'python') + p = daemon.Pidfile('thefile', sys.executable) p.unlock() self.fcntl.flock.assert_has_calls([ mock.call(FAKE_FD, self.fcntl.LOCK_EX | self.fcntl.LOCK_NB), @@ -178,7 +178,7 @@ class TestPidfile(base.BaseTestCase): ) def test_write(self): - p = daemon.Pidfile('thefile', 'python') + p = daemon.Pidfile('thefile', sys.executable) p.write(34) self.os.assert_has_calls([ @@ -189,13 +189,14 @@ class TestPidfile(base.BaseTestCase): def test_read(self): self.os.read.return_value = '34' - p = daemon.Pidfile('thefile', 'python') + p = daemon.Pidfile('thefile', sys.executable) self.assertEqual(34, p.read()) def test_is_running(self): mock_open = self.useFixture( - lib_fixtures.OpenFixture('/proc/34/cmdline', 'python')).mock_open - p = daemon.Pidfile('thefile', 'python') + lib_fixtures.OpenFixture('/proc/34/cmdline', + sys.executable)).mock_open + p = daemon.Pidfile('thefile', sys.executable) with mock.patch.object(p, 'read') as read: read.return_value = 34 @@ -206,8 +207,9 @@ class TestPidfile(base.BaseTestCase): def test_is_running_uuid_true(self): mock_open = self.useFixture( lib_fixtures.OpenFixture( - '/proc/34/cmdline', 'python 1234')).mock_open - p = daemon.Pidfile('thefile', 'python', uuid='1234') + '/proc/34/cmdline', '{} 1234'.format( + sys.executable))).mock_open + p = daemon.Pidfile('thefile', sys.executable, uuid='1234') with mock.patch.object(p, 'read') as read: read.return_value = 34 @@ -218,8 +220,9 @@ class TestPidfile(base.BaseTestCase): def test_is_running_uuid_false(self): mock_open = self.useFixture( lib_fixtures.OpenFixture( - '/proc/34/cmdline', 'python 1234')).mock_open - p = daemon.Pidfile('thefile', 'python', uuid='6789') + '/proc/34/cmdline', '{} 1234'.format( + sys.executable))).mock_open + p = daemon.Pidfile('thefile', sys.executable, uuid='6789') with mock.patch.object(p, 'read') as read: read.return_value = 34 @@ -239,11 +242,11 @@ class TestDaemon(base.BaseTestCase): def test_init(self): d = daemon.Daemon('pidfile') - self.assertEqual(d.procname, 'python') + self.assertEqual(d.procname, sys.executable) def test_init_nopidfile(self): d = daemon.Daemon(pidfile=None) - self.assertEqual(d.procname, 'python') + self.assertEqual(d.procname, sys.executable) self.assertFalse(self.pidfile.called) def test_fork_parent(self):