def test_extract_ovsdb_config(self): fake_ovsdb_config = { n_const.OVSDB_IDENTIFIER: 'host2', 'ovsdb_ip': '10.10.10.10', 'ovsdb_port': '6632', 'private_key': '/home/someuser/fakedir/host2.key', 'use_ssl': True, 'certificate': '/home/someuser/fakedir/host2.cert', 'ca_cert': '/home/someuser/fakedir/host2.ca_cert' } cfg.CONF.set_override('ovsdb_hosts', 'host2:10.10.10.10:6632', 'ovsdb') cfg.CONF.set_override('l2_gw_agent_priv_key_base_path', '/home/someuser/fakedir', 'ovsdb') cfg.CONF.set_override('l2_gw_agent_cert_base_path', '/home/someuser/fakedir', 'ovsdb') cfg.CONF.set_override('l2_gw_agent_ca_cert_base_path', '/home/someuser/fakedir', 'ovsdb') self.l2gw_agent_manager._extract_ovsdb_config(cfg.CONF) l2gwconfig = l2gateway_config.L2GatewayConfig(fake_ovsdb_config) gw = self.l2gw_agent_manager.gateways.get( fake_ovsdb_config[n_const.OVSDB_IDENTIFIER]) self.assertEqual(l2gwconfig.ovsdb_identifier, gw.ovsdb_identifier) self.assertEqual(l2gwconfig.use_ssl, gw.use_ssl) self.assertEqual(l2gwconfig.ovsdb_ip, gw.ovsdb_ip) self.assertEqual(l2gwconfig.ovsdb_port, gw.ovsdb_port) self.assertEqual(l2gwconfig.private_key, gw.private_key) self.assertEqual(l2gwconfig.certificate, gw.certificate) self.assertEqual(l2gwconfig.ca_cert, gw.ca_cert) cfg.CONF.set_override('max_connection_retries', 25, 'ovsdb') self.assertRaises(SystemExit, self.l2gw_agent_manager._extract_ovsdb_config, cfg.CONF)
def test_is_valid_request_fails(self): self.l2gw_agent_manager.gateways = {} fake_ovsdb_identifier = 'fake_ovsdb_identifier_2' gateway = l2gateway_config.L2GatewayConfig(self.fake_config_json) self.l2gw_agent_manager.gateways['fake_ovsdb_identifier'] = gateway with mock.patch.object(manager.LOG, 'warning') as logger_call: self.l2gw_agent_manager._is_valid_request(fake_ovsdb_identifier) self.assertEqual(1, logger_call.call_count)
def test_open_connection_with_socket_error(self): self.l2gw_agent_manager.gateways = {} gateway = l2gateway_config.L2GatewayConfig(self.fake_config_json) self.l2gw_agent_manager.gateways['fake_ovsdb_identifier'] = gateway with mock.patch.object(manager.LOG, 'warning') as logger_call, \ mock.patch.object(socket.socket, 'connect') as mock_connect, \ mock.patch.object(ovsdb_writer.OVSDBWriter, 'delete_logical_switch') as mock_del_ls: mock_connect.side_effect = socket.error self.l2gw_agent_manager.delete_network(self.context, mock.Mock(), mock.Mock()) self.assertEqual(1, logger_call.call_count) self.assertFalse(mock_del_ls.called)
def test_open_connection(self): self.l2gw_agent_manager.gateways = {} fake_ovsdb_identifier = 'fake_ovsdb_identifier' gateway = l2gateway_config.L2GatewayConfig(self.fake_config_json) self.l2gw_agent_manager.gateways['fake_ovsdb_identifier'] = gateway with mock.patch.object(manager.LOG, 'warning') as logger_call: with mock.patch.object(ovsdb_writer, 'OVSDBWriter') as ovsdb_connection: is_valid_request = self.l2gw_agent_manager._is_valid_request( fake_ovsdb_identifier) with self.l2gw_agent_manager._open_connection( fake_ovsdb_identifier): self.assertTrue(is_valid_request) self.assertEqual(0, logger_call.call_count) self.assertTrue(ovsdb_connection.called)
def test_connect_to_ovsdb_server_with_exc(self): self.l2gw_agent_manager.gateways = {} self.l2gw_agent_manager.l2gw_agent_type = n_const.MONITOR gateway = l2gateway_config.L2GatewayConfig(self.fake_config_json) ovsdb_ident = self.fake_config_json.get(n_const.OVSDB_IDENTIFIER) self.l2gw_agent_manager.gateways[ovsdb_ident] = gateway with mock.patch.object(ovsdb_monitor.OVSDBMonitor, '__init__', side_effect=socket.error ), \ mock.patch.object(eventlet.greenthread, 'spawn_n') as event_spawn, \ mock.patch.object(manager.LOG, 'error'): self.l2gw_agent_manager._connect_to_ovsdb_server() event_spawn.assert_not_called()
def _process_ovsdb_host(self, host): try: host_splits = str(host).split(':') ovsdb_identifier = str(host_splits[0]).strip() ovsdb_conf = { n_const.OVSDB_IDENTIFIER: ovsdb_identifier, 'ovsdb_ip': str(host_splits[1]).strip(), 'ovsdb_port': str(host_splits[2]).strip() } priv_key_path = self.conf.ovsdb.l2_gw_agent_priv_key_base_path cert_path = self.conf.ovsdb.l2_gw_agent_cert_base_path ca_cert_path = self.conf.ovsdb.l2_gw_agent_ca_cert_base_path use_ssl = priv_key_path and cert_path and ca_cert_path if use_ssl: ssl_ovsdb = { 'use_ssl': True, 'private_key': "/".join([ str(priv_key_path), '.'.join([str(host_splits[0]).strip(), 'key']) ]), 'certificate': "/".join([ str(cert_path), '.'.join([str(host_splits[0]).strip(), 'cert']) ]), 'ca_cert': "/".join([ str(ca_cert_path), '.'.join([str(host_splits[0]).strip(), 'ca_cert']) ]) } ovsdb_conf.update(ssl_ovsdb) LOG.debug("ovsdb_conf = %s", str(ovsdb_conf)) gateway = l2gateway_config.L2GatewayConfig(ovsdb_conf) self.gateways[ovsdb_identifier] = gateway except Exception as ex: LOG.exception( _LE("Exception %(ex)s occurred while processing " "host %(host)s"), { 'ex': ex, 'host': host })
def test_connect_to_ovsdb_server(self): self.l2gw_agent_manager.gateways = {} self.l2gw_agent_manager.l2gw_agent_type = n_const.MONITOR gateway = l2gateway_config.L2GatewayConfig(self.fake_config_json) ovsdb_ident = self.fake_config_json.get(n_const.OVSDB_IDENTIFIER) self.l2gw_agent_manager.gateways[ovsdb_ident] = gateway with mock.patch.object(ovsdb_monitor, 'OVSDBMonitor') as ovsdb_connection, \ mock.patch.object(eventlet.greenthread, 'spawn_n') as event_spawn, \ mock.patch.object(manager.OVSDBManager, 'agent_to_plugin_rpc') as call_back, \ mock.patch.object(self.plugin_rpc, 'notify_ovsdb_states') as notify: self.l2gw_agent_manager._connect_to_ovsdb_server() self.assertTrue(event_spawn.called) self.assertTrue(ovsdb_connection.called) ovsdb_connection.assert_called_with(self.conf.ovsdb, gateway, call_back) notify.assert_called_once_with(mock.ANY, mock.ANY)
def _process_ovsdb_host(self, host): try: host_splits = str(host).split(':') ovsdb_identifier = str(host_splits[0]).strip() ovsdb_conf = { n_const.OVSDB_IDENTIFIER: ovsdb_identifier, 'ovsdb_ip': str(host_splits[1]).strip(), 'ovsdb_port': str(host_splits[2]).strip() } priv_key_path = self.conf.ovsdb.l2_gw_agent_priv_key_base_path cert_path = self.conf.ovsdb.l2_gw_agent_cert_base_path ca_cert_path = self.conf.ovsdb.l2_gw_agent_ca_cert_base_path use_ssl = priv_key_path and cert_path and ca_cert_path if use_ssl: LOG.debug( "ssl is enabled with priv_key_path %s, cert_path " "%s, ca_cert_path %s", priv_key_path, cert_path, ca_cert_path) priv_key_file = priv_key_path + "/" + ovsdb_identifier + ".key" cert_file = cert_path + "/" + ovsdb_identifier + ".cert" ca_cert_file = (ca_cert_path + "/" + ovsdb_identifier + ".ca_cert") is_priv_key = os.path.isfile(priv_key_file) is_cert_file = os.path.isfile(cert_file) is_ca_cert_file = os.path.isfile(ca_cert_file) if not is_priv_key: LOG.exception( "Could not find private key in " "%(path)s dir, expecting in the " "file name %(file)s ", { 'path': priv_key_path, 'file': ovsdb_identifier + ".key" }) if not is_cert_file: LOG.exception( "Could not find cert in %(path)s dir, " "expecting in the file name %(file)s", { 'path': cert_path, 'file': ovsdb_identifier + ".cert" }) if not is_ca_cert_file: LOG.exception( "Could not find cacert in %(path)s " "dir, expecting in the file name " "%(file)s", { 'path': ca_cert_path, 'file': ovsdb_identifier + ".ca_cert" }) ssl_ovsdb = { 'use_ssl': True, 'private_key': "/".join([ str(priv_key_path), '.'.join([str(host_splits[0]).strip(), 'key']) ]), 'certificate': "/".join([ str(cert_path), '.'.join([str(host_splits[0]).strip(), 'cert']) ]), 'ca_cert': "/".join([ str(ca_cert_path), '.'.join([str(host_splits[0]).strip(), 'ca_cert']) ]) } ovsdb_conf.update(ssl_ovsdb) LOG.debug("ovsdb_conf = %s", str(ovsdb_conf)) gateway = l2gateway_config.L2GatewayConfig(ovsdb_conf) self.gateways[ovsdb_identifier] = gateway except Exception as ex: LOG.exception( "Exception %(ex)s occurred while processing " "host %(host)s", { 'ex': ex, 'host': host })