Browse Source

Add functional tests and gate hook functions

This patch adds few functional tests to test OVN NB DB sync
and adds the gate hook script functions.

The functional test setup
 - will start an ovsdb-server with OVN NB DB.
 - will start an ovsdb-server with OVN SB DB. This connection is
   not used by this patch. But it is created because
      - It can be used later to test the chassis up/down events.
      - ML2 OVN driver now also connects to the OVN SB DB.
 - The ML2 OVN driver is configured to connect to these ovsdb-servers
 - Another IDL connection is established with the OVN NB DB server.

The test cases are validated by checking if the OVN NB DB is
updated properly or not.

This can be extended later to add more functional tests and
full stack tests if required.

Change-Id: I4dc3c633e42776efc7b574f15c1bbd95f28f70f0
changes/15/313515/10
Numan Siddique 5 years ago
parent
commit
5998920e61
  1. 2
      .testr.conf
  2. 3
      networking_ovn/tests/contrib/README
  3. 30
      networking_ovn/tests/contrib/gate_hook.sh
  4. 42
      networking_ovn/tests/contrib/post_test_hook.sh
  5. 0
      networking_ovn/tests/functional/__init__.py
  6. 124
      networking_ovn/tests/functional/base.py
  7. 0
      networking_ovn/tests/functional/resources/__init__.py
  8. 87
      networking_ovn/tests/functional/resources/process.py
  9. 267
      networking_ovn/tests/functional/test_ovn_nb_sync.py
  10. 12
      tox.ini

2
.testr.conf

@ -3,6 +3,6 @@ test_command=OS_STDOUT_CAPTURE=${OS_STDOUT_CAPTURE:-1} \
OS_STDERR_CAPTURE=${OS_STDERR_CAPTURE:-1} \
OS_TEST_TIMEOUT=${OS_TEST_TIMEOUT:-60} \
OS_LOG_CAPTURE=1 \
${PYTHON:-python} -m subunit.run discover -t ./ . $LISTOPT $IDOPTION
${PYTHON:-python} -m subunit.run discover -t ./ ${OS_TEST_PATH:-./networking_ovn/tests/unit} $LISTOPT $IDOPTION
test_id_option=--load-list $IDFILE
test_list_option=--list

3
networking_ovn/tests/contrib/README

@ -0,0 +1,3 @@
The files in this directory are intended for use by the
networking-ovn infra jobs that run the various functional test
suites in the gate.

30
networking_ovn/tests/contrib/gate_hook.sh

@ -0,0 +1,30 @@
#!/usr/bin/env bash
set -ex
VENV=${1:-"dsvm-functional"}
GATE_DEST=$BASE/new
NEUTRON_PATH=$GATE_DEST/neutron
DEVSTACK_PATH=$GATE_DEST/devstack
case $VENV in
"dsvm-functional")
source $DEVSTACK_PATH/functions
source $NEUTRON_PATH/devstack/lib/ovs
# In order to run functional tests, we want to compile OVS
# from sources and installed. We don't need to start ovs services.
remove_ovs_packages
# compile_ovs expects "DEST" to be defined
DEST=$GATE_DEST
compile_ovs True /usr/local /var
# Make the workspace owned by the stack user
sudo chown -R $STACK_USER:$STACK_USER $BASE
;;
*)
echo "Unrecognized environment $VENV".
exit 1
esac

42
networking_ovn/tests/contrib/post_test_hook.sh

@ -0,0 +1,42 @@
#!/usr/bin/env bash
set -xe
NETWORKING_OVN_DIR="$BASE/new/networking-ovn"
SCRIPTS_DIR="/usr/os-testr-env/bin/"
venv=${1:-"dsvm-functional"}
function generate_testr_results {
# Give job user rights to access tox logs
sudo -H -u $owner chmod o+rw .
sudo -H -u $owner chmod o+rw -R .testrepository
if [ -f ".testrepository/0" ] ; then
.tox/$venv/bin/subunit-1to2 < .testrepository/0 > ./testrepository.subunit
$SCRIPTS_DIR/subunit2html ./testrepository.subunit testr_results.html
gzip -9 ./testrepository.subunit
gzip -9 ./testr_results.html
sudo mv ./*.gz /opt/stack/logs/
fi
}
if [[ "$venv" == dsvm-functional* ]]
then
owner=stack
sudo_env=
# Set owner permissions according to job's requirements.
cd $NETWORKING_OVN_DIR
sudo chown -R $owner:stack $NETWORKING_OVN_DIR
# Run tests
echo "Running networking-ovn $venv test suite"
set +e
sudo -H -u $owner $sudo_env tox -e $venv
testr_exit_code=$?
set -e
# Collect and parse results
generate_testr_results
exit $testr_exit_code
fi

0
networking_ovn/tests/functional/__init__.py

124
networking_ovn/tests/functional/base.py

@ -0,0 +1,124 @@
# Copyright 2016 Red Hat, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import fixtures
import mock
import time
from oslo_config import cfg
from oslo_log import log
from neutron._i18n import _LE
from neutron.agent.ovsdb import impl_idl
from neutron.agent.ovsdb.native import connection
from neutron import manager
from neutron.plugins.common import constants as service_constants
from neutron.plugins.ml2 import config
from neutron.tests.unit.plugins.ml2 import test_plugin
from networking_ovn.ovsdb import impl_idl_ovn
from networking_ovn.tests.functional.resources import process
PLUGIN_NAME = ('networking_ovn.plugin.OVNPlugin')
LOG = log.getLogger(__name__)
class TestOVNFunctionalBase(test_plugin.Ml2PluginV2TestCase):
# Please see networking_ovn/tests/contrib/gate_hook.sh.
# It installs openvswitch in the '/usr/local' path and the ovn-nb schema
# file will be present in this path.
OVS_INSTALL_SHARE_PATH = '/usr/local/share/openvswitch'
_mechanism_drivers = ['logger', 'ovn']
_extension_drivers = ['port_security']
l3_plugin = 'networking_ovn.l3.l3_ovn.OVNL3RouterPlugin'
def setUp(self):
config.cfg.CONF.set_override('extension_drivers',
self._extension_drivers,
group='ml2')
config.cfg.CONF.set_override('tenant_network_types',
['geneve'],
group='ml2')
config.cfg.CONF.set_override('vni_ranges',
['1:4095'],
group='ml2_type_geneve')
super(TestOVNFunctionalBase, self).setUp()
mm = manager.NeutronManager.get_plugin().mechanism_manager
self.mech_driver = mm.mech_drivers['ovn'].obj
self.mech_driver.initialize()
mgr = manager.NeutronManager.get_instance()
self.l3_plugin = mgr.get_service_plugins().get(
service_constants.L3_ROUTER_NAT)
self.ovsdb_server_mgr = None
self._start_ovsdb_server_and_idls()
def _start_ovsdb_server_and_idls(self):
self.temp_dir = self.useFixture(fixtures.TempDir()).path
# Start 2 ovsdb-servers one each for OVN NB DB and OVN SB DB
# ovsdb-server with OVN SB DB can be used to test the chassis up/down
# events.
self.ovsdb_server_mgr = self.useFixture(
process.OvsdbServer(self.temp_dir, self.OVS_INSTALL_SHARE_PATH,
ovn_nb_db=True, ovn_sb_db=True))
cfg.CONF.set_override(
'ovn_nb_connection',
self.ovsdb_server_mgr.get_ovsdb_connection_path(),
'ovn')
cfg.CONF.set_override(
'ovn_sb_connection',
self.ovsdb_server_mgr.get_ovsdb_connection_path(db_type='sb'),
'ovn')
num_attempts = 0
# Created monitor IDL connection to the OVN NB DB.
# This monitor IDL connection can be used to
# - Verify that the ML2 OVN driver has written to the OVN NB DB
# as expected.
# - Create and delete resources in OVN NB DB outside of the
# ML2 OVN driver scope to test scenarios like ovn_nb_sync.
while num_attempts < 3:
try:
self.monitor_idl_con = connection.Connection(
self.ovsdb_server_mgr.get_ovsdb_connection_path(),
60, 'OVN_Northbound')
self.monitor_idl_con.start()
self.monitor_nb_db_idl = self.monitor_idl_con.idl
break
except Exception:
LOG.exception(_LE("Error connecting to the OVN_Northbound DB"))
num_attempts += 1
time.sleep(1)
# mech_driver.post_fork_initialize creates the IDL connections
self.mech_driver.post_fork_initialize(mock.ANY, mock.ANY,
mock.MagicMock())
def idl_transaction(self, fake_api, check_error=False, log_errors=True,
**kwargs):
return impl_idl.Transaction(fake_api, self.monitor_idl_con, 60,
check_error, log_errors)
def restart(self):
if self.ovsdb_server_mgr:
self.ovsdb_server_mgr.stop()
impl_idl_ovn.OvsdbNbOvnIdl.ovsdb_connection = None
self.mech_driver._nb_ovn = None
self.mech_driver._sb_ovn = None
self.l3_plugin._nb_ovn = None
self.monitor_idl_con = None
self._start_ovsdb_server_and_idls()

0
networking_ovn/tests/functional/resources/__init__.py

87
networking_ovn/tests/functional/resources/process.py

@ -0,0 +1,87 @@
# Copyright 2016 Red Hat, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
from distutils import spawn
import fixtures
from neutron.agent.linux import utils
class OvsdbServer(fixtures.Fixture):
def __init__(self, temp_dir, ovs_dir, ovn_nb_db=True, ovn_sb_db=False):
super(OvsdbServer, self).__init__()
self.temp_dir = temp_dir
self.ovs_dir = ovs_dir
self.ovn_nb_db = ovn_nb_db
self.ovn_sb_db = ovn_sb_db
self.ovsdb_server_processes = []
def _setUp(self):
if self.ovn_nb_db:
self.ovsdb_server_processes.append(
{'db_path': self.temp_dir + '/ovn_nb.db',
'schema_path': self.ovs_dir + '/ovn-nb.ovsschema',
'remote_path': self.temp_dir + '/ovnnb_db.sock',
'unixctl_path': self.temp_dir + '/ovnnb_db.ctl',
'log_file_path': self.temp_dir + '/ovn_nb.log',
'db_type': 'nb'})
if self.ovn_sb_db:
self.ovsdb_server_processes.append(
{'db_path': self.temp_dir + '/ovn_sb.db',
'schema_path': self.ovs_dir + '/ovn-sb.ovsschema',
'remote_path': self.temp_dir + '/ovnsb_db.sock',
'unixctl_path': self.temp_dir + '/ovnsb_db.ctl',
'log_file_path': self.temp_dir + '/ovn_sb.log',
'db_type': 'sb'})
self.addCleanup(self.stop)
self.start()
def start(self):
for ovsdb_process in self.ovsdb_server_processes:
# create the db from the schema using ovsdb-tool
ovsdb_tool_cmd = [spawn.find_executable('ovsdb-tool'),
'create', ovsdb_process['db_path'],
ovsdb_process['schema_path']]
utils.execute(ovsdb_tool_cmd)
# start the ovsdb-server
ovsdb_server_cmd = [
spawn.find_executable('ovsdb-server'),
'--detach', '-vconsole:off',
'--log-file=%s' % (ovsdb_process['log_file_path']),
'--remote=punix:%s' % (ovsdb_process['remote_path']),
'--unixctl=%s' % (ovsdb_process['unixctl_path']),
ovsdb_process['db_path']]
utils.execute(ovsdb_server_cmd)
def stop(self):
for ovsdb_process in self.ovsdb_server_processes:
try:
stop_cmd = ['ovs-appctl', '-t', ovsdb_process['unixctl_path'],
'exit']
utils.execute(stop_cmd)
# Delete the db
cmd = ['rm', '-f', ovsdb_process['db_path']]
utils.execute(cmd)
except Exception:
pass
def get_ovsdb_connection_path(self, db_type='nb'):
for ovsdb_process in self.ovsdb_server_processes:
if ovsdb_process['db_type'] == db_type:
return 'unix:' + ovsdb_process['remote_path']

267
networking_ovn/tests/functional/test_ovn_nb_sync.py

@ -0,0 +1,267 @@
# Copyright 2016 Red Hat, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import mock
from networking_ovn import ovn_nb_sync
from networking_ovn.ovsdb import commands as cmd
from networking_ovn.tests.functional import base
from neutron.agent.ovsdb.native import idlutils
from neutron import context
from neutron.tests.unit.api import test_extensions
from neutron.tests.unit.extensions import test_l3
class TestOvnNbSync(base.TestOVNFunctionalBase):
def setUp(self):
super(TestOvnNbSync, self).setUp()
ext_mgr = test_l3.L3TestExtensionManager()
self.ext_api = test_extensions.setup_extensions_middleware(ext_mgr)
self.delete_lswitches = []
self.delete_lports = []
self.delete_lrouters = []
self.delete_lrouter_ports = []
def _create_resources(self):
n1 = self._make_network(self.fmt, 'n1', True)
res = self._create_subnet(self.fmt, n1['network']['id'],
'10.0.0.0/24')
n1_s1 = self.deserialize(self.fmt, res)
for p in ['p1', 'p2', 'p3']:
port = self._make_port(self.fmt, n1['network']['id'],
name='n1-' + p)
if p == 'p2':
self.delete_lports.append((port['port']['id'],
'neutron-' + n1['network']['id']))
n2 = self._make_network(self.fmt, 'n2', True)
res = self._create_subnet(self.fmt, n2['network']['id'],
'20.0.0.0/24')
n2_s1 = self.deserialize(self.fmt, res)
for p in ['p1', 'p2']:
port = self._make_port(self.fmt, n2['network']['id'],
name='n2-' + p)
self.delete_lswitches.append('neutron-' + n2['network']['id'])
r1 = self.l3_plugin.create_router(
self.context,
{'router': {'name': 'r1', 'admin_state_up': True,
'tenant_id': self._tenant_id}})
self.l3_plugin.add_router_interface(
self.context, r1['id'], {'subnet_id': n1_s1['subnet']['id']})
r1_p2 = self.l3_plugin.add_router_interface(
self.context, r1['id'], {'subnet_id': n2_s1['subnet']['id']})
self.delete_lrouter_ports.append(('lrp-' + r1_p2['port_id'],
'neutron-' + r1['id']))
r2 = self.l3_plugin.create_router(
self.context,
{'router': {'name': 'r2', 'admin_state_up': True,
'tenant_id': self._tenant_id}})
n1_p4 = self._make_port(self.fmt, n1['network']['id'],
name='n1-p4')
self.l3_plugin.add_router_interface(
self.context, r2['id'], {'port_id': n1_p4['port']['id']})
self.delete_lrouters.append('neutron-' + r2['id'])
def _delete_resources_in_nb_db(self):
# TODO(numans) Rename this function and also create resources
# in OVN NB DB using the monitor IDL connection so that after the
# sync, these resources are deleted by the ovn_nb_sync from the
# OVN NB DB.
fake_api = mock.MagicMock()
fake_api.idl = self.monitor_nb_db_idl
fake_api._tables = self.monitor_nb_db_idl.tables
with self.idl_transaction(fake_api, check_error=True) as txn:
for lswitch_name in self.delete_lswitches:
txn.add(cmd.DelLSwitchCommand(fake_api, lswitch_name, True))
for lport_name, lswitch_name in self.delete_lports:
txn.add(cmd.DelLSwitchPortCommand(fake_api, lport_name,
lswitch_name, True))
for lrouter_name in self.delete_lrouters:
txn.add(cmd.DelLRouterCommand(fake_api, lrouter_name, True))
for lrport, lrouter_name in self.delete_lrouter_ports:
txn.add(cmd.DelLRouterPortCommand(fake_api, lrport,
lrouter_name, True))
def _validate_networks(self, should_match=True):
db_networks = self._list('networks')
db_net_ids = [net['id'] for net in db_networks['networks']]
# Get the list of lswitch ids stored in the OVN plugin IDL
_plugin_nb_ovn = self.mech_driver._nb_ovn
plugin_lswitch_ids = [
row.name.replace('neutron-', '') for row in (
_plugin_nb_ovn._tables['Logical_Switch'].rows.values())]
# Get the list of lswitch ids stored in the monitor IDL connection
monitor_lswitch_ids = [
row.name.replace('neutron-', '') for row in (
self.monitor_nb_db_idl.tables['Logical_Switch'].rows.values())]
if should_match:
self.assertItemsEqual(db_net_ids, plugin_lswitch_ids)
self.assertItemsEqual(db_net_ids, monitor_lswitch_ids)
else:
self.assertRaises(
AssertionError, self.assertItemsEqual, db_net_ids,
plugin_lswitch_ids)
self.assertRaises(
AssertionError, self.assertItemsEqual, db_net_ids,
monitor_lswitch_ids)
def _validate_ports(self, should_match=True):
db_ports = self._list('ports')
db_port_ids = [port['id'] for port in db_ports['ports']]
_plugin_nb_ovn = self.mech_driver._nb_ovn
plugin_lport_ids = [
row.name for row in (
_plugin_nb_ovn._tables['Logical_Switch_Port'].rows.values())]
monitor_lport_ids = [
row.name for row in (
self.monitor_nb_db_idl.tables['Logical_Switch_Port'].
rows.values())]
if should_match:
self.assertItemsEqual(db_port_ids, plugin_lport_ids)
self.assertItemsEqual(db_port_ids, monitor_lport_ids)
else:
self.assertRaises(
AssertionError, self.assertItemsEqual, db_port_ids,
plugin_lport_ids)
self.assertRaises(
AssertionError, self.assertItemsEqual, db_port_ids,
monitor_lport_ids)
def _validate_routers_and_router_ports(self, should_match=True):
db_routers = self._list('routers')
db_router_ids = [r['id'] for r in db_routers['routers']]
_plugin_nb_ovn = self.mech_driver._nb_ovn
plugin_lrouter_ids = [
row.name.replace('neutron-', '') for row in (
_plugin_nb_ovn._tables['Logical_Router'].rows.values())]
monitor_lrouter_ids = [
row.name.replace('neutron-', '') for row in (
self.monitor_nb_db_idl.tables['Logical_Router'].rows.values())]
if should_match:
self.assertItemsEqual(db_router_ids, plugin_lrouter_ids)
self.assertItemsEqual(db_router_ids, monitor_lrouter_ids)
else:
self.assertRaises(
AssertionError, self.assertItemsEqual, db_router_ids,
plugin_lrouter_ids)
self.assertRaises(
AssertionError, self.assertItemsEqual, db_router_ids,
monitor_lrouter_ids)
for router_id in db_router_ids:
r_ports = self._list('ports',
query_params='device_id=%s' % (router_id))
r_port_ids = [p['id'] for p in r_ports['ports']]
try:
lrouter = idlutils.row_by_value(
self.mech_driver._nb_ovn.idl, 'Logical_Router', 'name',
'neutron-' + str(router_id), None)
lports = getattr(lrouter, 'ports', [])
plugin_lrouter_port_ids = [lport.name.replace('lrp-', '')
for lport in lports]
except idlutils.RowNotFound:
plugin_lrouter_port_ids = []
try:
lrouter = idlutils.row_by_value(
self.monitor_nb_db_idl, 'Logical_Router', 'name',
'neutron-' + router_id, None)
lports = getattr(lrouter, 'ports', [])
monitor_lrouter_port_ids = [lport.name.replace('lrp-', '')
for lport in lports]
except idlutils.RowNotFound:
monitor_lrouter_port_ids = []
if should_match:
self.assertItemsEqual(r_port_ids, plugin_lrouter_port_ids)
self.assertItemsEqual(r_port_ids, monitor_lrouter_port_ids)
else:
self.assertRaises(
AssertionError, self.assertItemsEqual, r_port_ids,
plugin_lrouter_port_ids)
self.assertRaises(
AssertionError, self.assertItemsEqual, r_port_ids,
monitor_lrouter_port_ids)
def _validate_resources(self, should_match=True):
self._validate_networks(should_match=should_match)
self._validate_ports(should_match=should_match)
self._validate_routers_and_router_ports(should_match=should_match)
def _sync_resources(self, mode):
# TODO(numans) - Need to sync ACLs, Static routes
nb_synchronizer = ovn_nb_sync.OvnNbSynchronizer(
self.plugin, self.mech_driver._nb_ovn, mode, self.mech_driver)
ctx = context.get_admin_context()
nb_synchronizer.sync_networks_and_ports(ctx)
nb_synchronizer.sync_routers_and_rports(ctx)
def _test_ovn_nb_sync_helper(self, mode, delete_resources=True,
restart_ovsdb_processes=False,
should_match_after_sync=True):
self._create_resources()
self._validate_resources(should_match=True)
if delete_resources:
self._delete_resources_in_nb_db()
if restart_ovsdb_processes:
# Restart the ovsdb-server and plugin idl.
# This causes a new ovsdb-server to be started with empty
# OVN NB DB
self.restart()
if delete_resources or restart_ovsdb_processes:
self._validate_resources(should_match=False)
self._sync_resources(mode)
self._validate_resources(should_match=should_match_after_sync)
def test_ovn_nb_sync_repair(self):
self._test_ovn_nb_sync_helper('repair')
def test_ovn_nb_sync_repair_delete_ovn_nb_db(self):
# In this test case, the ovsdb-server for OVN NB DB is restarted
# with empty OVN NB DB.
self._test_ovn_nb_sync_helper('repair', delete_resources=False,
restart_ovsdb_processes=True)
def test_ovn_nb_sync_log(self):
self._test_ovn_nb_sync_helper('log', should_match_after_sync=False)
def test_ovn_nb_sync_off(self):
self._test_ovn_nb_sync_helper('off', should_match_after_sync=False)

12
tox.ini

@ -22,6 +22,18 @@ commands = flake8
[testenv:venv]
commands = {posargs}
[testenv:functional]
setenv =
{[testenv]setenv}
OS_TEST_PATH=./networking_ovn/tests/functional
deps = {[testenv]deps}
[testenv:dsvm-functional]
setenv = {[testenv:functional]setenv}
deps = {[testenv:functional]deps}
commands =
bash tools/pretty_tox.sh '{posargs}'
[testenv:cover]
commands = python setup.py test --coverage --coverage-package-name=networking_ovn --testr-args='{posargs}'

Loading…
Cancel
Save