def package_install(): install_official_packages() has_qemu, install_qemu = check_package("Qemu", "which qemu-system-x86_64") has_openipmi, install_openipmi = check_package("Openipmi", "which ipmi_sim") has_seabios, install_seabios = check_package( "Seabios", "ls /usr/local/share/qemu/bios-256k.bin") if install_qemu: if not has_qemu: install_bintray_packages("deb", "Infrasim_Qemu") elif not install_seabios: run_command( "mv /usr/local/share/qemu/bios-256k.bin /usr/local/share/qemu/bios-256k.bin.bk" ) run_command("dpkg -r infrasim-qemu") install_bintray_packages("deb", "Infrasim_Qemu") run_command( "mv /usr/local/share/qemu/bios-256k.bin.bk /usr/local/share/qemu/bios-256k.bin" ) else: run_command("dpkg -r infrasim-qemu") install_bintray_packages("deb", "Infrasim_Qemu") if install_openipmi: if has_openipmi: run_command("dpkg -r infrasim-openipmi") install_bintray_packages("deb", "OpenIpmi") if install_seabios: install_bintray_packages("generic", "Seabios")
def test_chassis_power_reset(self): try: # Get qemu mac addresses macs_former = get_mac() pid_before = get_qemu_pid() assert pid_before != "" assert os.path.exists("/proc/{}".format(pid_before)) run_command(power_reset_cmd) time.sleep(2.5) pid_after = get_qemu_pid() assert pid_after != "" assert os.path.exists("/proc/{}".format(pid_after)) assert pid_after != pid_before # Get qemu mac addresses again macs_latter = get_mac() # Verify mac address list remains the same assert sorted(macs_former) == sorted(macs_latter) except Exception as e: print e assert False
def test_init(self): """ CLI test: test init "-f" which will remove existing workspace """ output_info = {} output_start = run_command("infrasim node start") self.assertEqual(output_start[0], 0) self.assertTrue(Workspace.check_workspace_exists(self.node_name)) output_info['start'] = run_command("infrasim node info") self.assertEqual(output_info['start'][0], 0) self.assertRaises(CommandRunFailed, run_command, cmd="infrasim init -s", shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) run_command("infrasim init -s -f") self.assertEqual(len(os.listdir(config.infrasim_home)), 1) # Verify if it will reinstall packages when user confirmed 'Y' result = run_command_with_user_input("infrasim init", True, subprocess.PIPE, subprocess.PIPE, subprocess.PIPE, "Y\nY\nY\n") assert "downloading Infrasim_Qemu" in result[1] assert "downloading OpenIpmi" in result[1] assert "downloading Seabios" in result[1] result = run_command_with_user_input("infrasim init", True, subprocess.PIPE, subprocess.PIPE, subprocess.PIPE, "Y\nyes\nn\n") assert "downloading Infrasim_Qemu" in result[1] assert "downloading OpenIpmi" in result[1] assert "downloading Seabios" not in result[1] result = run_command_with_user_input("infrasim init", True, subprocess.PIPE, subprocess.PIPE, subprocess.PIPE, "no\nN\nY\n") assert "downloading Infrasim_Qemu" not in result[1] assert "downloading OpenIpmi" not in result[1] assert "downloading Seabios" in result[1]
def test_stop_stop(self): """ CLI test: stop a node will hint it's already stopped """ output_start = run_command("infrasim node start") self.assertEqual(output_start[0], 0) self.assertTrue(Workspace.check_workspace_exists(self.node_name)) output_stop_1 = run_command("infrasim node stop") self.assertEqual(output_stop_1[0], 0) self.assertTrue(Workspace.check_workspace_exists(self.node_name)) output_stop_2 = run_command("infrasim node stop") self.assertEqual(output_stop_2[0], 0) self.assertTrue(Workspace.check_workspace_exists(self.node_name)) assert "{}-socat is running".format(self.node_name) in output_start[1] assert "{}-bmc is running".format(self.node_name) in output_start[1] assert "{}-node is running".format(self.node_name) in output_start[1] assert "{}-socat is stopped".format(self.node_name) in output_stop_1[1] assert "{}-bmc is stopped".format(self.node_name) in output_stop_1[1] assert "{}-node is stopped".format(self.node_name) in output_stop_1[1] assert "{}-node is stopped".format(self.node_name) in output_stop_2[1] assert "{}-bmc is stopped".format(self.node_name) in output_stop_2[1] assert "{}-socat is stopped".format(self.node_name) in output_stop_2[1]
def __init__(self, update_cache=True, purge=True, install_recommends=True, force=True, autoremove=True, autoclean=False, only_upgrade=True, allow_unauthenticated=True, source_list_entry=None): self.__update_cache = update_cache self.__purge = purge self.__install_recommends = install_recommends self.__force = force self.__autoremove = autoremove self.__autoclean = autoclean self.__only_upgrade = only_upgrade self.__allow_unauthenticated = allow_unauthenticated if not HAS_PYTHON_APT: run_command("apt-get update") run_command( "apt-get install --no-install-recommends python-apt -y -q") global apt, apt_pkg, aptsources import apt import apt_pkg import aptsources.sourceslist self.__add_entry(source_list_entry) self.__cache = self.__get_cache() self.init()
def test_stop_stop(self): """ CLI test: stop a node will hint it's already stopped """ output_start = run_command("infrasim node start") self.assertEqual(output_start[0], 0) self.assertTrue(Workspace.check_workspace_exists(self.node_name)) output_stop_1 = run_command("infrasim node stop") self.assertEqual(output_stop_1[0], 0) self.assertTrue(Workspace.check_workspace_exists(self.node_name)) output_stop_2 = run_command("infrasim node stop") self.assertEqual(output_stop_2[0], 0) self.assertTrue(Workspace.check_workspace_exists(self.node_name)) assert "{}-socat starts to run".format( self.node_name) in output_start[1] assert "{}-bmc starts to run".format(self.node_name) in output_start[1] assert "{}-node is running".format(self.node_name) in output_start[1] assert "{}-socat stop".format(self.node_name) in output_stop_1[1] assert "{}-bmc stop".format(self.node_name) in output_stop_1[1] assert "{}-node stop".format(self.node_name) in output_stop_1[1] assert "[ ] {}-node is stopped".format( self.node_name) in output_stop_2[1] assert "[ ] {}-bmc is stopped".format( self.node_name) in output_stop_2[1] assert "[ ] {}-socat is stopped".format( self.node_name) in output_stop_2[1]
def test_chassis_power_off_on(self): try: status_output = run_command(power_status_cmd)[1] qemu_output = run_command(test_cmd)[1] assert 'Chassis Power is on' in status_output assert 'qemu-system-x86_64' in qemu_output # Get qemu mac addresses macs_former = r.findall(qemu_output) run_command(power_off_cmd) qemu_output = run_command(test_cmd)[1] status_output = run_command(power_status_cmd)[1] assert 'Chassis Power is off' in status_output assert 'qemu-system-x86_64' not in qemu_output run_command(power_on_cmd) qemu_output = run_command(test_cmd)[1] status_output = run_command(power_status_cmd)[1] assert 'Chassis Power is on' in status_output assert 'qemu-system-x86_64' in qemu_output # Get qemu mac addresses again macs_latter = r.findall(qemu_output) # Verify mac address list remains the same assert sorted(macs_former) == sorted(macs_latter) except Exception as e: print e import traceback print traceback.format_exc() assert False
def __init__(self, update_cache=True, purge=True, install_recommends=True, force=True, autoremove=True, autoclean=False, only_upgrade=True, allow_unauthenticated=True, source_list_entry=None): self.__update_cache = update_cache self.__purge = purge self.__install_recommends = install_recommends self.__force = force self.__autoremove = autoremove self.__autoclean = autoclean self.__only_upgrade = only_upgrade self.__allow_unauthenticated = allow_unauthenticated if not HAS_PYTHON_APT: run_command("apt-get update") run_command("apt-get install --no-install-recommends python-apt -y -q") global apt, apt_pkg, aptsources import apt import apt_pkg import aptsources.sourceslist self.__add_entry(source_list_entry) self.__cache = self.__get_cache() self.init()
def test_start_node_with_conflict_ipmi_console_ssh_port(self): """ Port test: after node1 start, if node2 also use the same ipmi_console_ssh_port to start, it won't start """ socat_result = run_command(PS_SOCAT, True, subprocess.PIPE, subprocess.PIPE)[1] ipmi_result = run_command(PS_IPMI, True, subprocess.PIPE, subprocess.PIPE)[1] qemu_result = run_command(PS_QEMU, True, subprocess.PIPE, subprocess.PIPE)[1] if 'dell' in self.node_info['type']: racadm_result = run_command(PS_RACADM, True, subprocess.PIPE, subprocess.PIPE)[1] # check if node test1 is running assert "test1" in socat_result assert "test1" in ipmi_result assert "test1-node" in qemu_result if 'dell' in self.node_info['type']: assert "test1" in racadm_result ipmi_console_thread = threading.Thread(target=ipmiconsole.start, args=(self.node_info["name"], )) ipmi_console_thread.setDaemon(True) ipmi_console_thread.start() time.sleep(20) self.node_info_2['name'] = "test2" # modify node configuration to resolve port conflict self.node_info_2['ipmi_console_port'] = 9001 self.node_info_2['bmc_connection_port'] = 9101 if 'dell' in self.node_info_2['type']: self.node_info_2['racadm'] = {} self.node_info_2['racadm']['port'] = 10023 self.node_info_2['compute']['vnc_display'] = 2 self.node_info_2['monitor'] = { 'enable': True, 'interface': None, 'port': 9006 } node2 = model.CNode(self.node_info_2) node2.init() node2.precheck() node2.start() # FIXME: Sleep is not a good way to wait for vbmc start time.sleep(5) ipmi_console_cmd = "sudo ipmi-console start test2" self.assertRaises(Exception, run_command, cmd=ipmi_console_cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) ipmiconsole.stop(self.node_info["name"]) node2.stop()
def init(self): self.__bootindex = self._drive_info.get("bootindex") self.__serial = self._drive_info.get("serial") self.__version = self._drive_info.get("version") self.__wwn = self._drive_info.get("wwn") self.__share_rw = self._drive_info.get("share-rw", "false") self.__page_file = self._drive_info.get("page-file") self.__format = self._drive_info.get("format", "qcow2") self.__cache = self._drive_info.get("cache", "writeback") self.__aio = self._drive_info.get("aio") self.__drive_file = self._drive_info.get("file") self.__l2_cache_size = self._drive_info.get("l2-cache-size") self.__refcount_cache_size = self._drive_info.get("refcount-cache-size") self.__cluster_size = self._drive_info.get("cluster-size") self.__preallocation_mode = self._drive_info.get("preallocation") self.__discard = self._drive_info.get("discard") self.__sector_size = self._drive_info.get("sector_size") self.__size = self._drive_info.get("size", 8) # assume the files starts with "/dev/" are block device # all the block devices are assumed to be raw format if self.__drive_file and self.__drive_file.startswith("/dev/"): self.__format = "raw" elif self.__drive_file is None: parent = self.owner while parent and not hasattr(parent, "get_workspace"): parent = parent.owner ws = None if hasattr(parent, "get_workspace"): ws = parent.get_workspace() if ws is None or not os.path.exists(ws): ws = "" # If user announce drive file in config, use it # else create for them. disk_file_base = os.path.join(config.infrasim_home, ws) self.__drive_file = os.path.join(disk_file_base, "disk-{}-{}.img". format(self.prefix, self.get_uniq_name())) if not os.path.exists(self.__drive_file): self.logger.info("[BaseDrive] Creating drive: {}".format(self.__drive_file)) create_option_list = [] if self.__cluster_size: create_option_list.append("=".join(["cluster_size", self.__cluster_size])) if self.__preallocation_mode: create_option_list.append("=".join(["preallocation", self.__preallocation_mode])) command = "qemu-img create -f {0} {1} {2}G".format(self.__format, self.__drive_file, self.__size) if len(create_option_list) > 0: command = "{} -o {}".format(command, ",".join(create_option_list)) run_command(command)
def test_start_node_with_conflict_ipmi_console_ssh_port(self): """ Port test: after node1 start, if node2 also use the same ipmi_console_ssh_port to start, it won't start """ socat_result = run_command(PS_SOCAT, True, subprocess.PIPE, subprocess.PIPE)[1] ipmi_result = run_command(PS_IPMI, True, subprocess.PIPE, subprocess.PIPE)[1] qemu_result = run_command(PS_QEMU, True, subprocess.PIPE, subprocess.PIPE)[1] if 'dell' in self.node_info['type']: racadm_result = run_command(PS_RACADM, True, subprocess.PIPE, subprocess.PIPE)[1] # check if node test1 is running assert "test1" in socat_result assert "test1" in ipmi_result assert "test1-node" in qemu_result if 'dell' in self.node_info['type']: assert "test1" in racadm_result ipmi_console_thread = threading.Thread( target=ipmiconsole.start, args=(self.node_info["name"],)) ipmi_console_thread.setDaemon(True) ipmi_console_thread.start() time.sleep(20) self.node_info_2['name'] = "test2" # modify node configuration to resolve port conflict self.node_info_2['ipmi_console_port'] = 9001 self.node_info_2['bmc_connection_port'] = 9101 if 'dell' in self.node_info_2['type']: self.node_info_2['racadm'] = {} self.node_info_2['racadm']['port'] = 10023 self.node_info_2['compute']['vnc_display'] = 2 self.node_info_2['monitor'] = { 'enable': True, 'interface': None, 'port': 9006 } node2 = model.CNode(self.node_info_2) node2.init() node2.precheck() node2.start() # FIXME: Sleep is not a good way to wait for vbmc start time.sleep(5) ipmi_console_cmd = "sudo ipmi-console start test2" self.assertRaises( Exception, run_command, cmd=ipmi_console_cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) ipmiconsole.stop(self.node_info["name"]) node2.stop()
def bind_cpus_mono(self, cores, threads): try: i = 0 for core in cores: if i < len(cores): cmd = "taskset -pc {} {}".format(core, threads[i]) run_command(cmd) i = i + 1 except Exception as e: self.logger.warning('[CCPUBinding] {}'.format(str(e)))
def test_chassis_power_reset(self): try: pid_before = run_command(pid_cmd)[1] run_command(power_reset_cmd) qemu_output = run_command(test_cmd)[1] assert 'qemu-system-x86_64' in qemu_output pid_after = run_command(pid_cmd)[1] assert pid_after != pid_before except Exception as e: print e assert False
def test_delete_config_then_start_node(self): """ CLI test: node will not start after config is deleted if there is no runtime workspace """ output_delete = run_command("infrasim config delete {}".format(self.test_name)) self.assertEqual(output_delete[0], 0) output_start = run_command("infrasim node start {}".format(self.test_name)) self.assertEqual(output_start[0], 0) assert "{}'s configuration mapping removed".format(self.test_name) in output_delete[1] assert "{}'s configuration is not defined.".format(self.test_name) in output_start[1]
def test_delete_config_then_start_node(self): """ CLI test: node will not start after config is deleted if there is no runtime workspace """ output_delete = run_command("infrasim config delete {}".format(self.test_name)) self.assertEqual(output_delete[0], 0) output_start = run_command("infrasim node start {}".format(self.test_name)) self.assertEqual(output_start[0], 0) assert "Node {}'s configuration mapping removed".format(self.test_name) in output_delete[1] assert "Node {}'s configuration is not defined.".format(self.test_name) in output_start[1]
def precheck(self): # check if qemu-system-x86_64 exists # if self.__qemu_bin is an absolute path, check if it exists if not self.__qemu_bin.startswith("qemu"): try: run_command("which {}".format(self.__qemu_bin)) except CommandRunFailed: self.logger.exception("[Compute] Can not find file {}".format( self.__qemu_bin)) raise CommandNotFound(self.__qemu_bin) # check if smbios exists if not os.path.isfile(self.__smbios): raise ArgsNotCorrect( "[Compute] Target SMBIOS file doesn't exist: {}".format( self.__smbios)) if self.__kernel and os.path.exists(self.__kernel) is False: raise ArgsNotCorrect("[Compute] Kernel {} does not exist.".format( self.__kernel)) if self.__initrd and os.path.exists(self.__initrd) is False: raise ArgsNotCorrect("[Compute] Kernel {} does not exist.".format( self.__initrd)) # check if VNC port is in use if helper.check_if_port_in_use("0.0.0.0", self.__display + 5900): raise ArgsNotCorrect( "[Compute] VNC port {} is already in use.".format( self.__display + 5900)) # check sub-elements for element in self.__element_list: element.precheck() if 'boot' in self.__compute: if 'menu' in self.__compute['boot']: if isinstance(self.__compute['boot']['menu'], str): menu_option = str( self.__compute['boot']['menu']).strip(" ").lower() if menu_option not in ["on", "off"]: msg = "[Compute] Error: illegal config option. " \ "The 'menu' must be either 'on' or 'off'." raise ArgsNotCorrect(msg) elif not isinstance(self.__compute['boot']['menu'], bool): msg = "[Compute] Error: illegal config option. The 'menu' " \ "must be either 'on' or 'off'." raise ArgsNotCorrect(msg) # check kvm enabled is bool if self.__enable_kvm is not True and self.__enable_kvm is not False: raise ArgsNotCorrect( "[Compute] KVM enabled is not a boolean: {}".format( self.__enable_kvm))
def teardown_module(): # Destroy bridge cmd = "ip link set dev {} down".format(FAKE_BRIDGE) run_command(cmd) cmd = "brctl delbr {}".format(FAKE_BRIDGE) run_command(cmd) os.system("pkill socat") os.system("pkill ipmi") os.system("pkill qemu") os.environ["PATH"] = old_path
def test_full_log(self): try: run_command(ipmi_fru_list)[1] with open(log_path, "r") as fp: lines = fp.readlines() assert "Activate session" in str(lines) except Exception as e: print e import traceback print traceback.format_exc() assert False
def __get_cache(self): cache = None try: cache = apt.Cache() except SystemError: try: run_command("apt-get update -q") except Exception as e: logger.exception(e) raise e cache = apt.Cache() return cache
def test_delete_config_then_start_node(self): """ CLI test: node will start even config is deleted if there is runtime workspace """ output_delete = run_command("infrasim config delete {}".format(self.test_name)) self.assertEqual(output_delete[0], 0) output_start = run_command("infrasim node start {}".format(self.test_name)) self.assertEqual(output_start[0], 0) assert "{}'s configuration mapping removed".format(self.test_name) in output_delete[1] assert "{}-socat is running".format(self.test_name) in output_start[1] assert "{}-bmc is running".format(self.test_name) in output_start[1] assert "{}-node is running".format(self.test_name) in output_start[1]
def test_delete_config_then_start_node(self): """ CLI test: node will start even config is deleted if there is runtime workspace """ output_delete = run_command("infrasim config delete {}".format(self.test_name)) self.assertEqual(output_delete[0], 0) output_start = run_command("infrasim node start {}".format(self.test_name)) self.assertEqual(output_start[0], 0) assert "Node {}'s configuration mapping removed".format(self.test_name) in output_delete[1] assert "{}-socat is running".format(self.test_name) in output_start[1] assert "{}-bmc is running".format(self.test_name) in output_start[1] assert "{}-node is running".format(self.test_name) in output_start[1]
def setup_module(): os.environ["PATH"] = new_path # Setup bridge cmd = "brctl addbr {}".format(FAKE_BRIDGE) run_command(cmd) cmd = "ip link set dev {} up".format(FAKE_BRIDGE) run_command(cmd) cmd = "ifconfig {}".format(FAKE_BRIDGE) ret, rsp = run_command(cmd) if ret != 0 or FAKE_BRIDGE not in rsp: raise unittest.SkipTest("Fail to create fake bridge for test")
def precheck(self): # check if qemu-system-x86_64 exists # if self.__qemu_bin is an absolute path, check if it exists if not self.__qemu_bin.startswith("qemu"): try: run_command("which {}".format(self.__qemu_bin)) except CommandRunFailed: self.logger.exception("[Compute] Can not find file {}".format(self.__qemu_bin)) raise CommandNotFound(self.__qemu_bin) # check if smbios exists if not os.path.isfile(self.__smbios): raise ArgsNotCorrect("[Compute] Target SMBIOS file doesn't exist: {}". format(self.__smbios)) if self.__kernel and os.path.exists(self.__kernel) is False: raise ArgsNotCorrect("[Compute] Kernel {} does not exist.". format(self.__kernel)) if self.__initrd and os.path.exists(self.__initrd) is False: raise ArgsNotCorrect("[Compute] Kernel {} does not exist.". format(self.__initrd)) # check if VNC port is in use if helper.check_if_port_in_use("0.0.0.0", self.__display + 5900): raise ArgsNotCorrect("[Compute] VNC port {} is already in use.". format(self.__display + 5900)) # check sub-elements for element in self.__element_list: element.precheck() if 'boot' in self.__compute: if 'menu' in self.__compute['boot']: if isinstance(self.__compute['boot']['menu'], str): menu_option = str(self.__compute['boot']['menu']).strip(" ").lower() if menu_option not in ["on", "off"]: msg = "[Compute] Error: illegal config option. " \ "The 'menu' must be either 'on' or 'off'." raise ArgsNotCorrect(msg) elif not isinstance(self.__compute['boot']['menu'], bool): msg = "[Compute] Error: illegal config option. The 'menu' " \ "must be either 'on' or 'off'." raise ArgsNotCorrect(msg) # check kvm enabled is bool if self.__enable_kvm is not True and self.__enable_kvm is not False: raise ArgsNotCorrect("[Compute] KVM enabled is not a boolean: {}". format(self.__enable_kvm))
def __mark_installed_manually(self, pkg_name): cmd = "{} manual {}".format("apt-mark", pkg_name) try: _, out = run_command(cmd) except Exception as e: logger.exception(e) raise e if "Invalid operation" in out: cmd = "{} unmarkauto {}".format("apt-mark", pkg_name) try: run_command(cmd) except Exception as e: logger.exception(e) raise e
def install_bintray_packages(repo, package): # get latest version number print("downloading " + package + "...") if package is "Infrasim_Qemu": infrasim_version = "1.0" elif package is "OpenIpmi": infrasim_version = "1.4" elif package is "Seabios": infrasim_version = "1.3" else: raise Exception("No {} package in {}".format(package, BASE_URL)) download_link = BASE_URL + repo + "/" + package + "/versions/" \ + infrasim_version + "/files" print("downloading " + download_link + "...") response = requests.get(download_link) data = response.json() latest_time = data[0]["created"] path = "" file_name = "" sha256 = "" for item in data: if item["created"] >= latest_time: latest_time = item["created"] path = item["path"] file_name = item["name"] sha256 = item["sha256"] download_link = "https://dl.bintray.com/infrasim/" + repo + "/" + path print("downloading " + download_link + "...") response = requests.get(download_link) if not response: raise Exception("Failed to fetch package {} from bintray.\n" "response code is {}".format(package, response)) if not len(response.content): raise Exception("Failed to fetch package {} from bintray.\n" "Length of file is zero.".format(package)) if package is "Seabios": file_name = os.path.join("/usr/local/share/qemu/", "bios-256k.bin") else: file_name = "/tmp/" + file_name with open(file_name, "wb") as f: for chunk in response.iter_content(8192): f.write(chunk) if hashlib.sha256(open(file_name, "rb").read()).hexdigest() != sha256: raise Exception( "The file {} downloaded is not complete, please try again!") if package is not "Seabios": print("installing {} {}...".format(package, infrasim_version)) run_command("dpkg -i " + file_name)
def test_start_node_with_conflict_bmc_connection_port(self): """ Port test: after node1 start, if node2 also use the same bmc_connection_port to start, it won't start """ socat_result = run_command(PS_SOCAT, True, subprocess.PIPE, subprocess.PIPE)[1] ipmi_result = run_command(PS_IPMI, True, subprocess.PIPE, subprocess.PIPE)[1] qemu_result = run_command(PS_QEMU, True, subprocess.PIPE, subprocess.PIPE)[1] if 'dell' in self.node_info['type']: racadm_result = run_command(PS_RACADM, True, subprocess.PIPE, subprocess.PIPE)[1] # check if node test1 is running assert "test1" in socat_result assert "test1" in ipmi_result assert "test1-node" in qemu_result if 'dell' in self.node_info['type']: assert "test1" in racadm_result self.node_info_2['name'] = "test2" # modify node configuration to resolve port conflict self.node_info_2['ipmi_console_ssh'] = 9301 self.node_info_2['ipmi_console_port'] = 9001 if 'dell' in self.node_info_2['type']: self.node_info_2['racadm'] = {} self.node_info_2['racadm']['port'] = 10023 # self.node_info_2['bmc_connection_port'] = 9101 self.node_info_2['compute']['vnc_display'] = 2 self.node_info_2['compute']['monitor'] = { 'mode': 'readline', 'chardev': { 'backend': 'socket', 'host': '127.0.0.1', 'port': 2346, 'server': True, 'wait': False } } try: node2 = model.CNode(self.node_info_2) node2.init() node2.precheck() except ArgsNotCorrect, e: assert "Port 9002 is already in use." in e.value assert True
def test_controller_with_drive2(self): # Update ahci controller with two drives self.conf["compute"]["storage_backend"] = [{ "type": "ahci", "use_jbod": "true", "use_msi": "true", "max_cmds": 1024, "max_sge": 128, "max_drive_per_controller": 6, "drives": drive2 }] with open('/tmp/test.yml', 'w') as outfile: yaml.dump(self.conf, outfile, default_flow_style=False) os.system("infrasim config add test {}".format(tmp_conf_file)) node = model.CNode(self.conf) node.init() node.precheck() node.start() controller_type = run_command("infrasim node info {} | grep -c ahci". format(self.conf["name"])) self.assertEqual(int(controller_type[1]), 1) qemu_pid = get_qemu_pid(node) qemu_cmdline = open("/proc/{}/cmdline".format(qemu_pid)).read().replace("\x00", " ") assert "qemu-system-x86_64" in qemu_cmdline assert "/tmp/sda.img" in qemu_cmdline assert "/tmp/sdb.img" in qemu_cmdline assert "format=qcow2" in qemu_cmdline
def test_qemu_mac_persist_across_power_cycle(self): """ Verify all network mac address persists across power cycle by ipmi command """ # Get qemu mac addresses qemu_rsp = run_command(cmd_ps_qemu)[1] macs_former = r.findall(qemu_rsp) run_command(cmd_power_cycle) # sleep 2.5s to wait qemu up. time.sleep(2.5) # Get qemu mac addresses qemu_rsp = run_command(cmd_ps_qemu)[1] macs_latter = r.findall(qemu_rsp) # Verify mac address list remains the same assert sorted(macs_former) == sorted(macs_latter)
def test_qemu_mac_persist_across_power_reset(self): """ Verify all network mac address persists across power reset by ipmi command """ # Get qemu mac addresses qemu_rsp = run_command(cmd_ps_qemu)[1] macs_former = r.findall(qemu_rsp) run_command(cmd_power_reset) # Get qemu mac addresses qemu_rsp = run_command(cmd_ps_qemu)[1] macs_latter = r.findall(qemu_rsp) # Verify mac address list remains the same assert sorted(macs_former) == sorted(macs_latter)
def precheck(self): if self.__page_file and not os.path.exists(self.__page_file): raise ArgsNotCorrect( "[CBaseDrive] page file {0} doesnot exist".format( self.__page_file)) if self.__share_rw != "true" and self.__share_rw != "false": raise ArgsNotCorrect( "[CBaseDrive] share-rw: {} is not a valid option [true/false]". format(self.__share_rw)) if self.__sector_size not in [None, 512, 520, 4160]: raise ArgsNotCorrect( "[CBaseDrive] sector_size only support 512, 520, 4160") if self.__sector_size in [520, 4160] and self.__format != "raw": raise ArgsNotCorrect( "[CBaseDrive] sector_size {} is only supported 'raw' format". format(self.__sector_size)) if os.path.exists(self._drive_info.get( "file", "")) and self._drive_info.get("size"): cmd = "qemu-img info {} --output json".format( self._drive_info.get("file")) img_size = json.loads(run_command(cmd)[1])["virtual-size"] if img_size != math.ceil( self._drive_info.get("size") * 1024 * 1024 * 1024 / 512) * 512: print "\033[93mWarning: Existing drive image size {}GB is " \ "different from the size {}GB defined in yaml.\033[0m" \ .format((img_size >> 30), self._drive_info.get("size")) self.logger.warning( "Existing drive image size %dGB is different from the size %dGB defined in yaml.", (img_size >> 30), math.ceil(self._drive_info.get("size")))
def test_controller_with_drive6(self): # Update ahci controller with six drives self.conf["compute"]["storage_backend"] = [{ "type": "ahci", "use_msi": "true", "max_cmds": 1024, "max_sge": 128, "max_drive_per_controller": 6, "drives": drive6 }] with open('/tmp/test.yml', 'w') as outfile: yaml.dump(self.conf, outfile, default_flow_style=False) os.system("infrasim config add test {}".format(tmp_conf_file)) node = model.CNode(self.conf) node.init() node.precheck() node.start() controller_type = run_command( "infrasim node info {} | grep -c ahci".format(self.conf["name"])) self.assertEqual(int(controller_type[1]), 1) qemu_pid = get_qemu_pid(node) qemu_cmdline = open( "/proc/{}/cmdline".format(qemu_pid)).read().replace("\x00", " ") assert "qemu-system-x86_64" in qemu_cmdline assert "/tmp/sda.img" in qemu_cmdline assert "/tmp/sdb.img" in qemu_cmdline assert "/tmp/sdc.img" in qemu_cmdline assert "/tmp/sdd.img" in qemu_cmdline assert "/tmp/sde.img" in qemu_cmdline assert "/tmp/sdf.img" in qemu_cmdline assert "format=qcow2" in qemu_cmdline
def create_network_config_file(networkconfig): """ sample network config: version: 1 config: - type: physical name: enp0s3 mac_address: 00:60:16:93:b9:2a subnets: - type: dhcp - type: physical name: eth0 mac_address: 00:60:16:93:b9:1d subnets: - type: static address: 192.168.188.12 netmask: 255.255.255.0 routes: - network: 0.0.0.0 netmask: 0.0.0.0 gateway: 192.168.188.1 - type: nameserver address: [192.168.188.1, 8.8.8.8, 8.8.4.4] search: [example.com, foo.biz, bar.info] """ script_name = str(os.getcwd()) + "/cloudimgs/network-config" script_content = generate_script_content(networkconfig) status, output = run_command("echo \"{}\" > {}".format( script_content, script_name)) return script_name
def handle_parms(self): if self.__network_mode == "bridge": if self.__bridge_name is None: self.__bridge_name = "br0" qemu_sys_prefix = os.path.dirname( run_command("which qemu-system-x86_64")[1]).replace("bin", "") bridge_helper = os.path.join(qemu_sys_prefix, "libexec", "qemu-bridge-helper") netdev_option = ",".join([ 'bridge', 'id=netdev{}'.format(self.__index), 'br={}'.format(self.__bridge_name), 'helper={}'.format(bridge_helper) ]) elif self.__network_mode == "nat": netdev_option = ",".join( ["user", "id=netdev{}".format(self.__index)]) else: raise ArgsNotCorrect( "[CNetwork] ERROR: Network mode '{}'' is not supported now.". format(self.__network_mode)) nic_option = ",".join([ "{}".format(self.__nic_name), "netdev=netdev{}".format(self.__index), "mac={}".format(self.__mac_address) ]) network_option = " ".join([ "-netdev {}".format(netdev_option), "-device {}".format(nic_option) ]) self.add_option(network_option)
def create_meta_data(instance_id): script_name = str(os.getcwd()) + "/cloudimgs/meta-data" script_content = '''instance-id: {} local-hostname: cloud '''.format(instance_id) status, output = run_command("echo \"{}\" > {}".format(script_content, script_name)) return script_name
def create_network_config_file(networkconfig): """ sample network config: version: 1 config: - type: physical name: enp0s3 mac_address: 00:60:16:93:b9:2a subnets: - type: dhcp - type: physical name: eth0 mac_address: 00:60:16:93:b9:1d subnets: - type: static address: 192.168.188.12 netmask: 255.255.255.0 routes: - network: 0.0.0.0 netmask: 0.0.0.0 gateway: 192.168.188.1 - type: nameserver address: [192.168.188.1, 8.8.8.8, 8.8.4.4] search: [example.com, foo.biz, bar.info] """ script_name = str(os.getcwd()) + "/cloudimgs/network-config" script_content = generate_script_content(networkconfig) status, output = run_command("echo \"{}\" > {}".format(script_content, script_name)) return script_name
def test_ip4addr_list(self): try: test_ip_list = helper.ip4_addresses() assert test_ip_list str_result = run_command( 'hostname -I', True, subprocess.PIPE, subprocess.PIPE)[1] host_ip = str_result.split() # remove IPV6 addresses from host ip list host_ip = [ip for ip in host_ip if ":" not in ip] # Verify IP address, except 127.0.0.1, both lists share # same set of ip address hit = True for ip in host_ip: hit = False for test_ip in test_ip_list: if ip == test_ip: hit = True break assert hit except Exception as e: print e assert False
def test_controller_with_drive6(self): # Update megasas controller with six drives self.conf["compute"]["storage_backend"] = [{ "type": "megasas", "use_msi": "true", "max_cmds": 1024, "max_sge": 128, "max_drive_per_controller": 6, "drives": drive6 }] self.conf['compute']['storage_backend'].insert( 0, { "type": "ahci", "max_drive_per_controller": 6, "drives": [{ 'file': fixtures.image, 'bootindex': 1, 'use_msi': 'true', 'size': 8 }] }) with open('/tmp/test.yml', 'w') as outfile: yaml.dump(self.conf, outfile, default_flow_style=False) os.system("infrasim config add test {}".format(tmp_conf_file)) node = model.CNode(self.conf) node.init() node.precheck() node.start() helper.port_forward(node) controller_type = run_command( "infrasim node info {} | grep -c megasas".format( self.conf["name"])) self.assertEqual(int(controller_type[1]), 1) qemu_pid = get_qemu_pid(node) qemu_cmdline = open( "/proc/{}/cmdline".format(qemu_pid)).read().replace("\x00", " ") assert "qemu-system-x86_64" in qemu_cmdline assert "/tmp/sda.img" in qemu_cmdline assert "/tmp/sdb.img" in qemu_cmdline assert "/tmp/sdc.img" in qemu_cmdline assert "/tmp/sdd.img" in qemu_cmdline assert "/tmp/sde.img" in qemu_cmdline assert "/tmp/sdf.img" in qemu_cmdline assert "format=qcow2" in qemu_cmdline storage_list = get_storage_list() megasas_info = None for c_map in storage_list: if c_map.get('name') == 'megaraid_sas': megasas_info = c_map break assert megasas_info assert len(megasas_info.get('disks')) == 6
def test_ip4addr_list(self): try: test_ip_list = helper.ip4_addresses() assert test_ip_list str_result = run_command('hostname -I', True, subprocess.PIPE, subprocess.PIPE)[1] host_ip = str_result.split() # remove IPV6 addresses from host ip list host_ip = [ip for ip in host_ip if ":" not in ip] # Verify IP address, except 127.0.0.1, both lists share # same set of ip address hit = True for ip in host_ip: hit = False for test_ip in test_ip_list: if ip == test_ip: hit = True break assert hit except Exception as e: print e assert False
def geniso(myseed_name, instance_id, networkconfig): create_meta_data(instance_id) create_network_config_file(networkconfig) create_user_data() status, output = run_command("genisoimage -output cloudimgs/{} -volid cidata -joliet -rock " "cloudimgs/user-data cloudimgs/meta-data cloudimgs/network-config".format(myseed_name)) return str(os.getcwd() + "/cloudimgs/") + myseed_name
def handle_parms(self): if self.__network_mode == "bridge": if self.__bridge_name is None: self.__bridge_name = "br0" qemu_sys_prefix = os.path.dirname( run_command("which qemu-system-x86_64")[1] ).replace("bin", "") bridge_helper = os.path.join(qemu_sys_prefix, "libexec", "qemu-bridge-helper") netdev_option = ",".join(['bridge', 'id=netdev{}'.format(self.__index), 'br={}'.format(self.__bridge_name), 'helper={}'.format(bridge_helper)]) elif self.__network_mode == "nat": netdev_option = ",".join(["user", "id=netdev{}".format(self.__index)]) else: raise ArgsNotCorrect("[CNetwork] ERROR: Network mode '{}'' is not supported now.". format(self.__network_mode)) nic_option = ",".join(["{}".format(self.__nic_name), "netdev=netdev{}".format(self.__index), "mac={}".format(self.__mac_address)]) network_option = " ".join(["-netdev {}".format(netdev_option), "-device {}".format(nic_option)]) self.add_option(network_option)
def handle_parms(self): if self.__network_mode == "bridge": if self.__bridge_name is None: self.__bridge_name = "br0" qemu_sys_prefix = os.path.dirname( run_command("which qemu-system-x86_64")[1]).replace("bin", "") bridge_helper = os.path.join(qemu_sys_prefix, "libexec", "qemu-bridge-helper") netdev_option = ",".join([ 'bridge', 'id=netdev{}'.format(self.__index), 'br={}'.format(self.__bridge_name), 'helper={}'.format(bridge_helper) ]) elif self.__network_mode == "nat": netdev_option = ",".join( ["user", "id=netdev{}".format(self.__index)]) else: raise ArgsNotCorrect( "[CNetwork] ERROR: Network mode '{}'' is not supported now.". format(self.__network_mode)) for item in self.__port_forwards: netdev_option = ",".join([ "{}".format(netdev_option), "hostfwd={}::{}-:{}".format(item["protocal"], item["outside"], item["inside"]) ]) nic_option = ",".join([ "{}".format(self.__nic_name), "netdev=netdev{}".format(self.__index), "mac={}".format(self.__mac_address) ]) if self.__bus: nic_option = ",".join( ["{}".format(nic_option), "bus={}".format(self.__bus)]) if self.__addr: nic_option = ",".join( ["{}".format(nic_option), "addr={}".format(self.__addr)]) if self.__multifunction: nic_option = ",".join([ "{}".format(nic_option), "multifunction={}".format(self.__multifunction) ]) if self.__model: nic_option = ",".join( ["{}".format(nic_option), "model={}".format(self.__model)]) if self.__id: nic_option = ",".join( ["{}".format(nic_option), "id={}".format(self.__id)]) if self.__extra_options: nic_option = ",".join( ["{}".format(nic_option), self.__extra_options]) network_option = " ".join([ "-netdev {}".format(netdev_option), "-device {}".format(nic_option) ]) self.add_option(network_option)
def tearDownClass(cls): # Trying to clean up the environment try: _, output = run_command("ip netns") netns_list = output.strip().split(os.linesep) for netns in netns_list: run_command("ip netns delete {}".format(netns)) run_command("pkill ipmi_sim") run_command("pkill socat") run_command("pkill qemu-system-x86_64") except CommandRunFailed: pass
def test_controller_with_drive6(self): # Update lsi controller with six drives self.conf["compute"]["storage_backend"] = [{ "type": "lsi", "use_msi": "true", "max_cmds": 1024, "max_sge": 128, "max_drive_per_controller": 6, "drives": drive6 }] self.conf['compute']['storage_backend'].insert(0, { "type": "ahci", "max_drive_per_controller": 6, "drives": [ { 'file': fixtures.image, 'bootindex': 1, 'use_msi': 'true', 'size': 8 } ] }) with open('/tmp/test.yml', 'w') as outfile: yaml.dump(self.conf, outfile, default_flow_style=False) os.system("infrasim config add test {}".format(tmp_conf_file)) node = model.CNode(self.conf) node.init() node.precheck() node.start() helper.port_forward(node) controller_type = run_command("infrasim node info {} | grep -c lsi". format(self.conf["name"])) self.assertEqual(int(controller_type[1]), 1) qemu_pid = get_qemu_pid(node) qemu_cmdline = open("/proc/{}/cmdline".format(qemu_pid)).read().replace("\x00", " ") qemu_cmdline = helper.get_full_qemu_cmd(qemu_cmdline) assert "qemu-system-x86_64" in qemu_cmdline assert "/tmp/sda.img" in qemu_cmdline assert "/tmp/sdb.img" in qemu_cmdline assert "/tmp/sdc.img" in qemu_cmdline assert "/tmp/sdd.img" in qemu_cmdline assert "/tmp/sde.img" in qemu_cmdline assert "/tmp/sdf.img" in qemu_cmdline assert "format=qcow2" in qemu_cmdline storage_list = get_storage_list() lsi_info = None for c_map in storage_list: if c_map.get('name') == 'sym53c8xx': lsi_info = c_map break assert lsi_info assert len(lsi_info.get('disks')) == 6
def create_meta_data(instance_id): script_name = str(os.getcwd()) + "/cloudimgs/meta-data" script_content = '''instance-id: {} local-hostname: cloud '''.format(instance_id) status, output = run_command("echo \"{}\" > {}".format( script_content, script_name)) return script_name
def _setup_netns(self, netns): # Create net namespace "test" run_command("ip netns add {}".format(netns)) command_prefix = "ip netns exec {} ".format(netns) # Create a virtual interface in namespace run_command("{} ip link add name {} type dummy".format(command_prefix, self.dummy_intf_name)) # Link up lo device run_command("{} ip link set dev lo up".format(command_prefix)) # Link up dummy interface run_command("{} ip link set dev {} up".format(command_prefix, self.dummy_intf_name)) # Add IP address for this dummy interface run_command("{} ip address add 10.0.2.10/24 dev {}".format(command_prefix, self.dummy_intf_name))
def install_bintray_packages(repo, package): # get latest version number print("downloading " + package + "...") if package is "Infrasim_Qemu": infrasim_version = "1.0" elif package is "OpenIpmi": infrasim_version = "1.3" elif package is "Seabios": infrasim_version = "1.3" else: raise Exception("No {} package in {}".format(package, BASE_URL)) download_link = BASE_URL + repo + "/" + package + "/versions/" \ + infrasim_version + "/files" response = requests.get(download_link) data = response.json() latest_time = data[0]["created"] path = "" file_name = "" sha256 = "" for item in data: if item["created"] >= latest_time: latest_time = item["created"] path = item["path"] file_name = item["name"] sha256 = item["sha256"] response = requests.get( "https://dl.bintray.com/infrasim/" + repo + "/" + path) if not response: raise Exception("Failed to fetch package {} from bintray.\n" "response code is {}".format(package, response)) if not len(response.content): raise Exception("Failed to fetch package {} from bintray.\n" "Length of file is zero.".format(package)) if package is "Seabios": file_name = os.path.join("/usr/local/share/qemu/", "bios-256k.bin") else: file_name = "/tmp/" + file_name with open(file_name, "wb") as f: for chunk in response.iter_content(8192): f.write(chunk) if hashlib.sha256(open(file_name, "rb").read()).hexdigest() != sha256: raise Exception( "The file {} downloaded is not complete, please try again!") if package is not "Seabios": print("installing {} {}...".format(package, infrasim_version)) run_command("dpkg -i " + file_name)
def test_start_destroy(self): """ CLI test: start, then destroy the node directly """ output_start = run_command("infrasim node start") self.assertEqual(output_start[0], 0) self.assertTrue(Workspace.check_workspace_exists(self.node_name)) output_destroy = run_command("infrasim node destroy") self.assertEqual(output_destroy[0], 0) self.assertFalse(Workspace.check_workspace_exists(self.node_name)) assert "{}-socat is running".format(self.node_name) in output_start[1] assert "{}-bmc is running".format(self.node_name) in output_start[1] assert "{}-node is running".format(self.node_name) in output_start[1] assert "Node {} runtime workspace is destroyed".format(self.node_name) in output_destroy[1]