def test_get_dhcp_opts_for_nw01(self): self.mox.StubOutWithMock(db, 'network_get_associated_fixed_ips') self.mox.StubOutWithMock(db, 'virtual_interface_get_by_instance') db.network_get_associated_fixed_ips(mox.IgnoreArg(), mox.IgnoreArg())\ .AndReturn([fixed_ips[1], fixed_ips[2], fixed_ips[5]]) db.virtual_interface_get_by_instance(mox.IgnoreArg(), mox.IgnoreArg())\ .AndReturn([vifs[0], vifs[1], vifs[4]]) db.virtual_interface_get_by_instance(mox.IgnoreArg(), mox.IgnoreArg())\ .AndReturn([vifs[2], vifs[3], vifs[5]]) self.mox.ReplayAll() expected_opts = "NW-i00000000-1,3" actual_opts = self.driver.get_dhcp_opts(None, networks[1]) self.assertEquals(actual_opts, expected_opts)
def test_get_dhcp_hosts_for_nw01(self): self.flags(use_single_default_gateway=True) self.mox.StubOutWithMock(db, 'network_get_associated_fixed_ips') db.network_get_associated_fixed_ips(mox.IgnoreArg(), mox.IgnoreArg())\ .AndReturn([fixed_ips[1], fixed_ips[2]]) self.mox.ReplayAll() expected = \ "10.0.0.2,fake_instance00.enginelocal,"\ "192.168.1.100,net:NW-i00000000-1\n"\ "10.0.0.3,fake_instance01.enginelocal,"\ "192.168.0.101,net:NW-i00000001-1" actual_hosts = self.driver.get_dhcp_hosts(None, networks[0]) self.assertEquals(actual_hosts, expected)
def get_dhcp_hosts(context, network_ref): """Get network's hosts config in dhcp-host format.""" hosts = [] for fixed_ref in db.network_get_associated_fixed_ips(context, network_ref['id']): host = fixed_ref['instance']['host'] if network_ref['multi_host'] and FLAGS.host != host: continue hosts.append(_host_dhcp(fixed_ref)) return '\n'.join(hosts)
def test_update_dhcp_for_nw00(self): self.flags(use_single_default_gateway=True) self.mox.StubOutWithMock(db, 'network_get_associated_fixed_ips') self.mox.StubOutWithMock(db, 'virtual_interface_get_by_instance') self.mox.StubOutWithMock(self.driver, 'write_to_file') self.mox.StubOutWithMock(self.driver, 'ensure_path') self.mox.StubOutWithMock(os, 'chmod') db.network_get_associated_fixed_ips(mox.IgnoreArg(), mox.IgnoreArg())\ .AndReturn([fixed_ips[0], fixed_ips[3]]) db.network_get_associated_fixed_ips(mox.IgnoreArg(), mox.IgnoreArg())\ .AndReturn([fixed_ips[0], fixed_ips[3]]) db.virtual_interface_get_by_instance(mox.IgnoreArg(), mox.IgnoreArg())\ .AndReturn([vifs[0], vifs[1]]) db.virtual_interface_get_by_instance(mox.IgnoreArg(), mox.IgnoreArg())\ .AndReturn([vifs[2], vifs[3]]) self.driver.write_to_file(mox.IgnoreArg(), mox.IgnoreArg()) self.driver.write_to_file(mox.IgnoreArg(), mox.IgnoreArg()) self.driver.ensure_path(mox.IgnoreArg()) self.driver.ensure_path(mox.IgnoreArg()) self.driver.ensure_path(mox.IgnoreArg()) self.driver.ensure_path(mox.IgnoreArg()) self.driver.ensure_path(mox.IgnoreArg()) self.driver.ensure_path(mox.IgnoreArg()) self.driver.ensure_path(mox.IgnoreArg()) os.chmod(mox.IgnoreArg(), mox.IgnoreArg()) os.chmod(mox.IgnoreArg(), mox.IgnoreArg()) self.mox.ReplayAll() self.driver.update_dhcp(None, "eth0", networks[0])
def get_dhcp_opts(context, network_ref): """Get network's hosts config in dhcp-opts format.""" hosts = [] ips_ref = db.network_get_associated_fixed_ips(context, network_ref['id']) if ips_ref: #set of instance ids instance_set = set([fixed_ip_ref['instance_id'] for fixed_ip_ref in ips_ref]) default_gw_network_node = {} for instance_id in instance_set: vifs = db.virtual_interface_get_by_instance(context, instance_id) if vifs: #offer a default gateway to the first virtual interface default_gw_network_node[instance_id] = vifs[0]['network_id'] for fixed_ip_ref in ips_ref: instance_id = fixed_ip_ref['instance_id'] if instance_id in default_gw_network_node: target_network_id = default_gw_network_node[instance_id] # we don't want default gateway for this fixed ip if target_network_id != fixed_ip_ref['network_id']: hosts.append(_host_dhcp_opts(fixed_ip_ref)) return '\n'.join(hosts)