1+ import json
12import logging
3+ from typing import Any
24
3- from locust import FastHttpUser
5+ import locust_plugins
6+ from locust import FastHttpUser , events
7+ from locust .env import Environment
48
59from .auth_settings import DeploymentAuth , OsparcAuth
610
711_logger = logging .getLogger (__name__ )
812
13+ # NOTE: 'import locust_plugins' is necessary to use --check-fail-ratio
14+ # this assert is added to avoid that pycln pre-commit hook does not
15+ # remove the import (the tool assumes the import is not necessary)
16+ assert locust_plugins # nosec
17+
918
1019class OsparcUserBase (FastHttpUser ):
1120 """
@@ -49,6 +58,23 @@ def authenticated_patch(self, url, **kwargs):
4958 return self .client .patch (url , ** kwargs )
5059
5160
61+ @events .init_command_line_parser .add_listener
62+ def _ (parser ) -> None :
63+ parser .add_argument (
64+ "--requires-login" ,
65+ action = "store_true" ,
66+ default = False ,
67+ help = "Indicates if the user requires login before accessing the endpoint" ,
68+ )
69+
70+
71+ @events .init .add_listener
72+ def _ (environment : Environment , ** _kwargs : Any ) -> None :
73+ # Only log the parsed options, as the full environment is not JSON serializable
74+ options_dict : dict [str , Any ] = vars (environment .parsed_options )
75+ _logger .debug ("Testing environment options: %s" , json .dumps (options_dict , indent = 2 ))
76+
77+
5278class OsparcWebUserBase (OsparcUserBase ):
5379 """
5480 Base class for web users in Locust that provides common functionality.
@@ -85,10 +111,6 @@ def on_stop(self) -> None:
85111
86112 def _login (self ) -> None :
87113 # Implement login logic here
88- logging .info (
89- "Logging in user with email: %s" ,
90- self .osparc_auth .OSPARC_USER_NAME ,
91- )
92114 response = self .authenticated_post (
93115 "/v0/auth/login" ,
94116 json = {
@@ -97,11 +119,13 @@ def _login(self) -> None:
97119 },
98120 )
99121 response .raise_for_status ()
100- logging .info ("Logged in user with email: %s" , self .osparc_auth .OSPARC_USER_NAME )
122+ logging .debug (
123+ "Logged in user with email: %s" , self .osparc_auth .OSPARC_USER_NAME
124+ )
101125
102126 def _logout (self ) -> None :
103127 # Implement logout logic here
104128 self .authenticated_post ("/v0/auth/logout" )
105- logging .info (
129+ logging .debug (
106130 "Logged out user with email: %s" , self .osparc_auth .OSPARC_USER_NAME
107131 )
0 commit comments