conftest.py
Overview
The conftest.py file is a pytest configuration and fixture setup module for the InfiniFlow testing environment. It provides command-line options to customize test runs, handles user registration and authentication, manages API tokens, and configures tenant-specific settings and model integrations for testing workflows.
This file is essential for initializing the test environment, ensuring the necessary user context and system state are established before any tests are executed. It interacts with the InfiniFlow backend API to automate user lifecycle management, token generation, and model registration, facilitating smooth and repeatable test executions.
Detailed Explanation
Global Constants
MARKER_EXPRESSIONS
A dictionary mapping test level keys (p1,p2,p3) to pytest marker expressions. It controls which tests run based on their level of priority or scope:p1: smoke testsp2: core tests (default)p3: full test suite
pytest_addoption(parser: pytest.Parser) -> None
Purpose:
Adds custom command-line options to pytest to control test behavior.
Parameters:
parser: pytest's option parser object.
Behavior:
Adds
--leveloption to select the test level (p1,p2, orp3), defaulting top2.Adds
--client-typeoption to specify the client type used in tests (python_sdk,http,web), defaulting tohttp.
Usage Example:
pytest --level=p1 --client-type=python_sdk
pytest_configure(config: pytest.Config) -> None
Purpose:
Configures pytest before tests start based on the command-line options.
Parameters:
config: pytest configuration object.
Behavior:
Sets the marker expression for filtering tests according to the
--leveloption.Prints the active test level if verbosity is enabled.
register() -> None
Purpose:
Registers a user for testing if not already registered.
Behavior:
Sends a POST request to the
/user/registerendpoint with email, nickname ("qa"), and password.If registration fails for reasons other than "already registered", raises an exception with the error message.
login() -> str
Purpose:
Logs in the registered user and retrieves an authorization token.
Returns:
auth(str): Authorization token string from the response header.
Behavior:
Sends a POST request to
/user/loginwith email and password.Raises an exception if login fails.
Returns the
Authorizationheader for authenticated requests.
auth pytest fixture
Scope: session
Purpose:
Provides an authenticated token for the test session.
Behavior:
Calls
register()to ensure user existence (catches and logs exceptions).Calls
login()to fetch an authorization token.Returns the authorization token.
Usage Example:
def test_example(auth):
# auth is an authorization token string
pass
token pytest fixture
Scope: session
Purpose:
Generates a new system token by calling the backend after authentication.
Parameters:
auth: Authorization token from theauthfixture.
Returns:
A new token string extracted from the response JSON.
Behavior:
Sends a POST request to
/system/new_tokenwith the authorization header.Raises an exception on failure.
get_my_llms(auth: str, name: str) -> bool
Purpose:
Checks if a specified LLM (Large Language Model) is registered for the current user.
Parameters:
auth: Authorization token string.name: Name of the LLM model to check.
Returns:
Trueif the model exists for the user, otherwiseFalse.
Behavior:
Sends a GET request to
/llm/my_llms.Parses the response data to verify presence of the model name.
add_models(auth: str) -> None
Purpose:
Registers predefined models with the backend for the authenticated user if not already present.
Parameters:
auth: Authorization token string.
Behavior:
Defines a dictionary
models_infowith model names and their API keys.For each model, checks if already registered (
get_my_llms).If not registered, sends a POST request to
/llm/set_api_key.If registration fails, calls
pytest.exit()to abort testing.
get_tenant_info(auth: str) -> str
Purpose:
Retrieves tenant information for the authenticated user.
Parameters:
auth: Authorization token string.
Returns:
Tenant ID (string).
Behavior:
Sends a GET request to
/user/tenant_info.Raises exception on failure.
set_tenant_info pytest fixture
Scope: session, autouse=True
Purpose:
Automatically prepares tenant information and links necessary LLM and service IDs for the test session.
Parameters:
auth: Authorization token string.
Behavior:
Calls
add_models()to ensure models are registered.Calls
get_tenant_info()to fetch tenant ID.Sends a POST request to
/user/set_tenant_infowith tenant and model details.On failure, exits pytest with an error.
Important Implementation Details
The file uses the
requestslibrary for HTTP interactions with the InfiniFlow backend API.It leverages pytest's fixture and configuration hooks to integrate setup steps seamlessly.
Exception handling is used to fail early on critical errors (e.g., registration, token retrieval).
The
add_modelsfunction dynamically checks and sets API keys for language models required for testing.The tenant setup fixture runs automatically, ensuring all tests run with proper tenant context.
Test selection via
--levelleverages pytest marker expressions to filter tests by priority or scope.
Interactions with Other System Components
Backend API: All user, token, tenant, and model management is done through REST API calls to the InfiniFlow backend specified by
HOST_ADDRESSandVERSION.Configurations: Uses constants (
EMAIL,PASSWORD,ZHIPU_AI_API_KEY, etc.) imported from aconfigsmodule, which must provide environment-specific credentials and keys.Test Suites: Provides fixtures and configuration options consumed by test modules to run tests under authenticated and tenant-aware contexts.
Models: Manages registration and availability of AI models (e.g., ZHIPU-AI) necessary for model-dependent tests.
Usage Summary
Configure test run options using command-line arguments (
--level,--client-type).On pytest startup, user registration and login occur automatically.
Session-scoped fixtures provide authentication tokens and tenant setup.
Tests can access these fixtures to perform authenticated and tenant-aware API calls.
Models are registered and tenant information is set automatically before tests run.
Mermaid Flowchart Diagram
flowchart TD
A[pytest_addoption] --> B[pytest_configure]
B --> C[register()]
C --> D[login()]
D --> E[auth fixture]
E --> F[token fixture]
E --> G[add_models()]
G --> H[get_my_llms()]
E --> I[get_tenant_info()]
G & I --> J[set_tenant_info fixture]
Diagram Explanation:
pytest_addoptionandpytest_configureset up test run parameters.registerandloginhandle user lifecycle.authfixture provides authorization token.tokenfixture usesauthto get a system token.add_modelsandget_my_llmsmanage model registration.get_tenant_infofetches tenant details.set_tenant_infodepends onadd_modelsandget_tenant_infoto configure tenant settings automatically.