2022-09-20 15:03:52 +00:00
|
|
|
import logging
|
2022-05-26 13:40:18 +00:00
|
|
|
import random
|
2022-07-08 17:24:55 +00:00
|
|
|
import re
|
2022-08-04 21:03:06 +00:00
|
|
|
import time
|
2022-07-08 17:24:55 +00:00
|
|
|
from dataclasses import dataclass
|
2022-07-14 07:33:45 +00:00
|
|
|
from typing import Optional
|
2022-05-26 13:40:18 +00:00
|
|
|
|
2022-09-20 15:03:52 +00:00
|
|
|
import allure
|
2022-10-13 18:53:44 +00:00
|
|
|
from common import (
|
|
|
|
MORPH_BLOCK_TIME,
|
|
|
|
NEOFS_CLI_EXEC,
|
|
|
|
NEOFS_NETMAP_DICT,
|
|
|
|
STORAGE_WALLET_CONFIG,
|
|
|
|
STORAGE_WALLET_PASS,
|
|
|
|
)
|
2022-08-04 21:03:06 +00:00
|
|
|
from data_formatters import get_wallet_public_key
|
|
|
|
from epoch import tick_epoch
|
2022-10-13 18:53:44 +00:00
|
|
|
from neofs_testlib.cli import NeofsCli
|
2022-10-09 20:01:59 +00:00
|
|
|
from neofs_testlib.hosting import Hosting
|
2022-10-13 16:13:45 +00:00
|
|
|
from neofs_testlib.shell import Shell
|
2022-09-23 11:09:41 +00:00
|
|
|
from utility import parse_time
|
2022-05-26 13:40:18 +00:00
|
|
|
|
2022-09-20 15:03:52 +00:00
|
|
|
logger = logging.getLogger("NeoLogger")
|
2022-05-26 13:40:18 +00:00
|
|
|
|
2022-06-09 13:08:11 +00:00
|
|
|
|
2022-07-08 17:24:55 +00:00
|
|
|
@dataclass
|
|
|
|
class HealthStatus:
|
2022-09-28 09:53:08 +00:00
|
|
|
network_status: Optional[str] = None
|
|
|
|
health_status: Optional[str] = None
|
2022-07-08 17:24:55 +00:00
|
|
|
|
|
|
|
@staticmethod
|
2022-09-20 15:03:52 +00:00
|
|
|
def from_stdout(output: str) -> "HealthStatus":
|
2022-07-08 17:24:55 +00:00
|
|
|
network, health = None, None
|
2022-09-20 15:03:52 +00:00
|
|
|
for line in output.split("\n"):
|
|
|
|
if "Network status" in line:
|
|
|
|
network = line.split(":")[-1].strip()
|
|
|
|
if "Health status" in line:
|
|
|
|
health = line.split(":")[-1].strip()
|
2022-07-08 17:24:55 +00:00
|
|
|
return HealthStatus(network, health)
|
|
|
|
|
|
|
|
|
2022-09-28 09:53:08 +00:00
|
|
|
@allure.step("Stop storage nodes")
|
2022-10-09 20:01:59 +00:00
|
|
|
def stop_nodes(hosting: Hosting, number: int, nodes: list[str]) -> list[str]:
|
2022-05-26 13:40:18 +00:00
|
|
|
"""
|
2022-09-28 09:53:08 +00:00
|
|
|
Shuts down the given number of randomly selected storage nodes.
|
2022-09-20 15:03:52 +00:00
|
|
|
Args:
|
|
|
|
number (int): the number of nodes to shut down
|
|
|
|
nodes (list): the list of nodes for possible shut down
|
|
|
|
Returns:
|
2022-09-28 09:53:08 +00:00
|
|
|
(list): the list of nodes that were shut down
|
2022-05-26 13:40:18 +00:00
|
|
|
"""
|
2022-07-26 19:44:05 +00:00
|
|
|
nodes_to_stop = random.sample(nodes, number)
|
|
|
|
for node in nodes_to_stop:
|
2022-10-09 20:01:59 +00:00
|
|
|
host = hosting.get_host_by_service(node)
|
|
|
|
host.stop_service(node)
|
2022-07-26 19:44:05 +00:00
|
|
|
return nodes_to_stop
|
2022-05-26 13:40:18 +00:00
|
|
|
|
|
|
|
|
2022-09-28 09:53:08 +00:00
|
|
|
@allure.step("Start storage nodes")
|
2022-10-09 20:01:59 +00:00
|
|
|
def start_nodes(hosting: Hosting, nodes: list[str]) -> None:
|
2022-05-26 13:40:18 +00:00
|
|
|
"""
|
2022-09-28 09:53:08 +00:00
|
|
|
The function starts specified storage nodes.
|
2022-09-20 15:03:52 +00:00
|
|
|
Args:
|
2022-09-28 09:53:08 +00:00
|
|
|
nodes (list): the list of nodes to start
|
2022-05-26 13:40:18 +00:00
|
|
|
"""
|
|
|
|
for node in nodes:
|
2022-10-09 20:01:59 +00:00
|
|
|
host = hosting.get_host_by_service(node)
|
|
|
|
host.start_service(node)
|
2022-07-05 21:35:32 +00:00
|
|
|
|
|
|
|
|
2022-09-20 15:03:52 +00:00
|
|
|
@allure.step("Get Locode")
|
2022-09-28 09:53:08 +00:00
|
|
|
def get_locode() -> str:
|
2022-07-05 21:35:32 +00:00
|
|
|
endpoint_values = random.choice(list(NEOFS_NETMAP_DICT.values()))
|
2022-09-20 15:03:52 +00:00
|
|
|
locode = endpoint_values["UN-LOCODE"]
|
|
|
|
logger.info(f"Random locode chosen: {locode}")
|
2022-07-05 21:35:32 +00:00
|
|
|
return locode
|
2022-07-08 17:24:55 +00:00
|
|
|
|
|
|
|
|
2022-09-28 09:53:08 +00:00
|
|
|
@allure.step("Healthcheck for node {node_name}")
|
2022-10-09 20:01:59 +00:00
|
|
|
def node_healthcheck(hosting: Hosting, node_name: str) -> HealthStatus:
|
2022-07-08 17:24:55 +00:00
|
|
|
"""
|
2022-09-20 15:03:52 +00:00
|
|
|
The function returns node's health status.
|
|
|
|
Args:
|
2022-09-28 09:53:08 +00:00
|
|
|
node_name str: node name for which health status should be retrieved.
|
2022-09-20 15:03:52 +00:00
|
|
|
Returns:
|
|
|
|
health status as HealthStatus object.
|
2022-07-08 17:24:55 +00:00
|
|
|
"""
|
2022-07-14 07:33:45 +00:00
|
|
|
command = "control healthcheck"
|
2022-10-09 20:01:59 +00:00
|
|
|
output = _run_control_command_with_retries(hosting, node_name, command)
|
2022-07-14 07:33:45 +00:00
|
|
|
return HealthStatus.from_stdout(output)
|
2022-07-08 17:24:55 +00:00
|
|
|
|
|
|
|
|
2022-09-28 09:53:08 +00:00
|
|
|
@allure.step("Set status for node {node_name}")
|
2022-10-09 20:01:59 +00:00
|
|
|
def node_set_status(hosting: Hosting, node_name: str, status: str, retries: int = 0) -> None:
|
2022-07-08 17:24:55 +00:00
|
|
|
"""
|
2022-09-20 15:03:52 +00:00
|
|
|
The function sets particular status for given node.
|
|
|
|
Args:
|
2022-10-13 18:53:44 +00:00
|
|
|
node_name: node name for which status should be set.
|
|
|
|
status: online or offline.
|
2022-09-20 15:03:52 +00:00
|
|
|
retries (optional, int): number of retry attempts if it didn't work from the first time
|
2022-07-08 17:24:55 +00:00
|
|
|
"""
|
2022-07-14 07:33:45 +00:00
|
|
|
command = f"control set-status --status {status}"
|
2022-10-09 20:01:59 +00:00
|
|
|
_run_control_command_with_retries(hosting, node_name, command, retries)
|
2022-07-08 17:24:55 +00:00
|
|
|
|
|
|
|
|
2022-09-20 15:03:52 +00:00
|
|
|
@allure.step("Get netmap snapshot")
|
2022-10-13 18:53:44 +00:00
|
|
|
def get_netmap_snapshot(node_name: str, shell: Shell) -> str:
|
2022-07-08 17:24:55 +00:00
|
|
|
"""
|
2022-09-28 09:53:08 +00:00
|
|
|
The function returns string representation of netmap snapshot.
|
2022-09-20 15:03:52 +00:00
|
|
|
Args:
|
2022-09-28 09:53:08 +00:00
|
|
|
node_name str: node name from which netmap snapshot should be requested.
|
2022-09-20 15:03:52 +00:00
|
|
|
Returns:
|
2022-09-28 09:53:08 +00:00
|
|
|
string representation of netmap
|
2022-07-08 17:24:55 +00:00
|
|
|
"""
|
2022-09-28 09:53:08 +00:00
|
|
|
node_info = NEOFS_NETMAP_DICT[node_name]
|
2022-10-13 18:53:44 +00:00
|
|
|
cli = NeofsCli(shell, NEOFS_CLI_EXEC, config_file=STORAGE_WALLET_CONFIG)
|
2022-09-28 09:53:08 +00:00
|
|
|
return cli.netmap.snapshot(
|
|
|
|
rpc_endpoint=node_info["rpc"],
|
|
|
|
wallet=node_info["wallet_path"],
|
2022-10-13 18:53:44 +00:00
|
|
|
).stdout
|
2022-07-08 17:24:55 +00:00
|
|
|
|
|
|
|
|
2022-09-28 09:53:08 +00:00
|
|
|
@allure.step("Get shard list for node {node_name}")
|
2022-10-09 20:01:59 +00:00
|
|
|
def node_shard_list(hosting: Hosting, node_name: str) -> list[str]:
|
2022-07-08 17:24:55 +00:00
|
|
|
"""
|
2022-09-28 09:53:08 +00:00
|
|
|
The function returns list of shards for specified node.
|
2022-09-20 15:03:52 +00:00
|
|
|
Args:
|
2022-09-28 09:53:08 +00:00
|
|
|
node_name str: node name for which shards should be returned.
|
2022-09-20 15:03:52 +00:00
|
|
|
Returns:
|
|
|
|
list of shards.
|
2022-07-08 17:24:55 +00:00
|
|
|
"""
|
2022-07-14 07:33:45 +00:00
|
|
|
command = "control shards list"
|
2022-10-09 20:01:59 +00:00
|
|
|
output = _run_control_command_with_retries(hosting, node_name, command)
|
2022-09-20 15:03:52 +00:00
|
|
|
return re.findall(r"Shard (.*):", output)
|
2022-07-08 17:24:55 +00:00
|
|
|
|
|
|
|
|
2022-09-28 09:53:08 +00:00
|
|
|
@allure.step("Shard set for node {node_name}")
|
2022-10-09 20:01:59 +00:00
|
|
|
def node_shard_set_mode(hosting: Hosting, node_name: str, shard: str, mode: str) -> str:
|
2022-07-08 17:24:55 +00:00
|
|
|
"""
|
2022-09-28 09:53:08 +00:00
|
|
|
The function sets mode for specified shard.
|
2022-09-20 15:03:52 +00:00
|
|
|
Args:
|
2022-09-28 09:53:08 +00:00
|
|
|
node_name str: node name on which shard mode should be set.
|
2022-07-08 17:24:55 +00:00
|
|
|
"""
|
2022-09-28 09:53:08 +00:00
|
|
|
command = f"control shards set-mode --id {shard} --mode {mode}"
|
2022-10-09 20:01:59 +00:00
|
|
|
return _run_control_command_with_retries(hosting, node_name, command)
|
2022-07-08 17:24:55 +00:00
|
|
|
|
|
|
|
|
2022-09-20 15:03:52 +00:00
|
|
|
@allure.step("Drop object from node {node_name}")
|
2022-10-09 20:01:59 +00:00
|
|
|
def drop_object(hosting: Hosting, node_name: str, cid: str, oid: str) -> str:
|
2022-07-08 17:24:55 +00:00
|
|
|
"""
|
2022-09-28 09:53:08 +00:00
|
|
|
The function drops object from specified node.
|
2022-09-20 15:03:52 +00:00
|
|
|
Args:
|
2022-09-28 09:53:08 +00:00
|
|
|
node_name str: node name from which object should be dropped.
|
2022-07-08 17:24:55 +00:00
|
|
|
"""
|
2022-07-14 07:33:45 +00:00
|
|
|
command = f"control drop-objects -o {cid}/{oid}"
|
2022-10-09 20:01:59 +00:00
|
|
|
return _run_control_command_with_retries(hosting, node_name, command)
|
2022-07-26 19:44:05 +00:00
|
|
|
|
|
|
|
|
2022-09-20 15:03:52 +00:00
|
|
|
@allure.step("Delete data of node {node_name}")
|
2022-10-09 20:01:59 +00:00
|
|
|
def delete_node_data(hosting: Hosting, node_name: str) -> None:
|
|
|
|
host = hosting.get_host_by_service(node_name)
|
|
|
|
host.stop_service(node_name)
|
|
|
|
host.delete_storage_node_data(node_name)
|
2022-09-23 11:09:41 +00:00
|
|
|
time.sleep(parse_time(MORPH_BLOCK_TIME))
|
2022-08-04 21:03:06 +00:00
|
|
|
|
|
|
|
|
2022-09-23 14:45:44 +00:00
|
|
|
@allure.step("Exclude node {node_to_exclude} from network map")
|
2022-10-13 16:13:45 +00:00
|
|
|
def exclude_node_from_network_map(
|
|
|
|
hosting: Hosting, node_to_exclude: str, alive_node: str, shell: Shell
|
|
|
|
) -> None:
|
2022-09-20 15:03:52 +00:00
|
|
|
node_wallet_path = NEOFS_NETMAP_DICT[node_to_exclude]["wallet_path"]
|
2022-09-28 09:53:08 +00:00
|
|
|
node_netmap_key = get_wallet_public_key(node_wallet_path, STORAGE_WALLET_PASS)
|
2022-08-04 21:03:06 +00:00
|
|
|
|
2022-10-09 20:01:59 +00:00
|
|
|
node_set_status(hosting, node_to_exclude, status="offline")
|
2022-08-04 21:03:06 +00:00
|
|
|
|
2022-09-23 11:09:41 +00:00
|
|
|
time.sleep(parse_time(MORPH_BLOCK_TIME))
|
2022-08-04 21:03:06 +00:00
|
|
|
tick_epoch()
|
|
|
|
|
2022-10-13 18:53:44 +00:00
|
|
|
snapshot = get_netmap_snapshot(node_name=alive_node, shell=shell)
|
2022-09-20 15:03:52 +00:00
|
|
|
assert (
|
|
|
|
node_netmap_key not in snapshot
|
|
|
|
), f"Expected node with key {node_netmap_key} not in network map"
|
2022-08-04 21:03:06 +00:00
|
|
|
|
|
|
|
|
2022-09-20 15:03:52 +00:00
|
|
|
@allure.step("Include node {node_to_include} into network map")
|
2022-10-13 16:13:45 +00:00
|
|
|
def include_node_to_network_map(
|
|
|
|
hosting: Hosting, node_to_include: str, alive_node: str, shell: Shell
|
|
|
|
) -> None:
|
2022-10-09 20:01:59 +00:00
|
|
|
node_set_status(hosting, node_to_include, status="online")
|
2022-08-04 21:03:06 +00:00
|
|
|
|
2022-09-23 11:09:41 +00:00
|
|
|
time.sleep(parse_time(MORPH_BLOCK_TIME))
|
2022-08-04 21:03:06 +00:00
|
|
|
tick_epoch()
|
|
|
|
|
2022-10-13 16:13:45 +00:00
|
|
|
check_node_in_map(node_to_include, shell, alive_node)
|
2022-08-04 21:03:06 +00:00
|
|
|
|
|
|
|
|
2022-09-20 15:03:52 +00:00
|
|
|
@allure.step("Check node {node_name} in network map")
|
2022-10-13 18:53:44 +00:00
|
|
|
def check_node_in_map(node_name: str, shell: Shell, alive_node: Optional[str] = None) -> None:
|
2022-08-04 21:03:06 +00:00
|
|
|
alive_node = alive_node or node_name
|
2022-09-20 15:03:52 +00:00
|
|
|
node_wallet_path = NEOFS_NETMAP_DICT[node_name]["wallet_path"]
|
2022-09-28 09:53:08 +00:00
|
|
|
node_netmap_key = get_wallet_public_key(node_wallet_path, STORAGE_WALLET_PASS)
|
2022-08-04 21:03:06 +00:00
|
|
|
|
2022-09-20 15:03:52 +00:00
|
|
|
logger.info(f"Node {node_name} netmap key: {node_netmap_key}")
|
2022-08-04 21:03:06 +00:00
|
|
|
|
2022-10-13 18:53:44 +00:00
|
|
|
snapshot = get_netmap_snapshot(node_name=alive_node, shell=shell)
|
2022-09-20 15:03:52 +00:00
|
|
|
assert node_netmap_key in snapshot, f"Expected node with key {node_netmap_key} in network map"
|
2022-08-04 21:03:06 +00:00
|
|
|
|
|
|
|
|
2022-10-09 20:01:59 +00:00
|
|
|
def _run_control_command_with_retries(
|
|
|
|
hosting: Hosting, node_name: str, command: str, retries: int = 0
|
|
|
|
) -> str:
|
2022-07-26 19:44:05 +00:00
|
|
|
for attempt in range(1 + retries): # original attempt + specified retries
|
|
|
|
try:
|
2022-10-09 20:01:59 +00:00
|
|
|
return _run_control_command(hosting, node_name, command)
|
2022-07-26 19:44:05 +00:00
|
|
|
except AssertionError as err:
|
|
|
|
if attempt < retries:
|
2022-09-20 15:03:52 +00:00
|
|
|
logger.warning(f"Command {command} failed with error {err} and will be retried")
|
2022-07-26 19:44:05 +00:00
|
|
|
continue
|
2022-09-20 15:03:52 +00:00
|
|
|
raise AssertionError(f"Command {command} failed with error {err}") from err
|
2022-10-09 20:01:59 +00:00
|
|
|
|
|
|
|
|
|
|
|
def _run_control_command(hosting: Hosting, service_name: str, command: str) -> None:
|
|
|
|
host = hosting.get_host_by_service(service_name)
|
|
|
|
|
|
|
|
service_config = host.get_service_config(service_name)
|
|
|
|
wallet_path = service_config.attributes["wallet_path"]
|
|
|
|
wallet_password = service_config.attributes["wallet_password"]
|
|
|
|
control_endpoint = service_config.attributes["control_endpoint"]
|
|
|
|
|
|
|
|
shell = host.get_shell()
|
|
|
|
wallet_config_path = f"/tmp/{service_name}-config.yaml"
|
|
|
|
wallet_config = f'password: "{wallet_password}"'
|
|
|
|
shell.exec(f"echo '{wallet_config}' > {wallet_config_path}")
|
|
|
|
|
|
|
|
cli_config = host.get_cli_config("neofs-cli")
|
|
|
|
|
|
|
|
# TODO: implement cli.control
|
|
|
|
# cli = NeofsCli(shell, cli_config.exec_path, wallet_config_path)
|
|
|
|
result = shell.exec(
|
|
|
|
f"{cli_config.exec_path} {command} --endpoint {control_endpoint} "
|
|
|
|
f"--wallet {wallet_path} --config {wallet_config_path}"
|
|
|
|
)
|
|
|
|
return result.stdout
|