def test_set_interface(self): # Find two valid interface with IP in to a list, e.g: # [{"interface":"ens160","ip":"192.168.190.9"}, {}] # If the list has no less than 2 interface, do this test valid_nic = [] for interface in helper.get_all_interfaces(): ip = helper.get_interface_ip(interface) if ip and (interface.startswith("en") or interface.startswith("eth") or interface.startswith("br")): valid_nic.append({"interface": interface, "ip": ip}) if len(valid_nic) < 2: raise self.skipTest("No enough nic for test") print valid_nic # Set BMC to listen on first valid nic # Try to access via first one, it works # Try to access via second one, it fails self.conf["monitor"] = { "enable": True, "interface": valid_nic[0]["interface"] } print self.conf["monitor"] node = model.CNode(self.conf) node.init() node.precheck() node.start() # Check if port is opened start = time.time() while helper.check_if_port_in_use(valid_nic[0]["ip"], 9005) is False: time.sleep(1) if time.time() - start > 10: break assert helper.check_if_port_in_use(valid_nic[0]["ip"], 9005) is True # Connect to wrong interface try: rsp = requests.get("http://{}:9005/admin".format( valid_nic[1]["ip"])) except requests.exceptions.ConnectionError: pass else: assert False # Connect to correct interface rsp = requests.get("http://{}:9005/admin".format(valid_nic[0]["ip"])) assert rsp.status_code == 200
def test_set_interface(self): # Find two valid interface with IP in to a list, e.g: # [{"interface":"ens160","ip":"192.168.190.9"}, {}] # If the list has no less than 2 interface, do this test valid_nic = [] for interface in helper.get_all_interfaces(): ip = helper.get_interface_ip(interface) if ip and (interface.startswith("en") or interface.startswith("eth") or interface.startswith("br")): valid_nic.append({"interface": interface, "ip": ip}) if len(valid_nic) < 2: raise self.skipTest("No enough nic for test") print valid_nic # Set BMC to listen on first valid nic # Try to access via first one, it works # Try to access via second one, it fails self.conf["monitor"] = { "enable": True, "interface": valid_nic[0]["interface"] } print self.conf["monitor"] node = model.CNode(self.conf) node.init() node.precheck() node.start() # Check if port is opened start = time.time() while helper.check_if_port_in_use(valid_nic[0]["ip"], 9005) is False: time.sleep(1) if time.time() - start > 10: break assert helper.check_if_port_in_use(valid_nic[0]["ip"], 9005) is True # Connect to wrong interface try: rsp = requests.get("http://{}:9005/admin".format(valid_nic[1]["ip"])) except requests.exceptions.ConnectionError: pass else: assert False # Connect to correct interface rsp = requests.get("http://{}:9005/admin".format(valid_nic[0]["ip"])) assert rsp.status_code == 200
def precheck(self): if not self.__backend_type: raise ArgsNotCorrect("[Chardev] Backend of chardev should be set.") if self.__host and not helper.is_valid_ip(self.__host): raise ArgsNotCorrect("[CharDev] Invalid chardev host: {}".format( self.__host)) if self.__port: try: int(self.__port) except ValueError: raise ArgsNotCorrect( "[Chardev] Port is not a valid integer: {}".format( self.__port)) if helper.check_if_port_in_use("0.0.0.0", self.__port): raise ArgsNotCorrect( "[Chardev] Port {} is already in use".format(self.__port)) if self.__path: dir_path = os.path.dirname(self.__path) if not os.path.isdir(dir_path): raise ArgsNotCorrect( "[Chardev] Path folder doesn't exist: {}".format(dir_path))
def precheck(self): if not self.__ip: raise ArgsNotCorrect("[Racadm] Specified racadm interface {} doesn\'t exist". format(self.__interface)) if helper.check_if_port_in_use(self.__ip, self.__port_idrac): raise ArgsNotCorrect("[Racadm] Racadm port {}:{} is already in use.". format(self.__ip, self.__port_idrac))
def precheck(self): if not self.__ip: raise ArgsNotCorrect( "[Racadm] Specified racadm interface {} doesn\'t exist".format( self.__interface)) if helper.check_if_port_in_use(self.__ip, self.__port_idrac): raise ArgsNotCorrect( "[Racadm] Racadm port {}:{} is already in use.".format( self.__ip, self.__port_idrac))
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 wait_port_up(self, addr, port, timeout=10): start = time.time() while True: if helper.check_if_port_in_use(addr, port): return True if time.time() - start > timeout: break time.sleep(0.1) return False
def precheck(self): if not helper.is_valid_ip(self.__ip): self.logger.exception("[Monitor] Invalid IP: {} of interface: {}". format(self.__ip, self.__interface)) raise ArgsNotCorrect("Invalid IP: {} of interface: {}".format( self.__ip, self.__interface)) if helper.check_if_port_in_use(self.__ip, self.__port): self.logger.exception("[Monitor] Monitor port {}:{} is already in use.". format(self.__ip, self.__port)) raise ArgsNotCorrect("Monitor port {}:{} is already in use.". format(self.__ip, self.__port))
def test_conflict_port(self): if not helper.check_if_port_in_use("0.0.0.0", 22): self.skipTest("Port 22 is not in use, skip port conflict test") racadm_info = {"port": 22} racadm_obj = model.CRacadm(racadm_info) racadm_obj.init() try: racadm_obj.precheck() except ArgsNotCorrect, e: assert ":22 is already in use" in str(e)
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 precheck(self): if not self.__backend_type: raise ArgsNotCorrect("[Chardev] Backend of chardev should be set.") if self.__host and not helper.is_valid_ip(self.__host): raise ArgsNotCorrect("[CharDev] Invalid chardev host: {}".format(self.__host)) if self.__port: try: int(self.__port) except ValueError, e: raise ArgsNotCorrect("[Chardev] Port is not a valid integer: {}".format(self.__port)) if helper.check_if_port_in_use("0.0.0.0", self.__port): raise ArgsNotCorrect("[Chardev] Port {} is already in use".format(self.__port))
def precheck(self): if not self.__backend_type: raise ArgsNotCorrect("[Chardev] Backend of chardev should be set.") if self.__host and not helper.is_valid_ip(self.__host): raise ArgsNotCorrect("[CharDev] Invalid chardev host: {}".format( self.__host)) if self.__port: try: int(self.__port) except ValueError, e: raise ArgsNotCorrect( "[Chardev] Port is not a valid integer: {}".format( self.__port)) if helper.check_if_port_in_use("0.0.0.0", self.__port): raise ArgsNotCorrect( "[Chardev] Port {} is already in use".format(self.__port))
def precheck(self): if not self.__backend_type: raise ArgsNotCorrect("[Chardev] Backend of chardev should be set.") if self.__host and not helper.is_valid_ip(self.__host): raise ArgsNotCorrect("[CharDev] Invalid chardev host: {}".format(self.__host)) if self.__port: try: int(self.__port) except ValueError: raise ArgsNotCorrect("[Chardev] Port is not a valid integer: {}".format(self.__port)) if helper.check_if_port_in_use("0.0.0.0", self.__port): raise ArgsNotCorrect("[Chardev] Port {} is already in use".format(self.__port)) if self.__path: dir_path = os.path.dirname(self.__path) if not os.path.isdir(dir_path): raise ArgsNotCorrect("[Chardev] Path folder doesn't exist: {}".format(dir_path))
def init_env(instance): """ This is to sync ipmi-console with runtime vBMC configuration. Initial version capture infrasim instance name by infrasim-main status, while we have a plan to give instance name to ipmi-console so that it can be attached to target vBMC instance. """ # Threading quit flag initilization env.local_env.quit_flag = False # Verify node instance runtime cur_path = os.environ["PATH"] os.environ["PATH"] = "{}/bin:{}".format(os.environ.get("PYTHONPATH"), cur_path) if not Workspace.check_workspace_exists(instance): logger.error("Warning: there is no node {} workspace. " "Please start node {} first.".format(instance, instance)) raise IpmiError( "Warning: there is no node {} workspace. " "Please start node {} first.".format(instance, instance)) try: with open("{}/{}/.{}-bmc.pid".format( config.infrasim_home, instance, instance), "r") as f: pid = f.readline().strip() if not os.path.exists("/proc/{}".format(pid)): raise Exception except Exception: logger.error("Warning: node {} has not started BMC. " "Please start node {} first.".format(instance, instance)) raise IpmiError( "Warning: node {} has not started BMC. " "Please start node {} first.".format(instance, instance)) logger.info("Init ipmi-console environment for infrasim instance: {}". format(instance)) # Get runtime vbmc.conf vbmc_conf_path = os.path.join(config.infrasim_home, instance, "etc", "vbmc.conf") if not os.path.exists(vbmc_conf_path): msg = "{} vBMC configuration is not defined at {}".format(instance, vbmc_conf_path) logger.error(msg) raise Exception(msg) else: msg = "Target vbmc to attach is: {}".format(vbmc_conf_path) logger.info(msg) # Get runtime infrasim.yml infrasim_yml_path = os.path.join(config.infrasim_home, instance, "etc", "infrasim.yml") if not os.path.exists(infrasim_yml_path): msg = "{} infrasim instance is not defined at {}".format(instance, infrasim_yml_path) logger.error(msg) raise Exception(msg) else: msg = "Target infrasim instance to attach is: {}".format(infrasim_yml_path) logger.info(msg) # Get variable and set to ipmi-console env # - PORT_TELNET_TO_VBMC # - VBMC_IP # - VBMC_PORT with open(vbmc_conf_path, 'r') as fp: conf = fp.read() p_telnet = re.compile(r"^\s*console\s*[\d:\.]+\s+(?P<port_telnet_to_vbmc>\d+)", re.MULTILINE) s_telnet = p_telnet.search(conf) if s_telnet: env.PORT_TELNET_TO_VBMC = int(s_telnet.group("port_telnet_to_vbmc")) logger.info("PORT_TELNET_TO_VBMC: {}".format(env.PORT_TELNET_TO_VBMC)) else: logger.error("PORT_TELNET_TO_VBMC is not found") raise Exception("PORT_TELNET_TO_VBMC is not found") p_vbmc = re.compile(r"^\s*addr\s*(?P<vbmc_ip>[\d:\.]+)\s*(?P<vbmc_port>\d+)", re.MULTILINE) s_vbmc = p_vbmc.search(conf) if s_vbmc: ip = s_vbmc.group("vbmc_ip") if ip == "::" or ip == "0.0.0.0": env.VBMC_IP = "localhost" else: env.VBMC_IP = ip logger.info("VBMC_IP: {}".format(env.VBMC_IP)) env.VBMC_PORT = int(s_vbmc.group("vbmc_port")) logger.info("VBMC_PORT: {}".format(env.VBMC_PORT)) else: logger.error("VBMC_IP and VBMC_PORT is not found") raise Exception("VBMC_IP and VBMC_PORT is not found") # Get variable and set to ipmi-console env # - PORT_SSH_FOR_CLIENT with open(infrasim_yml_path, 'r') as fp: conf = fp.read() p_port = re.compile(r"^\s*ipmi_console_ssh:\s*(?P<port_ssh_for_client>\d+)", re.MULTILINE) s_port = p_port.search(conf) if s_port: env.PORT_SSH_FOR_CLIENT = int(s_port.group("port_ssh_for_client")) else: env.PORT_SSH_FOR_CLIENT = 9300 logger.info("PORT_SSH_FOR_CLIENT: {}".format(env.PORT_SSH_FOR_CLIENT)) # check if ipmi_console_ssh port is in use if helper.check_if_port_in_use("0.0.0.0", env.PORT_SSH_FOR_CLIENT): logger.error("ssh port {} is already in use.".format(env.PORT_SSH_FOR_CLIENT)) raise IpmiError("ssh port {} is already in use.".format(env.PORT_SSH_FOR_CLIENT))
def precheck(self): # check if ipmi_sim exists try: run_command("which {}".format(self.__bin)) except CommandRunFailed: self.logger.exception("[BMC] Cannot find {}".format(self.__bin)) raise CommandNotFound(self.__bin) # check configuration file exists if not os.path.isfile(self.__config_file): raise ArgsNotCorrect("[BMC] Target config file doesn't exist: {}". format(self.__config_file)) if not os.path.isfile(self.__emu_file): raise ArgsNotCorrect("[BMC] Target emulation file doesn't exist: {}". format(self.__emu_file)) # check script exists if not os.path.exists(self.__lancontrol_script): raise ArgsNotCorrect("[BMC] Lan control script {} doesn\'t exist". format(self.__lancontrol_script)) if not os.path.exists(self.__chassiscontrol_script): raise ArgsNotCorrect("[BMC] Chassis control script {} doesn\'t exist". format(self.__chassiscontrol_script)) if not os.path.exists(self.__startcmd_script): raise ArgsNotCorrect("[BMC] startcmd script {} doesn\'t exist". format(self.__startcmd_script)) if not os.path.exists(self.__workspace): raise ArgsNotCorrect("[BMC] workspace {} doesn\'t exist". format(self.__workspace)) # check if main channel is included in all lan channels if self.__main_channel not in self.__channels: raise ArgsNotCorrect("[BMC] main channel {} should be included in channels {}". format(self.__main_channel, self.__channels)) # check if self.__port_qemu_ipmi in use if helper.check_if_port_in_use("0.0.0.0", self.__port_qemu_ipmi): raise ArgsNotCorrect("[BMC] Port {} is already in use.". format(self.__port_qemu_ipmi)) if helper.check_if_port_in_use("0.0.0.0", self.__port_ipmi_console): raise ArgsNotCorrect("[BMC] Port {} is already in use.". format(self.__port_ipmi_console)) # check lan interface exists if self.__lan_interface not in helper.get_all_interfaces(): self.logger.warning("[BMC] Specified BMC interface {} doesn\'t exist.". format(self.__lan_interface)) # check if lan interface has IP address elif not self.__ipmi_listen_range: self.logger.warning("[BMC] No IP is found on BMC interface {}.". format(self.__lan_interface)) # check attribute if self.__poweroff_wait < 0: raise ArgsNotCorrect("[BMC] poweroff_wait is expected to be >= 0, " "it's set to {} now". format(self.__poweroff_wait)) if not isinstance(self.__poweroff_wait, int): raise ArgsNotCorrect("[BMC] poweroff_wait is expected to be integer, " "it's set to {} now". format(self.__poweroff_wait)) if self.__kill_wait < 0: raise ArgsNotCorrect("[BMC] kill_wait is expected to be >= 0, " "it's set to {} now". format(self.__kill_wait)) if not isinstance(self.__kill_wait, int): raise ArgsNotCorrect("[BMC] kill_wait is expected to be integer, " "it's set to {} now". format(self.__kill_wait)) if self.__port_iol < 0: raise ArgsNotCorrect("[BMC] Port for IOL(IPMI over LAN) is expected " "to be >= 0, it's set to {} now". format(self.__port_iol)) if not isinstance(self.__port_iol, int): raise ArgsNotCorrect("[BMC] Port for IOL(IPMI over LAN) is expected " "to be integer, it's set to {} now". format(self.__port_iol)) if self.__historyfru < 0: raise ArgsNotCorrect("[BMC] History FRU is expected to be >= 0, " "it's set to {} now". format(self.__historyfru)) if not isinstance(self.__historyfru, int): raise ArgsNotCorrect("[BMC] History FRU is expected to be integer, " "it's set to {} now". format(self.__historyfru)) self.__check_peer_bmc_info()
def precheck(self): # check if ipmi_sim exists try: run_command("which {}".format(self.__bin)) except CommandRunFailed: self.logger.exception("[BMC] Cannot find {}".format(self.__bin)) raise CommandNotFound(self.__bin) # check configuration file exists if not os.path.isfile(self.__config_file): raise ArgsNotCorrect( "[BMC] Target config file doesn't exist: {}".format( self.__config_file)) if not os.path.isfile(self.__emu_file): raise ArgsNotCorrect( "[BMC] Target emulation file doesn't exist: {}".format( self.__emu_file)) # check script exists if not os.path.exists(self.__lancontrol_script): raise ArgsNotCorrect( "[BMC] Lan control script {} doesn\'t exist".format( self.__lancontrol_script)) if not os.path.exists(self.__chassiscontrol_script): raise ArgsNotCorrect( "[BMC] Chassis control script {} doesn\'t exist".format( self.__chassiscontrol_script)) if not os.path.exists(self.__startcmd_script): raise ArgsNotCorrect( "[BMC] startcmd script {} doesn\'t exist".format( self.__startcmd_script)) if not os.path.exists(self.__workspace): raise ArgsNotCorrect("[BMC] workspace {} doesn\'t exist".format( self.__workspace)) # check if main channel is included in all lan channels if self.__main_channel not in self.__channels: raise ArgsNotCorrect( "[BMC] main channel {} should be included in channels {}". format(self.__main_channel, self.__channels)) # check if self.__port_qemu_ipmi in use if helper.check_if_port_in_use("0.0.0.0", self.__port_qemu_ipmi): raise ArgsNotCorrect("[BMC] Port {} is already in use.".format( self.__port_qemu_ipmi)) if helper.check_if_port_in_use("0.0.0.0", self.__port_ipmi_console): raise ArgsNotCorrect("[BMC] Port {} is already in use.".format( self.__port_ipmi_console)) # check lan interface exists if self.__lan_interface not in helper.get_all_interfaces(): self.logger.warning( "[BMC] Specified BMC interface {} doesn\'t exist.".format( self.__lan_interface)) # check if lan interface has IP address elif not self.__ipmi_listen_range: self.logger.warning( "[BMC] No IP is found on BMC interface {}.".format( self.__lan_interface)) # check attribute if self.__poweroff_wait < 0: raise ArgsNotCorrect("[BMC] poweroff_wait is expected to be >= 0, " "it's set to {} now".format( self.__poweroff_wait)) if not isinstance(self.__poweroff_wait, int): raise ArgsNotCorrect( "[BMC] poweroff_wait is expected to be integer, " "it's set to {} now".format(self.__poweroff_wait)) if self.__kill_wait < 0: raise ArgsNotCorrect("[BMC] kill_wait is expected to be >= 0, " "it's set to {} now".format(self.__kill_wait)) if not isinstance(self.__kill_wait, int): raise ArgsNotCorrect("[BMC] kill_wait is expected to be integer, " "it's set to {} now".format(self.__kill_wait)) if self.__port_iol < 0: raise ArgsNotCorrect( "[BMC] Port for IOL(IPMI over LAN) is expected " "to be >= 0, it's set to {} now".format(self.__port_iol)) if not isinstance(self.__port_iol, int): raise ArgsNotCorrect( "[BMC] Port for IOL(IPMI over LAN) is expected " "to be integer, it's set to {} now".format(self.__port_iol)) if self.__historyfru < 0: raise ArgsNotCorrect("[BMC] History FRU is expected to be >= 0, " "it's set to {} now".format( self.__historyfru)) if not isinstance(self.__historyfru, int): raise ArgsNotCorrect( "[BMC] History FRU is expected to be integer, " "it's set to {} now".format(self.__historyfru)) self.__check_peer_bmc_info()