|
| 1 | +# |
| 2 | +# Copyright(c) 2022 Intel Corporation |
| 3 | +# Copyright(c) 2024-2025 Huawei Technologies Co., Ltd. |
| 4 | +# SPDX-License-Identifier: BSD-3-Clause |
| 5 | +# |
| 6 | + |
| 7 | +import pytest |
| 8 | + |
| 9 | +from itertools import cycle |
| 10 | +from random import shuffle |
| 11 | +from api.cas import casadm |
| 12 | +from api.cas.casadm_parser import get_cores, get_detached_cores, get_inactive_cores |
| 13 | +from connection.utils.output import CmdException |
| 14 | +from core.test_run import TestRun |
| 15 | +from storage_devices.disk import DiskType, DiskTypeSet, DiskTypeLowerThan |
| 16 | +from test_tools.fs_tools import readlink, remove |
| 17 | +from test_utils.filesystem.symlink import Symlink |
| 18 | +from type_def.size import Size, Unit |
| 19 | + |
| 20 | +cores_number = 4 |
| 21 | + |
| 22 | + |
| 23 | +@pytest.mark.require_disk("cache", DiskTypeSet([DiskType.optane, DiskType.nand])) |
| 24 | +@pytest.mark.require_disk("core", DiskTypeLowerThan("cache")) |
| 25 | +def test_add_core_path_by_id(): |
| 26 | + """ |
| 27 | + title: Test for adding core with by-id path. |
| 28 | + description: | |
| 29 | + Check if core can be added to cache using by-id path. |
| 30 | + pass_criteria: |
| 31 | + - Cores are added to cache |
| 32 | + - Cores are added to cache with the same path as given |
| 33 | + """ |
| 34 | + with TestRun.step("Prepare partitions for cache and for cores."): |
| 35 | + cache_dev = TestRun.disks["cache"] |
| 36 | + cache_dev.create_partitions([Size(200, Unit.MebiByte)]) |
| 37 | + cache_part = cache_dev.partitions[0] |
| 38 | + core_dev = TestRun.disks["core"] |
| 39 | + core_dev.create_partitions([Size(400, Unit.MebiByte)] * cores_number) |
| 40 | + |
| 41 | + with TestRun.step("Start cache and add cores"): |
| 42 | + cache = casadm.start_cache(cache_part, force=True) |
| 43 | + for core_dev_part in core_dev.partitions: |
| 44 | + cache.add_core(core_dev_part) |
| 45 | + |
| 46 | + with TestRun.step("Check if all cores are added with proper paths."): |
| 47 | + added_cores = get_cores(cache.cache_id) |
| 48 | + added_cores_number = len(added_cores) |
| 49 | + if added_cores_number != cores_number: |
| 50 | + TestRun.fail(f"Expected {cores_number} cores, got {added_cores_number}!") |
| 51 | + |
| 52 | + for core, partition in zip(added_cores, core_dev.partitions): |
| 53 | + if partition.path != core.core_device.path: |
| 54 | + TestRun.LOGGER.error( |
| 55 | + f"Paths are different and can cause problems!\n" |
| 56 | + f"Path passed as an argument to add core: {partition.path}\n" |
| 57 | + f"Path displayed by 'casadm -L': {core.core_device.path}" |
| 58 | + ) |
| 59 | + |
| 60 | + |
| 61 | +@pytest.mark.require_disk("cache", DiskTypeSet([DiskType.optane, DiskType.nand])) |
| 62 | +@pytest.mark.require_disk("core", DiskTypeLowerThan("cache")) |
| 63 | +def test_add_core_path_not_by_id(): |
| 64 | + """ |
| 65 | + title: Negative test for adding core with non-by-id path. |
| 66 | + description: | |
| 67 | + Check if it is not permitted to use any other than by-id path to disks added as cores. |
| 68 | + pass_criteria: |
| 69 | + - Cores are not added to cache |
| 70 | + """ |
| 71 | + |
| 72 | + with TestRun.step("Prepare partitions for cache and for cores."): |
| 73 | + cache_dev = TestRun.disks["cache"] |
| 74 | + cache_dev.create_partitions([Size(200, Unit.MebiByte)]) |
| 75 | + cache_part = cache_dev.partitions[0] |
| 76 | + core_dev = TestRun.disks["core"] |
| 77 | + core_dev.create_partitions([Size(400, Unit.MebiByte)] * cores_number) |
| 78 | + |
| 79 | + with TestRun.step("Start cache."): |
| 80 | + cache = casadm.start_cache(cache_part, force=True) |
| 81 | + |
| 82 | + with TestRun.step( |
| 83 | + f"Create symlinks for {core_dev.path} partitions in " |
| 84 | + f"{TestRun.TEST_RUN_DATA_PATH} directory." |
| 85 | + ): |
| 86 | + core_dev_links = [ |
| 87 | + Symlink.create_symlink( |
| 88 | + f"{TestRun.TEST_RUN_DATA_PATH}_{path.split('/')[-1]}", |
| 89 | + path |
| 90 | + ) for path in [readlink(part.path) for part in core_dev.partitions] |
| 91 | + ] |
| 92 | + |
| 93 | + with TestRun.step(f"Find various symlinks to {core_dev.path}."): |
| 94 | + links = [] |
| 95 | + for partition in core_dev.partitions: |
| 96 | + links.append(Symlink(get_by_partuuid_link(partition.path))) |
| 97 | + links.append(Symlink(readlink(partition.path))) |
| 98 | + core_dev_links.extend([ |
| 99 | + link for link in links if |
| 100 | + readlink(partition.path) in link.get_target() |
| 101 | + ]) |
| 102 | + |
| 103 | + with TestRun.step(f"Select different links to {core_dev.path} partitions."): |
| 104 | + selected_links = select_random_links(core_dev_links) |
| 105 | + |
| 106 | + with TestRun.step(f"Try to add {cores_number} cores with non-by-id path."): |
| 107 | + for dev, symlink in zip(core_dev.partitions, selected_links): |
| 108 | + dev.path = symlink.full_path |
| 109 | + try: |
| 110 | + cache.add_core(dev) |
| 111 | + TestRun.fail(f"Core {core_dev.path} is added!") |
| 112 | + except CmdException: |
| 113 | + pass |
| 114 | + TestRun.LOGGER.info("Cannot add cores as expected.") |
| 115 | + |
| 116 | + with TestRun.step("Check if cores are not added."): |
| 117 | + get_core_methods = [get_cores, get_inactive_cores, get_detached_cores] |
| 118 | + core_types = ["active", "inactive", "detached"] |
| 119 | + for method, core_type in zip(get_core_methods, core_types): |
| 120 | + added_cores_number = len(method(cache.cache_id)) |
| 121 | + if added_cores_number > 0: |
| 122 | + TestRun.LOGGER.error( |
| 123 | + f"Expected 0 cores, got {added_cores_number} {core_type} cores!" |
| 124 | + ) |
| 125 | + |
| 126 | + with TestRun.step("Cleanup test symlinks."): |
| 127 | + remove(f"{TestRun.TEST_RUN_DATA_PATH}_*", True, True) |
| 128 | + |
| 129 | + |
| 130 | +def get_by_partuuid_link(path): |
| 131 | + output = TestRun.executor.run(f"blkid {path}") |
| 132 | + if "PARTUUID" not in output.stdout: |
| 133 | + return path |
| 134 | + |
| 135 | + uuid = output.stdout.split()[-1] |
| 136 | + start = uuid.index('"') |
| 137 | + end = uuid.index('"', start + 1) |
| 138 | + uuid = uuid[start + 1:end] |
| 139 | + |
| 140 | + return f"/dev/disk/by-partuuid/{uuid}" |
| 141 | + |
| 142 | + |
| 143 | +def select_random_links(links): |
| 144 | + shuffle(links) |
| 145 | + selected_links = [] |
| 146 | + links_cycle = cycle(links) |
| 147 | + |
| 148 | + while len(selected_links) < cores_number: |
| 149 | + link = next(links_cycle) |
| 150 | + target = link.get_target() |
| 151 | + if target not in [sel_link.get_target() for sel_link in selected_links]: |
| 152 | + selected_links.append(link) |
| 153 | + |
| 154 | + return selected_links |
0 commit comments