def __init__(self, file): file = fs_utils.parse_ls_output(fs_utils.ls_item(file.full_path))[0] self.full_path = file.full_path self.parent_dir = self.get_parent_dir(self.full_path) self.name = self.get_name(self.full_path) self.modification_time = file.modification_time self.owner = file.owner self.group = file.group self.permissions = file.permissions self.size = file.size
def compare_md5sums(md5_sums_source, files_to_check_path): md5_sums_elements = len(md5_sums_source) for i in range(md5_sums_elements): file_to_check_path = f"{files_to_check_path}/file{i}" file_to_check = fs_utils.parse_ls_output(fs_utils.ls_item(file_to_check_path))[0] if md5_sums_source[i] != file_to_check.md5sum(): TestRun.fail(f"Source and target files {file_to_check_path} checksums are different.") TestRun.LOGGER.info(f"Successful verification, md5sums match.")
def copy(self, destination, force: bool = False, recursive: bool = False, dereference: bool = False): fs_utils.copy(str(self), destination, force, recursive, dereference) if fs_utils.check_if_directory_exists(destination): path = f"{destination}{'/' if destination[-1] != '/' else ''}{self.name}" else: path = destination output = fs_utils.ls_item(f"{path}") return fs_utils.parse_ls_output(output)[0]
def refresh_item(self): updated_file = fs_utils.parse_ls_output( fs_utils.ls_item(self.full_path))[0] # keep order the same as in __init__() self.parent_dir = updated_file.parent_dir self.name = updated_file.name self.modification_time = updated_file.modification_time self.owner = updated_file.owner self.group = updated_file.group self.permissions = updated_file.permissions self.size = updated_file.size return self
def check_if_is_present_in_os(self, should_be_visible=True): device_in_system_message = "CAS device exists in OS." device_not_in_system_message = "CAS device does not exist in OS." item = fs_utils.ls_item(f"{self.system_path}") if item is not None: if should_be_visible: TestRun.LOGGER.info(device_in_system_message) else: TestRun.fail(device_in_system_message) else: if should_be_visible: TestRun.fail(device_not_in_system_message) else: TestRun.LOGGER.info(device_not_in_system_message)
def is_detected(self): if self.serial_number: serial_numbers = disk_finder.get_all_serial_numbers() if self.serial_number not in serial_numbers: return False else: self.path = serial_numbers[self.serial_number] for part in self.partitions: part.path = disk_utils.get_partition_path( part.parent_device.path, part.number) return True elif self.path: output = fs_utils.ls_item(f"{self.path}") return fs_utils.parse_ls_output(output)[0] is not None raise Exception("Couldn't check if device is detected by the system")
def get_sysfs_properties(self, device_name): ls_command = f"$(find -H /sys/devices/ -name {device_name} -type d)" output = fs_utils.ls_item(f"{ls_command}") sysfs_addr = fs_utils.parse_ls_output(output)[0] if not sysfs_addr: raise Exception( f"Failed to find sysfs address: ls -l {ls_command}") dirs = sysfs_addr.full_path.split('/') scsi_address = dirs[-3] matches = re.search( r"^(?P<controller>\d+)[-:](?P<port>\d+)[-:](?P<target>\d+)[-:](?P<lun>\d+)$", scsi_address) controller_id = matches["controller"] port_id = matches["port"] target_id = matches["target"] lun = matches["lun"] host_path = "/".join( itertools.takewhile(lambda x: not x.startswith("host"), dirs)) self.plug_command = f"echo '{port_id} {target_id} {lun}' > " \ f"{host_path}/host{controller_id}/scsi_host/host{controller_id}/scan" return sysfs_addr
def test_user_service(): """ title: Test that OpenCAS does not allow to change service status by non-root user. description: | Verify that changing OpenCAS service status by non-root user is forbidden by OpenCAS. pass_criteria: - Non-root user cannot change OpenCAS service state. - Non-root sudoer user can change OpenCAS service state. """ with TestRun.step("Prepare cache and core devices."): cache_dev = TestRun.disks['cache'] cache_dev.create_partitions([Size(1, Unit.GibiByte)]) cache_dev = cache_dev.partitions[0] core_dev = TestRun.disks['core'] core_dev.create_partitions([Size(2, Unit.GibiByte)]) core_dev = core_dev.partitions[0] with TestRun.step("Start cache."): cache = casadm.start_cache(cache_dev, force=True) with TestRun.step("Add core to cache and mount it."): core_dev.create_filesystem(Filesystem.ext3) core = cache.add_core(core_dev) core.mount(mount_point) with TestRun.step("Create 'opencas.conf' from running configuration."): InitConfig.create_init_config_from_running_configuration() with TestRun.step(f"Copy casadm bin from {system_casadm_bin_path} " f"to {user_casadm_bin_dest_path}."): casadm_bin = fs_utils.parse_ls_output(fs_utils.ls_item(f"{system_casadm_bin_path}"))[0] casadm_bin_copy = casadm_bin.copy(user_casadm_bin_dest_path, True) casadm_bin_copy.chmod_numerical(777) with TestRun.step("Unmount core."): core.unmount() with TestRun.step("Add non-root user account."): TestRun.executor.run(f"useradd -N -r -l {user_name}") user_home_dir = fs_utils.parse_ls_output(fs_utils.ls_item(f"/home/{user_name}"))[0] user_home_dir.chmod_numerical(777, True) with TestRun.step("Try to stop OpenCAS service."): try: output = run_as_other_user(cli.ctl_stop(False), user_name) if output.exit_code == 0: TestRun.LOGGER.error("Stopping OpenCAS should fail!") except CmdException: TestRun.LOGGER.info("Non-root user cannot stop OpenCAS.") with TestRun.step("Try to start OpenCAS service."): try: output = run_as_other_user(cli.ctl_start(), user_name) if output.exit_code == 0: TestRun.LOGGER.error("Starting OpenCAS should fail!") except CmdException: TestRun.LOGGER.info("Non-root user cannot start OpenCAS.") with TestRun.step("Stop caches."): casadm.stop_all_caches() with TestRun.step("Try to init OpenCAS service."): try: output = run_as_other_user(cli.ctl_init(True), user_name) if output.exit_code == 0: TestRun.LOGGER.error("Initiating OpenCAS should fail!") except CmdException: TestRun.LOGGER.info("Non-root user cannot init OpenCAS.") with TestRun.step("Add non-root user account to sudoers group."): TestRun.executor.run(f'echo "{user_name} ALL = (root) NOPASSWD:ALL" ' f'| sudo tee /etc/sudoers.d/{user_name}') with TestRun.step("Try to stop OpenCAS service with 'sudo'."): try: run_as_other_user(cli.ctl_stop(False), user_name, True) except CmdException: TestRun.LOGGER.error("Non-root sudoer user should be able to stop OpenCAS.") with TestRun.step("Try to start OpenCAS service with 'sudo'."): try: run_as_other_user(cli.ctl_start(), user_name, True) except CmdException: TestRun.LOGGER.error("Non-root sudoer user should be able to start OpenCAS.") with TestRun.step("Stop caches."): casadm.stop_all_caches() with TestRun.step("Try to init OpenCAS service with 'sudo'."): try: run_as_other_user(cli.ctl_init(True), user_name, True) except CmdException: TestRun.LOGGER.error("Non-root sudoer user should be able to init OpenCAS.") with TestRun.step("Remove user account."): TestRun.executor.run(f"userdel -r -Z {user_name}") with TestRun.step("Stop all caches."): casadm.stop_all_caches()
def test_user_cli(): """ title: Test that OpenCAS does not allow to change parameters in CLI by non-root user. description: | Checking if changing parameters in CLI by non-root user is forbidden by OpenCAS, but is permitted with 'sudo' command. pass_criteria: - Non-root user can only print help and CAS version. - Sudoer user is allowed to change OpenCAS parameters in CLI with sudo. """ with TestRun.step("Prepare cache and core devices."): cache_dev = TestRun.disks['cache'] cache_dev.create_partitions([Size(256, Unit.MebiByte)]) cache_dev = cache_dev.partitions[0] core_dev = TestRun.disks['core'] core_dev.create_partitions([Size(1, Unit.GibiByte), Size(256, Unit.MebiByte)]) core_part1 = core_dev.partitions[0] core_part2 = core_dev.partitions[1] with TestRun.step("Start cache."): cache = casadm.start_cache(cache_dev, force=True) with TestRun.step("Add core to cache and mount it."): core_part1.create_filesystem(Filesystem.ext3) core = cache.add_core(core_part1) core.mount(mount_point) with TestRun.step(f"Copy casadm bin from {system_casadm_bin_path} " f"to {user_casadm_bin_dest_path}."): casadm_bin = fs_utils.parse_ls_output(fs_utils.ls_item(f"{system_casadm_bin_path}"))[0] casadm_bin_copy = casadm_bin.copy(user_casadm_bin_dest_path, True) casadm_bin_copy.chmod_numerical(777) with TestRun.step("Copy IO class config."): io_conf = fs_utils.parse_ls_output(fs_utils.ls_item(f"{ioclass_config_path}"))[0] io_conf_copy = io_conf.copy(ioclass_config_copy_path, force=True) with TestRun.step("Unmount core."): core.unmount() with TestRun.step("Stop cache."): casadm.stop_all_caches() with TestRun.step("Add non-root user account."): TestRun.executor.run(f"useradd -N -r -l {user_name}") user_home_dir = fs_utils.parse_ls_output(fs_utils.ls_item(f"/home/{user_name}"))[0] user_home_dir.chmod_numerical(777, True) with TestRun.step("Try to start cache."): try: output = run_as_other_user(cli.start_cmd(cache_dev.path), user_name) if output.exit_code == 0: TestRun.LOGGER.error("Starting cache should fail!") except CmdException: TestRun.LOGGER.info("Non-root user cannot start cache.") with TestRun.step("Start cache again."): casadm.load_cache(cache_dev) with TestRun.step("Try to stop cache."): try: output = run_as_other_user(cli.stop_cmd(str(cache.cache_id)), user_name) if output.exit_code == 0: TestRun.LOGGER.error("Stopping cache should fail!") except CmdException: TestRun.LOGGER.info("Non-root user cannot stop cache.") with TestRun.step("Try to set cache mode."): try: output = run_as_other_user(cli.set_cache_mode_cmd(CacheMode.WB, str(cache.cache_id)), user_name) if output.exit_code == 0: TestRun.LOGGER.error("Setting cache mode should fail!") except CmdException: TestRun.LOGGER.info("Non-root user cannot set cache mode.") with TestRun.step("Try to add core to cache."): try: output = run_as_other_user(cli.add_core_cmd(str(cache.cache_id), core_part2.path), user_name) if output.exit_code == 0: TestRun.LOGGER.error("Adding core to cache should fail!") except CmdException: TestRun.LOGGER.info("Non-root user cannot add core.") with TestRun.step("Try to remove core from cache."): try: output = run_as_other_user(cli.remove_core_cmd(str(cache.cache_id), str(core.core_id)), user_name) if output.exit_code == 0: TestRun.LOGGER.error("Removing core from cache should fail!") except CmdException: TestRun.LOGGER.info("Non-root user cannot remove core.") with TestRun.step("Try to zero metadata."): try: output = run_as_other_user(cli.zero_metadata_cmd(str(cache_dev)), user_name) if output.exit_code == 0: TestRun.LOGGER.error("Zeroing metadata should fail!") except CmdException: TestRun.LOGGER.info("Non-root user cannot zero metadata.") with TestRun.step("Try to list caches."): try: output = run_as_other_user(cli.list_cmd(), user_name) if output.exit_code == 0: TestRun.LOGGER.error("Listing caches should fail!") except CmdException: TestRun.LOGGER.info("Non-root user cannot list caches.") with TestRun.step("Try to print stats."): try: output = run_as_other_user(cli.print_statistics_cmd(str(cache.cache_id)), user_name) if output.exit_code == 0: TestRun.LOGGER.error("Printing stats should fail!") except CmdException: TestRun.LOGGER.info("Non-root user cannot print statistics.") with TestRun.step("Try to reset stats."): try: output = run_as_other_user(cli.reset_counters_cmd(str(cache.cache_id)), user_name) if output.exit_code == 0: TestRun.LOGGER.error("Resetting stats should fail!") except CmdException: TestRun.LOGGER.info("Non-root user cannot reset statistics.") with TestRun.step("Try to flush cache."): try: output = run_as_other_user(cli.flush_cache_cmd(str(cache.cache_id)), user_name) if output.exit_code == 0: TestRun.LOGGER.error("Flushing cache should fail!") except CmdException: TestRun.LOGGER.info("Non-root user cannot flush cache.") with TestRun.step("Try to flush core."): try: output = run_as_other_user(cli.flush_core_cmd(str(cache.cache_id), str(core.core_id)), user_name) if output.exit_code == 0: TestRun.LOGGER.error("Flushing core should fail!") except CmdException: TestRun.LOGGER.info("Non-root user cannot flush core.") with TestRun.step("Try to set cleaning policy and its parameters."): try: output = run_as_other_user(cli.set_param_cleaning_cmd( str(cache.cache_id), "nop"), user_name) if output.exit_code == 0: TestRun.LOGGER.error("Setting cleaning policy should fail!") except CmdException: TestRun.LOGGER.info("Non-root user cannot set cleaning policy as nop.") try: output = run_as_other_user(cli.set_param_cleaning_cmd( str(cache.cache_id), "alru"), user_name) if output.exit_code == 0: TestRun.LOGGER.error("Setting cleaning policy should fail!") except CmdException: TestRun.LOGGER.info("Non-root user cannot set cleaning policy as alru.") try: output = run_as_other_user(cli.set_param_cleaning_alru_cmd(str(cache.cache_id), "15", "60", "1000", "8000"), user_name) if output.exit_code == 0: TestRun.LOGGER.error("Setting cleaning policy parameters should fail!") except CmdException: TestRun.LOGGER.info("Non-root user cannot set alru cleaning policy parameters.") try: output = run_as_other_user(cli.set_param_cleaning_cmd( str(cache.cache_id), "acp"), user_name) if output.exit_code == 0: TestRun.LOGGER.error("Setting cleaning policy should fail!") except CmdException: TestRun.LOGGER.info("Non-root user cannot set cleaning policy as acp.") try: output = run_as_other_user(cli.set_param_cleaning_acp_cmd(str(cache.cache_id), "15", "1000"), user_name) if output.exit_code == 0: TestRun.LOGGER.error("Setting cleaning policy parameters should fail!") except CmdException: TestRun.LOGGER.info("Non-root user cannot set acp cleaning policy parameters.") with TestRun.step("Try to list IO class configuration."): try: output = run_as_other_user(cli.list_io_classes_cmd( str(cache.cache_id), OutputFormat.table.name), user_name) if output.exit_code == 0: TestRun.LOGGER.error("Listing IO class configuration should fail!") except CmdException: TestRun.LOGGER.info("Non-root user cannot list IO class configuration.") with TestRun.step("Try to load IO class configuration."): try: output = run_as_other_user(cli.load_io_classes_cmd( str(cache.cache_id), io_conf_copy), user_name) if output.exit_code == 0: TestRun.LOGGER.error("Loading IO class configuration should fail!") except CmdException: TestRun.LOGGER.info("Non-root user cannot load IO class configuration.") with TestRun.step("Try to print help for casadm."): try: run_as_other_user(cli.help_cmd(), user_name) except CmdException: TestRun.LOGGER.error("Non-root user should be able to print help for casadm.") with TestRun.step("Try to print version of OpenCAS."): try: run_as_other_user(cli.version_cmd(), user_name) except CmdException: TestRun.LOGGER.error("Non-root user should be able to print version of OpenCAS.") with TestRun.step("Add non-root user account to sudoers group."): TestRun.executor.run(f'echo "{user_name} ALL = (root) NOPASSWD:ALL" ' f'| sudo tee /etc/sudoers.d/{user_name}') with TestRun.step("Try to stop cache with 'sudo'."): try: run_as_other_user(cli.stop_cmd(str(cache.cache_id)), user_name, True) except CmdException: TestRun.LOGGER.error("Non-root sudoer user should be able to stop cache.") with TestRun.step("Try to start cache with 'sudo'."): try: run_as_other_user(cli.start_cmd(cache_dev.path, force=True), user_name, True) except CmdException: TestRun.LOGGER.error("Non-root sudoer user should be able to start cache.") with TestRun.step("Try to set cache mode with 'sudo'."): try: run_as_other_user( cli.set_cache_mode_cmd(str(CacheMode.WB.name).lower(), str(cache.cache_id)), user_name, True) except CmdException: TestRun.LOGGER.error("Non-root sudoer user should be able to set cache mode.") with TestRun.step("Try to add core to cache with 'sudo'."): try: run_as_other_user(cli.add_core_cmd(str(cache.cache_id), core_part1.path), user_name, True) except CmdException: TestRun.LOGGER.error("Non-root sudoer user should be able to add core to cache.") with TestRun.step("Try to list caches with 'sudo'."): try: run_as_other_user(cli.list_cmd(), user_name, True) except CmdException: TestRun.LOGGER.error("Non-root sudoer user should be able to list caches.") with TestRun.step("Try to print stats with 'sudo'."): try: run_as_other_user(cli.print_statistics_cmd(str(cache.cache_id)), user_name, True) except CmdException: TestRun.LOGGER.error("Non-root sudoer user should be able to print stats.") with TestRun.step("Try to reset stats with 'sudo'."): try: run_as_other_user(cli.reset_counters_cmd(str(cache.cache_id)), user_name, True) except CmdException: TestRun.LOGGER.error("Non-root sudoer user should be able to reset stats.") with TestRun.step("Try to flush cache with 'sudo'."): try: run_as_other_user(cli.flush_cache_cmd(str(cache.cache_id)), user_name, True) except CmdException: TestRun.LOGGER.error("Non-root sudoer user should be able to flush cache.") with TestRun.step("Try to flush core with 'sudo'."): try: run_as_other_user(cli.flush_core_cmd(str(cache.cache_id), str(core.core_id)), user_name, True) except CmdException: TestRun.LOGGER.error("Non-root sudoer user should be able to flush core.") with TestRun.step("Try to set cleaning policy and its parameters with 'sudo'."): try: run_as_other_user(cli.set_param_cleaning_cmd(str(cache.cache_id), "nop"), user_name, True) run_as_other_user(cli.set_param_cleaning_cmd(str(cache.cache_id), "alru"), user_name, True) try: run_as_other_user(cli.set_param_cleaning_alru_cmd(str(cache.cache_id), "15", "60", "1000", "8000"), user_name, True) except CmdException: TestRun.LOGGER.error("Non-root sudoer user should be able to " "set alru cleaning policy parameters.") run_as_other_user(cli.set_param_cleaning_cmd(str(cache.cache_id), "acp"), user_name, True) try: run_as_other_user(cli.set_param_cleaning_acp_cmd(str(cache.cache_id), "15", "1000"), user_name, True) except CmdException: TestRun.LOGGER.error("Non-root sudoer user should be able to " "set acp cleaning policy parameters.") except CmdException: TestRun.LOGGER.error("Non-root sudoer user should be able to " "set cleaning policy and its parameters.") with TestRun.step("Try to list IO class with 'sudo'."): try: run_as_other_user(cli.list_io_classes_cmd(str(cache.cache_id), OutputFormat.table.name), user_name, True) except CmdException: TestRun.LOGGER.error("Non-root sudoer user should be able to list IO class.") with TestRun.step("Try to load IO class configuration with 'sudo'."): try: run_as_other_user(cli.load_io_classes_cmd(str(cache.cache_id), io_conf_copy), user_name, True) except CmdException: TestRun.LOGGER.error("Non-root sudoer user should be able to " "load IO class configuration.") with TestRun.step("Try to remove core from cache with 'sudo'."): try: run_as_other_user(cli.remove_core_cmd(str(cache.cache_id), str(core.core_id)), user_name, True) except CmdException: TestRun.LOGGER.error("Non-root sudoer user should be able to remove core from cache.") with TestRun.step("Try to zero metadata with 'sudo'."): try: run_as_other_user(cli.zero_metadata_cmd(str(cache_dev)), user_name, True) except CmdException: TestRun.LOGGER.error("Non-root sudoer user should be able to zero metadata.") with TestRun.step("Try to print help for casadm with 'sudo'."): try: run_as_other_user(cli.help_cmd(), user_name, True) except CmdException: TestRun.LOGGER.error("Non-root sudoer user should be able to print help for casadm.") with TestRun.step("Try to print version of OpenCAS with 'sudo'."): try: run_as_other_user(cli.version_cmd(), user_name, True) except CmdException: TestRun.LOGGER.error("Non-root sudoer user should be able to print version of OpenCAS.") with TestRun.step("Stop caches."): casadm.stop_all_caches() with TestRun.step("Remove user account."): TestRun.executor.run(f"userdel -r -Z {user_name}")
def create_file(path: str): fs_utils.create_file(path) output = fs_utils.ls_item(f"{path}") return fs_utils.parse_ls_output(output)[0]
def create_directory(path: str, parents: bool = False): fs_utils.create_directory(path, parents) output = fs_utils.ls_item(path) return fs_utils.parse_ls_output(output)[0]