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 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()
def test_register_vm(self): """ 1) Create an env object. 2) Create a VM and register it from env. 3) Get the vm back from the env. 4) Verify that the 2 objects are the same. """ env = utils_misc.Env() params = utils_misc.Params({"main_vm": 'rhel7-migration'}) vm1 = FakeVm(params['main_vm'], params) vm1.is_alive() env.register_vm(params['main_vm'], vm1) vm2 = env.get_vm(params['main_vm']) vm2.is_alive() assert vm1 == vm2
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 __init__(self, params): self.params = utils_misc.Params(params) self.bindir = ROOT_PATH self.testdir = os.path.join(self.bindir, 'tests') self.virtdir = os.path.join(self.bindir, 'shared') self.builddir = os.path.join(self.bindir, params.get("vm_type")) self.srcdir = os.path.join(self.builddir, 'src') if not os.path.isdir(self.srcdir): os.makedirs(self.srcdir) self.tmpdir = os.path.join(self.bindir, 'tmp') if not os.path.isdir(self.tmpdir): os.makedirs(self.tmpdir) self.iteration = 0 self.tag = ("%s.%s" % (params.get("vm_type"), params.get("shortname"))) self.debugdir = None self.outputdir = None self.logfile = None self.file_handler = None
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 setUp(self): """ Runs before every test """ # MAC generator produces from incrementing byte list # at random starting point (class property). # make sure it starts counting at zero before every test utils_misc.VirtIface.LASTBYTE = -1 # These warnings are annoying during testing utils_misc.VMNet.DISCARD_WARNINGS - 1 parser = cartesian_config.Parser() parser.parse_string(self.nettests_cartesian) self.CartesianResult = [] for d in parser.get_dicts(): params = utils_misc.Params(d) self.CartesianResult.append(params) for vm_name in params.objects('vms'): vm = params.object_params(vm_name) nics = vm.get('nics') if nics and len(nics.split()) > 0: self.db_item_count += 1
def test_get_all_vms(self): """ 1) Create an env object. 2) Create 2 vms and register them in the env. 3) Create a SyncListenServer and register it in the env. 4) Verify that the 2 vms are in the output of get_all_vms. 5) Verify that the sync server is not in the output of get_all_vms. """ env = utils_misc.Env() params = utils_misc.Params({"main_vm": 'rhel7-migration'}) vm1 = FakeVm(params['main_vm'], params) vm1.is_alive() vm2 = FakeVm('vm2', params) vm2.is_alive() env.register_vm(params['main_vm'], vm1) env.register_vm('vm2', vm2) sync1 = FakeSyncListenServer(port=333) env.register_syncserver(333, sync1) assert vm1 in env.get_all_vms() assert vm2 in env.get_all_vms() assert sync1 not in env.get_all_vms()
def test_unregister_vm(self): """ 1) Create an env object. 2) Register 2 vms to the env. 3) Verify both vms are in the env. 4) Remove one of those vms. 5) Verify that the removed vm is no longer in env. """ env = utils_misc.Env() params = utils_misc.Params({"main_vm": 'rhel7-migration'}) vm1 = FakeVm(params['main_vm'], params) vm1.is_alive() vm2 = FakeVm('vm2', params) vm2.is_alive() env.register_vm(params['main_vm'], vm1) env.register_vm('vm2', vm2) assert vm1 in env.get_all_vms() assert vm2 in env.get_all_vms() env.unregister_vm('vm2') assert vm1 in env.get_all_vms() assert vm2 not in env.get_all_vms()
def test_save(self): """ 1) Verify that calling env.save() with no filename where env doesn't specify a filename will throw an EnvSaveError. 2) Register a VM in environment, save env to a file, recover env from that file, get the vm and verify that the instance attribute of the 2 objects is the same. 3) Register a SyncListenServer and don't save env. Restore env from file and try to get the syncserver, verify it doesn't work. 4) Now save env to a file, restore env from file and verify that the syncserver can be found there, and that the sync server instance attribute is equal to the initial sync server instance. """ fname = "/dev/shm/EnvUnittest" env = utils_misc.Env() self.assertRaises(utils_misc.EnvSaveError, env.save, {}) params = utils_misc.Params({"main_vm": 'rhel7-migration'}) vm1 = FakeVm(params['main_vm'], params) vm1.is_alive() env.register_vm(params['main_vm'], vm1) env.save(filename=fname) env2 = utils_misc.Env(filename=fname) vm2 = env2.get_vm(params['main_vm']) vm2.is_alive() assert vm1.instance == vm2.instance sync1 = FakeSyncListenServer(port=222) env.register_syncserver(222, sync1) env3 = utils_misc.Env(filename=fname) syncnone = env3.get_syncserver(222) assert syncnone is None env.save(filename=fname) env4 = utils_misc.Env(filename=fname) sync2 = env4.get_syncserver(222) assert sync2.instance == sync1.instance if os.path.isfile(fname): os.unlink(fname)
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 run_once(self, params): # Convert params to a Params object params = utils_misc.Params(params) # If a dependency test prior to this test has failed, let's fail # it right away as TestNA. if params.get("dependency_failed") == 'yes': raise error.TestNAError("Test dependency failed") # Report the parameters we've received and write them as keyvals logging.debug("Test parameters:") keys = params.keys() keys.sort() for key in keys: logging.debug(" %s = %s", key, params[key]) self.write_test_keyval({key: params[key]}) # Set the log file dir for the logging mechanism used by kvm_subprocess # (this must be done before unpickling env) utils_misc.set_log_file_dir(self.debugdir) # Open the environment file env_filename = os.path.join(self.bindir, params.get("env", "env")) env = utils_misc.Env(env_filename, self.env_version) test_passed = False try: try: try: subtest_dirs = [] tests_dir = self.job.testdir other_subtests_dirs = params.get("other_tests_dirs", "") for d in other_subtests_dirs.split(): subtestdir = os.path.join(tests_dir, d, "tests") if not os.path.isdir(subtestdir): raise error.TestError("Directory %s not" " exist." % (subtestdir)) subtest_dirs.append(subtestdir) # Verify if we have the correspondent source file for it virt_dir = os.path.dirname(utils_misc.__file__) subtest_dirs.append(os.path.join(virt_dir, "tests")) subtest_dirs.append(os.path.join(self.bindir, "tests")) subtest_dir = None # Get the test routine corresponding to the specified # test type t_types = params.get("type").split() test_modules = [] for t_type in t_types: for d in subtest_dirs: module_path = os.path.join(d, "%s.py" % t_type) if os.path.isfile(module_path): subtest_dir = d break if subtest_dir is None: msg = "Could not find test file %s.py on tests"\ "dirs %s" % (t_type, subtest_dirs) raise error.TestError(msg) # Load the test module f, p, d = imp.find_module(t_type, [subtest_dir]) test_modules.append( (t_type, imp.load_module(t_type, f, p, d))) f.close() # Preprocess try: env_process.preprocess(self, params, env) finally: env.save() # Run the test function for t_type, test_module in test_modules: msg = "Running function: %s.run_%s()" % (t_type, t_type) logging.info(msg) run_func = getattr(test_module, "run_%s" % t_type) try: run_func(self, params, env) finally: env.save() test_passed = True except Exception, e: logging.error("Test failed: %s: %s", e.__class__.__name__, e) try: env_process.postprocess_on_error(self, params, env) finally: env.save() raise finally: # Postprocess try: try: env_process.postprocess(self, params, env) except Exception, e: if test_passed: raise logging.error( "Exception raised during " "postprocessing: %s", e) finally: env.save() except Exception, e: if params.get("abort_on_error") != "yes": raise # Abort on error logging.info("Aborting job (%s)", e) if params.get("vm_type") == "kvm": for vm in env.get_all_vms(): if vm.is_dead(): continue logging.info("VM '%s' is alive.", vm.name) for m in vm.monitors: logging.info( "'%s' has a %s monitor unix socket at: %s", vm.name, m.protocol, m.filename) logging.info( "The command line used to start '%s' was:\n%s", vm.name, vm.make_qemu_command()) raise error.JobError("Abort requested (%s)" % e)