Exemple #1
0
 def test_cleanup_stale_devices(self):
     plugin = mock.MagicMock()
     dev_mgr = dhcp.DeviceManager(self.conf, plugin)
     network = {
         'id':
         'foo_id',
         'tenant_id':
         'foo_tenant',
         'namespace':
         'qdhcp-foo_id',
         'ports': [],
         'subnets': [
             tests_base.AttributeDict({
                 'id': 'subnet_foo_id',
                 'enable_dhcp': True,
                 'ipv6_address_mode': None,
                 'ipv6_ra_mode': None,
                 'cidr': '10.0.0.0/24',
                 'ip_version': 4,
                 'gateway_ip': '10.0.0.1'
             })
         ]
     }
     dhcp_port = {
         'id':
         'foo_port_id',
         'mac_address':
         '10:22:33:44:55:67',
         'fixed_ips': [
             tests_base.AttributeDict({
                 'subnet_id': 'subnet_foo_id',
                 'ip_address': '10.0.0.1'
             })
         ]
     }
     plugin.create_dhcp_port.return_value = tests_base.AttributeDict(
         dhcp_port)
     dev_mgr.driver.plug("foo_id",
                         "foo_id2",
                         "tapfoo_id2",
                         "10:22:33:44:55:68",
                         namespace="qdhcp-foo_id")
     dev_mgr.driver.plug("foo_id",
                         "foo_id3",
                         "tapfoo_id3",
                         "10:22:33:44:55:69",
                         namespace="qdhcp-foo_id")
     ipw = ip_lib.IPWrapper(namespace="qdhcp-foo_id")
     devices = ipw.get_devices(exclude_loopback=True)
     self.addCleanup(ipw.netns.delete, 'qdhcp-foo_id')
     self.assertEqual(2, len(devices))
     # setting up dhcp for the network
     dev_mgr.setup(tests_base.AttributeDict(network))
     common_utils.wait_until_true(
         lambda: 1 == len(ipw.get_devices(exclude_loopback=True)),
         timeout=5,
         sleep=0.1,
         exception=RuntimeError("only one non-loopback device must remain"))
     devices = ipw.get_devices(exclude_loopback=True)
     self.assertEqual("tapfoo_port_id", devices[0].name)
Exemple #2
0
 def get_interface_name(self, network, port):
     device_manager = dhcp.DeviceManager(conf=self.conf, plugin=mock.Mock())
     return device_manager.get_interface_name(network, port)
def t_create_testns(testns_name, network):
    try:
        neutron_credentials = get_neutron_credentials()
        neutron = neutron_client.Client(**neutron_credentials)
        dhcp_agent.register_options()
        dhcp_agent.cfg.CONF(project='neutron')
        conf = dhcp_agent.cfg.CONF
        root_helper = config.get_root_helper(conf)
        ctx = context.get_admin_context_without_session()
        plugin = dhcp_agent.DhcpPluginApi(topics.PLUGIN, ctx,
                                          conf.use_namespaces)
        conf.interface_driver = "neutron.agent.linux.interface.OVSInterfaceDriver"

        a = os.system("ip netns add " + testns_name)
        if a != 0:
            return
        time.sleep(2)

        test_device = dhcp.DeviceManager(conf, root_helper, plugin)
        test_device.test = True
        pnetwork = plugin.get_network_info(network['id'])
        test_tap_interface = test_device.setup(
            pnetwork,
            reuse_existing=False,
            test_tap=True,
            test_tap_mac="ee:ff:ff:ff:ff:ef",
            test_ns=testns_name)
        subnet_id = network['subnets'][0]

        while True:
            try:
                subnet = neutron.list_subnets(id=subnet_id)['subnets'][0]
                break
            except:
                continue

        gateway_ip = subnet['gateway_ip']
        os.system("sudo ip netns exec " + testns_name + " ifconfig " +
                  test_tap_interface + " " + gateway_ip +
                  " netmask 255.255.255.0 up")
        os.system("sudo ip netns exec " + testns_name + " ifconfig " +
                  test_tap_interface + " up")
        print "\nAdded test tap interface {0} ({1}) in namespace {2}".format(
            test_tap_interface, gateway_ip, testns_name)

        time.sleep(2)

        try:
            f = os.popen("sudo ip netns exec qdhcp-" + str(network['id']) +
                         " ip a | grep inet | grep -v 'inet6\|127\|169'")
            output = f.read().splitlines()[0]
            ping_ip = output.split()[1].split('/')[0]
        except Exception as e:
            print "\n ERROR : Error when running command in namespace {0}".format(
                "qdhcp-" + str(network['id']))
            print e
            return

        f = os.popen("sudo ip netns exec " + testns_name + " ping -c 5 " +
                     ping_ip)
        output = f.read()
        ping_output = output.splitlines()
        ping_output = ping_output[len(ping_output) - 2]
        # if " 0% packet loss" not in ping_output:
        # print "\n ERROR: Cannot ping {0} from test namespace {1}\n".format(ping_ip, testns_name)

        f = os.popen("sudo ip netns exec " + testns_name + " ping -c 5 " +
                     ping_ip)
        output = f.read()
        ping_output = output.splitlines()
        ping_output = ping_output[len(ping_output) - 2]
        # if " 0% packet loss" not in ping_output:
        # print "\n ERROR: Cannot ping {0} from test namespace {1}\n".format(ping_ip, testns_name)

        f = os.popen("sudo ip netns exec " + testns_name + " ping -c 5 " +
                     ping_ip)
        output = f.read()
        ping_output = output.splitlines()
        ping_output = ping_output[len(ping_output) - 2]
        # if " 0% packet loss" not in ping_output:
        # print "\n ERROR: Cannot ping {0} from test namespace {1}\n".format(ping_ip, testns_name)
    except Exception as e:
        print "\n ERROR : An exception occurred when creating test namespace {0} for {1}".format(
            testns_name, network['name'])
        print e
        os.system("ip netns delete " + testns_name)
        time.sleep(0.5)