From e689f74735aafc1fb80097d45cb06455536dbd07 Mon Sep 17 00:00:00 2001 From: Chris Date: Fri, 26 Apr 2024 15:50:54 +0800 Subject: [PATCH] test(robot): add storageclass for different replica count longhorn/longhorn-8440 Signed-off-by: Chris --- e2e/keywords/common.resource | 1 + e2e/keywords/storageclass.resource | 12 +++++++ e2e/libs/keywords/storageclass_keywords.py | 23 ++++++++++++ e2e/libs/keywords/workload_keywords.py | 10 ------ e2e/libs/storageclass/__init__.py | 1 + e2e/libs/storageclass/storageclass.py | 42 ++++++++++++++++++++++ e2e/libs/utility/constant.py | 1 + e2e/libs/utility/utility.py | 6 +++- e2e/libs/workload/workload.py | 22 ------------ 9 files changed, 85 insertions(+), 33 deletions(-) create mode 100644 e2e/keywords/storageclass.resource create mode 100644 e2e/libs/keywords/storageclass_keywords.py create mode 100644 e2e/libs/storageclass/__init__.py create mode 100644 e2e/libs/storageclass/storageclass.py diff --git a/e2e/keywords/common.resource b/e2e/keywords/common.resource index 85c1cdf7ca..4dbd6010a2 100644 --- a/e2e/keywords/common.resource +++ b/e2e/keywords/common.resource @@ -12,6 +12,7 @@ Library ../libs/keywords/workload_keywords.py Library ../libs/keywords/persistentvolumeclaim_keywords.py Library ../libs/keywords/network_keywords.py Library ../libs/keywords/backupstore_keywords.py +Library ../libs/keywords/storageclass_keywords.py *** Keywords *** Set test environment diff --git a/e2e/keywords/storageclass.resource b/e2e/keywords/storageclass.resource new file mode 100644 index 0000000000..81bdab6719 --- /dev/null +++ b/e2e/keywords/storageclass.resource @@ -0,0 +1,12 @@ +*** Settings *** +Documentation Volume Keywords + +Library Collections +Library ../libs/keywords/common_keywords.py +Library ../libs/keywords/storageclass_keywords.py + +*** Keywords *** +Create storageclass ${storageclass_id} with + [Arguments] &{config} + ${storageclass_name} = generate_name_with_suffix storageclass ${storageclass_id} + create_storageclass ${storageclass_name} &{config} \ No newline at end of file diff --git a/e2e/libs/keywords/storageclass_keywords.py b/e2e/libs/keywords/storageclass_keywords.py new file mode 100644 index 0000000000..888d3c34a0 --- /dev/null +++ b/e2e/libs/keywords/storageclass_keywords.py @@ -0,0 +1,23 @@ +from utility.utility import logging +from storageclass.storageclass import create +from storageclass.storageclass import delete +from storageclass.storageclass import delete_all + +class storageclass_keywords: + + def __init__(self): + pass + + def init_storageclasses(self): + create('longhorn-test') + create('longhorn-test-strict-local', replica_count="1", data_locality='strict-local') + + def create_storageclass(self, name, replica_count="", migratable="", data_locality="", from_backup=""): + logging(f'Creating storageclass {name}') + create(name, replica_count, migratable, data_locality, from_backup) + + def delete_storageclass(self, name): + delete(name) + + def cleanup_storageclasses(self): + delete_all() diff --git a/e2e/libs/keywords/workload_keywords.py b/e2e/libs/keywords/workload_keywords.py index 0e49e877f1..5964b8fa69 100644 --- a/e2e/libs/keywords/workload_keywords.py +++ b/e2e/libs/keywords/workload_keywords.py @@ -8,8 +8,6 @@ from workload.pod import get_volume_name_by_pod from workload.workload import check_pod_data_checksum -from workload.workload import create_storageclass -from workload.workload import delete_storageclass from workload.workload import get_workload_pod_names from workload.workload import get_workload_persistent_volume_claim_name from workload.workload import get_workload_volume_name @@ -36,14 +34,6 @@ def __init__(self): self.persistentvolumeclaim = PersistentVolumeClaim() self.volume = Volume() - def init_storageclasses(self): - create_storageclass('longhorn-test') - create_storageclass('longhorn-test-strict-local') - - def cleanup_storageclasses(self): - delete_storageclass('longhorn-test') - delete_storageclass('longhorn-test-strict-local') - def check_pod_data_checksum(self, expected_checksum, pod_name, file_name): logging(f'Checking checksum for file {file_name} in pod {pod_name}') check_pod_data_checksum(expected_checksum, pod_name, file_name) diff --git a/e2e/libs/storageclass/__init__.py b/e2e/libs/storageclass/__init__.py new file mode 100644 index 0000000000..1a8cbc2ec4 --- /dev/null +++ b/e2e/libs/storageclass/__init__.py @@ -0,0 +1 @@ +from storageclass import storageclass \ No newline at end of file diff --git a/e2e/libs/storageclass/storageclass.py b/e2e/libs/storageclass/storageclass.py new file mode 100644 index 0000000000..5accd3055a --- /dev/null +++ b/e2e/libs/storageclass/storageclass.py @@ -0,0 +1,42 @@ +import yaml + +from kubernetes import client +from kubernetes.client.rest import ApiException +from kubernetes.stream import stream + + +def create(name, replica_count="", migratable="", data_locality="", from_backup=""): + filepath = "./templates/workload/storageclass.yaml" + + with open(filepath, 'r') as f: + manifest_dict = yaml.safe_load(f) + manifest_dict['metadata']['name'] = name + + if replica_count != "": + manifest_dict['parameters']['numberOfReplicas'] = replica_count + if migratable != "": + manifest_dict['parameters']['migratable'] = migratable + if data_locality != "": + manifest_dict['parameters']['dataLocality'] = data_locality + if from_backup != "": + manifest_dict['parameters']['fromBackup'] = from_backup + + api = client.StorageV1Api() + api.create_storage_class(body=manifest_dict) + + +def delete(name): + api = client.StorageV1Api() + try: + api.delete_storage_class(name, grace_period_seconds=0) + except ApiException as e: + assert e.status == 404 + + +def delete_all(): + api = client.StorageV1Api() + storage_classes = api.list_storage_class() + + for item in storage_classes.items: + if "longhorn-test" in item.metadata.name: + delete(item.metadata.name) diff --git a/e2e/libs/utility/constant.py b/e2e/libs/utility/constant.py index 5c92d9f9aa..6930fae83b 100644 --- a/e2e/libs/utility/constant.py +++ b/e2e/libs/utility/constant.py @@ -8,6 +8,7 @@ ANNOT_EXPANDED_SIZE = f'{LABEL_TEST}/last-recorded-expanded-size' NAME_PREFIX = 'e2e-test' +STORAGECLASS_NAME_PREFIX = 'longhorn-test' STREAM_EXEC_TIMEOUT = 300 diff --git a/e2e/libs/utility/utility.py b/e2e/libs/utility/utility.py index 96154aea65..187b4dd52d 100644 --- a/e2e/libs/utility/utility.py +++ b/e2e/libs/utility/utility.py @@ -18,6 +18,7 @@ from utility.constant import NAME_PREFIX from utility.constant import STREAM_EXEC_TIMEOUT +from utility.constant import STORAGECLASS_NAME_PREFIX class timeout: @@ -57,7 +58,10 @@ def generate_name_random(name_prefix="test-"): def generate_name_with_suffix(kind, suffix): - return f"{NAME_PREFIX}-{kind}-{suffix}" + if kind == "storageclass": + return f"{STORAGECLASS_NAME_PREFIX}-{suffix}" + else: + return f"{NAME_PREFIX}-{kind}-{suffix}" def init_k8s_api_client(): diff --git a/e2e/libs/workload/workload.py b/e2e/libs/workload/workload.py index 8cdca817ef..02ac833502 100644 --- a/e2e/libs/workload/workload.py +++ b/e2e/libs/workload/workload.py @@ -1,8 +1,6 @@ import time -import yaml from kubernetes import client -from kubernetes.client.rest import ApiException from kubernetes.stream import stream from utility.utility import get_retry_count_and_interval @@ -11,26 +9,6 @@ from workload.constant import WAIT_FOR_POD_STABLE_MAX_RETRY -def create_storageclass(name): - if name == 'longhorn-test-strict-local': - filepath = "./templates/workload/strict_local_storageclass.yaml" - else: - filepath = "./templates/workload/storageclass.yaml" - - with open(filepath, 'r') as f: - manifest_dict = yaml.safe_load(f) - api = client.StorageV1Api() - api.create_storage_class(body=manifest_dict) - - -def delete_storageclass(name): - api = client.StorageV1Api() - try: - api.delete_storage_class(name, grace_period_seconds=0) - except ApiException as e: - assert e.status == 404 - - def get_workload_pod_names(workload_name): api = client.CoreV1Api() label_selector = f"app={workload_name}"