def test_release_vif_delete_failed(self):
        cls = nested_macvlan_vif.NestedMacvlanPodVIFDriver
        m_driver = mock.Mock(spec=cls)
        os_net = self.useFixture(k_fix.MockNetworkClient()).client

        port_id = lib_utils.get_hash()
        pod = mock.sentinel.pod
        vif = mock.Mock()
        vif.id = port_id

        container_mac = mock.sentinel.mac_address
        container_ip = mock.sentinel.ip_addresses
        container_port = fake.get_port_obj(port_id=port_id,
                                           ip_address=container_ip,
                                           mac_address=container_mac)
        os_net.get_port.return_value = container_port
        os_net.delete_port.side_effect = o_exc.NotFoundException

        vm_port = fake.get_port_obj()
        m_driver._get_parent_port.return_value = vm_port
        m_driver._try_update_port.return_value = 0
        m_driver.lock = mock.MagicMock(spec=threading.Lock())

        cls.release_vif(m_driver, pod, vif)

        os_net.get_port.assert_called_once_with(port_id)
        m_driver._get_parent_port.assert_called_once_with(pod)
        m_driver._try_update_port.assert_called_once()
        os_net.delete_port.assert_called_once_with(vif.id,
                                                   ignore_missing=False)
    def test_neutron_to_osvif_vif_ovs_no_bridge(self):
        vif_plugin = 'ovs'
        port = fake.get_port_obj(port_id=str(uuid.uuid4()))
        subnets = {}

        self.assertRaises(o_cfg.RequiredOptError, ovu.neutron_to_osvif_vif_ovs,
                          vif_plugin, port, subnets)
    def test_neutron_to_osvif_vif_ovs_native(self, m_mk_profile, m_mk_vif,
                                             m_make_vif_network,
                                             m_is_port_active, m_get_vif_name):
        vif_plugin = 'ovs'
        vif_details = {
            'ovs_hybrid_plug': False,
            'bridge_name': mock.sentinel.ovs_bridge
        }
        port = fake.get_port_obj(vif_details=vif_details)
        port.active = mock.sentinel.port_active
        port.profile = mock.sentinel.port_profile

        subnets = mock.sentinel.subnets
        network = mock.sentinel.network
        vif_name = "vhu01234567-89"
        vif = mock.sentinel.vif

        m_mk_profile.return_value = port.profile
        m_make_vif_network.return_value = network
        m_is_port_active.return_value = port.active
        m_get_vif_name.return_value = vif_name
        m_mk_vif.return_value = vif

        self.assertEqual(
            vif, ovu.neutron_to_osvif_vif_ovs(vif_plugin, port, subnets))
        m_mk_profile.assert_called_once_with(interface_id=port.id)
        m_make_vif_network.assert_called_once_with(port, subnets)
        m_is_port_active.assert_called_once_with(port)
        m_get_vif_name.assert_called_once_with(port)
        self.assertEqual(network.bridge,
                         port.binding_vif_details['bridge_name'])
    def test_release_vif_parent_not_found(self):
        cls = nested_macvlan_vif.NestedMacvlanPodVIFDriver
        m_driver = mock.Mock(spec=cls)
        os_net = self.useFixture(k_fix.MockNetworkClient()).client

        port_id = lib_utils.get_hash()
        pod = mock.sentinel.pod
        vif = mock.Mock()
        vif.id = port_id

        container_mac = mock.sentinel.mac_address
        container_ip = mock.sentinel.ip_address
        container_port = fake.get_port_obj(port_id=port_id,
                                           ip_address=container_ip,
                                           mac_address=container_mac)
        os_net.get_port.return_value = container_port

        m_driver.lock = mock.MagicMock(spec=threading.Lock())
        m_driver._get_parent_port.side_effect = o_exc.SDKException

        self.assertRaises(o_exc.SDKException, cls.release_vif, m_driver, pod,
                          vif)
        os_net.get_port.assert_called_with(port_id)
        self.assertEqual(os_net.get_port.call_count, 1)
        m_driver._get_parent_port.assert_called_with(pod)
        self.assertEqual(m_driver._get_parent_port.call_count, 1)
        m_driver._remove_from_allowed_address_pairs.assert_not_called()
        os_net.delete_port.assert_not_called()
    def test_request_vif_parent_not_found(self, m_to_vif):
        cls = nested_macvlan_vif.NestedMacvlanPodVIFDriver
        cls._tag_on_creation = True
        m_driver = mock.Mock(spec=cls)
        os_net = self.useFixture(k_fix.MockNetworkClient()).client

        pod = mock.sentinel.pod
        project_id = mock.sentinel.project_id
        subnets = mock.sentinel.subnets
        security_groups = mock.sentinel.security_groups
        container_mac = mock.sentinel.mac_address
        container_ip = mock.sentinel.ip_address
        container_port = fake.get_port_obj(mac_address=container_mac,
                                           ip_address=container_ip)

        port_request = mock.sentinel.port_request
        m_driver._get_port_request.return_value = port_request
        m_driver.lock = mock.MagicMock(spec=threading.Lock())
        os_net.create_port.return_value = container_port
        m_driver._get_parent_port.side_effect = o_exc.SDKException

        self.assertRaises(o_exc.SDKException, cls.request_vif, m_driver, pod,
                          project_id, subnets, security_groups)
        m_driver._get_port_request.assert_called_once_with(
            pod, project_id, subnets, security_groups)
        os_net.create_port.assert_not_called()
        m_driver._get_parent_port.assert_called_once_with(pod)
        m_driver._try_update_port.assert_not_called()
        m_to_vif.assert_not_called()
    def test_try_update_port(self, aaapf_mock):
        cls = nested_macvlan_vif.NestedMacvlanPodVIFDriver
        m_driver = mock.Mock(spec=cls)
        m_driver.lock = mock.MagicMock(spec=threading.Lock())
        self.useFixture(k_fix.MockNetworkClient()).client

        port_id = lib_utils.get_hash()
        vm_port = fake.get_port_obj(port_id)

        mac_addr = 'fa:16:3e:1b:30:00'
        address_pairs = [
            {
                'ip_address': '10.0.0.30',
                'mac_address': mac_addr
            },
            {
                'ip_address': 'fe80::f816:3eff:fe1c:36a9',
                'mac_address': mac_addr
            },
        ]
        vm_port['allowed_address_pairs'].extend(address_pairs)

        ip_addr = ['10.0.0.29']
        attempts = cls._try_update_port(m_driver, 3,
                                        cls._add_to_allowed_address_pairs,
                                        vm_port, frozenset(ip_addr), mac_addr)
        self.assertEqual(attempts, 0)
        aaapf_mock.assert_called_once()
    def test_try_update_port_failure(self, aaapf_mock):
        cls = nested_macvlan_vif.NestedMacvlanPodVIFDriver
        m_driver = mock.Mock(spec=cls)
        m_driver.lock = mock.MagicMock(spec=threading.Lock())
        self.useFixture(k_fix.MockNetworkClient()).client

        port_id = lib_utils.get_hash()
        vm_port = fake.get_port_obj(port_id)

        mac_addr = 'fa:16:3e:1b:30:00'
        address_pairs = [
            {
                'ip_address': '10.0.0.30',
                'mac_address': mac_addr
            },
            {
                'ip_address': 'fe80::f816:3eff:fe1c:36a9',
                'mac_address': mac_addr
            },
        ]
        vm_port['allowed_address_pairs'].extend(address_pairs)

        ip_addr = ['10.0.0.29']

        aaapf_mock.side_effect = o_exc.SDKException
        self.assertRaises(o_exc.SDKException, cls._try_update_port, m_driver,
                          1, cls._add_to_allowed_address_pairs, vm_port,
                          frozenset(ip_addr), mac_addr)
    def test_get_vif_name(self, m_get_veth_pair_names):
        vif_name = mock.sentinel.vif_name
        port = fake.get_port_obj(port_id=str(uuid.uuid4()))
        m_get_veth_pair_names.return_value = (vif_name, mock.sentinel.any)

        self.assertEqual(vif_name, ovu._get_vif_name(port))
        m_get_veth_pair_names.assert_called_once_with(port.id)
    def test_remove_from_allowed_address_pairs_no_update(self, m_mac):
        cls = nested_macvlan_vif.NestedMacvlanPodVIFDriver
        m_driver = mock.Mock(spec=cls)
        self.useFixture(k_fix.MockNetworkClient()).client

        port_id = lib_utils.get_hash()
        vm_port = fake.get_port_obj(port_id)

        mac_addr = 'fa:16:3e:1b:30:00' if m_mac else vm_port['mac_address']
        address_pairs = [
            {
                'ip_address': '10.0.0.30',
                'mac_address': mac_addr
            },
            {
                'ip_address': 'fe80::f816:3eff:fe1c:36a9',
                'mac_address': mac_addr
            },
        ]
        vm_port['allowed_address_pairs'].extend(address_pairs)

        ip_addr = ['10.0.0.29']

        cls._remove_from_allowed_address_pairs(m_driver, vm_port,
                                               frozenset(ip_addr), m_mac)

        m_driver._update_port_address_pairs.assert_not_called()
    def test_add_to_allowed_address_pairs_already_present(self):
        cls = nested_macvlan_vif.NestedMacvlanPodVIFDriver
        m_driver = mock.Mock(spec=cls)
        self.useFixture(k_fix.MockNetworkClient()).client

        port_id = lib_utils.get_hash()
        vm_port = fake.get_port_obj(port_id)
        address_pairs = [
            {
                'ip_address': '10.0.0.30',
                'mac_address': 'fa:16:3e:1b:30:00'
            },
            {
                'ip_address': 'fe80::f816:3eff:fe1c:36a9',
                'mac_address': 'fa:16:3e:1b:30:00'
            },
        ]
        vm_port['allowed_address_pairs'].extend(address_pairs)

        mac_addr = 'fa:16:3e:1b:30:00'
        ip_addr = '10.0.0.30'

        self.assertRaises(k_exc.AllowedAddressAlreadyPresent,
                          cls._add_to_allowed_address_pairs, m_driver, vm_port,
                          frozenset([ip_addr]), mac_addr)
    def test_add_to_allowed_address_pairs_same_ip(self):
        cls = nested_macvlan_vif.NestedMacvlanPodVIFDriver
        m_driver = mock.Mock(spec=cls)
        self.useFixture(k_fix.MockNetworkClient()).client

        port_id = lib_utils.get_hash()
        vm_port = fake.get_port_obj(port_id)
        address_pairs = [
            {
                'ip_address': '10.0.0.30',
                'mac_address': 'fa:16:3e:1b:30:00'
            },
            {
                'ip_address': 'fe80::f816:3eff:fe1c:36a9',
                'mac_address': 'fa:16:3e:1b:30:00'
            },
        ]
        vm_port['allowed_address_pairs'].extend(address_pairs)

        mac_addr = 'fa:16:3e:71:cb:80'
        ip_addr = '10.0.0.30'
        address_pairs.append({'ip_address': ip_addr, 'mac_address': mac_addr})

        cls._add_to_allowed_address_pairs(m_driver, vm_port,
                                          frozenset([ip_addr]), mac_addr)

        m_driver._update_port_address_pairs.assert_called_once_with(
            port_id, address_pairs, revision_number=9)
    def test_remove_from_allowed_address_pairs_no_ip_addresses(self):
        cls = nested_macvlan_vif.NestedMacvlanPodVIFDriver
        m_driver = mock.Mock(spec=cls)
        self.useFixture(k_fix.MockNetworkClient()).client

        port_id = lib_utils.get_hash()
        vm_port = fake.get_port_obj(port_id)

        self.assertRaises(k_exc.IntegrityError,
                          cls._remove_from_allowed_address_pairs, m_driver,
                          vm_port, frozenset())
    def test_neutron_to_osvif_vif_ovs_hybrid(self,
                                             m_mk_profile,
                                             m_mk_vif,
                                             m_make_vif_network,
                                             m_is_port_active,
                                             m_get_vif_name,
                                             m_get_ovs_hybrid_bridge_name):
        vif_plugin = 'ovs'
        port_id = mock.sentinel.port_id
        ovs_bridge = mock.sentinel.ovs_bridge
        port_filter = mock.sentinel.port_filter
        subnets = mock.sentinel.subnets
        port_profile = mock.sentinel.port_profile
        network = mock.sentinel.network
        port_active = mock.sentinel.port_active
        vif_name = "vhu01234567-89"
        hybrid_bridge = mock.sentinel.hybrid_bridge
        vif = mock.sentinel.vif
        port = fake.get_port_obj(port_id=port_id,
                                 vif_details={'ovs_hybrid_plug': True,
                                              'bridge_name': ovs_bridge,
                                              'port_filter': port_filter})

        m_mk_profile.return_value = port_profile
        m_make_vif_network.return_value = network
        m_is_port_active.return_value = port_active
        m_get_vif_name.return_value = vif_name
        m_get_ovs_hybrid_bridge_name.return_value = hybrid_bridge
        m_mk_vif.return_value = vif

        self.assertEqual(vif, ovu.neutron_to_osvif_vif_ovs(vif_plugin, port,
                                                           subnets))

        m_mk_profile.assert_called_once_with(interface_id=port_id)
        m_make_vif_network.assert_called_once_with(port, subnets)
        m_is_port_active.assert_called_once_with(port)
        m_get_ovs_hybrid_bridge_name.assert_called_once_with(port)
        m_get_vif_name.assert_called_once_with(port)
        self.assertEqual(ovs_bridge, network.bridge)
        m_mk_vif.assert_called_once_with(
            id=port_id,
            address=port.mac_address,
            network=network,
            has_traffic_filtering=port.binding_vif_details['port_filter'],
            preserve_on_delete=False,
            active=port_active,
            port_profile=port_profile,
            plugin=vif_plugin,
            vif_name=vif_name,
            bridge_name=hybrid_bridge)
    def test_request_vif(self, m_to_vif):
        cls = nested_macvlan_vif.NestedMacvlanPodVIFDriver
        cls._tag_on_creation = True
        m_driver = mock.Mock(spec=cls)
        os_net = self.useFixture(k_fix.MockNetworkClient()).client

        pod = mock.sentinel.pod
        project_id = mock.sentinel.project_id
        subnets = mock.sentinel.subnets
        security_groups = mock.sentinel.security_groups
        container_mac = mock.sentinel.mac_address
        container_ip = mock.sentinel.ip_address
        container_port = fake.get_port_obj(mac_address=container_mac,
                                           ip_address=container_ip)

        vif = mock.Mock()
        port_request = {'foo': mock.sentinel.port_request}
        vm_port = fake.get_port_obj()

        m_to_vif.return_value = vif
        m_driver._get_port_request.return_value = port_request
        m_driver._get_parent_port.return_value = vm_port
        m_driver._try_update_port.return_value = 0
        m_driver.lock = mock.MagicMock(spec=threading.Lock())
        os_net.create_port.return_value = container_port

        self.assertEqual(
            vif,
            cls.request_vif(m_driver, pod, project_id, subnets,
                            security_groups))

        m_driver._get_port_request.assert_called_once_with(
            pod, project_id, subnets, security_groups)
        os_net.create_port.assert_called_once_with(**port_request)
        m_driver._get_parent_port.assert_called_once_with(pod)
        m_driver._try_update_port.assert_called_once()
        m_to_vif.assert_called_once_with(container_port, subnets)
    def test_neutron_to_osvif_vif_ovs_vu_server(self, m_mk_profile, m_mk_vif,
                                                m_make_vif_network,
                                                m_is_port_active,
                                                m_get_vif_name):
        vif_plugin = 'vhostuser'
        o_cfg.CONF.set_override('mount_point',
                                '/var/lib/cni/vhostuser',
                                group='vhostuser')
        port_id = mock.sentinel.port_id
        mac_address = mock.sentinel.mac_address
        ovs_bridge = mock.sentinel.ovs_bridge
        subnets = mock.sentinel.subnets
        port_profile = mock.sentinel.port_profile
        network = mock.sentinel.network
        port_active = mock.sentinel.port_active
        vif_name = "vhu01234567-89"
        vif = mock.sentinel.vif

        m_mk_profile.return_value = port_profile
        m_make_vif_network.return_value = network
        m_is_port_active.return_value = port_active
        m_get_vif_name.return_value = vif_name
        m_mk_vif.return_value = vif

        port = fake.get_port_obj(port_id=port_id,
                                 vif_details={
                                     'ovs_hybrid_plug': False,
                                     'bridge_name': ovs_bridge,
                                     'vhostuser_mode': 'server'
                                 })
        port.mac_address = mac_address

        self.assertEqual(
            vif, ovu.neutron_to_osvif_vif_ovs(vif_plugin, port, subnets))
        m_mk_profile.assert_called_once_with(interface_id=port_id)
        m_make_vif_network.assert_called_once_with(port, subnets)
        m_is_port_active.assert_called_once_with(port)
        m_get_vif_name.assert_called_once_with(port_id)
        self.assertEqual(ovs_bridge, network.bridge)
    def test_neutron_to_osvif_nested_vlan(self, m_mk_vif, m_make_vif_network,
                                          m_is_port_active, m_get_vif_name):
        vif_plugin = const.K8S_OS_VIF_NOOP_PLUGIN
        port_id = mock.sentinel.port_id
        mac_address = mock.sentinel.mac_address
        port_filter = mock.sentinel.port_filter
        subnets = mock.sentinel.subnets
        network = mock.sentinel.network
        port_active = mock.sentinel.port_active
        vif_name = mock.sentinel.vif_name
        vif = mock.sentinel.vif
        vlan_id = mock.sentinel.vlan_id
        port = fake.get_port_obj(port_id=port_id,
                                 vif_details={'port_filter': port_filter})
        port.mac_address = mac_address

        m_make_vif_network.return_value = network
        m_is_port_active.return_value = port_active
        m_get_vif_name.return_value = vif_name
        m_mk_vif.return_value = vif

        self.assertEqual(vif, ovu.neutron_to_osvif_vif_nested_vlan(port,
                         subnets, vlan_id))

        m_make_vif_network.assert_called_once_with(port, subnets)
        m_is_port_active.assert_called_once_with(port)
        m_get_vif_name.assert_called_once_with(port)
        m_mk_vif.assert_called_once_with(
            id=port_id,
            address=mac_address,
            network=network,
            has_traffic_filtering=port_filter,
            preserve_on_delete=False,
            active=port_active,
            plugin=vif_plugin,
            vif_name=vif_name,
            vlan_id=vlan_id)
    def test_is_port_inactive(self):
        port = fake.get_port_obj(port_id=str(uuid.uuid4()))

        self.assertFalse(ovu._is_port_active(port))
    def test_is_port_active(self):
        port = fake.get_port_obj(port_id=str(uuid.uuid4()))
        port.status = 'ACTIVE'

        self.assertTrue(ovu._is_port_active(port))
    def test_get_ovs_hybrid_bridge_name(self):
        port = fake.get_port_obj(port_id=str(uuid.uuid4()))

        self.assertEqual("qbr" + port.id[:11],
                         ovu._get_ovs_hybrid_bridge_name(port))