/usr/lib/python2.7/dist-packages/ovsdbapp/tests/functional/schema/open_vswitch/test_impl_idl.py is in python-ovsdbapp 0.9.1-0ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 | # -*- coding: utf-8 -*-
# Copyright (c) 2017 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 ovsdbapp import exceptions as exc
from ovsdbapp.schema.open_vswitch import impl_idl
from ovsdbapp.tests.functional import base
from ovsdbapp.tests import utils
# NOTE(twilson) functools.partial does not work for this
def trpatch(*args, **kwargs):
def wrapped(fn):
return mock.patch.object(impl_idl.OvsVsctlTransaction,
*args, **kwargs)(fn)
return wrapped
class TestOvsdbIdl(base.FunctionalTestCase):
schemas = ["Open_vSwitch"]
def setUp(self):
super(TestOvsdbIdl, self).setUp()
self.api = impl_idl.OvsdbIdl(self.connection)
self.brname = utils.get_rand_device_name()
# Destroying the bridge cleans up most things created by tests
cleanup_cmd = self.api.del_br(self.brname)
self.addCleanup(cleanup_cmd.execute)
def test_br_exists_false(self):
exists = self.api.br_exists(self.brname).execute(check_error=True)
self.assertFalse(exists)
def test_add_br_may_exist(self):
self.api.add_br(self.brname).execute(check_error=True)
with self.api.transaction(check_error=True) as txn:
txn.add(self.api.add_br(self.brname, datapath_type="netdev"))
exists = txn.add(self.api.br_exists(self.brname))
dpt = txn.add(self.api.db_get("Bridge", self.brname,
"datapath_type"))
self.assertTrue(exists)
self.assertEqual("netdev", dpt.result)
def test_add_br_may_not_exist(self):
self.api.add_br(self.brname).execute(check_error=True)
cmd = self.api.add_br(self.brname, may_exist=False)
self.assertRaises(RuntimeError, cmd.execute, check_error=True)
def test_del_br_if_exists_false(self):
cmd = self.api.del_br(self.brname, if_exists=False)
self.assertRaises(RuntimeError, cmd.execute, check_error=True)
def test_del_br_if_exists_true(self):
self.api.del_br(self.brname).execute(check_error=True)
def test_del_br(self):
self.api.add_br(self.brname).execute(check_error=True)
self.api.del_br(self.brname).execute(check_error=True)
exists = self.api.br_exists(self.brname).execute(check_error=True)
self.assertFalse(exists)
def _test_add_port(self):
pname = utils.get_rand_device_name()
with self.api.transaction(check_error=True) as txn:
txn.add(self.api.add_br(self.brname))
txn.add(self.api.add_port(self.brname, pname))
return pname
def test_add_port(self):
pname = self._test_add_port()
plist_cmd = self.api.list_ports(self.brname)
ports = plist_cmd.execute(check_error=True)
self.assertIn(pname, ports)
def test_add_port_may_exist_false(self):
pname = self._test_add_port()
cmd = self.api.add_port(self.brname, pname, may_exist=False)
self.assertRaises(RuntimeError, cmd.execute, check_error=True)
def test_del_port(self):
pname = self._test_add_port()
plist_cmd = self.api.list_ports(self.brname)
self.assertIn(pname, plist_cmd.execute(check_error=True))
self.api.del_port(pname).execute(check_error=True)
self.assertNotIn(pname, plist_cmd.execute(check_error=True))
def test_del_port_if_exists_false(self):
cmd = self.api.del_port(utils.get_rand_device_name(), if_exists=False)
self.assertRaises(RuntimeError, cmd.execute, check_error=True)
def test_connection_reconnect(self):
self.api.ovsdb_connection.stop()
existsCmd = self.api.br_exists(self.brname)
txn = self.api.create_transaction(check_error=True)
txn.add(existsCmd)
self.api.ovsdb_connection.queue_txn(txn)
self.api.ovsdb_connection.start()
result = txn.results.get(timeout=self.api.ovsdb_connection.timeout)
self.assertEqual(result, [False])
def test_connection_disconnect_timeout(self):
_is_running_mock = mock.PropertyMock(return_value=True)
connection = self.api.ovsdb_connection
type(connection)._is_running = _is_running_mock
self.addCleanup(delattr, type(connection), '_is_running')
self.assertFalse(connection.stop(1))
class ImplIdlTestCase(base.FunctionalTestCase):
schemas = ['Open_vSwitch']
def setUp(self):
super(ImplIdlTestCase, self).setUp()
self.api = impl_idl.OvsdbIdl(self.connection)
self.brname = utils.get_rand_device_name()
# Make sure exceptions pass through by calling do_post_commit directly
mock.patch.object(
impl_idl.OvsVsctlTransaction, "post_commit",
side_effect=impl_idl.OvsVsctlTransaction.do_post_commit,
autospec=True).start()
def _add_br(self):
# NOTE(twilson) we will be raising exceptions with add_br, so schedule
# cleanup before that.
cmd = self.api.del_br(self.brname)
self.addCleanup(cmd.execute)
with self.api.transaction(check_error=True) as tr:
tr.add(self.api.add_br(self.brname))
return tr
def _add_br_and_test(self):
self._add_br()
ofport = self.api.db_get("Interface", self.brname, "ofport").execute(
check_error=True)
self.assertTrue(int(ofport))
self.assertGreater(ofport, -1)
def test_post_commit_vswitchd_completed_no_failures(self):
self._add_br_and_test()
@trpatch("vswitchd_has_completed", return_value=True)
@trpatch("post_commit_failed_interfaces", return_value=["failed_if1"])
@trpatch("timeout_exceeded", return_value=False)
def test_post_commit_vswitchd_completed_failures(self, *args):
self.assertRaises(impl_idl.VswitchdInterfaceAddException,
self._add_br)
@trpatch("vswitchd_has_completed", return_value=False)
def test_post_commit_vswitchd_incomplete_timeout(self, *args):
# Due to timing issues we may rarely hit the global timeout, which
# raises RuntimeError to match the vsctl implementation
self.api.ovsdb_connection.timeout = 1
self.assertRaises((exc.TimeoutException, RuntimeError), self._add_br)
|