diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index e1bb227..06fc3b0 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -277,6 +277,12 @@ jobs: source venv/bin/activate && pytest test_downloads.py --base_url="$OUTPUT_CONTAINER_URL" --report_dir="$REPORT_DIR" --data_dir_prefix=../$REPORT_DIR working-directory: ./tests - - name: tmate session - if: always() - uses: mxschmitt/action-tmate@v3 + - name: Run obj count test + env: + NEOFS_WALLET: "../wallet.json" + NEOFS_WALLET_PASSWORD: ${{ secrets.NEOFS_WALLET_PASSWORD }} + NEOFS_NETWORK_DOMAIN: ${{ vars.NEOFS_NETWORK_DOMAIN }} + STORE_OBJECTS_CID: ${{ vars.STORE_OBJECTS_CID }} + run: | + source venv/bin/activate && pytest test_objects.py + working-directory: ./tests diff --git a/helpers/neofs.py b/helpers/neofs.py new file mode 100644 index 0000000..92baa09 --- /dev/null +++ b/helpers/neofs.py @@ -0,0 +1,45 @@ +import subprocess +import json + + +def neofs_cli_execute(cmd: str, json_output: bool = False, timeout: int = None): + """ + Executes a given command and returns its output. + + :param cmd: Command to execute. + :param json_output: Specifies if the command output is JSON. + :param timeout: Optional timeout for command execution. + :return: Command output as a string or a JSON object. + """ + + try: + compl_proc = subprocess.run( + cmd, + check=True, + text=True, + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + timeout=timeout, + shell=True, + ) + + print(f"RC: {compl_proc.returncode}") + print(f"Output: {compl_proc.stdout}") + + if json_output: + try: + return json.loads(compl_proc.stdout) + except json.JSONDecodeError: + output_list = compl_proc.stdout.splitlines() + return json.dumps(output_list) + else: + return compl_proc.stdout.splitlines() + + except subprocess.CalledProcessError as e: + raise Exception( + f"Command failed: {e.cmd}\n" + f"Error code: {e.returncode}\n" + f"Output: {e.output}\n" + f"Stdout: {e.stdout}\n" + f"Stderr: {e.stderr}\n" + ) diff --git a/push-to-neofs.py b/push-to-neofs.py index 0c75de8..eecbbd4 100644 --- a/push-to-neofs.py +++ b/push-to-neofs.py @@ -3,8 +3,8 @@ import subprocess import argparse import magic -import json import distutils.util +from helpers.neofs import neofs_cli_execute FILE_PATH = "FilePath" # the key for the attribute, is the path for the static page and allure report zip files CONTENT_TYPE = "ContentType" @@ -113,45 +113,6 @@ def get_rpc_endpoint(neofs_domain: str) -> str: return f"{neofs_domain}:{PORT_8080}" -def neofs_cli_execute(cmd: str, json_output: bool = False, timeout: int = None): - """ - Executes a given command and returns its output. - - :param cmd: Command to execute. - :param json_output: Specifies if the command output is JSON. - :param timeout: Optional timeout for command execution. - :return: Command output as a string or a JSON object. - """ - - try: - compl_proc = subprocess.run( - cmd, - check=True, - text=True, - stdout=subprocess.PIPE, - stderr=subprocess.PIPE, - timeout=timeout, - shell=True, - ) - - print(f"RC: {compl_proc.returncode}") - print(f"Output: {compl_proc.stdout}") - - if json_output: - return json.loads(compl_proc.stdout) - else: - return compl_proc.stdout.splitlines() - - except subprocess.CalledProcessError as e: - raise Exception( - f"Command failed: {e.cmd}\n" - f"Error code: {e.returncode}\n" - f"Output: {e.output}\n" - f"Stdout: {e.stdout}\n" - f"Stderr: {e.stderr}\n" - ) - - def search_objects_in_container(endpoint: str, wallet: str, password: str, diff --git a/tests/conftest.py b/tests/conftest.py index f1a3d98..831c13f 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,6 +1,16 @@ +import os import pytest +DATA_PATH_LIST = [ + "data/1.txt", + "data/2.txt", + "data/dir/3.txt", + "data/dir/subdir/4.txt", + "data/dir/subdir/subdir_2/5.txt", + ] + + def pytest_addoption(parser): parser.addoption( "--base_url", action="store", default=None, help="Base URL to test against" @@ -17,18 +27,42 @@ def pytest_addoption(parser): default=None, help="Prefix dir to add to modified test data", ) + parser.addoption("--wallet", action="store", help="NeoFS wallet") + parser.addoption("--wallet-password", action="store", help="NeoFS wallet password") + parser.addoption("--network-domain", action="store", help="NeoFS network domain") + parser.addoption("--cid", action="store", help="NeoFS Container ID") @pytest.fixture def base_url(request): - return request.config.getoption("--base_url") + return os.environ.get('BASE_URL') or request.config.getoption("--base_url") @pytest.fixture def report_dir(request): - return request.config.getoption("--report_dir") + return os.environ.get('REPORT_DIR') or request.config.getoption("--report_dir") @pytest.fixture def data_dir_prefix(request): - return request.config.getoption("--data_dir_prefix") + return os.environ.get('DATA_DIR_PREFIX') or request.config.getoption("--data_dir_prefix") + + +@pytest.fixture +def wallet(request): + return os.environ.get('NEOFS_WALLET') or request.config.getoption("--wallet") + + +@pytest.fixture +def wallet_password(request): + return os.environ.get('NEOFS_WALLET_PASSWORD') or request.config.getoption("--wallet-password") + + +@pytest.fixture +def network_domain(request): + return os.environ.get('NEOFS_NETWORK_DOMAIN') or request.config.getoption("--network-domain") + + +@pytest.fixture +def cid(request): + return os.environ.get('STORE_OBJECTS_CID') or request.config.getoption("--cid") diff --git a/tests/test_downloads.py b/tests/test_downloads.py index c0c3bda..46f68b2 100644 --- a/tests/test_downloads.py +++ b/tests/test_downloads.py @@ -2,6 +2,7 @@ import requests import pytest from urllib.parse import urljoin +from conftest import DATA_PATH_LIST def download_file(url: str) -> str: @@ -10,16 +11,7 @@ def download_file(url: str) -> str: return response.text -@pytest.mark.parametrize( - "path", - [ - "data/1.txt", - "data/2.txt", - "data/dir/3.txt", - "data/dir/subdir/4.txt", - "data/dir/subdir/subdir_2/5.txt", - ], -) +@pytest.mark.parametrize("path", DATA_PATH_LIST) def test_file_content(base_url, report_dir, data_dir_prefix, path): if base_url is None: pytest.fail("base_url is not provided. Provide it using --base_url option.") diff --git a/tests/test_objects.py b/tests/test_objects.py new file mode 100644 index 0000000..477b388 --- /dev/null +++ b/tests/test_objects.py @@ -0,0 +1,21 @@ +import sys +import json +import pytest +from conftest import DATA_PATH_LIST + +sys.path.insert(0, '..') +from helpers.neofs import neofs_cli_execute + + +def test_objects_number(wallet, wallet_password, network_domain, cid): + cmd = ( + f"NEOFS_CLI_PASSWORD={wallet_password} neofs-cli --rpc-endpoint {network_domain}:8080 " + f"--wallet {wallet} container list-objects --cid {cid}" + ) + objects_json = neofs_cli_execute(cmd, json_output=True) + objects_list = json.loads(objects_json) + obj_number = len(objects_list) + files_num = len(DATA_PATH_LIST) + assert ( + obj_number == files_num + ), f"Objects number of {obj_number} in the container and the number {files_num} of uploaded files doesn't match"