From c1872c3365f68d30bf6294ad0c0c9af3e2c02ba7 Mon Sep 17 00:00:00 2001 From: Daniel Madej Date: Fri, 8 Nov 2019 12:36:49 +0100 Subject: [PATCH 1/4] Update test-framework version Signed-off-by: Daniel Madej --- test/functional/test-framework | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/functional/test-framework b/test/functional/test-framework index 547fcaa..a2f3d66 160000 --- a/test/functional/test-framework +++ b/test/functional/test-framework @@ -1 +1 @@ -Subproject commit 547fcaa5fb0cdc3ce5696785ce7d7ced81a85005 +Subproject commit a2f3d6631e6c21cdea238bc2a53851655d498ff8 From ca115949add0cdc815b71f782f9d2ca1c1cf8057 Mon Sep 17 00:00:00 2001 From: Daniel Madej Date: Wed, 6 Nov 2019 13:28:40 +0100 Subject: [PATCH 2/4] Create IoClass class for rule management Signed-off-by: Daniel Madej --- test/functional/api/cas/ioclass_config.py | 150 ++++++++++++++++++++-- 1 file changed, 140 insertions(+), 10 deletions(-) diff --git a/test/functional/api/cas/ioclass_config.py b/test/functional/api/cas/ioclass_config.py index 3d92565..2261403 100644 --- a/test/functional/api/cas/ioclass_config.py +++ b/test/functional/api/cas/ioclass_config.py @@ -3,24 +3,154 @@ # SPDX-License-Identifier: BSD-3-Clause-Clear # +import enum +import re +import string from datetime import timedelta +from random import randint, randrange + +from packaging import version from core.test_run import TestRun +from test_tools import fs_utils +from test_utils import os_utils +from test_utils.generator import random_string default_config_file_path = "/tmp/opencas_ioclass.conf" MAX_IO_CLASS_ID = 32 - +MAX_IO_CLASS_PRIORITY = 255 MAX_CLASSIFICATION_DELAY = timedelta(seconds=6) +IO_CLASS_CONFIG_HEADER = "IO class id,IO class name,Eviction priority,Allocation" +class IoClass: + def __init__(self, class_id: int, rule: str = '', priority: int = None, + allocation: bool = True): + self.id = class_id + self.rule = rule + self.priority = priority + self.allocation = allocation + + def __str__(self): + return (f'{self.id},{self.rule},{"" if self.priority is None else self.priority}' + f',{int(self.allocation)}') + + def __eq__(self, other): + return type(other) is IoClass and self.id == other.id and self.rule == other.rule \ + and self.priority == other.priority and self.allocation == other.allocation + + @staticmethod + def from_string(ioclass_str: str): + parts = [part.strip() for part in re.split('[,|]', ioclass_str.replace('║', ''))] + return IoClass( + class_id=int(parts[0]), + rule=parts[1], + priority=int(parts[2]), + allocation=parts[3] in ['1', 'YES']) + + @staticmethod + def list_to_csv(ioclass_list: [], add_default_rule: bool = True): + list_copy = ioclass_list[:] + if add_default_rule and not len([c for c in list_copy if c.id == 0]): + list_copy.insert(0, IoClass.default()) + list_copy.insert(0, IO_CLASS_CONFIG_HEADER) + return '\n'.join(str(c) for c in list_copy) + + @staticmethod + def csv_to_list(csv: str): + ioclass_list = [] + for line in csv.splitlines(): + if line.strip() == IO_CLASS_CONFIG_HEADER: + continue + ioclass_list.append(IoClass.from_string(line)) + return ioclass_list + + @staticmethod + def save_list_to_config_file(ioclass_list: [], + add_default_rule: bool = True, + ioclass_config_path: str = default_config_file_path): + TestRun.LOGGER.info(f"Creating config file {ioclass_config_path}") + fs_utils.write_file(ioclass_config_path, + IoClass.list_to_csv(ioclass_list, add_default_rule)) + + @staticmethod + def default(): + return IoClass(0, 'unclassified', 255) + + @staticmethod + def compare_ioclass_lists(list1: [], list2: []): + if len(list1) != len(list2): + return False + sorted_list1 = sorted(list1, key=lambda c: (c.id, c.priority, c.allocation)) + sorted_list2 = sorted(list2, key=lambda c: (c.id, c.priority, c.allocation)) + for i in range(len(list1)): + if sorted_list1[i] != sorted_list2[i]: + return False + return True + + @staticmethod + def generate_random_ioclass_list(count: int, max_priority: int = MAX_IO_CLASS_PRIORITY): + random_list = [IoClass.default().set_priority(randint(0, max_priority)) + .set_allocation(bool(randint(0, 1)))] + for i in range(1, count): + random_list.append(IoClass(i).set_random_rule().set_priority(randint(0, max_priority)) + .set_allocation(bool(randint(0, 1)))) + return random_list + + def set_priority(self, priority: int): + self.priority = priority + return self + + def set_allocation(self, allocation: bool): + self.allocation = allocation + return self + + def set_rule(self, rule: str): + self.rule = rule + return self + + def set_random_rule(self): + rules = ["metadata", "direct", "file_size", "directory", "io_class", "extension", "lba", + "pid", "process_name", "file_offset", "request_size"] + if os_utils.get_kernel_version() >= version.Version("4.13"): + rules.append("wlth") + + rule = rules[randrange(len(rules))] + self.set_rule(IoClass.add_random_params(rule)) + return self + + @staticmethod + def add_random_params(rule: str): + if rule == "directory": + rule += \ + f":/{random_string(randint(1, 40), string.ascii_letters + string.digits + '/')}" + elif rule in ["file_size", "lba", "pid", "file_offset", "request_size", "wlth"]: + rule += f":{Operator(randrange(len(Operator))).name}:{randrange(1000000)}" + elif rule == "io_class": + rule += f":{randrange(MAX_IO_CLASS_PRIORITY + 1)}" + elif rule in ["extension", "process_name"]: + rule += f":{random_string(randint(1, 10))}" + if randrange(2): + rule += "&done" + return rule + + +class Operator(enum.Enum): + eq = 0 + gt = 1 + ge = 2 + lt = 3 + le = 4 + + +# TODO: replace below methods with methods using IoClass def create_ioclass_config( - add_default_rule: bool = True, ioclass_config_path: str = default_config_file_path + add_default_rule: bool = True, ioclass_config_path: str = default_config_file_path ): TestRun.LOGGER.info(f"Creating config file {ioclass_config_path}") output = TestRun.executor.run( - 'echo "IO class id,IO class name,Eviction priority,Allocation" ' - + f"> {ioclass_config_path}" + f'echo {IO_CLASS_CONFIG_HEADER} > {ioclass_config_path}' ) if output.exit_code != 0: raise Exception( @@ -49,11 +179,11 @@ def remove_ioclass_config(ioclass_config_path: str = default_config_file_path): def add_ioclass( - ioclass_id: int, - rule: str, - eviction_priority: int, - allocation: bool, - ioclass_config_path: str = default_config_file_path, + ioclass_id: int, + rule: str, + eviction_priority: int, + allocation: bool, + ioclass_config_path: str = default_config_file_path, ): new_ioclass = f"{ioclass_id},{rule},{eviction_priority},{int(allocation)}" TestRun.LOGGER.info( @@ -89,7 +219,7 @@ def get_ioclass(ioclass_id: int, ioclass_config_path: str = default_config_file_ def remove_ioclass( - ioclass_id: int, ioclass_config_path: str = default_config_file_path + ioclass_id: int, ioclass_config_path: str = default_config_file_path ): TestRun.LOGGER.info( f"Removing rule no.{ioclass_id} " + f"from config file {ioclass_config_path}" From 830bcfd1b0304e8ae893ffb9a5bcf9b6915ca7d6 Mon Sep 17 00:00:00 2001 From: Daniel Madej Date: Wed, 6 Nov 2019 13:29:32 +0100 Subject: [PATCH 3/4] Test for exporting current IO class configuration to a file Signed-off-by: Daniel Madej --- .../tests/io_class/io_class_common.py | 1 - .../tests/io_class/test_io_class_cli.py | 104 ++++++++++++++++++ 2 files changed, 104 insertions(+), 1 deletion(-) create mode 100644 test/functional/tests/io_class/test_io_class_cli.py diff --git a/test/functional/tests/io_class/io_class_common.py b/test/functional/tests/io_class/io_class_common.py index 96e4aad..4f0a4f5 100644 --- a/test/functional/tests/io_class/io_class_common.py +++ b/test/functional/tests/io_class/io_class_common.py @@ -6,7 +6,6 @@ from api.cas import casadm from api.cas import ioclass_config from api.cas.cache_config import CacheMode, CleaningPolicy -from storage_devices.disk import DiskType from core.test_run import TestRun from test_utils.size import Size, Unit diff --git a/test/functional/tests/io_class/test_io_class_cli.py b/test/functional/tests/io_class/test_io_class_cli.py new file mode 100644 index 0000000..4339afc --- /dev/null +++ b/test/functional/tests/io_class/test_io_class_cli.py @@ -0,0 +1,104 @@ +# +# Copyright(c) 2019 Intel Corporation +# SPDX-License-Identifier: BSD-3-Clause-Clear +# + + +import pytest + +from api.cas import casadm, ioclass_config +from api.cas.cache_config import CacheMode +from api.cas.casadm_params import OutputFormat +from api.cas.ioclass_config import IoClass +from core.test_run import TestRun +from storage_devices.disk import DiskType, DiskTypeSet, DiskTypeLowerThan +from test_tools import fs_utils +from test_utils.size import Size, Unit + +ioclass_config_path = "/tmp/opencas_ioclass.conf" + + +@pytest.mark.require_disk("cache", DiskTypeSet([DiskType.optane, DiskType.nand])) +@pytest.mark.require_disk("core", DiskTypeLowerThan("cache")) +@pytest.mark.parametrize("cache_mode", CacheMode) +def test_ioclass_export_configuration(cache_mode): + """ + title: Export IO class configuration to a file + description: Test CAS ability to create a properly formatted file with current IO class + configuration + pass_criteria: + - CAS default IO class configuration contains unclassified class only + - CAS properly imports previously exported configuration + """ + cache, core = prepare(cache_mode) + saved_config_path = "/tmp/opencas_saved.conf" + default_list = [IoClass.default()] + + with TestRun.LOGGER.step(f"Check IO class configuration (should contain only default class)"): + csv = casadm.list_io_classes(cache.cache_id, OutputFormat.csv).stdout + if not IoClass.compare_ioclass_lists(IoClass.csv_to_list(csv), default_list): + TestRun.LOGGER.error("Default configuration does not match expected\n" + f"Current:\n{csv}\n" + f"Expected:{IoClass.list_to_csv(default_list)}") + + with TestRun.LOGGER.step("Create and load configuration file for 33 IO classes " + "with random names, allocation and priority values"): + random_list = IoClass.generate_random_ioclass_list(33) + IoClass.save_list_to_config_file(random_list, ioclass_config_path=ioclass_config_path) + casadm.load_io_classes(cache.cache_id, ioclass_config_path) + + with TestRun.LOGGER.step("Display and export IO class configuration - displayed configuration " + "should be the same as created"): + TestRun.executor.run( + f"{casadm.list_io_classes_cmd(str(cache.cache_id), OutputFormat.csv.name)}" + f" > {saved_config_path}") + csv = fs_utils.read_file(saved_config_path) + if not IoClass.compare_ioclass_lists(IoClass.csv_to_list(csv), random_list): + TestRun.LOGGER.error("Exported configuration does not match expected\n" + f"Current:\n{csv}\n" + f"Expected:{IoClass.list_to_csv(random_list)}") + + with TestRun.LOGGER.step("Stop Intel CAS"): + casadm.stop_cache(cache.cache_id) + + with TestRun.LOGGER.step("Start cache and add core"): + cache = casadm.start_cache(cache.cache_device, force=True) + casadm.add_core(cache, core.core_device) + + with TestRun.LOGGER.step("Check IO class configuration (should contain only default class)"): + csv = casadm.list_io_classes(cache.cache_id, OutputFormat.csv).stdout + if not IoClass.compare_ioclass_lists(IoClass.csv_to_list(csv), default_list): + TestRun.LOGGER.error("Default configuration does not match expected\n" + f"Current:\n{csv}\n" + f"Expected:{IoClass.list_to_csv(default_list)}") + + with TestRun.LOGGER.step("Load exported configuration file for 33 IO classes"): + casadm.load_io_classes(cache.cache_id, saved_config_path) + + with TestRun.LOGGER.step("Display IO class configuration - should be the same as created"): + csv = casadm.list_io_classes(cache.cache_id, OutputFormat.csv).stdout + if not IoClass.compare_ioclass_lists(IoClass.csv_to_list(csv), random_list): + TestRun.LOGGER.error("Exported configuration does not match expected\n" + f"Current:\n{csv}\n" + f"Expected:{IoClass.list_to_csv(random_list)}") + + fs_utils.remove(saved_config_path) + + +def prepare(cache_mode: CacheMode = None): + ioclass_config.remove_ioclass_config() + cache_device = TestRun.disks['cache'] + core_device = TestRun.disks['core'] + + cache_device.create_partitions([Size(150, Unit.MebiByte)]) + core_device.create_partitions([Size(300, Unit.MebiByte)]) + + cache_device = cache_device.partitions[0] + core_device = core_device.partitions[0] + + TestRun.LOGGER.info(f"Starting cache") + cache = casadm.start_cache(cache_device, cache_mode=cache_mode, force=True) + TestRun.LOGGER.info(f"Adding core device") + core = casadm.add_core(cache, core_dev=core_device) + + return cache, core From 695d9a688f34a735c4e7ed496ec81b73a76ec5ec Mon Sep 17 00:00:00 2001 From: Daniel Madej Date: Wed, 6 Nov 2019 17:29:29 +0100 Subject: [PATCH 4/4] Changes after review Signed-off-by: Daniel Madej --- test/functional/api/cas/ioclass_config.py | 62 ++++++++----------- .../tests/io_class/test_io_class_cli.py | 16 ++--- 2 files changed, 34 insertions(+), 44 deletions(-) diff --git a/test/functional/api/cas/ioclass_config.py b/test/functional/api/cas/ioclass_config.py index 2261403..1830304 100644 --- a/test/functional/api/cas/ioclass_config.py +++ b/test/functional/api/cas/ioclass_config.py @@ -4,10 +4,11 @@ # import enum +import functools +import random import re import string from datetime import timedelta -from random import randint, randrange from packaging import version @@ -24,6 +25,7 @@ MAX_CLASSIFICATION_DELAY = timedelta(seconds=6) IO_CLASS_CONFIG_HEADER = "IO class id,IO class name,Eviction priority,Allocation" +@functools.total_ordering class IoClass: def __init__(self, class_id: int, rule: str = '', priority: int = None, allocation: bool = True): @@ -37,8 +39,12 @@ class IoClass: f',{int(self.allocation)}') def __eq__(self, other): - return type(other) is IoClass and self.id == other.id and self.rule == other.rule \ - and self.priority == other.priority and self.allocation == other.allocation + return ((self.id, self.rule, self.priority, self.allocation) + == (other.id, other.rule, other.priority, other.allocation)) + + def __lt__(self, other): + return ((self.id, self.rule, self.priority, self.allocation) + < (other.id, other.rule, other.priority, other.allocation)) @staticmethod def from_string(ioclass_str: str): @@ -75,63 +81,45 @@ class IoClass: IoClass.list_to_csv(ioclass_list, add_default_rule)) @staticmethod - def default(): - return IoClass(0, 'unclassified', 255) + def default(priority: int = 255, allocation: bool = True): + return IoClass(0, 'unclassified', priority, allocation) @staticmethod def compare_ioclass_lists(list1: [], list2: []): - if len(list1) != len(list2): - return False - sorted_list1 = sorted(list1, key=lambda c: (c.id, c.priority, c.allocation)) - sorted_list2 = sorted(list2, key=lambda c: (c.id, c.priority, c.allocation)) - for i in range(len(list1)): - if sorted_list1[i] != sorted_list2[i]: - return False - return True + return sorted(list1) == sorted(list2) @staticmethod def generate_random_ioclass_list(count: int, max_priority: int = MAX_IO_CLASS_PRIORITY): - random_list = [IoClass.default().set_priority(randint(0, max_priority)) - .set_allocation(bool(randint(0, 1)))] + random_list = [IoClass.default(priority=random.randint(0, max_priority), + allocation=bool(random.randint(0, 1)))] for i in range(1, count): - random_list.append(IoClass(i).set_random_rule().set_priority(randint(0, max_priority)) - .set_allocation(bool(randint(0, 1)))) + random_list.append(IoClass(i, priority=random.randint(0, max_priority), + allocation=bool(random.randint(0, 1))) + .set_random_rule()) return random_list - def set_priority(self, priority: int): - self.priority = priority - return self - - def set_allocation(self, allocation: bool): - self.allocation = allocation - return self - - def set_rule(self, rule: str): - self.rule = rule - return self - def set_random_rule(self): rules = ["metadata", "direct", "file_size", "directory", "io_class", "extension", "lba", "pid", "process_name", "file_offset", "request_size"] if os_utils.get_kernel_version() >= version.Version("4.13"): rules.append("wlth") - rule = rules[randrange(len(rules))] - self.set_rule(IoClass.add_random_params(rule)) + rule = random.choice(rules) + self.rule = IoClass.add_random_params(rule) return self @staticmethod def add_random_params(rule: str): if rule == "directory": - rule += \ - f":/{random_string(randint(1, 40), string.ascii_letters + string.digits + '/')}" + allowed_chars = string.ascii_letters + string.digits + '/' + rule += f":/{random_string(random.randint(1, 40), allowed_chars)}" elif rule in ["file_size", "lba", "pid", "file_offset", "request_size", "wlth"]: - rule += f":{Operator(randrange(len(Operator))).name}:{randrange(1000000)}" + rule += f":{Operator(random.randrange(len(Operator))).name}:{random.randrange(1000000)}" elif rule == "io_class": - rule += f":{randrange(MAX_IO_CLASS_PRIORITY + 1)}" + rule += f":{random.randrange(MAX_IO_CLASS_PRIORITY + 1)}" elif rule in ["extension", "process_name"]: - rule += f":{random_string(randint(1, 10))}" - if randrange(2): + rule += f":{random_string(random.randint(1, 10))}" + if random.randrange(2): rule += "&done" return rule diff --git a/test/functional/tests/io_class/test_io_class_cli.py b/test/functional/tests/io_class/test_io_class_cli.py index 4339afc..8dffee0 100644 --- a/test/functional/tests/io_class/test_io_class_cli.py +++ b/test/functional/tests/io_class/test_io_class_cli.py @@ -24,15 +24,16 @@ ioclass_config_path = "/tmp/opencas_ioclass.conf" def test_ioclass_export_configuration(cache_mode): """ title: Export IO class configuration to a file - description: Test CAS ability to create a properly formatted file with current IO class - configuration + description: | + Test CAS ability to create a properly formatted file with current IO class configuration pass_criteria: - CAS default IO class configuration contains unclassified class only - CAS properly imports previously exported configuration """ - cache, core = prepare(cache_mode) - saved_config_path = "/tmp/opencas_saved.conf" - default_list = [IoClass.default()] + with TestRun.LOGGER.step(f"Test prepare"): + cache, core = prepare(cache_mode) + saved_config_path = "/tmp/opencas_saved.conf" + default_list = [IoClass.default()] with TestRun.LOGGER.step(f"Check IO class configuration (should contain only default class)"): csv = casadm.list_io_classes(cache.cache_id, OutputFormat.csv).stdout @@ -50,7 +51,7 @@ def test_ioclass_export_configuration(cache_mode): with TestRun.LOGGER.step("Display and export IO class configuration - displayed configuration " "should be the same as created"): TestRun.executor.run( - f"{casadm.list_io_classes_cmd(str(cache.cache_id), OutputFormat.csv.name)}" + f"{casadm.list_io_classes_cmd(str(cache.cache_id), OutputFormat.csv.name)}" f" > {saved_config_path}") csv = fs_utils.read_file(saved_config_path) if not IoClass.compare_ioclass_lists(IoClass.csv_to_list(csv), random_list): @@ -82,7 +83,8 @@ def test_ioclass_export_configuration(cache_mode): f"Current:\n{csv}\n" f"Expected:{IoClass.list_to_csv(random_list)}") - fs_utils.remove(saved_config_path) + with TestRun.LOGGER.step(f"Test cleanup"): + fs_utils.remove(saved_config_path) def prepare(cache_mode: CacheMode = None):