def _test_port_up_down_helper(self, port, ovn_mech_driver):
        # Set the Logical_Switch_Port.up to True. This is to mock
        # the vif plug. When the Logical_Switch_Port.up changes from
        # False to True, ovsdb_monitor should call
        # mech_driver.set_port_status_up.
        with self.nb_idl_transaction(self.fake_api, check_error=True) as txn:
            txn.add(
                cmd.SetLSwitchPortCommand(self.fake_api,
                                          port['id'],
                                          True,
                                          up=True))

        ovn_mech_driver.set_port_status_up.assert_called_once_with(port['id'])
        ovn_mech_driver.set_port_status_down.assert_not_called()

        # Set the Logical_Switch_Port.up to False. ovsdb_monitor should
        # call mech_driver.set_port_status_down
        with self.nb_idl_transaction(self.fake_api, check_error=True) as txn:
            txn.add(
                cmd.SetLSwitchPortCommand(self.fake_api,
                                          port['id'],
                                          True,
                                          up=False))
        ovn_mech_driver.set_port_status_down.assert_called_once_with(
            port['id'])
Exemplo n.º 2
0
    def test_port_up_down_events(self):
        """Test the port up down events.

        This test case creates a port, sets the LogicalSwitchPort.up
        to True and False to test if the ovsdb monitor handles these
        events from the ovsdb server and calls the mech_driver
        functions 'set_port_status_up()' or 'set_port_status_down()' are not.

        For now mocking the 'set_port_status_up()' and 'set_port_status_down()'
        OVN mech driver functions to check if these functions are called or
        not by the ovsdb monitor.

        Ideally it would have been good to check that the port status
        is set to ACTIVE when mech_driver.set_port_status_up calls
        "provisioning_blocks.provisioning_complete". But it is not
        happening because port.binding.vif_type is unbound.

        TODO(numans) - Remove the mocking of these functions and instead create
        the port properly so that vif_type is set to "ovs".
        """
        self.mech_driver.set_port_status_up = mock.Mock()
        self.mech_driver.set_port_status_down = mock.Mock()
        with self.port(name='port') as p:
            p = p['port']
            # using the monitor IDL connection to the NB DB, set the
            # Logical_Switch_Port.up to False first. This is to mock the
            # ovn-controller setting it to False when the logical switch
            # port is created.
            with self.idl_transaction(self.fake_api, check_error=True) as txn:
                txn.add(cmd.SetLSwitchPortCommand(self.fake_api, p['id'], True,
                                                  up=False))

            self._test_port_up_down_helper(p, self.mech_driver)
Exemplo n.º 3
0
    def test_ovsdb_monitor_lock(self):
        """Test case to test the ovsdb monitor lock used by OvnConnection.

        This test case created another IDL connection to the NB DB using
        the ovsdb_monitor.OvnConnection.

        With this we will have 2 'ovsdb_monitor.OvnConnection's. At the
        start the lock should be with the IDL connection created by the
        'TestOVNFunctionalBase' setup() function.

        The port up/down events should be handled by the first IDL connection.
        Then we will restart the first IDL connection so that the 2nd IDL
        connection created in this test case gets the lock and it should
        handle the port up/down events.

        Please note that the "self.monitor_nb_idl_con" created by the base
        class is created using 'connection.Connection' and hence it will not
        contend for any lock.
        """
        fake_driver = mock.MagicMock()
        _idl = ovsdb_monitor.OvnNbIdl.from_server(
            self.ovsdb_server_mgr.get_ovsdb_connection_path(),
            'OVN_Northbound', fake_driver)
        tst_ovn_conn = self.useFixture(
            base.ConnectionFixture(idl=_idl, timeout=10)).connection
        tst_ovn_conn.start()

        self.mech_driver.set_port_status_up = mock.Mock()
        self.mech_driver.set_port_status_down = mock.Mock()

        with self.port(name='port') as p:
            p = p['port']
            with self.nb_idl_transaction(self.fake_api,
                                         check_error=True) as txn:
                txn.add(
                    cmd.SetLSwitchPortCommand(self.fake_api,
                                              p['id'],
                                              True,
                                              up=False))

            self._test_port_up_down_helper(p, self.mech_driver)
            fake_driver.set_port_status_up.assert_not_called()
            fake_driver.set_port_status_down.assert_not_called()

            # Now restart the mech_driver's IDL connection.
            self.mech_driver._nb_ovn.idl.force_reconnect()
            # Wait till the test_ovn_idl_conn has acquired the lock.
            n_utils.wait_until_true(lambda: tst_ovn_conn.idl.has_lock)

            self.mech_driver.set_port_status_up.reset_mock()
            self.mech_driver.set_port_status_down.reset_mock()
            fake_driver.set_port_status_up.reset_mock()
            fake_driver.set_port_status_down.reset_mock()

            self._test_port_up_down_helper(p, fake_driver)
            self.assertFalse(self.mech_driver.set_port_status_up.called)
            self.assertFalse(self.mech_driver.set_port_status_down.called)
Exemplo n.º 4
0
 def set_lswitch_port(self, lport_name, if_exists=True, **columns):
     return cmd.SetLSwitchPortCommand(self, lport_name,
                                      if_exists, **columns)
Exemplo n.º 5
0
    def _modify_resources_in_nb_db(self):
        fake_api = mock.MagicMock()
        fake_api.idl = self.monitor_nb_db_idl
        fake_api._tables = self.monitor_nb_db_idl.tables

        with self.nb_idl_transaction(fake_api, check_error=True) as txn:
            for lswitch_name in self.create_lswitches:
                external_ids = {
                    ovn_const.OVN_NETWORK_NAME_EXT_ID_KEY: lswitch_name
                }
                txn.add(
                    cmd.AddLSwitchCommand(fake_api,
                                          lswitch_name,
                                          True,
                                          external_ids=external_ids))

            for lswitch_name in self.delete_lswitches:
                txn.add(cmd.DelLSwitchCommand(fake_api, lswitch_name, True))

            for lport_name, lswitch_name in self.create_lswitch_ports:
                external_ids = {ovn_const.OVN_PORT_NAME_EXT_ID_KEY: lport_name}
                txn.add(
                    cmd.AddLSwitchPortCommand(fake_api,
                                              lport_name,
                                              lswitch_name,
                                              True,
                                              external_ids=external_ids))

            for lport_name, lswitch_name in self.delete_lswitch_ports:
                txn.add(
                    cmd.DelLSwitchPortCommand(fake_api, lport_name,
                                              lswitch_name, True))

            for lrouter_name in self.create_lrouters:
                external_ids = {
                    ovn_const.OVN_ROUTER_NAME_EXT_ID_KEY: lrouter_name
                }
                txn.add(
                    cmd.AddLRouterCommand(fake_api,
                                          lrouter_name,
                                          True,
                                          external_ids=external_ids))

            for lrouter_name in self.delete_lrouters:
                txn.add(cmd.DelLRouterCommand(fake_api, lrouter_name, True))

            for lrport, lrouter_name in self.create_lrouter_ports:
                txn.add(
                    cmd.AddLRouterPortCommand(fake_api, lrport, lrouter_name))

            for lrport, lrouter_name, networks in self.update_lrouter_ports:
                txn.add(
                    cmd.UpdateLRouterPortCommand(fake_api, lrport,
                                                 lrouter_name, True,
                                                 **{'networks': [networks]}))

            for lrport, lrouter_name in self.delete_lrouter_ports:
                txn.add(
                    cmd.DelLRouterPortCommand(fake_api, lrport, lrouter_name,
                                              True))

            for lrouter_name, ip_prefix, nexthop in self.create_lrouter_routes:
                txn.add(
                    cmd.AddStaticRouteCommand(fake_api,
                                              lrouter_name,
                                              ip_prefix=ip_prefix,
                                              nexthop=nexthop))

            for lrouter_name, ip_prefix, nexthop in self.delete_lrouter_routes:
                txn.add(
                    cmd.DelStaticRouteCommand(fake_api, lrouter_name,
                                              ip_prefix, nexthop, True))

            for acl in self.create_acls:
                txn.add(cmd.AddACLCommand(fake_api, **acl))

            for lport_name, lswitch_name in self.delete_acls:
                txn.add(
                    cmd.DelACLCommand(fake_api, lswitch_name, lport_name,
                                      True))

            for name, ip_version in self.create_address_sets:
                ovn_name = utils.ovn_addrset_name(name, ip_version)
                external_ids = {ovn_const.OVN_SG_NAME_EXT_ID_KEY: name}
                txn.add(
                    cmd.AddAddrSetCommand(fake_api,
                                          ovn_name,
                                          True,
                                          external_ids=external_ids))

            for name, ip_version in self.delete_address_sets:
                ovn_name = utils.ovn_addrset_name(name, ip_version)
                txn.add(cmd.DelAddrSetCommand(fake_api, ovn_name, True))

            for name, ip_version, ip_adds, ip_dels in self.update_address_sets:
                ovn_name = utils.ovn_addrset_name(name, ip_version)
                txn.add(
                    cmd.UpdateAddrSetCommand(fake_api, ovn_name, ip_adds,
                                             ip_dels, True))

            for lport_name in self.reset_lport_dhcpv4_options:
                txn.add(
                    cmd.SetLSwitchPortCommand(fake_api,
                                              lport_name,
                                              True,
                                              dhcpv4_options=[]))

            for dhcp_opts in self.stale_lport_dhcpv4_options:
                txn.add(
                    cmd.AddDHCPOptionsCommand(
                        fake_api,
                        dhcp_opts['subnet_id'],
                        port_id=dhcp_opts['port_id'],
                        cidr=dhcp_opts['cidr'],
                        options=dhcp_opts['options'],
                        external_ids=dhcp_opts['external_ids']))

            for row_uuid in self.missed_dhcpv4_options:
                txn.add(cmd.DelDHCPOptionsCommand(fake_api, row_uuid))

            for dhcp_opts in self.dirty_dhcpv4_options:
                txn.add(
                    cmd.AddDHCPOptionsCommand(fake_api,
                                              dhcp_opts['subnet_id'],
                                              port_id=dhcp_opts.get('port_id'),
                                              external_ids={
                                                  'subnet_id':
                                                  dhcp_opts['subnet_id'],
                                                  'port_id':
                                                  dhcp_opts.get('port_id')
                                              },
                                              options={'foo': 'bar'}))

            for port_id in self.lport_dhcpv4_disabled:
                txn.add(
                    cmd.SetLSwitchPortCommand(
                        fake_api,
                        port_id,
                        True,
                        dhcpv4_options=[self.lport_dhcpv4_disabled[port_id]]))

        with self.nb_idl_transaction(fake_api, check_error=True) as txn:
            for dhcp_opts in self.stale_lport_dhcpv4_options:
                if dhcp_opts['port_id'] in self.orphaned_lport_dhcpv4_options:
                    continue
                uuid = self.mech_driver._nb_ovn.get_port_dhcp_options(
                    dhcp_opts['subnet_id'], dhcp_opts['port_id'])['uuid']
                txn.add(
                    cmd.SetLSwitchPortCommand(fake_api,
                                              lport_name,
                                              True,
                                              dhcpv4_options=[uuid]))