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 = virt_utils.Params({ "nics": "nic1", "vms": vm_name, "mac_nic1": mac, }) virtnet = virt_utils.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 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 = virt_utils.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.assertEqual(14, len(result)) 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 = virt_utils.Params({"nics": "nic1 nic2", "vms": "vm255"}) virtnet = virt_utils.VirtNet(params, 'vm255', 'vm255', self.db_filename) virtnet.mac_prefix = self.mac_prefix self.assertRaises(virt_utils.PropCanKeyError, 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(virt_utils.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 = virt_utils.Params({"nics": "nic1", "vms": vm_name}) virtnet = virt_utils.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 = virt_utils.Params({ "nics": "nic1 nic2", "vms": "vm0" }) middle_params = virt_utils.Params({ "nics": "nic1 nic2", "vms": "vm127" }) end_params = virt_utils.Params({ "nics": "nic1 nic2", "vms": "vm255", }) for params in (beginning_params, middle_params, end_params): vm_name = params['vms'] virtnet = virt_utils.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(virt_utils.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 mac = 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()