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
|
2023-03-09 10:19:41 +00:00
|
|
|
from time import sleep
|
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
|
2023-03-09 10:19:41 +00:00
|
|
|
from frostfs_testlib.cli import FrostfsAdm, FrostfsCli
|
2023-01-09 12:46:03 +00:00
|
|
|
from frostfs_testlib.shell import Shell
|
2023-02-19 23:58:07 +00:00
|
|
|
from frostfs_testlib.utils import datetime_utils
|
2022-05-26 13:40:18 +00:00
|
|
|
|
2023-02-27 16:54:27 +00:00
|
|
|
from pytest_tests.helpers.cluster import Cluster, StorageNode
|
|
|
|
from pytest_tests.helpers.epoch import tick_epoch
|
2023-03-09 10:19:41 +00:00
|
|
|
from pytest_tests.resources.common import (
|
|
|
|
FROSTFS_CLI_EXEC,
|
|
|
|
FROSTFS_ADM_CONFIG_PATH,
|
|
|
|
FROSTFS_ADM_EXEC,
|
|
|
|
FROSTFS_CLI_EXEC,
|
|
|
|
MORPH_BLOCK_TIME,
|
|
|
|
)
|
2023-02-27 16:54:27 +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-12-05 22:31:45 +00:00
|
|
|
@allure.step("Stop random storage nodes")
|
|
|
|
def stop_random_storage_nodes(number: int, nodes: list[StorageNode]) -> list[StorageNode]:
|
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:
|
2022-12-05 22:31:45 +00:00
|
|
|
number: the number of storage nodes to stop
|
|
|
|
nodes: the list of storage nodes to stop
|
2022-09-20 15:03:52 +00:00
|
|
|
Returns:
|
2022-12-05 22:31:45 +00:00
|
|
|
the list of nodes that were stopped
|
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-12-05 22:31:45 +00:00
|
|
|
node.stop_service()
|
2022-07-26 19:44:05 +00:00
|
|
|
return nodes_to_stop
|
2022-05-26 13:40:18 +00:00
|
|
|
|
|
|
|
|
2022-12-05 22:31:45 +00:00
|
|
|
@allure.step("Start storage node")
|
|
|
|
def start_storage_nodes(nodes: list[StorageNode]) -> 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-12-05 22:31:45 +00:00
|
|
|
nodes: the list of nodes to start
|
2022-05-26 13:40:18 +00:00
|
|
|
"""
|
|
|
|
for node in nodes:
|
2022-12-05 22:31:45 +00:00
|
|
|
node.start_service()
|
2022-07-05 21:35:32 +00:00
|
|
|
|
|
|
|
|
2022-12-05 22:31:45 +00:00
|
|
|
@allure.step("Get Locode from random storage node")
|
|
|
|
def get_locode_from_random_node(cluster: Cluster) -> str:
|
|
|
|
node = random.choice(cluster.storage_nodes)
|
|
|
|
locode = node.get_un_locode()
|
|
|
|
logger.info(f"Chosen '{locode}' locode from node {node}")
|
2022-07-05 21:35:32 +00:00
|
|
|
return locode
|
2022-07-08 17:24:55 +00:00
|
|
|
|
|
|
|
|
2022-12-05 22:31:45 +00:00
|
|
|
@allure.step("Healthcheck for storage node {node}")
|
|
|
|
def storage_node_healthcheck(node: StorageNode) -> HealthStatus:
|
2022-07-08 17:24:55 +00:00
|
|
|
"""
|
2022-12-05 22:31:45 +00:00
|
|
|
The function returns storage node's health status.
|
2022-09-20 15:03:52 +00:00
|
|
|
Args:
|
2022-12-05 22:31:45 +00:00
|
|
|
node: storage node 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-12-05 22:31:45 +00:00
|
|
|
output = _run_control_command_with_retries(node, command)
|
2022-07-14 07:33:45 +00:00
|
|
|
return HealthStatus.from_stdout(output)
|
2022-07-08 17:24:55 +00:00
|
|
|
|
|
|
|
|
2022-12-05 22:31:45 +00:00
|
|
|
@allure.step("Set status for {node}")
|
|
|
|
def storage_node_set_status(node: StorageNode, 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-12-05 22:31:45 +00:00
|
|
|
node: node for which status should be set.
|
2022-10-13 18:53:44 +00:00
|
|
|
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-12-05 22:31:45 +00:00
|
|
|
_run_control_command_with_retries(node, command, retries)
|
2022-07-08 17:24:55 +00:00
|
|
|
|
|
|
|
|
2022-09-20 15:03:52 +00:00
|
|
|
@allure.step("Get netmap snapshot")
|
2022-12-05 22:31:45 +00:00
|
|
|
def get_netmap_snapshot(node: StorageNode, 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-12-05 22:31:45 +00:00
|
|
|
node: node 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-12-05 22:31:45 +00:00
|
|
|
|
|
|
|
storage_wallet_config = node.get_wallet_config_path()
|
|
|
|
storage_wallet_path = node.get_wallet_path()
|
|
|
|
|
2023-01-09 12:46:03 +00:00
|
|
|
cli = FrostfsCli(shell, FROSTFS_CLI_EXEC, config_file=storage_wallet_config)
|
2022-09-28 09:53:08 +00:00
|
|
|
return cli.netmap.snapshot(
|
2022-12-05 22:31:45 +00:00
|
|
|
rpc_endpoint=node.get_rpc_endpoint(),
|
|
|
|
wallet=storage_wallet_path,
|
2022-10-13 18:53:44 +00:00
|
|
|
).stdout
|
2022-07-08 17:24:55 +00:00
|
|
|
|
|
|
|
|
2022-12-05 22:31:45 +00:00
|
|
|
@allure.step("Get shard list for {node}")
|
|
|
|
def node_shard_list(node: StorageNode) -> list[str]:
|
2022-07-08 17:24:55 +00:00
|
|
|
"""
|
2022-12-05 22:31:45 +00:00
|
|
|
The function returns list of shards for specified storage node.
|
2022-09-20 15:03:52 +00:00
|
|
|
Args:
|
2022-12-05 22:31:45 +00:00
|
|
|
node: node 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-12-05 22:31:45 +00:00
|
|
|
output = _run_control_command_with_retries(node, command)
|
2022-09-20 15:03:52 +00:00
|
|
|
return re.findall(r"Shard (.*):", output)
|
2022-07-08 17:24:55 +00:00
|
|
|
|
|
|
|
|
2022-12-05 22:31:45 +00:00
|
|
|
@allure.step("Shard set for {node}")
|
|
|
|
def node_shard_set_mode(node: StorageNode, 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-12-05 22:31:45 +00:00
|
|
|
node: node 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-12-05 22:31:45 +00:00
|
|
|
return _run_control_command_with_retries(node, command)
|
2022-07-08 17:24:55 +00:00
|
|
|
|
|
|
|
|
2022-12-05 22:31:45 +00:00
|
|
|
@allure.step("Drop object from {node}")
|
|
|
|
def drop_object(node: StorageNode, 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-12-05 22:31:45 +00:00
|
|
|
node_id str: node 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-12-05 22:31:45 +00:00
|
|
|
return _run_control_command_with_retries(node, command)
|
2022-07-26 19:44:05 +00:00
|
|
|
|
|
|
|
|
2022-12-05 22:31:45 +00:00
|
|
|
@allure.step("Delete data from host for node {node}")
|
|
|
|
def delete_node_data(node: StorageNode) -> None:
|
|
|
|
node.stop_service()
|
|
|
|
node.host.delete_storage_node_data(node.name)
|
2023-02-19 23:58:07 +00:00
|
|
|
time.sleep(datetime_utils.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(
|
2022-12-05 22:31:45 +00:00
|
|
|
node_to_exclude: StorageNode,
|
|
|
|
alive_node: StorageNode,
|
|
|
|
shell: Shell,
|
|
|
|
cluster: Cluster,
|
2022-10-13 16:13:45 +00:00
|
|
|
) -> None:
|
2022-12-05 22:31:45 +00:00
|
|
|
node_netmap_key = node_to_exclude.get_wallet_public_key()
|
2022-08-04 21:03:06 +00:00
|
|
|
|
2022-12-05 22:31:45 +00:00
|
|
|
storage_node_set_status(node_to_exclude, status="offline")
|
2022-08-04 21:03:06 +00:00
|
|
|
|
2023-02-19 23:58:07 +00:00
|
|
|
time.sleep(datetime_utils.parse_time(MORPH_BLOCK_TIME))
|
2022-12-05 22:31:45 +00:00
|
|
|
tick_epoch(shell, cluster)
|
2022-08-04 21:03:06 +00:00
|
|
|
|
2022-12-05 22:31:45 +00:00
|
|
|
snapshot = get_netmap_snapshot(node=alive_node, shell=shell)
|
2022-09-20 15:03:52 +00:00
|
|
|
assert (
|
|
|
|
node_netmap_key not in snapshot
|
2022-12-05 22:31:45 +00:00
|
|
|
), f"Expected node with key {node_netmap_key} to be absent 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(
|
2022-12-05 22:31:45 +00:00
|
|
|
node_to_include: StorageNode,
|
|
|
|
alive_node: StorageNode,
|
|
|
|
shell: Shell,
|
|
|
|
cluster: Cluster,
|
2022-10-13 16:13:45 +00:00
|
|
|
) -> None:
|
2022-12-05 22:31:45 +00:00
|
|
|
storage_node_set_status(node_to_include, status="online")
|
2022-08-04 21:03:06 +00:00
|
|
|
|
2022-11-10 14:35:28 +00:00
|
|
|
# Per suggestion of @fyrchik we need to wait for 2 blocks after we set status and after tick epoch.
|
2023-01-09 12:46:03 +00:00
|
|
|
# First sleep can be omitted after https://github.com/nspcc-dev/frostfs-node/issues/1790 complete.
|
2022-11-10 14:35:28 +00:00
|
|
|
|
2023-02-19 23:58:07 +00:00
|
|
|
time.sleep(datetime_utils.parse_time(MORPH_BLOCK_TIME) * 2)
|
2022-12-05 22:31:45 +00:00
|
|
|
tick_epoch(shell, cluster)
|
2023-02-19 23:58:07 +00:00
|
|
|
time.sleep(datetime_utils.parse_time(MORPH_BLOCK_TIME) * 2)
|
2022-08-04 21:03:06 +00:00
|
|
|
|
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-12-05 22:31:45 +00:00
|
|
|
@allure.step("Check node {node} in network map")
|
|
|
|
def check_node_in_map(
|
|
|
|
node: StorageNode, shell: Shell, alive_node: Optional[StorageNode] = None
|
|
|
|
) -> None:
|
|
|
|
alive_node = alive_node or node
|
2022-08-04 21:03:06 +00:00
|
|
|
|
2022-12-05 22:31:45 +00:00
|
|
|
node_netmap_key = node.get_wallet_public_key()
|
|
|
|
logger.info(f"Node ({node.label}) netmap key: {node_netmap_key}")
|
2022-08-04 21:03:06 +00:00
|
|
|
|
2022-12-05 22:31:45 +00:00
|
|
|
snapshot = get_netmap_snapshot(alive_node, shell)
|
|
|
|
assert (
|
|
|
|
node_netmap_key in snapshot
|
|
|
|
), f"Expected node with key {node_netmap_key} to be in network map"
|
2022-08-04 21:03:06 +00:00
|
|
|
|
2023-03-09 10:19:41 +00:00
|
|
|
@allure.step("Check node {node} NOT in network map")
|
|
|
|
def check_node_not_in_map(
|
|
|
|
node: StorageNode, shell: Shell, alive_node: Optional[StorageNode] = None
|
|
|
|
) -> None:
|
|
|
|
alive_node = alive_node or node
|
|
|
|
|
|
|
|
node_netmap_key = node.get_wallet_public_key()
|
|
|
|
logger.info(f"Node ({node.label}) netmap key: {node_netmap_key}")
|
|
|
|
|
|
|
|
snapshot = get_netmap_snapshot(alive_node, shell)
|
|
|
|
assert (
|
|
|
|
node_netmap_key not in snapshot
|
|
|
|
), f"Expected node with key {node_netmap_key} to be NOT in network map"
|
|
|
|
|
|
|
|
@allure.step("Wait for node {node} is ready")
|
|
|
|
def wait_for_node_to_be_ready(node: StorageNode) -> None:
|
|
|
|
timeout, attempts = 30, 6
|
|
|
|
for _ in range(attempts):
|
|
|
|
try:
|
|
|
|
health_check = storage_node_healthcheck(node)
|
|
|
|
if health_check.health_status == "READY":
|
|
|
|
return
|
|
|
|
except Exception as err:
|
|
|
|
logger.warning(f"Node {node} is not ready:\n{err}")
|
|
|
|
sleep(timeout)
|
|
|
|
raise AssertionError(
|
|
|
|
f"Node {node} hasn't gone to the READY state after {timeout * attempts} seconds"
|
|
|
|
)
|
|
|
|
|
|
|
|
@allure.step("Remove nodes from network map trough cli-adm morph command")
|
|
|
|
def remove_nodes_from_map_morph(shell: Shell, cluster: Cluster, remove_nodes: list[StorageNode], alive_node: Optional[StorageNode] = None):
|
|
|
|
"""
|
|
|
|
Move node to the Offline state in the candidates list and tick an epoch to update the netmap
|
|
|
|
using frostfs-adm
|
|
|
|
Args:
|
|
|
|
shell: local shell to make queries about current epoch. Remote shell will be used to tick new one
|
|
|
|
cluster: cluster instance under test
|
|
|
|
alive_node: node to send requests to (first node in cluster by default)
|
|
|
|
remove_nodes: list of nodes which would be removed from map
|
|
|
|
"""
|
|
|
|
|
|
|
|
alive_node = alive_node if alive_node else remove_nodes[0]
|
|
|
|
remote_shell = alive_node.host.get_shell()
|
|
|
|
|
|
|
|
node_netmap_keys = list(map(StorageNode.get_wallet_public_key, remove_nodes))
|
|
|
|
logger.info(f"Nodes netmap keys are: {' '.join(node_netmap_keys)}")
|
|
|
|
|
|
|
|
if FROSTFS_ADM_EXEC and FROSTFS_ADM_CONFIG_PATH:
|
|
|
|
# If frostfs-adm is available, then we tick epoch with it (to be consistent with UAT tests)
|
|
|
|
frostfsadm = FrostfsAdm(
|
|
|
|
shell=remote_shell,
|
|
|
|
frostfs_adm_exec_path=FROSTFS_ADM_EXEC,
|
|
|
|
config_file=FROSTFS_ADM_CONFIG_PATH,
|
|
|
|
)
|
|
|
|
frostfsadm.morph.remove_nodes(node_netmap_keys)
|
|
|
|
|
2022-08-04 21:03:06 +00:00
|
|
|
|
2022-12-05 22:31:45 +00:00
|
|
|
def _run_control_command_with_retries(node: StorageNode, 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-12-05 22:31:45 +00:00
|
|
|
return _run_control_command(node, 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
|
|
|
|
|
|
|
|
2022-12-05 22:31:45 +00:00
|
|
|
def _run_control_command(node: StorageNode, command: str) -> None:
|
|
|
|
host = node.host
|
2022-10-09 20:01:59 +00:00
|
|
|
|
2022-12-05 22:31:45 +00:00
|
|
|
service_config = host.get_service_config(node.name)
|
2022-10-09 20:01:59 +00:00
|
|
|
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()
|
2022-12-05 22:31:45 +00:00
|
|
|
wallet_config_path = f"/tmp/{node.name}-config.yaml"
|
2022-10-09 20:01:59 +00:00
|
|
|
wallet_config = f'password: "{wallet_password}"'
|
|
|
|
shell.exec(f"echo '{wallet_config}' > {wallet_config_path}")
|
|
|
|
|
2023-01-09 12:46:03 +00:00
|
|
|
cli_config = host.get_cli_config("frostfs-cli")
|
2022-10-09 20:01:59 +00:00
|
|
|
|
|
|
|
# TODO: implement cli.control
|
2023-01-09 12:46:03 +00:00
|
|
|
# cli = frostfsCli(shell, cli_config.exec_path, wallet_config_path)
|
2022-10-09 20:01:59 +00:00
|
|
|
result = shell.exec(
|
|
|
|
f"{cli_config.exec_path} {command} --endpoint {control_endpoint} "
|
|
|
|
f"--wallet {wallet_path} --config {wallet_config_path}"
|
|
|
|
)
|
|
|
|
return result.stdout
|