From 00969e58ab9e18a0b6dbd82765f9e53f021959b0 Mon Sep 17 00:00:00 2001 From: Gil Bregman Date: Thu, 26 Dec 2024 19:24:52 +0200 Subject: [PATCH] Verify python source files with flake8. Fixes #994 Signed-off-by: Gil Bregman --- .github/workflows/build-container.yml | 6 + Makefile | 6 + control/cephutils.py | 13 +- control/discovery.py | 215 ++++--- control/grpc.py | 787 +++++++++++++------------- control/rebalance.py | 24 +- control/utils.py | 24 +- tests/conftest.py | 2 - tests/ha/start_up.sh | 3 +- tests/test_dhchap.py | 19 +- tests/test_grpc.py | 4 +- tests/test_log_files.py | 6 +- tests/test_multi_gateway.py | 1 - tests/test_nsid.py | 2 - tests/test_omap_lock.py | 7 +- tests/test_psk.py | 19 +- tests/test_server.py | 13 +- tests/test_state.py | 4 +- 18 files changed, 570 insertions(+), 585 deletions(-) diff --git a/.github/workflows/build-container.yml b/.github/workflows/build-container.yml index 3d7bead8f..7bb5e5a3c 100644 --- a/.github/workflows/build-container.yml +++ b/.github/workflows/build-container.yml @@ -31,6 +31,12 @@ jobs: with: submodules: recursive + - name: Install Flake8 + run: pip install flake8 + + - name: Verify Python source files + run: make verify + - name: Build container images - spdk run: make build SVC="spdk" SPDK_TARGET_ARCH=x86-64-v2 diff --git a/Makefile b/Makefile index 0b44bdda9..49159553c 100644 --- a/Makefile +++ b/Makefile @@ -30,6 +30,12 @@ include mk/autohelp.mk .DEFAULT_GOAL := all all: setup $(ALL) +FLAKE_IGNORE = --extend-ignore E501,E251,E225,E711,E128,F541,W291,E211,E202,E265,E117,E111,E203,E127,E261,E222,E201,E262,E303,E302,E221,E231 + +verify: ## Run Python source files through flake8 + @echo Verifying Python source files + flake8 $(FLAKE_IGNORE) control/*.py + setup: ## Configure huge-pages (requires sudo/root password) @echo Setup core dump pattern as /tmp/coredump/core.* diff --git a/control/cephutils.py b/control/cephutils.py index 7452a008b..614382a04 100644 --- a/control/cephutils.py +++ b/control/cephutils.py @@ -32,6 +32,7 @@ def execute_ceph_monitor_command(self, cmd): rply = cluster.mon_command(cmd, b'') self.logger.debug(f"Monitor reply: {rply}") return rply + def get_gw_id_owner_ana_group(self, pool, group, anagrp): str = '{' + f'"prefix":"nvme-gw show", "pool":"{pool}", "group":"{group}"' + '}' self.logger.debug(f"nvme-show string: {str}") @@ -51,10 +52,10 @@ def get_gw_id_owner_ana_group(self, pool, group, anagrp): return gw_id def is_rebalance_supported(self): - return self.rebalance_supported + return self.rebalance_supported def get_rebalance_ana_group(self): - return self.rebalance_ana_group + return self.rebalance_ana_group def get_number_created_gateways(self, pool, group): now = time.time() @@ -168,13 +169,13 @@ def create_image(self, pool_name, image_name, size) -> bool: rbd_inst = rbd.RBD() try: rbd_inst.create(ioctx, image_name, size) - except rbd.ImageExists as ex: + except rbd.ImageExists: self.logger.exception(f"Image {pool_name}/{image_name} was created just now") raise rbd.ImageExists(f"Image {pool_name}/{image_name} was just created by someone else, please retry", errno = errno.EAGAIN) - except Exception as ex: + except Exception: self.logger.exception(f"Can't create image {pool_name}/{image_name}") - raise ex + raise return True @@ -185,7 +186,7 @@ def get_image_size(self, pool_name, image_name) -> int: with rados.Rados(conffile=self.ceph_conf, rados_id=self.rados_id) as cluster: with cluster.open_ioctx(pool_name) as ioctx: - rbd_inst = rbd.RBD() + rbd.RBD() try: with rbd.Image(ioctx, image_name) as img: image_size = img.size() diff --git a/control/discovery.py b/control/discovery.py index eae1a5048..7a93789db 100644 --- a/control/discovery.py +++ b/control/discovery.py @@ -8,15 +8,12 @@ # import argparse -import grpc import json from .config import GatewayConfig from .state import GatewayState, LocalGatewayState, OmapGatewayState, GatewayStateHandler from .utils import GatewayLogger -from .proto import gateway_pb2 as pb2 -import rados -from typing import Dict, Optional +from typing import Dict import socket import threading @@ -27,8 +24,7 @@ import selectors import os from dataclasses import dataclass, field -from ctypes import Structure, LittleEndianStructure, c_bool, c_ubyte, c_uint8, c_uint16, c_uint32, c_uint64, c_float -from google.protobuf import json_format +from ctypes import LittleEndianStructure, c_ubyte, c_uint8, c_uint16, c_uint32, c_uint64 # NVMe tcp pdu type class NVME_TCP_PDU(enum.IntFlag): @@ -107,6 +103,7 @@ class NVMF_TREQ_SECURE_CHANNEL(enum.IntFlag): REQUIRED = 0x1 NOT_REQUIRED = 0x2 + # maximum number of connections MAX_CONNECTION = 10240 @@ -252,7 +249,7 @@ class NVMeIdentify(AutoSerializableStructure): ] # for set feature, keep alive and async -class CqeNVMe(AutoSerializableStructure): +class CqeNVMe(AutoSerializableStructure): _fields_ = [ ("dword0", c_uint32), ("dword1", c_uint32), @@ -345,28 +342,28 @@ def __exit__(self, exc_type, exc_value, traceback): for key in self.conn_vals: try: self.selector.unregister(self.conn_vals[key].connection) - except Except as ex: + except Exception: pass try: self.conn_vals[key].connection.close() - except Except as ex: + except Exception: pass self.conn_vals = {} if self.sock: try: self.selector.unregister(self.sock) - except Exception as ex: + except Exception: pass try: self.sock.close() - except Exception as ex: + except Exception: pass self.sock = None try: self.selector.close() - except Exception as ex: + except Exception: pass self.selector = None @@ -407,18 +404,18 @@ def reply_fc_cmd_connect(self, conn, data, cmd_id): self.logger.debug("handle connect request.") self_conn = self.conn_vals[conn.fileno()] - hf_nvmeof_cmd_connect_rsvd1 = struct.unpack_from('<19B', data, 13) + hf_nvmeof_cmd_connect_rsvd1 = struct.unpack_from('<19B', data, 13) # noqa: F841 SIGL1 = struct.unpack_from('> 8) & 0x1F - get_logpage_lsi = nvme_get_logpage_dword11 >> 16 - get_logpage_uid_idx = nvme_get_logpage_dword14 & 0x3F + get_logpage_lsp = (nvme_get_logpage_dword10 >> 8) & 0x1F # noqa: F841 + get_logpage_lsi = nvme_get_logpage_dword11 >> 16 # noqa: F841 + get_logpage_uid_idx = nvme_get_logpage_dword14 & 0x3F # noqa: F841 if get_logpage_lid != 0x70: self.logger.error("request type error, not discovery request.") @@ -747,7 +738,6 @@ def reply_get_log_page(self, conn, data, cmd_id): allow_listeners = self_conn.allow_listeners if len(allow_listeners) == 0: for host in hosts: - a = host["host_nqn"] if host["host_nqn"] == '*' or host["host_nqn"] == hostnqn: for listener in listeners: # TODO: It is better to change nqn in the "listener" @@ -784,22 +774,22 @@ def reply_get_log_page(self, conn, data, cmd_id): log_entry.asqsz = 128 # transport service indentifier str_trsvcid = str(allow_listeners[log_entry_counter]["trsvcid"]) - log_entry.trsvcid = (c_ubyte * 32)(*[c_ubyte(x) for x \ + log_entry.trsvcid = (c_ubyte * 32)(*[c_ubyte(x) for x in str_trsvcid.encode()]) log_entry.trsvcid[len(str_trsvcid):] = \ [c_ubyte(0x20)] * (32 - len(str_trsvcid)) # NVM subsystem qualified name - log_entry.subnqn = (c_ubyte * 256)(*[c_ubyte(x) for x \ + log_entry.subnqn = (c_ubyte * 256)(*[c_ubyte(x) for x in allow_listeners[log_entry_counter]["nqn"].encode()]) log_entry.subnqn[len(allow_listeners[log_entry_counter]["nqn"]):] = \ [c_ubyte(0x00)] * (256 - len(allow_listeners[log_entry_counter]["nqn"])) # Transport address - log_entry.traddr = (c_ubyte * 256)(*[c_ubyte(x) for x \ + log_entry.traddr = (c_ubyte * 256)(*[c_ubyte(x) for x in allow_listeners[log_entry_counter]["traddr"].encode()]) log_entry.traddr[len(allow_listeners[log_entry_counter]["traddr"]):] = \ [c_ubyte(0x20)] * (256 - len(allow_listeners[log_entry_counter]["traddr"])) - self_conn.log_page[1024*(log_entry_counter+1): \ + self_conn.log_page[1024*(log_entry_counter+1): 1024*(log_entry_counter+2)] = log_entry log_entry_counter += 1 else: @@ -850,21 +840,21 @@ def reply_keep_alive(self, conn, data, cmd_id): self.logger.debug("handle keep alive request.") self_conn = self.conn_vals[conn.fileno()] nvme_sgl = struct.unpack_from('<16B', data, 32) - nvme_sgl_desc_type = nvme_sgl[15] & 0xF0 - nvme_sgl_desc_sub_type = nvme_sgl[15] & 0x0F - nvme_keep_alive_dword10 = struct.unpack_from(' None: self.set_group_id = set_group_id - def group_id(self, request: monitor_pb2.group_id_req, context = None) -> Empty: + def group_id(self, request: monitor_pb2.group_id_req, context=None) -> Empty: self.set_group_id(request.id) return Empty() + class SubsystemHostAuth: MAX_PSK_KEY_NAME_LENGTH = 200 # taken from SPDK SPDK_TLS_PSK_MAX_LEN @@ -162,6 +166,7 @@ def get_subsystem_dhchap_key(self, subsys) -> str: key = self.subsys_dhchap_key[subsys] return key + class NamespaceInfo: def __init__(self, nsid, bdev, uuid, anagrpid, auto_visible): self.nsid = nsid @@ -204,6 +209,7 @@ def host_count(self): def set_ana_group_id(self, anagrpid): self.anagrpid = anagrpid + class NamespacesLocalList: EMPTY_NAMESPACE = NamespaceInfo(None, None, None, 0, False) @@ -225,7 +231,7 @@ def add_namespace(self, nqn, nsid, bdev, uuid, anagrpid, auto_visible): bdev = GatewayService.find_unique_bdev_name(uuid) self.namespace_list[nqn][nsid] = NamespaceInfo(nsid, bdev, uuid, anagrpid, auto_visible) - def find_namespace(self, nqn, nsid, uuid = None) -> NamespaceInfo: + def find_namespace(self, nqn, nsid, uuid=None) -> NamespaceInfo: if nqn not in self.namespace_list: return NamespacesLocalList.EMPTY_NAMESPACE @@ -242,7 +248,7 @@ def find_namespace(self, nqn, nsid, uuid = None) -> NamespaceInfo: return NamespacesLocalList.EMPTY_NAMESPACE - def get_namespace_count(self, nqn, auto_visible = None, min_hosts = 0) -> int: + def get_namespace_count(self, nqn, auto_visible=None, min_hosts=0) -> int: if nqn and nqn not in self.namespace_list: return 0 @@ -282,7 +288,7 @@ def get_all_namespaces_by_ana_group_id(self, anagrpid): if ns.empty(): continue if ns.anagrpid == anagrpid: - ns_list.append((nsid, nqn))#list of tupples + ns_list.append((nsid, nqn)) # list of tupples return ns_list def get_ana_group_id_by_nsid_subsys(self, nqn, nsid): @@ -295,7 +301,6 @@ def get_ana_group_id_by_nsid_subsys(self, nqn, nsid): return 0 return ns.anagrpid - def get_subsys_namespaces_by_ana_group_id(self, nqn, anagrpid): ns_list = [] if nqn not in self.namespace_list: @@ -310,6 +315,7 @@ def get_subsys_namespaces_by_ana_group_id(self, nqn, anagrpid): return ns_list + class GatewayService(pb2_grpc.GatewayServicer): """Implements gateway service interface. @@ -384,7 +390,7 @@ def __init__(self, config: GatewayConfig, gateway_state: GatewayStateHandler, rp self.ana_grp_state[i] = pb2.ana_state.INACCESSIBLE self.cluster_nonce = {} self.bdev_cluster = {} - self.bdev_params = {} + self.bdev_params = {} self.subsystem_nsid_bdev_and_uuid = NamespacesLocalList() self.subsystem_listeners = defaultdict(set) self._init_cluster_context() @@ -393,7 +399,7 @@ def __init__(self, config: GatewayConfig, gateway_state: GatewayStateHandler, rp self.up_and_running = True self.rebalance = Rebalance(self) - def get_directories_for_key_file(self, key_type : str, subsysnqn : str, create_dir : bool = False) -> []: + def get_directories_for_key_file(self, key_type: str, subsysnqn: str, create_dir: bool = False) -> []: tmp_dirs = [] dir_prefix = f"{key_type}_{subsysnqn}_" @@ -419,13 +425,13 @@ def get_directories_for_key_file(self, key_type : str, subsysnqn : str, create_d return None return [tmp_dir_name] - def create_host_key_file(self, key_type : str, subsysnqn : str, hostnqn : str, key_value : str) -> str: + def create_host_key_file(self, key_type: str, subsysnqn: str, hostnqn: str, key_value: str) -> str: assert subsysnqn, "Subsystem NQN can't be empty" assert hostnqn, "Host NQN can't be empty" assert key_type, "Key type can't be empty" assert key_value, "Key value can't be empty" - tmp_dir_names = self.get_directories_for_key_file(key_type, subsysnqn, create_dir = True) + tmp_dir_names = self.get_directories_for_key_file(key_type, subsysnqn, create_dir=True) if not tmp_dir_names: return None @@ -443,7 +449,7 @@ def create_host_key_file(self, key_type : str, subsysnqn : str, hostnqn : str, k with open(file_fd, "wt") as f: f.write(key_value) except Exception: - self.logger.exception(f"Error creating file") + self.logger.exception("Error creating file") try: os.remove(filepath) except Exception: @@ -451,17 +457,17 @@ def create_host_key_file(self, key_type : str, subsysnqn : str, hostnqn : str, k return None return filepath - def create_host_psk_file(self, subsysnqn : str, hostnqn : str, key_value : str) -> str: + def create_host_psk_file(self, subsysnqn: str, hostnqn: str, key_value: str) -> str: return self.create_host_key_file(self.PSK_PREFIX, subsysnqn, hostnqn, key_value) - def create_host_dhchap_file(self, subsysnqn : str, hostnqn : str, key_value : str) -> str: + def create_host_dhchap_file(self, subsysnqn: str, hostnqn: str, key_value: str) -> str: return self.create_host_key_file(self.DHCHAP_PREFIX, subsysnqn, hostnqn, key_value) - def remove_host_key_file(self, key_type : str, subsysnqn : str, hostnqn : str) -> None: + def remove_host_key_file(self, key_type: str, subsysnqn: str, hostnqn: str) -> None: assert key_type, "Key type can't be empty" assert subsysnqn, "Subsystem NQN can't be empty" - tmp_dir_names = self.get_directories_for_key_file(key_type, subsysnqn, create_dir = False) + tmp_dir_names = self.get_directories_for_key_file(key_type, subsysnqn, create_dir=False) if not tmp_dir_names: return @@ -469,7 +475,7 @@ def remove_host_key_file(self, key_type : str, subsysnqn : str, hostnqn : str) - if not hostnqn: for one_tmp_dir in tmp_dir_names: try: - shutil.rmtree(one_tmp_dir, ignore_errors = True) + shutil.rmtree(one_tmp_dir, ignore_errors=True) except Exception: pass return @@ -484,27 +490,27 @@ def remove_host_key_file(self, key_type : str, subsysnqn : str, hostnqn : str) - self.logger.exception(f"Error deleting file {f.name}") pass - def remove_host_psk_file(self, subsysnqn : str, hostnqn : str) -> None: + def remove_host_psk_file(self, subsysnqn: str, hostnqn: str) -> None: self.remove_host_key_file(self.PSK_PREFIX, subsysnqn, hostnqn) - def remove_host_dhchap_file(self, subsysnqn : str, hostnqn : str) -> None: + def remove_host_dhchap_file(self, subsysnqn: str, hostnqn: str) -> None: self.remove_host_key_file(self.DHCHAP_PREFIX, subsysnqn, hostnqn) - def remove_all_host_key_files(self, subsysnqn : str, hostnqn : str) -> None: + def remove_all_host_key_files(self, subsysnqn: str, hostnqn: str) -> None: self.remove_host_psk_file(subsysnqn, hostnqn) self.remove_host_dhchap_file(subsysnqn, hostnqn) - def remove_all_subsystem_key_files(self, subsysnqn : str) -> None: + def remove_all_subsystem_key_files(self, subsysnqn: str) -> None: self.remove_all_host_key_files(subsysnqn, None) @staticmethod - def construct_key_name_for_keyring(subsysnqn : str, hostnqn : str, prefix : str = None) -> str: + def construct_key_name_for_keyring(subsysnqn: str, hostnqn: str, prefix: str = None) -> str: key_name = hashlib.sha256(subsysnqn.encode()).hexdigest() + "_" + hashlib.sha256(hostnqn.encode()).hexdigest() if prefix: key_name = prefix + "_" + key_name return key_name - def remove_key_from_keyring(self, key_type : str, subsysnqn : str, hostnqn : str) -> None: + def remove_key_from_keyring(self, key_type: str, subsysnqn: str, hostnqn: str) -> None: assert self.rpc_lock.locked(), "RPC is unlocked when calling remove_key_from_keyring()" key_name = GatewayService.construct_key_name_for_keyring(subsysnqn, hostnqn, key_type) try: @@ -512,21 +518,21 @@ def remove_key_from_keyring(self, key_type : str, subsysnqn : str, hostnqn : str except Exception: pass - def remove_psk_key_from_keyring(self, subsysnqn : str, hostnqn : str) -> None: + def remove_psk_key_from_keyring(self, subsysnqn: str, hostnqn: str) -> None: self.remove_key_from_keyring(self.PSK_PREFIX, subsysnqn, hostnqn) - def remove_dhchap_key_from_keyring(self, subsysnqn : str, hostnqn : str) -> None: + def remove_dhchap_key_from_keyring(self, subsysnqn: str, hostnqn: str) -> None: self.remove_key_from_keyring(self.DHCHAP_PREFIX, subsysnqn, hostnqn) - def remove_dhchap_controller_key_from_keyring(self, subsysnqn : str, hostnqn : str) -> None: + def remove_dhchap_controller_key_from_keyring(self, subsysnqn: str, hostnqn: str) -> None: self.remove_key_from_keyring(self.DHCHAP_CONTROLLER_PREFIX, subsysnqn, hostnqn) - def remove_all_host_keys_from_keyring(self, subsysnqn : str, hostnqn : str) -> None: + def remove_all_host_keys_from_keyring(self, subsysnqn: str, hostnqn: str) -> None: self.remove_psk_key_from_keyring(subsysnqn, hostnqn) self.remove_dhchap_key_from_keyring(subsysnqn, hostnqn) self.remove_dhchap_controller_key_from_keyring(subsysnqn, hostnqn) - def remove_all_subsystem_keys_from_keyring(self, subsysnqn : str) -> None: + def remove_all_subsystem_keys_from_keyring(self, subsysnqn: str) -> None: assert self.rpc_lock.locked(), "RPC is unlocked when calling remove_all_subsystem_keys_from_keyring()" try: key_list = rpc_keyring.keyring_get_keys(self.spdk_rpc_client) @@ -544,8 +550,9 @@ def remove_all_subsystem_keys_from_keyring(self, subsysnqn : str) -> None: continue if not key_name or not key_path: continue - if (key_path.startswith(f"{self.KEYS_DIR}/{self.PSK_PREFIX}_{subsysnqn}_") or - key_path.startswith(f"{self.KEYS_DIR}/{self.DHCHAP_PREFIX}_{subsysnqn}_")): + should_remove = (key_path.startswith(f"{self.KEYS_DIR}/{self.PSK_PREFIX}_{subsysnqn}_") or + key_path.startswith(f"{self.KEYS_DIR}/{self.DHCHAP_PREFIX}_{subsysnqn}_")) + if should_remove: try: rpc_keyring.keyring_file_remove_key(self.spdk_rpc_client, key_name) except Exception: @@ -567,19 +574,19 @@ def parse_json_exeption(self, ex): try: resp_index = ex.message.find(json_error_text) if resp_index >= 0: - resp_str = ex.message[resp_index + len(json_error_text) :] + resp_str = ex.message[resp_index + len(json_error_text):] resp_index = resp_str.find("response:") if resp_index >= 0: - resp_str = resp_str[resp_index + len("response:") :] + resp_str = resp_str[resp_index + len("response:"):] resp = json.loads(resp_str) except Exception: - self.logger.exception(f"Got exception parsing JSON exception") + self.logger.exception("Got exception parsing JSON exception") pass if resp: if resp["code"] < 0: resp["code"] = -resp["code"] else: - resp={} + resp = {} if "timeout" in ex.message.lower(): resp["code"] = errno.ETIMEDOUT else: @@ -625,13 +632,13 @@ def _put_cluster(self, name: str) -> None: if self.clusters[anagrp][name] == 0: ret = rpc_bdev.bdev_rbd_unregister_cluster( self.spdk_rpc_client, - name = name + name=name ) self.logger.info(f"Free cluster {name=} {ret=}") assert ret self.clusters[anagrp].pop(name) - else : - self.logger.info(f"put_cluster {name=} number bdevs: {self.clusters[anagrp][name]}") + else: + self.logger.info(f"put_cluster {name=} number bdevs: {self.clusters[anagrp][name]}") return assert False, f"Cluster {name} is not found" # we should find the cluster in our state @@ -650,9 +657,9 @@ def _alloc_cluster(self, anagrp: int) -> str: name = self._alloc_cluster_name(anagrp) nonce = rpc_bdev.bdev_rbd_register_cluster( self.spdk_rpc_client, - name = name, - user_id = self.rados_id, - core_mask = self.librbd_core_mask, + name=name, + user_id=self.rados_id, + core_mask=self.librbd_core_mask, ) with self.shared_state_lock: self.logger.info(f"Allocated cluster {name=} {nonce=} {anagrp=}") @@ -680,7 +687,8 @@ def execute_grpc_function(self, func, request, context): return self.omap_lock.execute_omap_locking_function(self._grpc_function_with_lock, func, request, context) - def create_bdev(self, anagrp: int, name, uuid, rbd_pool_name, rbd_image_name, block_size, create_image, rbd_image_size, context, peer_msg = ""): + def create_bdev(self, anagrp: int, name, uuid, rbd_pool_name, rbd_image_name, + block_size, create_image, rbd_image_size, context, peer_msg=""): """Creates a bdev from an RBD image.""" if create_image: @@ -694,7 +702,7 @@ def create_bdev(self, anagrp: int, name, uuid, rbd_pool_name, rbd_image_name, bl if block_size == 0: return BdevStatus(status=errno.EINVAL, - error_message=f"Failure creating bdev {name}: block size can't be zero") + error_message=f"Failure creating bdev {name}: block size can't be zero") if create_image: if rbd_image_size <= 0: @@ -706,7 +714,7 @@ def create_bdev(self, anagrp: int, name, uuid, rbd_pool_name, rbd_image_name, bl rc = self.ceph_utils.pool_exists(rbd_pool_name) if not rc: return BdevStatus(status=errno.ENODEV, - error_message=f"Failure creating bdev {name}: RBD pool {rbd_pool_name} doesn't exist") + error_message=f"Failure creating bdev {name}: RBD pool {rbd_pool_name} doesn't exist") try: rc = self.ceph_utils.create_image(rbd_pool_name, rbd_image_name, rbd_image_size) @@ -730,7 +738,7 @@ def create_bdev(self, anagrp: int, name, uuid, rbd_pool_name, rbd_image_name, bl return BdevStatus(status=errcode, error_message=f"Failure creating bdev {name}: {errmsg}") try: - cluster_name=self._get_cluster(anagrp) + cluster_name = self._get_cluster(anagrp) bdev_name = rpc_bdev.bdev_rbd_create( self.spdk_rpc_client, name=name, @@ -742,7 +750,8 @@ def create_bdev(self, anagrp: int, name, uuid, rbd_pool_name, rbd_image_name, bl ) with self.shared_state_lock: self.bdev_cluster[name] = cluster_name - self.bdev_params[name] = {'uuid':uuid, 'pool_name':rbd_pool_name, 'image_name':rbd_image_name, 'image_size':rbd_image_size, 'block_size': block_size} + self.bdev_params[name] = {'uuid': uuid, 'pool_name': rbd_pool_name, 'image_name': rbd_image_name, + 'image_size': rbd_image_size, 'block_size': block_size} self.logger.debug(f"bdev_rbd_create: {bdev_name}, cluster_name {cluster_name}") except Exception as ex: @@ -767,7 +776,7 @@ def create_bdev(self, anagrp: int, name, uuid, rbd_pool_name, rbd_image_name, bl return BdevStatus(status=0, error_message=os.strerror(0), bdev_name=name) - def resize_bdev(self, bdev_name, new_size, peer_msg = ""): + def resize_bdev(self, bdev_name, new_size, peer_msg=""): """Resizes a bdev.""" self.logger.info(f"Received request to resize bdev {bdev_name} to {new_size} MiB{peer_msg}") @@ -913,7 +922,7 @@ def get_peer_message(self, context) -> str: addr_fam = "" return f", client address: {addr_fam} {addr}" except Exception: - self.logger.exception(f"Got exception trying to get peer's address") + self.logger.exception("Got exception trying to get peer's address") return "" @@ -928,13 +937,13 @@ def create_subsystem_safe(self, request, context): if not request.enable_ha: errmsg = f"{create_subsystem_error_prefix}: HA must be enabled for subsystems" - self.logger.error(f"{errmsg}") - return pb2.subsys_status(status = errno.EINVAL, error_message = errmsg, nqn = request.subsystem_nqn) + self.logger.error(errmsg) + return pb2.subsys_status(status=errno.EINVAL, error_message=errmsg, nqn=request.subsystem_nqn) if not request.subsystem_nqn: - errmsg = f"Failure creating subsystem, missing subsystem NQN" - self.logger.error(f"{errmsg}") - return pb2.subsys_status(status = errno.EINVAL, error_message = errmsg, nqn = request.subsystem_nqn) + errmsg = "Failure creating subsystem, missing subsystem NQN" + self.logger.error(errmsg) + return pb2.subsys_status(status=errno.EINVAL, error_message=errmsg, nqn=request.subsystem_nqn) if not request.max_namespaces: request.max_namespaces = self.max_namespaces_per_subsystem @@ -947,29 +956,29 @@ def create_subsystem_safe(self, request, context): errmsg = "" if not GatewayState.is_key_element_valid(request.subsystem_nqn): errmsg = f"{create_subsystem_error_prefix}: Invalid NQN \"{request.subsystem_nqn}\", contains invalid characters" - self.logger.error(f"{errmsg}") - return pb2.subsys_status(status = errno.EINVAL, error_message = errmsg, nqn = request.subsystem_nqn) + self.logger.error(errmsg) + return pb2.subsys_status(status=errno.EINVAL, error_message=errmsg, nqn=request.subsystem_nqn) if self.verify_nqns: rc = GatewayUtils.is_valid_nqn(request.subsystem_nqn) if rc[0] != 0: errmsg = f"{create_subsystem_error_prefix}: {rc[1]}" - self.logger.error(f"{errmsg}") - return pb2.subsys_status(status = rc[0], error_message = errmsg, nqn = request.subsystem_nqn) + self.logger.error(errmsg) + return pb2.subsys_status(status=rc[0], error_message=errmsg, nqn=request.subsystem_nqn) if GatewayUtils.is_discovery_nqn(request.subsystem_nqn): errmsg = f"{create_subsystem_error_prefix}: Can't create a discovery subsystem" - self.logger.error(f"{errmsg}") - return pb2.subsys_status(status = errno.EINVAL, error_message = errmsg, nqn = request.subsystem_nqn) + self.logger.error(errmsg) + return pb2.subsys_status(status=errno.EINVAL, error_message=errmsg, nqn=request.subsystem_nqn) if len(self.subsys_max_ns) >= self.max_subsystems: errmsg = f"{create_subsystem_error_prefix}: Maximal number of subsystems ({self.max_subsystems}) has already been reached" - self.logger.error(f"{errmsg}") - return pb2.subsys_status(status = errno.E2BIG, error_message = errmsg, nqn = request.subsystem_nqn) + self.logger.error(errmsg) + return pb2.subsys_status(status=errno.E2BIG, error_message=errmsg, nqn=request.subsystem_nqn) if context: if request.no_group_append or not self.gateway_group: - self.logger.info(f"Subsystem NQN will not be changed") + self.logger.info("Subsystem NQN will not be changed") else: group_name_to_use = self.gateway_group.replace(GatewayState.OMAP_KEY_DELIMITER, "-") request.subsystem_nqn += f".{group_name_to_use}" @@ -994,15 +1003,15 @@ def create_subsystem_safe(self, request, context): subsys_using_serial = None subsys_already_exists = self.subsystem_already_exists(context, request.subsystem_nqn) if subsys_already_exists: - errmsg = f"Subsystem already exists" + errmsg = "Subsystem already exists" else: subsys_using_serial = self.serial_number_already_used(context, request.serial_number) if subsys_using_serial: errmsg = f"Serial number {request.serial_number} is already used by subsystem {subsys_using_serial}" if subsys_already_exists or subsys_using_serial: errmsg = f"{create_subsystem_error_prefix}: {errmsg}" - self.logger.error(f"{errmsg}") - return pb2.subsys_status(status=errno.EEXIST, error_message=errmsg, nqn = request.subsystem_nqn) + self.logger.error(errmsg) + return pb2.subsys_status(status=errno.EEXIST, error_message=errmsg, nqn=request.subsystem_nqn) ret = rpc_nvmf.nvmf_create_subsystem( self.spdk_rpc_client, nqn=request.subsystem_nqn, @@ -1011,7 +1020,7 @@ def create_subsystem_safe(self, request, context): max_namespaces=request.max_namespaces, min_cntlid=min_cntlid, max_cntlid=max_cntlid, - ana_reporting = True, + ana_reporting=True, ) self.subsys_max_ns[request.subsystem_nqn] = request.max_namespaces if request.dhchap_key: @@ -1025,12 +1034,12 @@ def create_subsystem_safe(self, request, context): if resp: status = resp["code"] errmsg = f"{create_subsystem_error_prefix}: {resp['message']}" - return pb2.subsys_status(status=status, error_message=errmsg, nqn = request.subsystem_nqn) + return pb2.subsys_status(status=status, error_message=errmsg, nqn=request.subsystem_nqn) # Just in case SPDK failed with no exception if not ret: self.logger.error(create_subsystem_error_prefix) - return pb2.subsys_status(status=errno.EINVAL, error_message=create_subsystem_error_prefix, nqn = request.subsystem_nqn) + return pb2.subsys_status(status=errno.EINVAL, error_message=create_subsystem_error_prefix, nqn=request.subsystem_nqn) if context: # Update gateway state @@ -1042,9 +1051,9 @@ def create_subsystem_safe(self, request, context): errmsg = f"Error persisting subsystem {request.subsystem_nqn}" self.logger.exception(errmsg) errmsg = f"{errmsg}:\n{ex}" - return pb2.subsys_status(status=errno.EINVAL, error_message=errmsg, nqn = request.subsystem_nqn) + return pb2.subsys_status(status=errno.EINVAL, error_message=errmsg, nqn=request.subsystem_nqn) - return pb2.subsys_status(status=0, error_message=os.strerror(0), nqn = request.subsystem_nqn) + return pb2.subsys_status(status=0, error_message=os.strerror(0), nqn=request.subsystem_nqn) def create_subsystem(self, request, context=None): return self.execute_grpc_function(self.create_subsystem_safe, request, context) @@ -1130,7 +1139,7 @@ def delete_subsystem_safe(self, request, context): # Just in case SPDK failed with no exception if not ret: self.logger.error(delete_subsystem_error_prefix) - self.remove_subsystem_from_state( request.subsystem_nqn, context) + self.remove_subsystem_from_state(request.subsystem_nqn, context) return pb2.req_status(status=errno.EINVAL, error_message=delete_subsystem_error_prefix) return self.remove_subsystem_from_state(request.subsystem_nqn, context) @@ -1143,21 +1152,21 @@ def delete_subsystem(self, request, context=None): self.logger.info(f"Received request to delete subsystem {request.subsystem_nqn}, context: {context}{peer_msg}") if not request.subsystem_nqn: - errmsg = f"Failure deleting subsystem, missing subsystem NQN" - self.logger.error(f"{errmsg}") - return pb2.req_status(status = errno.EINVAL, error_message = errmsg) + errmsg = "Failure deleting subsystem, missing subsystem NQN" + self.logger.error(errmsg) + return pb2.req_status(status=errno.EINVAL, error_message=errmsg) if self.verify_nqns: rc = GatewayUtils.is_valid_nqn(request.subsystem_nqn) if rc[0] != 0: errmsg = f"{delete_subsystem_error_prefix}: {rc[1]}" - self.logger.error(f"{errmsg}") - return pb2.req_status(status = rc[0], error_message = errmsg) + self.logger.error(errmsg) + return pb2.req_status(status=rc[0], error_message=errmsg) if GatewayUtils.is_discovery_nqn(request.subsystem_nqn): errmsg = f"{delete_subsystem_error_prefix}: Can't delete a discovery subsystem" - self.logger.error(f"{errmsg}") - return pb2.req_status(status = errno.EINVAL, error_message = errmsg) + self.logger.error(errmsg) + return pb2.req_status(status=errno.EINVAL, error_message=errmsg) ns_list = [] if context: @@ -1215,9 +1224,9 @@ def create_namespace(self, subsystem_nqn, bdev_name, nsid, anagrpid, uuid, auto_ nsid_msg = f" using NSID {nsid}" if not subsystem_nqn: - errmsg = f"Failure adding namespace, missing subsystem NQN" - self.logger.error(f"{errmsg}") - return pb2.nsid_status(status=errno.EINVAL, error_message = errmsg) + errmsg = "Failure adding namespace, missing subsystem NQN" + self.logger.error(errmsg) + return pb2.nsid_status(status=errno.EINVAL, error_message=errmsg) add_namespace_error_prefix = f"Failure adding namespace{nsid_msg} to {subsystem_nqn}" @@ -1240,30 +1249,30 @@ def create_namespace(self, subsystem_nqn, bdev_name, nsid, anagrpid, uuid, auto_ return pb2.nsid_status(status=errno.EINVAL, error_message=errmsg) if not auto_visible and self.subsystem_nsid_bdev_and_uuid.get_namespace_count(subsystem_nqn, - False, 0) >= self.max_namespaces_with_netmask: + False, 0) >= self.max_namespaces_with_netmask: errmsg = f"{add_namespace_error_prefix}: Maximal number of namespaces which are only visible to selected hosts ({self.max_namespaces_with_netmask}) has already been reached" - self.logger.error(f"{errmsg}") + self.logger.error(errmsg) return pb2.req_status(status=errno.E2BIG, error_message=errmsg) if nsid and nsid > self.subsys_max_ns[subsystem_nqn]: errmsg = f"{add_namespace_error_prefix}: Requested NSID {nsid} is bigger than the maximal one ({self.subsys_max_ns[subsystem_nqn]})" - self.logger.error(f"{errmsg}") + self.logger.error(errmsg) return pb2.req_status(status=errno.E2BIG, error_message=errmsg) if not nsid and self.subsystem_nsid_bdev_and_uuid.get_namespace_count(subsystem_nqn, - None, 0) >= self.subsys_max_ns[subsystem_nqn]: + None, 0) >= self.subsys_max_ns[subsystem_nqn]: errmsg = f"{add_namespace_error_prefix}: Subsystem's maximal number of namespaces ({self.subsys_max_ns[subsystem_nqn]}) has already been reached" - self.logger.error(f"{errmsg}") + self.logger.error(errmsg) return pb2.req_status(status=errno.E2BIG, error_message=errmsg) if self.subsystem_nsid_bdev_and_uuid.get_namespace_count(None, None, 0) >= self.max_namespaces: errmsg = f"{add_namespace_error_prefix}: Maximal number of namespaces ({self.max_namespaces}) has already been reached" - self.logger.error(f"{errmsg}") + self.logger.error(errmsg) return pb2.req_status(status=errno.E2BIG, error_message=errmsg) if self.subsystem_nsid_bdev_and_uuid.get_namespace_count(subsystem_nqn, None, 0) >= self.subsys_max_ns[subsystem_nqn]: errmsg = f"{add_namespace_error_prefix}: Maximal number of namespaces per subsystem ({self.subsys_max_ns[subsystem_nqn]}) has already been reached" - self.logger.error(f"{errmsg}") + self.logger.error(errmsg) return pb2.req_status(status=errno.E2BIG, error_message=errmsg) try: @@ -1321,8 +1330,8 @@ def set_ana_state_safe(self, ana_info: pb2.ana_info, context=None): # fill the static gateway dictionary per nqn and grp_id nqn = nas.nqn for gs in nas.states: - self.ana_map[nqn][gs.grp_id] = gs.state - self.ana_grp_state[gs.grp_id] = gs.state + self.ana_map[nqn][gs.grp_id] = gs.state + self.ana_grp_state[gs.grp_id] = gs.state # If this is not set the subsystem was not created yet if nqn not in self.subsys_max_ns: @@ -1371,7 +1380,7 @@ def set_ana_state_safe(self, ana_info: pb2.ana_info, context=None): adrfam=adrfam, ana_state=ana_state, anagrpid=grp_id) - if ana_state == "inaccessible" : + if ana_state == "inaccessible": inaccessible_ana_groups[grp_id] = True self.logger.debug(f"set_ana_state nvmf_subsystem_listener_set_ana_state response {ret=}") if not ret: @@ -1384,10 +1393,10 @@ def set_ana_state_safe(self, ana_info: pb2.ana_info, context=None): return pb2.req_status() return pb2.req_status(status=True) - def choose_anagrpid_for_namespace(self, nsid) ->int: + def choose_anagrpid_for_namespace(self, nsid) -> int: grps_list = self.ceph_utils.get_number_created_gateways(self.gateway_pool, self.gateway_group) for ana_grp in grps_list: - if self.ana_grp_ns_load[ana_grp] == 0: # still no namespaces in this ana-group - probably the new GW added + if self.ana_grp_ns_load[ana_grp] == 0: # still no namespaces in this ana-group - probably the new GW added self.logger.info(f"New GW created: chosen ana group {ana_grp} for ns {nsid} ") return ana_grp min_load = 2000 @@ -1395,7 +1404,7 @@ def choose_anagrpid_for_namespace(self, nsid) ->int: for ana_grp in self.ana_grp_ns_load: if ana_grp in grps_list: self.logger.info(f" ana group {ana_grp} load = {self.ana_grp_ns_load[ana_grp]} ") - if self.ana_grp_ns_load[ana_grp] <= min_load: + if self.ana_grp_ns_load[ana_grp] <= min_load: min_load = self.ana_grp_ns_load[ana_grp] chosen_ana_group = ana_grp self.logger.info(f" ana group {ana_grp} load = {self.ana_grp_ns_load[ana_grp]} set as min {min_load} ") @@ -1406,9 +1415,9 @@ def namespace_add_safe(self, request, context): """Adds a namespace to a subsystem.""" if not request.subsystem_nqn: - errmsg = f"Failure adding namespace, missing subsystem NQN" - self.logger.error(f"{errmsg}") - return pb2.nsid_status(status=errno.EINVAL, error_message = errmsg) + errmsg = "Failure adding namespace, missing subsystem NQN" + self.logger.error(errmsg) + return pb2.nsid_status(status=errno.EINVAL, error_message=errmsg) grps_list = [] anagrp = 0 @@ -1432,14 +1441,14 @@ def namespace_add_safe(self, request, context): ns = self.subsystem_nsid_bdev_and_uuid.find_namespace(request.subsystem_nqn, request.nsid) if not ns.empty(): errmsg = f"Failure adding namespace, NSID {request.nsid} is already in use" - self.logger.error(f"{errmsg}") - return pb2.nsid_status(status=errno.EEXIST, error_message = errmsg) + self.logger.error(errmsg) + return pb2.nsid_status(status=errno.EEXIST, error_message=errmsg) ns = self.subsystem_nsid_bdev_and_uuid.find_namespace(request.subsystem_nqn, None, request.uuid) if not ns.empty(): errmsg = f"Failure adding namespace, UUID {request.uuid} is already in use" - self.logger.error(f"{errmsg}") - return pb2.nsid_status(status=errno.EEXIST, error_message = errmsg) + self.logger.error(errmsg) + return pb2.nsid_status(status=errno.EEXIST, error_message=errmsg) omap_lock = self.omap_lock.get_omap_lock_to_use(context) with omap_lock: @@ -1458,7 +1467,7 @@ def namespace_add_safe(self, request, context): create_image = request.create_image if not context: create_image = False - else: # new namespace + else: # new namespace # If an explicit load balancing group was passed, make sure it exists if request.anagrpid != 0: if request.anagrpid not in grps_list: @@ -1467,7 +1476,7 @@ def namespace_add_safe(self, request, context): self.logger.error(errmsg) return pb2.req_status(status=errno.ENODEV, error_message=errmsg) else: - request.anagrpid = anagrp + request.anagrpid = anagrp anagrp = request.anagrpid ret_bdev = self.create_bdev(anagrp, bdev_name, request.uuid, request.rbd_pool_name, @@ -1478,9 +1487,9 @@ def namespace_add_safe(self, request, context): # Delete the bdev unless there was one already there, just to be on the safe side if ret_bdev.status != errno.EEXIST: ns_bdev = self.get_bdev_info(bdev_name) - if ns_bdev != None: + if ns_bdev is not None: try: - ret_del = self.delete_bdev(bdev_name, peer_msg = peer_msg) + ret_del = self.delete_bdev(bdev_name, peer_msg=peer_msg) self.logger.debug(f"delete_bdev({bdev_name}): {ret_del.status}") except AssertionError: self.logger.exception(f"Got an assert while trying to delete bdev {bdev_name}") @@ -1500,7 +1509,7 @@ def namespace_add_safe(self, request, context): if ret_ns.status != 0: try: - ret_del = self.delete_bdev(bdev_name, peer_msg = peer_msg) + ret_del = self.delete_bdev(bdev_name, peer_msg=peer_msg) if ret_del.status != 0: self.logger.warning(f"Failure {ret_del.status} deleting bdev {bdev_name}: {ret_del.error_message}") except AssertionError: @@ -1541,8 +1550,8 @@ def namespace_change_load_balancing_group_safe(self, request, context): self.logger.info(f"Received {auto_lb_msg} request to change load balancing group for namespace with NSID {request.nsid} in {request.subsystem_nqn} to {request.anagrpid}, context: {context}{peer_msg}") if not request.subsystem_nqn: - errmsg = f"Failure changing load balancing group for namespace, missing subsystem NQN" - self.logger.error(f"{errmsg}") + errmsg = "Failure changing load balancing group for namespace, missing subsystem NQN" + self.logger.error(errmsg) return pb2.req_status(status=errno.EINVAL, error_message=errmsg) if not request.nsid: @@ -1550,7 +1559,7 @@ def namespace_change_load_balancing_group_safe(self, request, context): self.logger.error(errmsg) return pb2.req_status(status=errno.ENODEV, error_message=errmsg) - #below checks are legal only if command is initiated by local cli or is sent from the local rebalance logic. + # below checks are legal only if command is initiated by local cli or is sent from the local rebalance logic. if context: grps_list = self.ceph_utils.get_number_created_gateways(self.gateway_pool, self.gateway_group) if request.anagrpid not in grps_list: @@ -1592,7 +1601,6 @@ def namespace_change_load_balancing_group_safe(self, request, context): nqn=request.subsystem_nqn, nsid=request.nsid, anagrpid=request.anagrpid, - #transit_anagrpid=0, #temporary for spdk 24.05 ) self.logger.debug(f"nvmf_subsystem_set_ns_ana_group: {ret}") except Exception as ex: @@ -1618,7 +1626,7 @@ def namespace_change_load_balancing_group_safe(self, request, context): else: self.ana_grp_subs_load[request.anagrpid][request.subsystem_nqn] = 1 self.logger.debug(f"updated load in grp {request.anagrpid} = {self.ana_grp_ns_load[request.anagrpid]} ") - #here update find_ret.set_ana_group_id(request.anagrpid) + # here update find_ret.set_ana_group_id(request.anagrpid) if not find_ret.empty(): find_ret.set_ana_group_id(request.anagrpid) @@ -1671,8 +1679,8 @@ def namespace_change_visibility_safe(self, request, context): self.logger.info(f"Received request to change the visibility of namespace {request.nsid} in {request.subsystem_nqn} to {vis_txt}, force: {request.force}, context: {context}{peer_msg}") if not request.subsystem_nqn: - errmsg = f"Failure changing visibility for namespace, missing subsystem NQN" - self.logger.error(f"{errmsg}") + errmsg = "Failure changing visibility for namespace, missing subsystem NQN" + self.logger.error(errmsg) return pb2.req_status(status=errno.EINVAL, error_message=errmsg) if not request.nsid: @@ -1823,8 +1831,8 @@ def remove_namespace(self, subsystem_nqn, nsid, context): self.logger.info(f"Received request to remove namespace {nsid} from {subsystem_nqn}{peer_msg}") if GatewayUtils.is_discovery_nqn(subsystem_nqn): - errmsg=f"{namespace_failure_prefix}: Can't remove a namespace from a discovery subsystem" - self.logger.error(f"{errmsg}") + errmsg = f"{namespace_failure_prefix}: Can't remove a namespace from a discovery subsystem" + self.logger.error(errmsg) return pb2.req_status(status=errno.EINVAL, error_message=errmsg) try: @@ -1874,7 +1882,7 @@ def list_namespaces(self, request, context=None): """List namespaces.""" peer_msg = self.get_peer_message(context) - if request.nsid == None or request.nsid == 0: + if request.nsid is None or request.nsid == 0: if request.uuid: nsid_msg = f"namespace with UUID {request.uuid}" else: @@ -1887,8 +1895,8 @@ def list_namespaces(self, request, context=None): self.logger.info(f"Received request to list {nsid_msg} for {request.subsystem}, context: {context}{peer_msg}") if not request.subsystem: - errmsg = f"Failure listing namespaces, missing subsystem NQN" - self.logger.error(f"{errmsg}") + errmsg = "Failure listing namespaces, missing subsystem NQN" + self.logger.error(errmsg) return pb2.namespaces_info(status=errno.EINVAL, error_message=errmsg, subsystem_nqn=request.subsystem, namespaces=[]) with self.rpc_lock: @@ -1896,7 +1904,7 @@ def list_namespaces(self, request, context=None): ret = rpc_nvmf.nvmf_get_subsystems(self.spdk_rpc_client, nqn=request.subsystem) self.logger.debug(f"list_namespaces: {ret}") except Exception as ex: - errmsg = f"Failure listing namespaces" + errmsg = "Failure listing namespaces" self.logger.exception(errmsg) errmsg = f"{errmsg}:\n{ex}" resp = self.parse_json_exeption(ex) @@ -1936,15 +1944,15 @@ def list_namespaces(self, request, context=None): find_ret = self.subsystem_nsid_bdev_and_uuid.find_namespace(request.subsystem, nsid) if find_ret.empty(): self.logger.warning(f"Can't find info of namesapce {nsid} in {request.subsystem}. Visibility status will be inaccurate") - one_ns = pb2.namespace_cli(nsid = nsid, - bdev_name = bdev_name, - uuid = n["uuid"], - load_balancing_group = lb_group, - auto_visible = find_ret.auto_visible, - hosts = find_ret.host_list) + one_ns = pb2.namespace_cli(nsid=nsid, + bdev_name=bdev_name, + uuid=n["uuid"], + load_balancing_group=lb_group, + auto_visible=find_ret.auto_visible, + hosts=find_ret.host_list) with self.rpc_lock: ns_bdev = self.get_bdev_info(bdev_name) - if ns_bdev == None: + if ns_bdev is None: self.logger.warning(f"Can't find namespace's bdev {bdev_name}, will not list bdev's information") else: try: @@ -1955,15 +1963,15 @@ def list_namespaces(self, request, context=None): one_ns.block_size = ns_bdev["block_size"] one_ns.rbd_image_size = ns_bdev["block_size"] * ns_bdev["num_blocks"] assigned_limits = ns_bdev["assigned_rate_limits"] - one_ns.rw_ios_per_second=assigned_limits["rw_ios_per_sec"] - one_ns.rw_mbytes_per_second=assigned_limits["rw_mbytes_per_sec"] - one_ns.r_mbytes_per_second=assigned_limits["r_mbytes_per_sec"] - one_ns.w_mbytes_per_second=assigned_limits["w_mbytes_per_sec"] + one_ns.rw_ios_per_second = assigned_limits["rw_ios_per_sec"] + one_ns.rw_mbytes_per_second = assigned_limits["rw_mbytes_per_sec"] + one_ns.r_mbytes_per_second = assigned_limits["r_mbytes_per_sec"] + one_ns.w_mbytes_per_second = assigned_limits["w_mbytes_per_sec"] except KeyError as err: - self.logger.warning(f"Key {err} is not found, will not list bdev's information") + self.logger.warning(f"Key {err} is not found, will not list bdev's information") pass except Exception: - self.logger.exception(f"{ns_bdev=} parse error") + self.logger.exception(f"{ns_bdev=} parse error") pass namespaces.append(one_ns) break @@ -1971,7 +1979,7 @@ def list_namespaces(self, request, context=None): self.logger.exception(f"{s=} parse error") pass - return pb2.namespaces_info(status = 0, error_message = os.strerror(0), subsystem_nqn=request.subsystem, namespaces=namespaces) + return pb2.namespaces_info(status=0, error_message=os.strerror(0), subsystem_nqn=request.subsystem, namespaces=namespaces) def namespace_get_io_stats(self, request, context=None): """Get namespace's IO stats.""" @@ -1979,13 +1987,13 @@ def namespace_get_io_stats(self, request, context=None): peer_msg = self.get_peer_message(context) self.logger.info(f"Received request to get IO stats for namespace {request.nsid} on {request.subsystem_nqn}, context: {context}{peer_msg}") if not request.nsid: - errmsg = f"Failure getting IO stats for namespace, missing NSID" - self.logger.error(f"{errmsg}") + errmsg = "Failure getting IO stats for namespace, missing NSID" + self.logger.error(errmsg) return pb2.namespace_io_stats_info(status=errno.EINVAL, error_message=errmsg) if not request.subsystem_nqn: errmsg = f"Failure getting IO stats for namespace {request.nsid}, missing subsystem NQN" - self.logger.error(f"{errmsg}") + self.logger.error(errmsg) return pb2.namespace_io_stats_info(status=errno.EINVAL, error_message=errmsg) with self.rpc_lock: @@ -2031,51 +2039,51 @@ def namespace_get_io_stats(self, request, context=None): return pb2.namespace_io_stats_info(status=errno.ENODEV, error_message=f"Failure getting IO stats for namespace {request.nsid} on {request.subsystem_nqn}: No associated block device found") if len(bdevs) > 1: - self.logger.warning(f"More than one associated block device found for namespace, will use the first one") + self.logger.warning("More than one associated block device found for namespace, will use the first one") bdev = bdevs[0] io_errs = [] try: - io_error=bdev["io_error"] + io_error = bdev["io_error"] for err_name in io_error.keys(): one_error = pb2.namespace_io_error(name=err_name, value=io_error[err_name]) io_errs.append(one_error) except Exception: - self.logger.exception(f"failure getting io errors") + self.logger.exception("failure getting io errors") io_stats = pb2.namespace_io_stats_info(status=0, - error_message=os.strerror(0), - subsystem_nqn=request.subsystem_nqn, - nsid=request.nsid, - uuid=uuid, - bdev_name=bdev_name, - tick_rate=ret["tick_rate"], - ticks=ret["ticks"], - bytes_read=bdev["bytes_read"], - num_read_ops=bdev["num_read_ops"], - bytes_written=bdev["bytes_written"], - num_write_ops=bdev["num_write_ops"], - bytes_unmapped=bdev["bytes_unmapped"], - num_unmap_ops=bdev["num_unmap_ops"], - read_latency_ticks=bdev["read_latency_ticks"], - max_read_latency_ticks=bdev["max_read_latency_ticks"], - min_read_latency_ticks=bdev["min_read_latency_ticks"], - write_latency_ticks=bdev["write_latency_ticks"], - max_write_latency_ticks=bdev["max_write_latency_ticks"], - min_write_latency_ticks=bdev["min_write_latency_ticks"], - unmap_latency_ticks=bdev["unmap_latency_ticks"], - max_unmap_latency_ticks=bdev["max_unmap_latency_ticks"], - min_unmap_latency_ticks=bdev["min_unmap_latency_ticks"], - copy_latency_ticks=bdev["copy_latency_ticks"], - max_copy_latency_ticks=bdev["max_copy_latency_ticks"], - min_copy_latency_ticks=bdev["min_copy_latency_ticks"], - io_error=io_errs) + error_message=os.strerror(0), + subsystem_nqn=request.subsystem_nqn, + nsid=request.nsid, + uuid=uuid, + bdev_name=bdev_name, + tick_rate=ret["tick_rate"], + ticks=ret["ticks"], + bytes_read=bdev["bytes_read"], + num_read_ops=bdev["num_read_ops"], + bytes_written=bdev["bytes_written"], + num_write_ops=bdev["num_write_ops"], + bytes_unmapped=bdev["bytes_unmapped"], + num_unmap_ops=bdev["num_unmap_ops"], + read_latency_ticks=bdev["read_latency_ticks"], + max_read_latency_ticks=bdev["max_read_latency_ticks"], + min_read_latency_ticks=bdev["min_read_latency_ticks"], + write_latency_ticks=bdev["write_latency_ticks"], + max_write_latency_ticks=bdev["max_write_latency_ticks"], + min_write_latency_ticks=bdev["min_write_latency_ticks"], + unmap_latency_ticks=bdev["unmap_latency_ticks"], + max_unmap_latency_ticks=bdev["max_unmap_latency_ticks"], + min_unmap_latency_ticks=bdev["min_unmap_latency_ticks"], + copy_latency_ticks=bdev["copy_latency_ticks"], + max_copy_latency_ticks=bdev["max_copy_latency_ticks"], + min_copy_latency_ticks=bdev["min_copy_latency_ticks"], + io_error=io_errs) return io_stats except Exception as ex: - self.logger.exception(f"parse error") + self.logger.exception("parse error") exmsg = str(ex) pass return pb2.namespace_io_stats_info(status=errno.EINVAL, - error_message=f"Failure getting IO stats for namespace {request.nsid} on {request.subsystem_nqn}: Error parsing returned stats:\n{exmsg}") + error_message=f"Failure getting IO stats for namespace {request.nsid} on {request.subsystem_nqn}: Error parsing returned stats:\n{exmsg}") def get_qos_limits_string(self, request): limits_to_set = "" @@ -2098,13 +2106,13 @@ def namespace_set_qos_limits_safe(self, request, context): self.logger.info(f"Received request to set QOS limits for namespace {request.nsid} on {request.subsystem_nqn},{limits_to_set}, context: {context}{peer_msg}") if not request.nsid: - errmsg = f"Failure setting QOS limits for namespace, missing NSID" - self.logger.error(f"{errmsg}") + errmsg = "Failure setting QOS limits for namespace, missing NSID" + self.logger.error(errmsg) return pb2.namespace_io_stats_info(status=errno.EINVAL, error_message=errmsg) if not request.subsystem_nqn: errmsg = f"Failure setting QOS limits for namespace {request.nsid}, missing subsystem NQN" - self.logger.error(f"{errmsg}") + self.logger.error(errmsg) return pb2.namespace_io_stats_info(status=errno.EINVAL, error_message=errmsg) find_ret = self.subsystem_nsid_bdev_and_uuid.find_namespace(request.subsystem_nqn, request.nsid) @@ -2141,13 +2149,13 @@ def namespace_set_qos_limits_safe(self, request, context): # Merge current limits with previous ones, if exist if ns_qos_entry: - if not request.HasField("rw_ios_per_second") and ns_qos_entry.get("rw_ios_per_second") != None: + if not request.HasField("rw_ios_per_second") and ns_qos_entry.get("rw_ios_per_second") is not None: request.rw_ios_per_second = int(ns_qos_entry["rw_ios_per_second"]) - if not request.HasField("rw_mbytes_per_second") and ns_qos_entry.get("rw_mbytes_per_second") != None: + if not request.HasField("rw_mbytes_per_second") and ns_qos_entry.get("rw_mbytes_per_second") is not None: request.rw_mbytes_per_second = int(ns_qos_entry["rw_mbytes_per_second"]) - if not request.HasField("r_mbytes_per_second") and ns_qos_entry.get("r_mbytes_per_second") != None: + if not request.HasField("r_mbytes_per_second") and ns_qos_entry.get("r_mbytes_per_second") is not None: request.r_mbytes_per_second = int(ns_qos_entry["r_mbytes_per_second"]) - if not request.HasField("w_mbytes_per_second") and ns_qos_entry.get("w_mbytes_per_second") != None: + if not request.HasField("w_mbytes_per_second") and ns_qos_entry.get("w_mbytes_per_second") is not None: request.w_mbytes_per_second = int(ns_qos_entry["w_mbytes_per_second"]) limits_to_set = self.get_qos_limits_string(request) @@ -2202,18 +2210,18 @@ def namespace_resize_safe(self, request, context=None): self.logger.info(f"Received request to resize namespace {request.nsid} on {request.subsystem_nqn} to {request.new_size} MiB, context: {context}{peer_msg}") if not request.nsid: - errmsg = f"Failure resizing namespace, missing NSID" - self.logger.error(f"{errmsg}") - return pb2.req_status(status = errno.EINVAL, error_message = errmsg) + errmsg = "Failure resizing namespace, missing NSID" + self.logger.error(errmsg) + return pb2.req_status(status=errno.EINVAL, error_message=errmsg) if not request.subsystem_nqn: errmsg = f"Failure resizing namespace {request.nsid}, missing subsystem NQN" - self.logger.error(f"{errmsg}") - return pb2.req_status(status = errno.EINVAL, error_message = errmsg) + self.logger.error(errmsg) + return pb2.req_status(status=errno.EINVAL, error_message=errmsg) if request.new_size <= 0: errmsg = f"Failure resizing namespace {request.nsid}: New size must be positive" - self.logger.error(f"{errmsg}") + self.logger.error(errmsg) return pb2.req_status(status=errno.EINVAL, error_message=errmsg) find_ret = self.subsystem_nsid_bdev_and_uuid.find_namespace(request.subsystem_nqn, request.nsid) @@ -2245,14 +2253,14 @@ def namespace_delete_safe(self, request, context): """Delete a namespace.""" if not request.nsid: - errmsg = f"Failure deleting namespace, missing NSID" - self.logger.error(f"{errmsg}") - return pb2.req_status(status = errno.EINVAL, error_message = errmsg) + errmsg = "Failure deleting namespace, missing NSID" + self.logger.error(errmsg) + return pb2.req_status(status=errno.EINVAL, error_message=errmsg) if not request.subsystem_nqn: errmsg = f"Failure deleting namespace {request.nsid}, missing subsystem NQN" - self.logger.error(f"{errmsg}") - return pb2.req_status(status = errno.EINVAL, error_message = errmsg) + self.logger.error(errmsg) + return pb2.req_status(status=errno.EINVAL, error_message=errmsg) peer_msg = self.get_peer_message(context) self.logger.info(f"Received request to delete namespace {request.nsid} from {request.subsystem_nqn}, context: {context}{peer_msg}") @@ -2264,7 +2272,7 @@ def namespace_delete_safe(self, request, context): return pb2.req_status(status=errno.ENODEV, error_message=errmsg) bdev_name = find_ret.bdev if not bdev_name: - self.logger.warning(f"Can't find namespace's bdev name, will try to delete namespace anyway") + self.logger.warning("Can't find namespace's bdev name, will try to delete namespace anyway") omap_lock = self.omap_lock.get_omap_lock_to_use(context) with omap_lock: @@ -2275,7 +2283,7 @@ def namespace_delete_safe(self, request, context): self.remove_namespace_from_state(request.subsystem_nqn, request.nsid, context) self.subsystem_nsid_bdev_and_uuid.remove_namespace(request.subsystem_nqn, request.nsid) if bdev_name: - ret_del = self.delete_bdev(bdev_name, peer_msg = peer_msg) + ret_del = self.delete_bdev(bdev_name, peer_msg=peer_msg) if ret_del.status != 0: errmsg = f"Failure deleting namespace {request.nsid} from {request.subsystem_nqn}: {ret_del.error_message}" self.logger.error(errmsg) @@ -2296,18 +2304,18 @@ def namespace_add_host_safe(self, request, context): if not request.nsid: errmsg = f"Failure adding host {request.host_nqn} to namespace on {request.subsystem_nqn}: Missing NSID" - self.logger.error(f"{errmsg}") - return pb2.req_status(status = errno.EINVAL, error_message = errmsg) + self.logger.error(errmsg) + return pb2.req_status(status=errno.EINVAL, error_message=errmsg) if not request.subsystem_nqn: errmsg = f"Failure adding host to namespace {request.nsid}: Missing subsystem NQN" - self.logger.error(f"{errmsg}") - return pb2.req_status(status = errno.EINVAL, error_message = errmsg) + self.logger.error(errmsg) + return pb2.req_status(status=errno.EINVAL, error_message=errmsg) if not request.host_nqn: errmsg = f"Failure adding host to namespace {request.nsid} on {request.subsystem_nqn}: Missing host NQN" - self.logger.error(f"{errmsg}") - return pb2.req_status(status = errno.EINVAL, error_message = errmsg) + self.logger.error(errmsg) + return pb2.req_status(status=errno.EINVAL, error_message=errmsg) # If this is not set the subsystem was not created yet if request.subsystem_nqn not in self.subsys_max_ns: @@ -2317,29 +2325,29 @@ def namespace_add_host_safe(self, request, context): if request.host_nqn == "*": errmsg = f"{failure_prefix}: Host NQN can't be \"*\"" - self.logger.error(f"{errmsg}") + self.logger.error(errmsg) return pb2.req_status(status=errno.EINVAL, error_message=errmsg) if self.verify_nqns: rc = GatewayUtils.is_valid_nqn(request.subsystem_nqn) if rc[0] != 0: errmsg = f"{failure_prefix}: Invalid subsystem NQN: {rc[1]}" - self.logger.error(f"{errmsg}") - return pb2.req_status(status = rc[0], error_message = errmsg) + self.logger.error(errmsg) + return pb2.req_status(status=rc[0], error_message=errmsg) rc = GatewayUtils.is_valid_nqn(request.host_nqn) if rc[0] != 0: errmsg = f"{failure_prefix}: Invalid host NQN: {rc[1]}" - self.logger.error(f"{errmsg}") - return pb2.req_status(status = rc[0], error_message = errmsg) + self.logger.error(errmsg) + return pb2.req_status(status=rc[0], error_message=errmsg) if GatewayUtils.is_discovery_nqn(request.subsystem_nqn): errmsg = f"{failure_prefix}: Subsystem NQN can't be a discovery NQN" - self.logger.error(f"{errmsg}") + self.logger.error(errmsg) return pb2.req_status(status=errno.EINVAL, error_message=errmsg) if GatewayUtils.is_discovery_nqn(request.host_nqn): errmsg = f"{failure_prefix}: Host NQN can't be a discovery NQN" - self.logger.error(f"{errmsg}") + self.logger.error(errmsg) return pb2.req_status(status=errno.EINVAL, error_message=errmsg) find_ret = self.subsystem_nsid_bdev_and_uuid.find_namespace(request.subsystem_nqn, request.nsid) @@ -2350,12 +2358,12 @@ def namespace_add_host_safe(self, request, context): if find_ret.auto_visible: errmsg = f"{failure_prefix}: Namespace is visible to all hosts" - self.logger.error(f"{errmsg}") + self.logger.error(errmsg) return pb2.req_status(status=errno.EINVAL, error_message=errmsg) if find_ret.host_count() >= self.max_hosts_per_namespace: errmsg = f"{failure_prefix}: Maximal host count for namespace ({self.max_hosts_per_namespace}) was already reached" - self.logger.error(f"{errmsg}") + self.logger.error(errmsg) return pb2.req_status(status=errno.E2BIG, error_message=errmsg) omap_lock = self.omap_lock.get_omap_lock_to_use(context) @@ -2403,18 +2411,18 @@ def namespace_delete_host_safe(self, request, context): if not request.nsid: errmsg = f"Failure deleting host {request.host_nqn} from namespace: Missing NSID" - self.logger.error(f"{errmsg}") - return pb2.req_status(status = errno.EINVAL, error_message = errmsg) + self.logger.error(errmsg) + return pb2.req_status(status=errno.EINVAL, error_message=errmsg) if not request.subsystem_nqn: errmsg = f"Failure deleting host {request.host_nqn} from namespace {request.nsid}: Missing subsystem NQN" - self.logger.error(f"{errmsg}") - return pb2.req_status(status = errno.EINVAL, error_message = errmsg) + self.logger.error(errmsg) + return pb2.req_status(status=errno.EINVAL, error_message=errmsg) if not request.host_nqn: errmsg = f"Failure deleting host from namespace {request.nsid} on {request.subsystem_nqn}: Missing host NQN" - self.logger.error(f"{errmsg}") - return pb2.req_status(status = errno.EINVAL, error_message = errmsg) + self.logger.error(errmsg) + return pb2.req_status(status=errno.EINVAL, error_message=errmsg) # If this is not set the subsystem was not created yet if request.subsystem_nqn not in self.subsys_max_ns: @@ -2424,29 +2432,29 @@ def namespace_delete_host_safe(self, request, context): if request.host_nqn == "*": errmsg = f"{failure_prefix}: Host NQN can't be \"*\"" - self.logger.error(f"{errmsg}") + self.logger.error(errmsg) return pb2.req_status(status=errno.EINVAL, error_message=errmsg) if self.verify_nqns: rc = GatewayUtils.is_valid_nqn(request.subsystem_nqn) if rc[0] != 0: errmsg = f"{failure_prefix}: Invalid subsystem NQN: {rc[1]}" - self.logger.error(f"{errmsg}") - return pb2.req_status(status = rc[0], error_message = errmsg) + self.logger.error(errmsg) + return pb2.req_status(status=rc[0], error_message=errmsg) rc = GatewayUtils.is_valid_nqn(request.host_nqn) if rc[0] != 0: errmsg = f"{failure_prefix}: Invalid host NQN: {rc[1]}" - self.logger.error(f"{errmsg}") - return pb2.req_status(status = rc[0], error_message = errmsg) + self.logger.error(errmsg) + return pb2.req_status(status=rc[0], error_message=errmsg) if GatewayUtils.is_discovery_nqn(request.subsystem_nqn): errmsg = f"{failure_prefix}: Subsystem NQN can't be a discovery NQN" - self.logger.error(f"{errmsg}") + self.logger.error(errmsg) return pb2.req_status(status=errno.EINVAL, error_message=errmsg) if GatewayUtils.is_discovery_nqn(request.host_nqn): errmsg = f"{failure_prefix}: Host NQN can't be a discovery NQN" - self.logger.error(f"{errmsg}") + self.logger.error(errmsg) return pb2.req_status(status=errno.EINVAL, error_message=errmsg) find_ret = self.subsystem_nsid_bdev_and_uuid.find_namespace(request.subsystem_nqn, request.nsid) @@ -2457,12 +2465,12 @@ def namespace_delete_host_safe(self, request, context): if find_ret.auto_visible: errmsg = f"{failure_prefix}: Namespace is visible to all hosts" - self.logger.error(f"{errmsg}") + self.logger.error(errmsg) return pb2.req_status(status=errno.EINVAL, error_message=errmsg) if not find_ret.is_host_in_namespace(request.host_nqn): errmsg = f"{failure_prefix}: Host is not found in namespace's host list" - self.logger.error(f"{errmsg}") + self.logger.error(errmsg) return pb2.req_status(status=errno.ENODEV, error_message=errmsg) omap_lock = self.omap_lock.get_omap_lock_to_use(context) @@ -2533,8 +2541,8 @@ def _create_dhchap_key_files(self, subsystem_nqn, host_nqn, dhchap_key, dhchap_c if dhchap_key: dhchap_file = self.create_host_dhchap_file(subsystem_nqn, host_nqn, dhchap_key) if not dhchap_file: - errmsg=f"{err_prefix}: Can't write DH-HMAC-CHAP file" - self.logger.error(f"{errmsg}") + errmsg = f"{err_prefix}: Can't write DH-HMAC-CHAP file" + self.logger.error(errmsg) return (errno.ENOENT, errmsg, None, None, None, None) dhchap_key_name = GatewayService.construct_key_name_for_keyring( subsystem_nqn, host_nqn, GatewayService.DHCHAP_PREFIX) @@ -2543,8 +2551,8 @@ def _create_dhchap_key_files(self, subsystem_nqn, host_nqn, dhchap_key, dhchap_c if dhchap_ctrlr_key: dhchap_ctrlr_file = self.create_host_dhchap_file(subsystem_nqn, host_nqn, dhchap_ctrlr_key) if not dhchap_ctrlr_file: - errmsg=f"{err_prefix}: Can't write DH-HMAC-CHAP controller file" - self.logger.error(f"{errmsg}") + errmsg = f"{err_prefix}: Can't write DH-HMAC-CHAP controller file" + self.logger.error(errmsg) if dhchap_file: self.remove_host_dhchap_file(subsystem_nqn, host_nqn) return (errno.ENOENT, errmsg, None, None, None, None) @@ -2594,23 +2602,23 @@ def add_host_safe(self, request, context): self.logger.info( f"Received request to add host {request.host_nqn} to {request.subsystem_nqn}, psk: {request.psk}, dhchap: {request.dhchap_key}, context: {context}{peer_msg}") - all_host_failure_prefix=f"Failure allowing open host access to {request.subsystem_nqn}" - host_failure_prefix=f"Failure adding host {request.host_nqn} to {request.subsystem_nqn}" + all_host_failure_prefix = f"Failure allowing open host access to {request.subsystem_nqn}" + host_failure_prefix = f"Failure adding host {request.host_nqn} to {request.subsystem_nqn}" if not GatewayState.is_key_element_valid(request.host_nqn): errmsg = f"{host_failure_prefix}: Invalid host NQN \"{request.host_nqn}\", contains invalid characters" - self.logger.error(f"{errmsg}") - return pb2.req_status(status = errno.EINVAL, error_message = errmsg) + self.logger.error(errmsg) + return pb2.req_status(status=errno.EINVAL, error_message=errmsg) if not GatewayState.is_key_element_valid(request.subsystem_nqn): errmsg = f"{host_failure_prefix}: Invalid subsystem NQN \"{request.subsystem_nqn}\", contains invalid characters" - self.logger.error(f"{errmsg}") - return pb2.req_status(status = errno.EINVAL, error_message = errmsg) + self.logger.error(errmsg) + return pb2.req_status(status=errno.EINVAL, error_message=errmsg) if request.host_nqn == "*" and self.host_info.does_subsystem_have_dhchap_key(request.subsystem_nqn): - errmsg=f"{all_host_failure_prefix}: Can't allow any host access on a subsystem having a DH-HMAC-CHAP key" - self.logger.error(f"{errmsg}") - return pb2.req_status(status = errno.EINVAL, error_message = errmsg) + errmsg = f"{all_host_failure_prefix}: Can't allow any host access on a subsystem having a DH-HMAC-CHAP key" + self.logger.error(errmsg) + return pb2.req_status(status=errno.EINVAL, error_message=errmsg) if request.host_nqn != "*" and self.host_info.is_any_host_allowed(request.subsystem_nqn): self.logger.warning(f"A specific host {request.host_nqn} was added to subsystem {request.subsystem_nqn} in which all hosts are allowed") @@ -2619,30 +2627,30 @@ def add_host_safe(self, request, context): rc = GatewayService.is_valid_host_nqn(request.host_nqn) if rc.status != 0: errmsg = f"{host_failure_prefix}: {rc.error_message}" - self.logger.error(f"{errmsg}") - return pb2.req_status(status = rc.status, error_message = errmsg) + self.logger.error(errmsg) + return pb2.req_status(status=rc.status, error_message=errmsg) if GatewayUtils.is_discovery_nqn(request.subsystem_nqn): if request.host_nqn == "*": - errmsg=f"{all_host_failure_prefix}: Can't allow host access to a discovery subsystem" + errmsg = f"{all_host_failure_prefix}: Can't allow host access to a discovery subsystem" else: - errmsg=f"{host_failure_prefix}: Can't add host to a discovery subsystem" - self.logger.error(f"{errmsg}") + errmsg = f"{host_failure_prefix}: Can't add host to a discovery subsystem" + self.logger.error(errmsg) return pb2.req_status(status=errno.EINVAL, error_message=errmsg) if GatewayUtils.is_discovery_nqn(request.host_nqn): - errmsg=f"{host_failure_prefix}: Can't use a discovery NQN as host's" - self.logger.error(f"{errmsg}") + errmsg = f"{host_failure_prefix}: Can't use a discovery NQN as host's" + self.logger.error(errmsg) return pb2.req_status(status=errno.EINVAL, error_message=errmsg) if request.psk and request.host_nqn == "*": - errmsg=f"{all_host_failure_prefix}: PSK is only allowed for specific hosts" - self.logger.error(f"{errmsg}") + errmsg = f"{all_host_failure_prefix}: PSK is only allowed for specific hosts" + self.logger.error(errmsg) return pb2.req_status(status=errno.EINVAL, error_message=errmsg) if request.dhchap_key and request.host_nqn == "*": - errmsg=f"{all_host_failure_prefix}: DH-HMAC-CHAP key is only allowed for specific hosts" - self.logger.error(f"{errmsg}") + errmsg = f"{all_host_failure_prefix}: DH-HMAC-CHAP key is only allowed for specific hosts" + self.logger.error(errmsg) return pb2.req_status(status=errno.EINVAL, error_message=errmsg) if request.dhchap_key and not self.host_info.does_subsystem_have_dhchap_key(request.subsystem_nqn): @@ -2654,8 +2662,8 @@ def add_host_safe(self, request, context): for listener in self.subsystem_listeners[request.subsystem_nqn]: (_, _, _, secure) = listener if secure: - errmsg=f"{all_host_failure_prefix}: Can't allow any host on a subsystem with secure listeners" - self.logger.error(f"{errmsg}") + errmsg = f"{all_host_failure_prefix}: Can't allow any host on a subsystem with secure listeners" + self.logger.error(errmsg) return pb2.req_status(status=errno.EINVAL, error_message=errmsg) except Exception: pass @@ -2664,25 +2672,25 @@ def add_host_safe(self, request, context): if host_already_exist: if request.host_nqn == "*": errmsg = f"{all_host_failure_prefix}: Open host access is already allowed" - self.logger.error(f"{errmsg}") + self.logger.error(errmsg) return pb2.req_status(status=errno.EEXIST, error_message=errmsg) else: errmsg = f"{host_failure_prefix}: Host is already added" - self.logger.error(f"{errmsg}") + self.logger.error(errmsg) return pb2.req_status(status=errno.EEXIST, error_message=errmsg) if request.host_nqn != "*" and self.host_info.get_host_count(request.subsystem_nqn) >= self.max_hosts_per_subsystem: errmsg = f"{host_failure_prefix}: Maximal number of hosts for subsystem ({self.max_hosts_per_subsystem}) has already been reached" - self.logger.error(f"{errmsg}") - return pb2.subsys_status(status = errno.E2BIG, error_message = errmsg, nqn = request.subsystem_nqn) + self.logger.error(errmsg) + return pb2.subsys_status(status=errno.E2BIG, error_message=errmsg, nqn=request.subsystem_nqn) dhchap_ctrlr_key = self.host_info.get_subsystem_dhchap_key(request.subsystem_nqn) if dhchap_ctrlr_key: self.logger.info(f"Got DHCHAP key {dhchap_ctrlr_key} for subsystem {request.subsystem_nqn}") if dhchap_ctrlr_key and not request.dhchap_key: - errmsg=f"{host_failure_prefix}: Host must have a DH-HMAC-CHAP key if the subsystem has one" - self.logger.error(f"{errmsg}") + errmsg = f"{host_failure_prefix}: Host must have a DH-HMAC-CHAP key if the subsystem has one" + self.logger.error(errmsg) return pb2.req_status(status=errno.EINVAL, error_message=errmsg) psk_file = None @@ -2690,14 +2698,14 @@ def add_host_safe(self, request, context): if request.psk: psk_file = self.create_host_psk_file(request.subsystem_nqn, request.host_nqn, request.psk) if not psk_file: - errmsg=f"{host_failure_prefix}: Can't write PSK file" - self.logger.error(f"{errmsg}") + errmsg = f"{host_failure_prefix}: Can't write PSK file" + self.logger.error(errmsg) return pb2.req_status(status=errno.ENOENT, error_message=errmsg) psk_key_name = GatewayService.construct_key_name_for_keyring( request.subsystem_nqn, request.host_nqn, GatewayService.PSK_PREFIX) if len(psk_key_name) >= SubsystemHostAuth.MAX_PSK_KEY_NAME_LENGTH: - errmsg=f"{host_failure_prefix}: PSK key name {psk_key_name} is too long, max length is {SubsystemHostAuth.MAX_PSK_KEY_NAME_LENGTH}" - self.logger.error(f"{errmsg}") + errmsg = f"{host_failure_prefix}: PSK key name {psk_key_name} is too long, max length is {SubsystemHostAuth.MAX_PSK_KEY_NAME_LENGTH}" + self.logger.error(errmsg) return pb2.req_status(status=errno.E2BIG, error_message=errmsg) dhchap_file = None @@ -2821,30 +2829,30 @@ def remove_host_safe(self, request, context): """Removes a host from a subsystem.""" peer_msg = self.get_peer_message(context) - all_host_failure_prefix=f"Failure disabling open host access to {request.subsystem_nqn}" - host_failure_prefix=f"Failure removing host {request.host_nqn} access from {request.subsystem_nqn}" + all_host_failure_prefix = f"Failure disabling open host access to {request.subsystem_nqn}" + host_failure_prefix = f"Failure removing host {request.host_nqn} access from {request.subsystem_nqn}" if self.verify_nqns: rc = GatewayService.is_valid_host_nqn(request.host_nqn) if rc.status != 0: errmsg = f"{host_failure_prefix}: {rc.error_message}" - self.logger.error(f"{errmsg}") - return pb2.req_status(status = rc.status, error_message = errmsg) + self.logger.error(errmsg) + return pb2.req_status(status=rc.status, error_message=errmsg) if GatewayUtils.is_discovery_nqn(request.subsystem_nqn): if request.host_nqn == "*": - errmsg=f"{all_host_failure_prefix}: Can't disable open host access to a discovery subsystem" + errmsg = f"{all_host_failure_prefix}: Can't disable open host access to a discovery subsystem" else: - errmsg=f"{host_failure_prefix}: Can't remove host access from a discovery subsystem" - self.logger.error(f"{errmsg}") + errmsg = f"{host_failure_prefix}: Can't remove host access from a discovery subsystem" + self.logger.error(errmsg) return pb2.req_status(status=errno.EINVAL, error_message=errmsg) if GatewayUtils.is_discovery_nqn(request.host_nqn): if request.host_nqn == "*": - errmsg=f"{all_host_failure_prefix}: Can't use a discovery NQN as host's" + errmsg = f"{all_host_failure_prefix}: Can't use a discovery NQN as host's" else: - errmsg=f"{host_failure_prefix}: Can't use a discovery NQN as host's" - self.logger.error(f"{errmsg}") + errmsg = f"{host_failure_prefix}: Can't use a discovery NQN as host's" + self.logger.error(errmsg) return pb2.req_status(status=errno.EINVAL, error_message=errmsg) omap_lock = self.omap_lock.get_omap_lock_to_use(context) @@ -2914,52 +2922,52 @@ def change_host_key_safe(self, request, context): """Changes host's inband authentication key.""" peer_msg = self.get_peer_message(context) - failure_prefix=f"Failure changing DH-HMAC-CHAP key for host {request.host_nqn} on subsystem {request.subsystem_nqn}" + failure_prefix = f"Failure changing DH-HMAC-CHAP key for host {request.host_nqn} on subsystem {request.subsystem_nqn}" self.logger.info( f"Received request to change inband authentication key for host {request.host_nqn} on subsystem {request.subsystem_nqn}, dhchap: {request.dhchap_key}, context: {context}{peer_msg}") if request.host_nqn == "*": - errmsg=f"{failure_prefix}: Host NQN can't be '*'" - self.logger.error(f"{errmsg}") + errmsg = f"{failure_prefix}: Host NQN can't be '*'" + self.logger.error(errmsg) return pb2.req_status(status=errno.EINVAL, error_message=errmsg) if not GatewayState.is_key_element_valid(request.host_nqn): errmsg = f"{failure_prefix}: Invalid host NQN \"{request.host_nqn}\", contains invalid characters" - self.logger.error(f"{errmsg}") - return pb2.req_status(status = errno.EINVAL, error_message = errmsg) + self.logger.error(errmsg) + return pb2.req_status(status=errno.EINVAL, error_message=errmsg) if not GatewayState.is_key_element_valid(request.subsystem_nqn): errmsg = f"{failure_prefix}: Invalid subsystem NQN \"{request.subsystem_nqn}\", contains invalid characters" - self.logger.error(f"{errmsg}") - return pb2.req_status(status = errno.EINVAL, error_message = errmsg) + self.logger.error(errmsg) + return pb2.req_status(status=errno.EINVAL, error_message=errmsg) if self.verify_nqns: rc = GatewayUtils.is_valid_nqn(request.subsystem_nqn) if rc[0] != 0: errmsg = f"{failure_prefix}: {rc[1]}" - self.logger.error(f"{errmsg}") - return pb2.req_status(status = rc[0], error_message = errmsg) + self.logger.error(errmsg) + return pb2.req_status(status=rc[0], error_message=errmsg) rc = GatewayUtils.is_valid_nqn(request.host_nqn) if rc[0] != 0: errmsg = f"{failure_prefix}: {rc[1]}" - self.logger.error(f"{errmsg}") - return pb2.req_status(status = rc[0], error_message = errmsg) + self.logger.error(errmsg) + return pb2.req_status(status=rc[0], error_message=errmsg) if GatewayUtils.is_discovery_nqn(request.subsystem_nqn): - errmsg=f"{failure_prefix}: Can't use a discovery NQN as subsystem's" - self.logger.error(f"{errmsg}") + errmsg = f"{failure_prefix}: Can't use a discovery NQN as subsystem's" + self.logger.error(errmsg) return pb2.req_status(status=errno.EINVAL, error_message=errmsg) if GatewayUtils.is_discovery_nqn(request.host_nqn): - errmsg=f"{failure_prefix}: Can't use a discovery NQN as host's" - self.logger.error(f"{errmsg}") + errmsg = f"{failure_prefix}: Can't use a discovery NQN as host's" + self.logger.error(errmsg) return pb2.req_status(status=errno.EINVAL, error_message=errmsg) dhchap_ctrlr_key = self.host_info.get_subsystem_dhchap_key(request.subsystem_nqn) if dhchap_ctrlr_key and not request.dhchap_key: - errmsg=f"{failure_prefix}: Host must have a DH-HMAC-CHAP key if the subsystem has one" - self.logger.error(f"{errmsg}") + errmsg = f"{failure_prefix}: Host must have a DH-HMAC-CHAP key if the subsystem has one" + self.logger.error(errmsg) return pb2.req_status(status=errno.EINVAL, error_message=errmsg) if request.dhchap_key and not dhchap_ctrlr_key: @@ -2967,8 +2975,8 @@ def change_host_key_safe(self, request, context): host_already_exist = self.matching_host_exists(context, request.subsystem_nqn, request.host_nqn) if not host_already_exist and context: - errmsg=f"{failure_prefix}: Can't find host on subsystem" - self.logger.error(f"{errmsg}") + errmsg = f"{failure_prefix}: Can't find host on subsystem" + self.logger.error(errmsg) return pb2.req_status(status=errno.EINVAL, error_message=errmsg) dhchap_file = None @@ -3059,7 +3067,7 @@ def list_hosts_safe(self, request, context): ret = rpc_nvmf.nvmf_get_subsystems(self.spdk_rpc_client, nqn=request.subsystem) self.logger.debug(f"list_hosts: {ret}") except Exception as ex: - errmsg = f"Failure listing hosts, can't get subsystems" + errmsg = "Failure listing hosts, can't get subsystems" self.logger.exception(errmsg) errmsg = f"{errmsg}:\n{ex}" resp = self.parse_json_exeption(ex) @@ -3086,14 +3094,14 @@ def list_hosts_safe(self, request, context): host_nqn = h["nqn"] psk = self.host_info.is_psk_host(request.subsystem, host_nqn) dhchap = self.host_info.is_dhchap_host(request.subsystem, host_nqn) - one_host = pb2.host(nqn = host_nqn, use_psk = psk, use_dhchap = dhchap) + one_host = pb2.host(nqn=host_nqn, use_psk=psk, use_dhchap=dhchap) hosts.append(one_host) break except Exception: self.logger.exception(f"{s=} parse error") pass - return pb2.hosts_info(status = 0, error_message = os.strerror(0), allow_any_host=allow_any_host, + return pb2.hosts_info(status=0, error_message=os.strerror(0), allow_any_host=allow_any_host, subsystem_nqn=request.subsystem, hosts=hosts) def list_hosts(self, request, context=None): @@ -3107,15 +3115,15 @@ def list_connections_safe(self, request, context): self.logger.log(log_level, f"Received request to list connections for {request.subsystem}, context: {context}{peer_msg}") if not request.subsystem: - errmsg = f"Failure listing connections, missing subsystem NQN" - self.logger.error(f"{errmsg}") - return pb2.connections_info(status=errno.EINVAL, error_message = errmsg, connections=[]) + errmsg = "Failure listing connections, missing subsystem NQN" + self.logger.error(errmsg) + return pb2.connections_info(status=errno.EINVAL, error_message=errmsg, connections=[]) try: qpair_ret = rpc_nvmf.nvmf_subsystem_get_qpairs(self.spdk_rpc_client, nqn=request.subsystem) self.logger.debug(f"list_connections get_qpairs: {qpair_ret}") except Exception as ex: - errmsg = f"Failure listing connections, can't get qpairs" + errmsg = "Failure listing connections, can't get qpairs" self.logger.exception(errmsg) errmsg = f"{errmsg}:\n{ex}" resp = self.parse_json_exeption(ex) @@ -3129,7 +3137,7 @@ def list_connections_safe(self, request, context): ctrl_ret = rpc_nvmf.nvmf_subsystem_get_controllers(self.spdk_rpc_client, nqn=request.subsystem) self.logger.debug(f"list_connections get_controllers: {ctrl_ret}") except Exception as ex: - errmsg = f"Failure listing connections, can't get controllers" + errmsg = "Failure listing connections, can't get controllers" self.logger.exception(errmsg) errmsg = f"{errmsg}:\n{ex}" resp = self.parse_json_exeption(ex) @@ -3143,7 +3151,7 @@ def list_connections_safe(self, request, context): subsys_ret = rpc_nvmf.nvmf_get_subsystems(self.spdk_rpc_client, nqn=request.subsystem) self.logger.debug(f"list_connections subsystems: {subsys_ret}") except Exception as ex: - errmsg = f"Failure listing connections, can't get subsystems" + errmsg = "Failure listing connections, can't get subsystems" self.logger.exception(errmsg) errmsg = f"{errmsg}:\n{ex}" resp = self.parse_json_exeption(ex) @@ -3250,8 +3258,8 @@ def list_connections_safe(self, request, context): qpairs_count=-1, controller_id=-1, use_psk=psk, use_dhchap=dhchap) connections.append(one_conn) - return pb2.connections_info(status = 0, error_message = os.strerror(0), - subsystem_nqn=request.subsystem, connections=connections) + return pb2.connections_info(status=0, error_message=os.strerror(0), + subsystem_nqn=request.subsystem, connections=connections) def list_connections(self, request, context=None): return self.execute_grpc_function(self.list_connections_safe, request, context) @@ -3263,9 +3271,9 @@ def create_listener_safe(self, request, context): create_listener_error_prefix = f"Failure adding {request.nqn} listener at {request.traddr}:{request.trsvcid}" adrfam = GatewayEnumUtils.get_key_from_value(pb2.AddressFamily, request.adrfam) - if adrfam == None: - errmsg=f"{create_listener_error_prefix}: Unknown address family {request.adrfam}" - self.logger.error(f"{errmsg}") + if adrfam is None: + errmsg = f"{create_listener_error_prefix}: Unknown address family {request.adrfam}" + self.logger.error(errmsg) return pb2.req_status(status=errno.ENOKEY, error_message=errmsg) peer_msg = self.get_peer_message(context) @@ -3276,18 +3284,18 @@ def create_listener_safe(self, request, context): traddr = GatewayUtils.unescape_address_if_ipv6(request.traddr, adrfam) if GatewayUtils.is_discovery_nqn(request.nqn): - errmsg=f"{create_listener_error_prefix}: Can't create a listener for a discovery subsystem" - self.logger.error(f"{errmsg}") + errmsg = f"{create_listener_error_prefix}: Can't create a listener for a discovery subsystem" + self.logger.error(errmsg) return pb2.req_status(status=errno.EINVAL, error_message=errmsg) if not GatewayState.is_key_element_valid(request.host_name): - errmsg=f"{create_listener_error_prefix}: Host name \"{request.host_name}\" contains invalid characters" - self.logger.error(f"{errmsg}") + errmsg = f"{create_listener_error_prefix}: Host name \"{request.host_name}\" contains invalid characters" + self.logger.error(errmsg) return pb2.req_status(status=errno.EINVAL, error_message=errmsg) if request.secure and self.host_info.is_any_host_allowed(request.nqn): - errmsg=f"{create_listener_error_prefix}: Secure channel is only allowed for subsystems in which \"allow any host\" is off" - self.logger.error(f"{errmsg}") + errmsg = f"{create_listener_error_prefix}: Secure channel is only allowed for subsystems in which \"allow any host\" is off" + self.logger.error(errmsg) return pb2.req_status(status=errno.EINVAL, error_message=errmsg) add_listener_args = {} @@ -3306,18 +3314,18 @@ def create_listener_safe(self, request, context): if (adrfam, traddr, request.trsvcid, False) in self.subsystem_listeners[request.nqn] or (adrfam, traddr, request.trsvcid, True) in self.subsystem_listeners[request.nqn]: self.logger.error(f"{request.nqn} already listens on address {request.traddr}:{request.trsvcid}") return pb2.req_status(status=errno.EEXIST, - error_message=f"{create_listener_error_prefix}: Subsystem already listens on this address") + error_message=f"{create_listener_error_prefix}: Subsystem already listens on this address") ret = rpc_nvmf.nvmf_subsystem_add_listener(self.spdk_rpc_client, **add_listener_args) self.logger.debug(f"create_listener: {ret}") self.subsystem_listeners[request.nqn].add((adrfam, traddr, request.trsvcid, request.secure)) else: if context: - errmsg=f"{create_listener_error_prefix}: Gateway's host name must match current host ({self.host_name})" - self.logger.error(f"{errmsg}") + errmsg = f"{create_listener_error_prefix}: Gateway's host name must match current host ({self.host_name})" + self.logger.error(errmsg) return pb2.req_status(status=errno.ENODEV, error_message=errmsg) else: - errmsg=f"Listener not created as gateway's host name {self.host_name} differs from requested host {request.host_name}" - self.logger.debug(f"{errmsg}") + errmsg = f"Listener not created as gateway's host name {self.host_name} differs from requested host {request.host_name}" + self.logger.debug(errmsg) return pb2.req_status(status=0, error_message=errmsg) except Exception as ex: self.logger.exception(create_listener_error_prefix) @@ -3350,7 +3358,7 @@ def create_listener_safe(self, request, context): # have been provided with ana state for this nqn prior to creation # update optimized ana groups if self.ana_map[request.nqn]: - for x in range (self.subsys_max_ns[request.nqn]): + for x in range(self.subsys_max_ns[request.nqn]): ana_grp = x+1 if ana_grp in self.ana_map[request.nqn] and self.ana_map[request.nqn][ana_grp] == pb2.ana_state.OPTIMIZED: _ana_state = "optimized" @@ -3363,13 +3371,13 @@ def create_listener_safe(self, request, context): traddr=traddr, trsvcid=str(request.trsvcid), adrfam=adrfam, - anagrpid=ana_grp ) + anagrpid=ana_grp) self.logger.debug(f"create_listener nvmf_subsystem_listener_set_ana_state response {ret=}") except Exception as ex: - errmsg=f"{create_listener_error_prefix}: Error setting ANA state" + errmsg = f"{create_listener_error_prefix}: Error setting ANA state" self.logger.exception(errmsg) - errmsg=f"{errmsg}:\n{ex}" + errmsg = f"{errmsg}:\n{ex}" resp = self.parse_json_exeption(ex) status = errno.EINVAL if resp: @@ -3453,8 +3461,8 @@ def delete_listener_safe(self, request, context): delete_listener_error_prefix = f"Listener {esc_traddr}:{request.trsvcid} failed to delete from {request.nqn}" adrfam = GatewayEnumUtils.get_key_from_value(pb2.AddressFamily, request.adrfam) - if adrfam == None: - errmsg=f"{delete_listener_error_prefix}. Unknown address family {request.adrfam}" + if adrfam is None: + errmsg = f"{delete_listener_error_prefix}. Unknown address family {request.adrfam}" self.logger.error(errmsg) return pb2.req_status(status=errno.ENOKEY, error_message=errmsg) @@ -3469,12 +3477,12 @@ def delete_listener_safe(self, request, context): f" {esc_traddr}:{request.trsvcid}{force_msg}, context: {context}{peer_msg}") if request.host_name == "*" and not request.force: - errmsg=f"{delete_listener_error_prefix}. Must use the \"--force\" parameter when setting the host name to \"*\"." + errmsg = f"{delete_listener_error_prefix}. Must use the \"--force\" parameter when setting the host name to \"*\"." self.logger.error(errmsg) return pb2.req_status(status=errno.EINVAL, error_message=errmsg) if GatewayUtils.is_discovery_nqn(request.nqn): - errmsg=f"{delete_listener_error_prefix}. Can't delete a listener from a discovery subsystem" + errmsg = f"{delete_listener_error_prefix}. Can't delete a listener from a discovery subsystem" self.logger.error(errmsg) return pb2.req_status(status=errno.EINVAL, error_message=errmsg) @@ -3482,7 +3490,7 @@ def delete_listener_safe(self, request, context): list_conn_req = pb2.list_connections_req(subsystem=request.nqn) list_conn_ret = self.list_connections_safe(list_conn_req, context) if list_conn_ret.status != 0: - errmsg=f"{delete_listener_error_prefix}. Can't verify there are no active connections for this address" + errmsg = f"{delete_listener_error_prefix}. Can't verify there are no active connections for this address" self.logger.error(errmsg) return pb2.req_status(status=errno.ENOTEMPTY, error_message=errmsg) for conn in list_conn_ret.connections: @@ -3492,7 +3500,7 @@ def delete_listener_safe(self, request, context): continue if conn.trsvcid != request.trsvcid: continue - errmsg=f"{delete_listener_error_prefix} due to active connections for {esc_traddr}:{request.trsvcid}. Deleting the listener terminates active connections. You can continue to delete the listener by adding the `--force` parameter." + errmsg = f"{delete_listener_error_prefix} due to active connections for {esc_traddr}:{request.trsvcid}. Deleting the listener terminates active connections. You can continue to delete the listener by adding the `--force` parameter." self.logger.error(errmsg) return pb2.req_status(status=errno.ENOTEMPTY, error_message=errmsg) @@ -3515,8 +3523,8 @@ def delete_listener_safe(self, request, context): if (adrfam, traddr, request.trsvcid, True) in self.subsystem_listeners[request.nqn]: self.subsystem_listeners[request.nqn].remove((adrfam, traddr, request.trsvcid, True)) else: - errmsg=f"{delete_listener_error_prefix}. Gateway's host name must match current host ({self.host_name}). You can continue to delete the listener by adding the `--force` parameter." - self.logger.error(f"{errmsg}") + errmsg = f"{delete_listener_error_prefix}. Gateway's host name must match current host ({self.host_name}). You can continue to delete the listener by adding the `--force` parameter." + self.logger.error(errmsg) return pb2.req_status(status=errno.ENOENT, error_message=errmsg) except Exception as ex: self.logger.exception(delete_listener_error_prefix) @@ -3569,18 +3577,18 @@ def list_listeners_safe(self, request, context): secure = False if "secure" in listener: secure = listener["secure"] - one_listener = pb2.listener_info(host_name = listener["host_name"], - trtype = "TCP", - adrfam = listener["adrfam"], - traddr = listener["traddr"], - trsvcid = listener["trsvcid"], - secure = secure) + one_listener = pb2.listener_info(host_name=listener["host_name"], + trtype="TCP", + adrfam=listener["adrfam"], + traddr=listener["traddr"], + trsvcid=listener["trsvcid"], + secure=secure) listeners.append(one_listener) except Exception: self.logger.exception(f"Got exception while parsing {val}") continue - return pb2.listeners_info(status = 0, error_message = os.strerror(0), listeners=listeners) + return pb2.listeners_info(status=0, error_message=os.strerror(0), listeners=listeners) def list_listeners(self, request, context=None): return self.execute_grpc_function(self.list_listeners_safe, request, context) @@ -3606,7 +3614,7 @@ def list_subsystems_safe(self, request, context): ret = rpc_nvmf.nvmf_get_subsystems(self.spdk_rpc_client) self.logger.debug(f"list_subsystems: {ret}") except Exception as ex: - errmsg = f"Failure listing subsystems" + errmsg = "Failure listing subsystems" self.logger.exception(errmsg) errmsg = f"{errmsg}:\n{ex}" resp = self.parse_json_exeption(ex) @@ -3640,7 +3648,7 @@ def list_subsystems_safe(self, request, context): self.logger.exception(f"{s=} parse error") pass - return pb2.subsystems_info_cli(status = 0, error_message = os.strerror(0), subsystems=subsystems) + return pb2.subsystems_info_cli(status=0, error_message=os.strerror(0), subsystems=subsystems) def get_subsystems_safe(self, request, context): """Gets subsystems.""" @@ -3651,7 +3659,7 @@ def get_subsystems_safe(self, request, context): try: ret = rpc_nvmf.nvmf_get_subsystems(self.spdk_rpc_subsystems_client) except Exception as ex: - self.logger.exception(f"get_subsystems failed") + self.logger.exception("get_subsystems failed") context.set_code(grpc.StatusCode.INTERNAL) context.set_details(f"{ex}") return pb2.subsystems_info() @@ -3689,26 +3697,26 @@ def list_subsystems(self, request, context=None): def change_subsystem_key_safe(self, request, context): """Change subsystem key.""" peer_msg = self.get_peer_message(context) - failure_prefix=f"Failure changing DH-HMAC-CHAP key for subsystem {request.subsystem_nqn}" + failure_prefix = f"Failure changing DH-HMAC-CHAP key for subsystem {request.subsystem_nqn}" self.logger.info( f"Received request to change inband authentication key for subsystem {request.subsystem_nqn}, dhchap: {request.dhchap_key}, context: {context}{peer_msg}") if not GatewayState.is_key_element_valid(request.subsystem_nqn): errmsg = f"{failure_prefix}: Invalid subsystem NQN \"{request.subsystem_nqn}\", contains invalid characters" - self.logger.error(f"{errmsg}") - return pb2.req_status(status = errno.EINVAL, error_message = errmsg) + self.logger.error(errmsg) + return pb2.req_status(status=errno.EINVAL, error_message=errmsg) if self.verify_nqns: rc = GatewayUtils.is_valid_nqn(request.subsystem_nqn) if rc[0] != 0: errmsg = f"{failure_prefix}: {rc[1]}" - self.logger.error(f"{errmsg}") - return pb2.req_status(status = rc[0], error_message = errmsg) + self.logger.error(errmsg) + return pb2.req_status(status=rc[0], error_message=errmsg) if GatewayUtils.is_discovery_nqn(request.subsystem_nqn): errmsg = f"{failure_prefix}: Can't change DH-HMAC-CHAP key for a discovery subsystem" - self.logger.error(f"{errmsg}") - return pb2.req_status(status = errno.EINVAL, error_message = errmsg) + self.logger.error(errmsg) + return pb2.req_status(status=errno.EINVAL, error_message=errmsg) omap_lock = self.omap_lock.get_omap_lock_to_use(context) with omap_lock: @@ -3724,8 +3732,8 @@ def change_subsystem_key_safe(self, request, context): assert hostnqn, "Shouldn't get an empty host NQN" if not self.host_info.is_dhchap_host(request.subsystem_nqn, hostnqn): errmsg = f"{failure_prefix}: Can't set a subsystem's DH-HMAC-CHAP key when it has hosts with no key, like host {hostnqn}" - self.logger.error(f"{errmsg}") - return pb2.req_status(status = errno.EINVAL, error_message = errmsg) + self.logger.error(errmsg) + return pb2.req_status(status=errno.EINVAL, error_message=errmsg) subsys_key = GatewayState.build_subsystem_key(request.subsystem_nqn) try: @@ -3739,11 +3747,11 @@ def change_subsystem_key_safe(self, request, context): assert subsys_entry, f"Can't find entry for subsystem {request.subsystem_nqn}" try: create_req = pb2.create_subsystem_req(subsystem_nqn=request.subsystem_nqn, - serial_number=subsys_entry["serial_number"], - max_namespaces=subsys_entry["max_namespaces"], - enable_ha=subsys_entry["enable_ha"], - no_group_append=subsys_entry["no_group_append"], - dhchap_key=request.dhchap_key) + serial_number=subsys_entry["serial_number"], + max_namespaces=subsys_entry["max_namespaces"], + enable_ha=subsys_entry["enable_ha"], + no_group_append=subsys_entry["no_group_append"], + dhchap_key=request.dhchap_key) json_req = json_format.MessageToJson( create_req, preserving_proto_field_name=True, including_default_value_fields=True) self.gateway_state.add_subsystem(request.subsystem_nqn, json_req) @@ -3769,7 +3777,6 @@ def change_subsystem_key_safe(self, request, context): except Exception: pass - return pb2.req_status(status=0, error_message=os.strerror(0)) def change_subsystem_key(self, request, context=None): @@ -3785,15 +3792,15 @@ def get_spdk_nvmf_log_flags_and_level_safe(self, request, context): nvmf_log_flags = {key: value for key, value in rpc_log.log_get_flags( self.spdk_rpc_client).items() if key.startswith('nvmf')} for flag, flagvalue in nvmf_log_flags.items(): - pb2_log_flag = pb2.spdk_log_flag_info(name = flag, enabled = flagvalue) + pb2_log_flag = pb2.spdk_log_flag_info(name=flag, enabled=flagvalue) log_flags.append(pb2_log_flag) spdk_log_level = rpc_log.log_get_level(self.spdk_rpc_client) spdk_log_print_level = rpc_log.log_get_print_level(self.spdk_rpc_client) - self.logger.debug(f"spdk log flags: {nvmf_log_flags}, " - f"spdk log level: {spdk_log_level}, " - f"spdk log print level: {spdk_log_print_level}") + self.logger.debug(f"spdk log flags: {nvmf_log_flags}, " + f"spdk log level: {spdk_log_level}, " + f"spdk log print level: {spdk_log_print_level}") except Exception as ex: - errmsg = f"Failure getting SPDK log levels and nvmf log flags" + errmsg = "Failure getting SPDK log levels and nvmf log flags" self.logger.exception(errmsg) errmsg = f"{errmsg}:\n{ex}" resp = self.parse_json_exeption(ex) @@ -3801,14 +3808,14 @@ def get_spdk_nvmf_log_flags_and_level_safe(self, request, context): if resp: status = resp["code"] errmsg = f"Failure getting SPDK log levels and nvmf log flags: {resp['message']}" - return pb2.spdk_nvmf_log_flags_and_level_info(status = status, error_message = errmsg) + return pb2.spdk_nvmf_log_flags_and_level_info(status=status, error_message=errmsg) return pb2.spdk_nvmf_log_flags_and_level_info( nvmf_log_flags=log_flags, - log_level = spdk_log_level, - log_print_level = spdk_log_print_level, - status = 0, - error_message = os.strerror(0)) + log_level=spdk_log_level, + log_print_level=spdk_log_print_level, + status=0, + error_message=os.strerror(0)) def get_spdk_nvmf_log_flags_and_level(self, request, context=None): return self.execute_grpc_function(self.get_spdk_nvmf_log_flags_and_level_safe, request, context) @@ -3823,16 +3830,16 @@ def set_spdk_nvmf_logs_safe(self, request, context): peer_msg = self.get_peer_message(context) if request.HasField("log_level"): log_level = GatewayEnumUtils.get_key_from_value(pb2.LogLevel, request.log_level) - if log_level == None: - errmsg=f"Unknown log level {request.log_level}" - self.logger.error(f"{errmsg}") + if log_level is None: + errmsg = f"Unknown log level {request.log_level}" + self.logger.error(errmsg) return pb2.req_status(status=errno.ENOKEY, error_message=errmsg) if request.HasField("print_level"): print_level = GatewayEnumUtils.get_key_from_value(pb2.LogLevel, request.print_level) - if print_level == None: - errmsg=f"Unknown print level {request.print_level}" - self.logger.error(f"{errmsg}") + if print_level is None: + errmsg = f"Unknown print level {request.print_level}" + self.logger.error(errmsg) return pb2.req_status(status=errno.ENOKEY, error_message=errmsg) self.logger.info(f"Received request to set SPDK nvmf logs: log_level: {log_level}, print_level: {print_level}{peer_msg}") @@ -3842,17 +3849,17 @@ def set_spdk_nvmf_logs_safe(self, request, context): ret = [rpc_log.log_set_flag( self.spdk_rpc_client, flag=flag) for flag in nvmf_log_flags] self.logger.debug(f"Set SPDK nvmf log flags {nvmf_log_flags} to TRUE: {ret}") - if log_level != None: + if log_level is not None: ret_log = rpc_log.log_set_level(self.spdk_rpc_client, level=log_level) self.logger.debug(f"Set log level to {log_level}: {ret_log}") - if print_level != None: + if print_level is not None: ret_print = rpc_log.log_set_print_level( self.spdk_rpc_client, level=print_level) self.logger.debug(f"Set log print level to {print_level}: {ret_print}") except Exception as ex: - errmsg="Failure setting SPDK log levels" + errmsg = "Failure setting SPDK log levels" self.logger.exception(errmsg) - errmsg="{errmsg}:\n{ex}" + errmsg = "{errmsg}:\n{ex}" for flag in nvmf_log_flags: rpc_log.log_clear_flag(self.spdk_rpc_client, flag=flag) resp = self.parse_json_exeption(ex) @@ -3864,10 +3871,10 @@ def set_spdk_nvmf_logs_safe(self, request, context): status = 0 errmsg = os.strerror(0) - if log_level != None and not ret_log: + if log_level is not None and not ret_log: status = errno.EINVAL errmsg = "Failure setting SPDK log level" - elif print_level != None and not ret_print: + elif print_level is not None and not ret_print: status = errno.EINVAL errmsg = "Failure setting SPDK print log level" elif not all(ret): @@ -3890,7 +3897,7 @@ def disable_spdk_nvmf_logs_safe(self, request, context): rpc_log.log_set_print_level(self.spdk_rpc_client, level='INFO')] ret.extend(logs_level) except Exception as ex: - errmsg = f"Failure in disable SPDK nvmf log flags" + errmsg = "Failure in disable SPDK nvmf log flags" self.logger.exception(errmsg) errmsg = f"{errmsg}:\n{ex}" resp = self.parse_json_exeption(ex) @@ -3935,25 +3942,25 @@ def get_gateway_info_safe(self, request, context): cli_version_string = request.cli_version addr = self.config.get_with_default("gateway", "addr", "") port = self.config.get_with_default("gateway", "port", "") - ret = pb2.gateway_info(cli_version = request.cli_version, - version = gw_version_string, - spdk_version = spdk_version_string, - name = self.gateway_name, - group = self.gateway_group, - addr = addr, - port = port, - load_balancing_group = self.group_id + 1, - bool_status = True, - hostname = self.host_name, - max_subsystems = self.max_subsystems, - max_namespaces = self.max_namespaces, - max_namespaces_per_subsystem = self.max_namespaces_per_subsystem, - max_hosts_per_subsystem = self.max_hosts_per_subsystem, - status = 0, - error_message = os.strerror(0)) + ret = pb2.gateway_info(cli_version=request.cli_version, + version=gw_version_string, + spdk_version=spdk_version_string, + name=self.gateway_name, + group=self.gateway_group, + addr=addr, + port=port, + load_balancing_group=self.group_id + 1, + bool_status=True, + hostname=self.host_name, + max_subsystems=self.max_subsystems, + max_namespaces=self.max_namespaces, + max_namespaces_per_subsystem=self.max_namespaces_per_subsystem, + max_hosts_per_subsystem=self.max_hosts_per_subsystem, + status=0, + error_message=os.strerror(0)) cli_ver = self.parse_version(cli_version_string) gw_ver = self.parse_version(gw_version_string) - if cli_ver != None and gw_ver != None and cli_ver < gw_ver: + if cli_ver is not None and gw_ver is not None and cli_ver < gw_ver: ret.bool_status = False ret.status = errno.EINVAL ret.error_message = f"CLI version {cli_version_string} is older than gateway's version {gw_version_string}" @@ -3966,7 +3973,7 @@ def get_gateway_info_safe(self, request, context): ret.status = errno.EINVAL ret.error_message = f"Invalid gateway's version {gw_version_string}" if not cli_version_string: - self.logger.warning(f"No CLI version specified, can't check version compatibility") + self.logger.warning("No CLI version specified, can't check version compatibility") elif not cli_ver: self.logger.warning(f"Invalid CLI version {cli_version_string}, can't check version compatibility") if ret.status == 0: @@ -3987,19 +3994,19 @@ def get_gateway_log_level(self, request, context=None): log_level = GatewayEnumUtils.get_key_from_value(pb2.GwLogLevel, self.logger.level) except Exception: self.logger.exception(f"Can't get string value for log level {self.logger.level}") - return pb2.gateway_log_level_info(status = errno.ENOKEY, - error_message=f"Invalid gateway log level") + return pb2.gateway_log_level_info(status=errno.ENOKEY, + error_message="Invalid gateway log level") self.logger.info(f"Received request to get gateway's log level. Level is {log_level}{peer_msg}") - return pb2.gateway_log_level_info(status = 0, error_message=os.strerror(0), log_level=log_level) + return pb2.gateway_log_level_info(status=0, error_message=os.strerror(0), log_level=log_level) def set_gateway_log_level(self, request, context=None): """Set gateway's log level""" peer_msg = self.get_peer_message(context) log_level = GatewayEnumUtils.get_key_from_value(pb2.GwLogLevel, request.log_level) - if log_level == None: - errmsg=f"Unknown log level {request.log_level}" - self.logger.error(f"{errmsg}") + if log_level is None: + errmsg = f"Unknown log level {request.log_level}" + self.logger.error(errmsg) return pb2.req_status(status=errno.ENOKEY, error_message=errmsg) log_level = log_level.upper() diff --git a/control/rebalance.py b/control/rebalance.py index 608fb8508..258549fbf 100755 --- a/control/rebalance.py +++ b/control/rebalance.py @@ -7,14 +7,8 @@ # Authors: leonidc@il.ibm.com # -#import uuid -import errno import threading import time -import json -import re -from .utils import GatewayLogger -from .config import GatewayConfig from .proto import gateway_pb2 as pb2 class Rebalance: @@ -51,8 +45,8 @@ def auto_rebalance_task(self, death_event): def find_min_loaded_group(self, grp_list)->int: min_load = 2000 chosen_ana_group = 0 - for ana_grp in self.gw_srv.ana_grp_ns_load : - if ana_grp in grp_list : + for ana_grp in self.gw_srv.ana_grp_ns_load: + if ana_grp in grp_list: if self.gw_srv.ana_grp_ns_load[ana_grp] <= min_load: min_load = self.gw_srv.ana_grp_ns_load[ana_grp] chosen_ana_group = ana_grp @@ -66,12 +60,12 @@ def find_min_loaded_group(self, grp_list)->int: def find_min_loaded_group_in_subsys(self, nqn, grp_list)->int: min_load = 2000 chosen_ana_group = 0 - for ana_grp in grp_list : - if self.gw_srv.ana_grp_ns_load[ana_grp] == 0: - self.gw_srv.ana_grp_subs_load[ana_grp][nqn] = 0 - return 0, ana_grp + for ana_grp in grp_list: + if self.gw_srv.ana_grp_ns_load[ana_grp] == 0: + self.gw_srv.ana_grp_subs_load[ana_grp][nqn] = 0 + return 0, ana_grp for ana_grp in self.gw_srv.ana_grp_subs_load : - if ana_grp in grp_list : + if ana_grp in grp_list: if nqn in self.gw_srv.ana_grp_subs_load[ana_grp]: if self.gw_srv.ana_grp_subs_load[ana_grp][nqn] <= min_load: min_load = self.gw_srv.ana_grp_subs_load[ana_grp][nqn] @@ -119,8 +113,8 @@ def rebalance_logic(self, request, context)->int: self.logger.info(f"warning: empty group {ana_grp} of Deleting GW still appears Optimized") return 1 else : - if not ongoing_scale_down_rebalance and (self.gw_srv.ana_grp_state[worker_ana_group] == pb2.ana_state.OPTIMIZED) : - # if my optimized ana group == worker-ana-group or worker-ana-group is also in optimized state on this GW machine + if not ongoing_scale_down_rebalance and (self.gw_srv.ana_grp_state[worker_ana_group] == pb2.ana_state.OPTIMIZED) : + # if my optimized ana group == worker-ana-group or worker-ana-group is also in optimized state on this GW machine for nqn in self.gw_srv.ana_grp_subs_load[ana_grp] : #need to search all nqns not only inside the current load num_ns_in_nqn = self.gw_srv.subsystem_nsid_bdev_and_uuid.get_namespace_count(nqn, None, 0) target_subs_per_ana = num_ns_in_nqn/num_active_ana_groups diff --git a/control/utils.py b/control/utils.py index 570f54159..6efc08259 100644 --- a/control/utils.py +++ b/control/utils.py @@ -31,10 +31,13 @@ def get_value_from_key(e_type, keyval, ignore_case = False): except IndexError: pass - if ignore_case and val == None and type(keyval) == str: - val = get_value_from_key(e_type, keyval.lower(), False) - if ignore_case and val == None and type(keyval) == str: - val = get_value_from_key(e_type, keyval.upper(), False) + if val is not None or not ignore_case: + return val + + if isinstance(keyval, str): + val = GatewayEnumUtils.get_value_from_key(e_type, keyval.lower(), False) + if val is None and isinstance(keyval, str): + val = GatewayEnumUtils.get_value_from_key(e_type, keyval.upper(), False) return val @@ -111,7 +114,7 @@ def is_valid_uuid(uuid_val) -> bool: for u in uuid_parts: try: - n = int(u, 16) + int(u, 16) except ValueError: return False @@ -125,11 +128,11 @@ def is_valid_nqn(nqn): NQN_UUID_PREFIX = "nqn.2014-08.org.nvmexpress:uuid:" NQN_UUID_PREFIX_LENGTH = len(NQN_UUID_PREFIX) - if type(nqn) != str: + if not isinstance(nqn, str): return (errno.EINVAL, f"Invalid type {type(nqn)} for NQN, must be a string") try: - b = nqn.encode(encoding="utf-8") + nqn.encode(encoding="utf-8") except UnicodeEncodeError: return (errno.EINVAL, f"Invalid NQN \"{nqn}\", must have an UTF-8 encoding") @@ -163,8 +166,8 @@ def is_valid_nqn(nqn): year_part, month_part = date_part.split("-") if len(year_part) != 4 or len(month_part) != 2: return (errno.EINVAL, f"Invalid NQN \"{nqn}\": invalid date code") - n = int(year_part) - n = int(month_part) + int(year_part) + int(month_part) except ValueError: return (errno.EINVAL, f"Invalid NQN \"{nqn}\": invalid date code") @@ -306,7 +309,7 @@ def rotate_backup_directories(dirname, count): pass def set_log_level(self, log_level): - if type(log_level) == str: + if isinstance(log_level, str): log_level = log_level.upper() self.logger.setLevel(log_level) logger_parent = self.logger.parent @@ -326,7 +329,6 @@ def log_file_rotate(src, dest): GatewayLogger.logger.info(m) for e in errs: GatewayLogger.logger.error(e) - else: os.rename(src, dest) diff --git a/tests/conftest.py b/tests/conftest.py index 73f4baa58..6bf548268 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,7 +1,5 @@ import pytest -import rados from control.config import GatewayConfig -from control.state import OmapGatewayState def pytest_addoption(parser): diff --git a/tests/ha/start_up.sh b/tests/ha/start_up.sh index 309668916..494538629 100755 --- a/tests/ha/start_up.sh +++ b/tests/ha/start_up.sh @@ -13,7 +13,7 @@ if [ $# -ge 1 ]; then fi fi echo ℹ️ Starting $SCALE nvmeof gateways -docker compose up -d --remove-orphans --scale nvmeof=$SCALE nvmeof +docker compose up -d --remove-orphans ceph # Waiting for the ceph container to become healthy while true; do @@ -28,6 +28,7 @@ while true; do fi done echo ✅ ceph is healthy +docker compose up -d --remove-orphans --scale nvmeof=$SCALE nvmeof echo ℹ️ Increase debug logs level docker compose exec -T ceph ceph config get mon.a diff --git a/tests/test_dhchap.py b/tests/test_dhchap.py index 6de95f56b..33fe8ac00 100644 --- a/tests/test_dhchap.py +++ b/tests/test_dhchap.py @@ -4,13 +4,7 @@ from control.cli import main as cli from control.cli import main_test as cli_test from control.cephutils import CephUtils -from control.utils import GatewayUtils -from control.config import GatewayConfig import grpc -from control.proto import gateway_pb2 as pb2 -from control.proto import gateway_pb2_grpc as pb2_grpc -import os -import os.path image = "mytestdevimage" pool = "rbd" @@ -64,7 +58,7 @@ def gateway(config): gateway.serve() # Bind the client and Gateway - channel = grpc.insecure_channel(f"{addr}:{port}") + grpc.insecure_channel(f"{addr}:{port}") yield gateway.gateway_rpc # Stop gateway @@ -72,7 +66,6 @@ def gateway(config): gateway.gateway_rpc.gateway_state.delete_state() def test_setup(caplog, gateway): - gw = gateway caplog.clear() cli(["subsystem", "add", "--subsystem", subsystem, "--no-group-append"]) assert f"create_subsystem {subsystem}: True" in caplog.text @@ -196,13 +189,13 @@ def test_list_listeners(caplog, gateway): listeners = cli_test(["listener", "list", "--subsystem", subsystem]) assert len(listeners.listeners) == 2 found = 0 - for l in listeners.listeners: - if l.trsvcid == 5001: + for lstnr in listeners.listeners: + if lstnr.trsvcid == 5001: found += 1 - assert l.secure - elif l.trsvcid == 5002: + assert lstnr.secure + elif lstnr.trsvcid == 5002: found += 1 - assert not l.secure + assert not lstnr.secure else: assert False assert found == 2 diff --git a/tests/test_grpc.py b/tests/test_grpc.py index 1a6936725..5034f538c 100644 --- a/tests/test_grpc.py +++ b/tests/test_grpc.py @@ -3,8 +3,6 @@ from control.server import GatewayServer from control.cli import main as cli from control.cephutils import CephUtils -import logging -import warnings image = "mytestdevimage" pool = "rbd" @@ -69,7 +67,7 @@ def test_create_get_subsys(caplog, config): "--r-megabytes-per-second", "5"]) assert f"Setting QOS limits of namespace 1 in {subsystem_prefix}0: Successful" in caplog.text assert f"No previous QOS limits found, this is the first time the limits are set for namespace 1 on {subsystem_prefix}0" not in caplog.text - + # add host to the first namespace caplog.clear() cli(["namespace", "add_host", "--subsystem", f"{subsystem_prefix}0", "--nsid", "1", "--host-nqn", f"{host_prefix}0"]) diff --git a/tests/test_log_files.py b/tests/test_log_files.py index 44eafccd9..6939808a6 100644 --- a/tests/test_log_files.py +++ b/tests/test_log_files.py @@ -1,7 +1,5 @@ import pytest from control.server import GatewayServer -from control.utils import GatewayLogger -import socket from control.cli import main as cli from control.cli import main_test as cli_test import grpc @@ -55,8 +53,7 @@ def gateway(config, request): gateway.serve() # Bind the client and Gateway - channel = grpc.insecure_channel(f"{addr}:{port}") - stub = pb2_grpc.GatewayStub(channel) + grpc.insecure_channel(f"{addr}:{port}") yield gateway # Stop gateway @@ -82,7 +79,6 @@ def test_log_files(gateway): assert f"spdk-{gw.name}" in spdk_files[0] def test_log_files_disabled(gateway): - gw = gateway cli(["subsystem", "add", "--subsystem", subsystem_prefix + "1"]) subs_list = cli_test(["--format", "text", "subsystem", "list"]) assert subs_list != None diff --git a/tests/test_multi_gateway.py b/tests/test_multi_gateway.py index 85fe57798..08a845653 100644 --- a/tests/test_multi_gateway.py +++ b/tests/test_multi_gateway.py @@ -134,4 +134,3 @@ def test_multi_gateway_coordination(config, image, conn): assert nsListB[0]["uuid"] == uuid assert nsListB[0]["rbd_image_name"] == image assert nsListB[0]["rbd_pool_name"] == pool - diff --git a/tests/test_nsid.py b/tests/test_nsid.py index f31867251..4119b38cd 100644 --- a/tests/test_nsid.py +++ b/tests/test_nsid.py @@ -1,4 +1,3 @@ -import pytest import copy import grpc import json @@ -8,7 +7,6 @@ from control.cephutils import CephUtils from control.proto import gateway_pb2 as pb2 from control.proto import gateway_pb2_grpc as pb2_grpc -import spdk.rpc.bdev as rpc_bdev image = "mytestdevimage" pool = "rbd" diff --git a/tests/test_omap_lock.py b/tests/test_omap_lock.py index e9473bb99..eecea3993 100644 --- a/tests/test_omap_lock.py +++ b/tests/test_omap_lock.py @@ -16,7 +16,7 @@ host_nqn_prefix = "nqn.2014-08.org.nvmexpress:uuid:22207d09-d8af-4ed2-84ec-a6d80b" created_resource_count = 10 -def setup_config(config, gw1_name, gw2_name, gw_group, update_notify ,update_interval_sec, disable_unlock, lock_duration, +def setup_config(config, gw1_name, gw2_name, gw_group, update_notify, update_interval_sec, disable_unlock, lock_duration, sock1_name, sock2_name, port_inc): """Sets up the config objects for gateways A and B """ @@ -118,7 +118,8 @@ def conn_concurrent(config, request): ceph_utils = CephUtils(config) # Setup GatewayA and GatewayB configs - configA, configB = setup_config(config, "GatewayAAA", "GatewayBBB", "Group3", True, 5, False, 60, + configA, configB = setup_config(config, "GatewayAAA", "GatewayBBB", "Group3", update_notify, update_interval_sec, + disable_unlock, lock_duration, "spdk_GatewayAAA.sock", "spdk_GatewayBBB.sock", 4) addr = configA.get("gateway", "addr") @@ -254,7 +255,7 @@ def test_trying_to_lock_twice(config, image, conn_lock_twice, caplog): caplog.clear() stubA, stubB = conn_lock_twice - with pytest.raises(Exception) as ex: + with pytest.raises(Exception): create_resource_by_index(stubA, 100000, None) create_resource_by_index(stubB, 100001, None) assert "OMAP file unlock was disabled, will not unlock file" in caplog.text diff --git a/tests/test_psk.py b/tests/test_psk.py index 536035c8d..2338737cb 100644 --- a/tests/test_psk.py +++ b/tests/test_psk.py @@ -4,13 +4,7 @@ from control.cli import main as cli from control.cli import main_test as cli_test from control.cephutils import CephUtils -from control.utils import GatewayUtils -from control.config import GatewayConfig import grpc -from control.proto import gateway_pb2 as pb2 -from control.proto import gateway_pb2_grpc as pb2_grpc -import os -import os.path image = "mytestdevimage" pool = "rbd" @@ -57,7 +51,7 @@ def gateway(config): gateway.serve() # Bind the client and Gateway - channel = grpc.insecure_channel(f"{addr}:{port}") + grpc.insecure_channel(f"{addr}:{port}") yield gateway.gateway_rpc # Stop gateway @@ -65,7 +59,6 @@ def gateway(config): gateway.gateway_rpc.gateway_state.delete_state() def test_setup(caplog, gateway): - gw = gateway caplog.clear() cli(["subsystem", "add", "--subsystem", subsystem]) assert f"create_subsystem {subsystem}: True" in caplog.text @@ -183,13 +176,13 @@ def test_list_listeners(caplog, gateway): listeners = cli_test(["listener", "list", "--subsystem", subsystem]) assert len(listeners.listeners) == 2 found = 0 - for l in listeners.listeners: - if l.trsvcid == 5001: + for lstnr in listeners.listeners: + if lstnr.trsvcid == 5001: found += 1 - assert l.secure - elif l.trsvcid == 5002: + assert lstnr.secure + elif lstnr.trsvcid == 5002: found += 1 - assert not l.secure + assert not lstnr.secure else: assert False assert found == 2 diff --git a/tests/test_server.py b/tests/test_server.py index fdfd209c4..fb2adb483 100644 --- a/tests/test_server.py +++ b/tests/test_server.py @@ -18,11 +18,11 @@ def _config(self, config): def validate_exception(self, e): pattern = r'Gateway subprocess terminated pid=(\d+) exit_code=(-?\d+)' m = re.match(pattern, e.code) - assert(m) + assert m pid = int(m.group(1)) code = int(m.group(2)) - assert(pid > 0) - assert(code) + assert pid > 0 + assert code def remove_core_files(self, directory_path): # List all files starting with "core." in the core directory @@ -38,9 +38,9 @@ def remove_core_files(self, directory_path): print(f"Removed: {file_path}") def assert_no_core_files(self, directory_path): - assert(os.path.exists(directory_path) and os.path.isdir(directory_path)) + assert os.path.exists(directory_path) and os.path.isdir(directory_path) files = [f for f in os.listdir(directory_path) if os.path.isfile(os.path.join(directory_path, f)) and f.startswith("core.")] - assert(len(files) == 0) + assert len(files) == 0 def test_spdk_exception(self): """Tests spdk sub process exiting with error.""" @@ -80,7 +80,7 @@ def test_monc_exit(self): time.sleep(2) # Send SIGABRT (abort signal) to the monitor client process - assert(gateway.monitor_client_process) + assert gateway.monitor_client_process gateway.monitor_client_process.send_signal(signal.SIGABRT) # Block on running keep alive ping @@ -116,5 +116,6 @@ def test_spdk_multi_gateway_exception(self): gatewayB.serve() self.validate_exception(cm.exception) + if __name__ == '__main__': unittest.main() diff --git a/tests/test_state.py b/tests/test_state.py index fda1366d0..59c670935 100644 --- a/tests/test_state.py +++ b/tests/test_state.py @@ -181,8 +181,8 @@ def _state_notify_update(update, is_add_req): # to test notify capability elapsed = time.time() - start wait_interval = update_interval_sec - elapsed - 0.5 - assert(wait_interval > 0) - assert(wait_interval < update_interval_sec) + assert wait_interval > 0 + assert wait_interval < update_interval_sec time.sleep(wait_interval) # expect 4 updates: addition, two-step change and removal