|
| 1 | +# |
| 2 | +# Copyright(c) 2020 Intel Corporation |
| 3 | +# SPDX-License-Identifier: BSD-3-Clause-Clear |
| 4 | +# |
| 5 | + |
| 6 | +import pytest |
| 7 | + |
| 8 | +from api.cas.cas_module import CasModule |
| 9 | +from core.test_run import TestRun |
| 10 | +from test_utils.size import Unit |
| 11 | +from test_utils.os_utils import (allocate_memory, |
| 12 | + defaultize_memory_affecting_functions, |
| 13 | + disable_memory_affecting_functions, |
| 14 | + drop_caches, |
| 15 | + get_free_memory, |
| 16 | + is_kernel_module_loaded, |
| 17 | + load_kernel_module, |
| 18 | + unload_kernel_module, |
| 19 | + unmount_ramfs) |
| 20 | + |
| 21 | + |
| 22 | +@pytest.mark.os_dependent |
| 23 | +def test_insufficient_memory_for_cas_module(): |
| 24 | + """ |
| 25 | + title: Negative test of ability to load OpenCAS kernel module with insufficient memory. |
| 26 | + description: | |
| 27 | + Check that OpenCAS kernel module won’t be loaded in case not enough memory is available. |
| 28 | + pass_criteria: |
| 29 | + - Loading OpenCAS kernel module returns error. |
| 30 | + """ |
| 31 | + with TestRun.step("Disable caching and memory over-committing."): |
| 32 | + disable_memory_affecting_functions() |
| 33 | + drop_caches() |
| 34 | + |
| 35 | + with TestRun.step("Measure memory usage without OpenCAS module."): |
| 36 | + if is_kernel_module_loaded(CasModule.cache.value): |
| 37 | + unload_kernel_module(CasModule.cache.value) |
| 38 | + unload_kernel_module(CasModule.disk.value) |
| 39 | + available_mem_before_cas = get_free_memory() |
| 40 | + |
| 41 | + with TestRun.step("Load OpenCAS module"): |
| 42 | + output = load_kernel_module(CasModule.cache.value) |
| 43 | + if output.exit_code != 0: |
| 44 | + TestRun.fail("Cannot load OpenCAS module!") |
| 45 | + |
| 46 | + with TestRun.step("Measure memory usage with OpenCAS module."): |
| 47 | + available_mem_with_cas = get_free_memory() |
| 48 | + memory_used_by_cas = available_mem_before_cas - available_mem_with_cas |
| 49 | + TestRun.LOGGER.info( |
| 50 | + f"OpenCAS module uses {memory_used_by_cas.get_value(Unit.MiB):.2f} MiB of DRAM." |
| 51 | + ) |
| 52 | + |
| 53 | + with TestRun.step("Unload OpenCAS module."): |
| 54 | + unload_kernel_module(CasModule.cache.value) |
| 55 | + unload_kernel_module(CasModule.disk.value) |
| 56 | + |
| 57 | + with TestRun.step("Allocate memory leaving not enough memory for OpenCAS module."): |
| 58 | + memory_to_leave = memory_used_by_cas * (3 / 4) |
| 59 | + try: |
| 60 | + allocate_memory(get_free_memory() - memory_to_leave) |
| 61 | + except Exception as ex: |
| 62 | + TestRun.LOGGER.error(f"{ex}") |
| 63 | + |
| 64 | + with TestRun.step( |
| 65 | + "Try to load OpenCAS module and check if error message is printed on failure." |
| 66 | + ): |
| 67 | + output = load_kernel_module(CasModule.cache.value) |
| 68 | + if output.stderr and output.exit_code != 0: |
| 69 | + memory_left = get_free_memory() |
| 70 | + TestRun.LOGGER.info( |
| 71 | + f"Memory left for OpenCAS module: {memory_left.get_value(Unit.MiB):0.2f} MiB." |
| 72 | + ) |
| 73 | + TestRun.LOGGER.info(f"Cannot load OpenCAS module as expected.\n{output.stderr}") |
| 74 | + else: |
| 75 | + TestRun.LOGGER.error("Loading OpenCAS module successfully finished, but should fail.") |
| 76 | + |
| 77 | + with TestRun.step("Set memory options to default"): |
| 78 | + unmount_ramfs() |
| 79 | + defaultize_memory_affecting_functions() |
0 commit comments