def __init__(self, name, params): self.name = name self.params = params # # Assuming all low-level hypervisors will have a serial (like) console # connection to the guest. libvirt also supports serial (like) consoles # (virDomainOpenConsole). subclasses should set this to an object that # is or behaves like aexpect.ShellSession. # self.serial_console = None # Create instance if not already set if not hasattr(self, 'instance'): self._generate_unique_id() # Don't overwrite existing state, update from params if hasattr(self, 'virtnet'): # Direct reference to self.virtnet makes pylint complain # note: virtnet.__init__() supports being called anytime getattr(self, 'virtnet').__init__(self.params, self.name, self.instance) else: # Create new self.virtnet = utils_misc.VirtNet(self.params, self.name, self.instance) if not hasattr(self, 'cpuinfo'): self.cpuinfo = CpuInfo()
def test_04_VirtNet(self): """ Populate database with max - 1 mac addresses """ try: os.unlink(self.db_filename) except OSError: pass self.zero_counter(25) # setup() method already set LASTBYTE to '-1' for lastbyte in xrange(0, 0xFF): # test_07_VirtNet demands last byte in name and mac match vm_name = "vm%d" % lastbyte if lastbyte < 16: mac = "%s0%x" % (self.mac_prefix, lastbyte) else: mac = "%s%x" % (self.mac_prefix, lastbyte) params = utils_misc.Params({ "nics": "nic1", "vms": vm_name, "mac_nic1": mac, }) virtnet = utils_misc.VirtNet(params, vm_name, vm_name, self.db_filename) virtnet.mac_prefix = self.mac_prefix self.assertEqual(virtnet['nic1'].mac, mac) self.assertEqual(virtnet.get_mac_address(0), mac) # Confirm only lower-case macs are stored self.assertEqual( virtnet.get_mac_address(0).lower(), virtnet.get_mac_address(0)) self.assertEqual(virtnet.mac_list(), [mac]) self.print_and_inc()
def needs_restart(self, name, params, basedir): """ Verifies whether the current virt_install commandline matches the requested one, based on the test parameters. """ if (self.make_create_command() != self.make_create_command( name, params, basedir)): logging.debug( "VM params in env don't match requested, restarting.") return True else: # Command-line encoded state doesn't include all params # TODO: Check more than just networking other_virtnet = utils_misc.VirtNet(params, name, self.instance) if self.virtnet != other_virtnet: logging.debug("VM params in env match, but network differs, " "restarting") logging.debug("\t" + str(self.virtnet)) logging.debug("\t!=") logging.debug("\t" + str(other_virtnet)) return True else: logging.debug( "VM params in env do match requested, continuing.") return False
def test_cmp_Virtnet(self): self.zero_counter() to_test = 600 # Random generator slows this test way down for fakevm1 in self.fakevm_generator(): to_test -= 1 if to_test < 1: break fvm1p = fakevm1.get_params() fakevm1.virtnet = utils_misc.VirtNet(fvm1p, fakevm1.name, fakevm1.instance, self.db_filename) if len(fakevm1.virtnet) < 2: continue fakevm2 = FakeVm(fakevm1.name + "_2", fvm1p) fakevm2.virtnet = utils_misc.VirtNet(fvm1p, fakevm2.name, fakevm2.instance, self.db_filename) # Verify nic order doesn't matter fvm3p = utils_misc.Params(fvm1p.items()) # work on copy nic_list = fvm1p.object_params(fakevm1.name).get( "nics", fvm1p.get('nics', "")).split() random.shuffle(nic_list) fvm3p['nics'] = " ".join(nic_list) fakevm3 = FakeVm(fakevm1.name + "_3", fvm3p) fakevm3.virtnet = utils_misc.VirtNet(fvm3p, fakevm3.name, fakevm3.instance, self.db_filename) self.assertTrue(fakevm1.virtnet == fakevm1.virtnet) self.assertTrue(fakevm1.virtnet == fakevm2.virtnet) self.assertTrue(fakevm1.virtnet == fakevm3.virtnet) self.assertTrue(fakevm2.virtnet == fakevm3.virtnet) if len(fakevm1.virtnet) > 1: del fakevm1.virtnet[0] self.assertFalse(fakevm1.virtnet == fakevm2.virtnet) self.assertFalse(fakevm1.virtnet == fakevm3.virtnet) self.assertTrue(fakevm1.virtnet != fakevm2.virtnet) self.assertTrue(fakevm1.virtnet != fakevm3.virtnet) self.print_and_inc()
def test_08_ifname(self): for fakevm in self.fakevm_generator(): # only need to test kvm instance if fakevm.vm_type != 'kvm': continue test_params = fakevm.get_params() virtnet = utils_misc.VirtNet(test_params, fakevm.name, fakevm.name) for virtnet_index in xrange(0, len(virtnet)): result = virtnet.generate_ifname(virtnet_index) self.assertEqual(result, virtnet[virtnet_index].ifname) # assume less than 10 nics self.assert_(len(result) < 11) if len(virtnet) == 2: break # no need to test every possible combination
def test_06_VirtNet(self): """ Generate last possibly mac and verify value. DEPENDS ON test_05_VirtNet running first """ self.zero_counter(25) # test two nics, second mac generation should fail (pool exhausted) params = utils_misc.Params({"nics": "nic1 nic2", "vms": "vm255"}) virtnet = utils_misc.VirtNet(params, 'vm255', 'vm255', self.db_filename) virtnet.mac_prefix = self.mac_prefix self.assertRaises(AttributeError, virtnet.get_mac_address, 'nic1') mac = "%s%x" % (self.mac_prefix, 255) # This will grab the last available address # only try 300 times, guarantees LASTBYTE counter will loop once self.assertEqual(virtnet.generate_mac_address(0, 300), mac) # This will fail allocation self.assertRaises(utils_misc.NetError, virtnet.generate_mac_address, 1, 300)
def test_05_VirtNet(self): """ Load max - 1 entries from db, overriding params. DEPENDS ON test_04_VirtNet running first """ self.zero_counter(25) # second loop forces db load from disk # also confirming params merge with db data for lastbyte in xrange(0, 0xFF): vm_name = "vm%d" % lastbyte params = utils_misc.Params({"nics": "nic1", "vms": vm_name}) virtnet = utils_misc.VirtNet(params, vm_name, vm_name, self.db_filename) if lastbyte < 16: mac = "%s0%x" % (self.mac_prefix, lastbyte) else: mac = "%s%x" % (self.mac_prefix, lastbyte) self.assertEqual(virtnet['nic1'].mac, mac) self.assertEqual(virtnet.get_mac_address(0), mac) self.print_and_inc()
def test_07_VirtNet(self): """ Release mac from beginning, midle, and end, re-generate + verify value """ self.zero_counter(1) beginning_params = utils_misc.Params({ "nics": "nic1 nic2", "vms": "vm0" }) middle_params = utils_misc.Params({ "nics": "nic1 nic2", "vms": "vm127" }) end_params = utils_misc.Params({ "nics": "nic1 nic2", "vms": "vm255", }) for params in (beginning_params, middle_params, end_params): vm_name = params['vms'] virtnet = utils_misc.VirtNet(params, vm_name, vm_name, self.db_filename) virtnet.mac_prefix = self.mac_prefix iface = virtnet['nic1'] last_db_mac_byte = iface.mac_str_to_int_list(iface.mac)[-1] last_vm_name_byte = int(vm_name[2:]) # Sequential generation from test_04_VirtNet guarantee self.assertEqual(last_db_mac_byte, last_vm_name_byte) # only try 300 times, guarantees LASTBYTE counter will loop once self.assertRaises(utils_misc.NetError, virtnet.generate_mac_address, 1, 300) virtnet.free_mac_address(0) virtnet.free_mac_address(1) # generate new on nic1 to verify mac_index generator catches it # and to signify database updated after generation virtnet.generate_mac_address(1, 300) last_db_mac_byte = virtnet['nic2'].mac_str_to_int_list( virtnet['nic2'].mac)[-1] self.assertEqual(last_db_mac_byte, last_vm_name_byte) self.assertEqual(virtnet.get_mac_address(1), virtnet[1].mac) self.print_and_inc()