def disable_isolated_metadata_proxy(self, network): pm = external_process.ProcessManager( self.conf, network.id, self.root_helper, self._ns_name(network)) pm.disable()
def test_get_pid_file_name_default(self): with mock.patch.object(ep.os.path, 'isdir') as isdir: isdir.return_value = True manager = ep.ProcessManager(self.conf, 'uuid') retval = manager.get_pid_file_name(ensure_pids_dir=False) self.assertEqual(retval, '/var/path/uuid.pid') self.assertFalse(isdir.called)
def test_active_none(self): dummy_cmd_line = 'python foo --router_id=uuid' self.execute.return_value = dummy_cmd_line with mock.patch.object(ep.ProcessManager, 'pid') as pid: pid.__get__ = mock.Mock(return_value=None) manager = ep.ProcessManager(self.conf, 'uuid') self.assertFalse(manager.active)
def test_pid_no_an_int(self): with mock.patch('__builtin__.open') as mock_open: mock_open.return_value.__enter__ = lambda s: s mock_open.return_value.__exit__ = mock.Mock() mock_open.return_value.read.return_value = 'foo' manager = ep.ProcessManager(self.conf, 'uuid') self.assertIsNone(manager.pid, 5)
def test_get_pid_file_name_not_existing(self): with mock.patch.object(ep.os.path, 'isdir') as isdir: with mock.patch.object(ep.os, 'makedirs') as makedirs: isdir.return_value = False manager = ep.ProcessManager(self.conf, 'uuid') retval = manager.get_pid_file_name(ensure_pids_dir=True) self.assertEqual(retval, '/var/path/uuid.pid') makedirs.assert_called_once_with('/var/path', 0755)
def test_disable_no_pid(self): with mock.patch.object(ep.ProcessManager, 'pid') as pid: pid.__get__ = mock.Mock(return_value=None) with mock.patch.object(ep.ProcessManager, 'active') as active: active.__get__ = mock.Mock(return_value=False) with mock.patch.object(ep.LOG, 'debug') as debug: manager = ep.ProcessManager(self.conf, 'uuid') manager.disable() debug.assert_called_once_with(mock.ANY)
def test_active_cmd_mismatch(self): dummy_cmd_line = 'python foo --router_id=anotherid' self.execute.return_value = dummy_cmd_line with mock.patch.object(ep.ProcessManager, 'pid') as pid: pid.__get__ = mock.Mock(return_value=4) manager = ep.ProcessManager(self.conf, 'uuid') self.assertFalse(manager.active) self.execute.assert_called_once_with(['cat', '/proc/4/cmdline'], 'sudo')
def test_disable_no_namespace(self): with mock.patch.object(ep.ProcessManager, 'pid') as pid: pid.__get__ = mock.Mock(return_value=4) with mock.patch.object(ep.ProcessManager, 'active') as active: active.__get__ = mock.Mock(return_value=True) manager = ep.ProcessManager(self.conf, 'uuid') manager.disable() self.execute(['kill', '-9', 4], 'sudo')
def test_enable_with_namespace_process_active(self): callback = mock.Mock() callback.return_value = ['the', 'cmd'] with mock.patch.object(ep.ProcessManager, 'active') as active: active.__get__ = mock.Mock(return_value=True) manager = ep.ProcessManager(self.conf, 'uuid', namespace='ns') with mock.patch.object(ep, 'ip_lib') as ip_lib: manager.enable(callback) self.assertFalse(callback.called)
def test_disable_namespace(self): with mock.patch.object(ep.ProcessManager, 'pid') as pid: pid.__get__ = mock.Mock(return_value=4) with mock.patch.object(ep.ProcessManager, 'active') as active: active.__get__ = mock.Mock(return_value=True) manager = ep.ProcessManager(self.conf, 'uuid', namespace='ns') with mock.patch.object(ep, 'utils') as utils: manager.disable() utils.assert_has_calls( mock.call.execute(['kill', '-9', 4], 'sudo'))
def _spawn_metadata_agent(self, router_info): def callback(pid_file): return [ 'quantum-ns-metadata-proxy', '--pid_file=%s' % pid_file, '--router_id=%s' % router_info.router_id, '--state_path=%s' % self.conf.state_path ] pm = external_process.ProcessManager(self.conf, router_info.router_id, self.conf.root_helper, router_info.ns_name()) pm.enable(callback)
def test_disable_namespace(self): with mock.patch.object(ep.ProcessManager, 'pid') as pid: pid.__get__ = mock.Mock(return_value=4) with mock.patch.object(ep.ProcessManager, 'active') as active: active.__get__ = mock.Mock(return_value=True) manager = ep.ProcessManager(self.conf, 'uuid', namespace='ns') with mock.patch.object(ep, 'ip_lib') as ip_lib: manager.disable() ip_lib.assert_has_calls([ mock.call.IPWrapper('sudo', 'ns'), mock.call.IPWrapper().netns.execute(['kill', '-9', 4]) ])
def test_enable_no_namespace(self): callback = mock.Mock() callback.return_value = ['the', 'cmd'] with mock.patch.object(ep.ProcessManager, 'get_pid_file_name') as name: name.return_value = 'pidfile' with mock.patch.object(ep.ProcessManager, 'active') as active: active.__get__ = mock.Mock(return_value=False) manager = ep.ProcessManager(self.conf, 'uuid') manager.enable(callback) callback.assert_called_once_with('pidfile') name.assert_called_once_with(ensure_pids_dir=True) self.execute.assert_called_once_with(['the', 'cmd'], 'sudo')
def enable_isolated_metadata_proxy(self, network): # The proxy might work for either a single network # or all the networks connected via a router # to the one passed as a parameter quantum_lookup_param = '--network_id=%s' % network.id meta_cidr = netaddr.IPNetwork(METADATA_DEFAULT_IP) has_metadata_subnet = any( netaddr.IPNetwork(s.cidr) in meta_cidr for s in network.subnets) if (self.conf.enable_metadata_network and has_metadata_subnet): router_ports = [ port for port in network.ports if (port.device_owner == constants.DEVICE_OWNER_ROUTER_INTF) ] if router_ports: # Multiple router ports should not be allowed if len(router_ports) > 1: LOG.warning( _("%(port_num)d router ports found on the " "metadata access network. Only the port " "%(port_id)s, for router %(router_id)s " "will be considered"), { 'port_num': len(router_ports), 'port_id': router_ports[0].id, 'router_id': router_ports[0].device_id }) quantum_lookup_param = ('--router_id=%s' % router_ports[0].device_id) def callback(pid_file): proxy_cmd = [ 'quantum-ns-metadata-proxy', '--pid_file=%s' % pid_file, quantum_lookup_param, '--state_path=%s' % self.conf.state_path, '--metadata_port=%d' % METADATA_PORT ] proxy_cmd.extend( config.get_log_args( cfg.CONF, 'quantum-ns-metadata-proxy-%s.log' % network.id)) return proxy_cmd pm = external_process.ProcessManager(self.conf, network.id, self.root_helper, self._ns_name(network)) pm.enable(callback)
def test_enable_with_namespace(self): callback = mock.Mock() callback.return_value = ['the', 'cmd'] with mock.patch.object(ep.ProcessManager, 'get_pid_file_name') as name: name.return_value = 'pidfile' with mock.patch.object(ep.ProcessManager, 'active') as active: active.__get__ = mock.Mock(return_value=False) manager = ep.ProcessManager(self.conf, 'uuid', namespace='ns') with mock.patch.object(ep, 'ip_lib') as ip_lib: manager.enable(callback) callback.assert_called_once_with('pidfile') name.assert_called_once_with(ensure_pids_dir=True) ip_lib.assert_has_calls([ mock.call.IPWrapper('sudo', 'ns'), mock.call.IPWrapper().netns.execute(['the', 'cmd']) ])
def _spawn_metadata_proxy(self, router_info): def callback(pid_file): proxy_cmd = [ 'quantum-ns-metadata-proxy', '--pid_file=%s' % pid_file, '--router_id=%s' % router_info.router_id, '--state_path=%s' % self.conf.state_path, '--metadata_port=%s' % self.conf.metadata_port ] proxy_cmd.extend( config.get_log_args( cfg.CONF, 'quantum-ns-metadata-proxy%s.log' % router_info.router_id)) return proxy_cmd pm = external_process.ProcessManager(self.conf, router_info.router_id, self.root_helper, router_info.ns_name()) pm.enable(callback)
def _destroy_metadata_proxy(self, router_info): pm = external_process.ProcessManager(self.conf, router_info.router_id, self.root_helper, router_info.ns_name()) pm.disable()
def test_pid_invalid_file(self): with mock.patch.object(ep.ProcessManager, 'get_pid_file_name') as name: name.return_value = '.doesnotexist/pid' manager = ep.ProcessManager(self.conf, 'uuid') self.assertIsNone(manager.pid)