Mercurial > p > roundup > code
diff roundup/password.py @ 7167:f6b24a8524cd
Modify code to reduce runtime when testing
The prior change to set default number of PBKDF2 rounds to 2000000
(2M) raised runtime in CI from 12 minutes to an hour.
This commit checks to see if we are invoked from a pytest test using:
if ("pytest" in sys.modules and
"PYTEST_CURRENT_TEST" in os.environ):
when no config object is present. I assume that the number of times we
have a full config object is less than with a missing config object.
See if this brings CI runtimes back down. It reduces runtimes on my
local box, but....
Code adapted from
https://stackoverflow.com/questions/25188119/test-if-code-is-executed-from-within-a-py-test-session/44595269#
| author | John Rouillard <rouilj@ieee.org> |
|---|---|
| date | Sat, 25 Feb 2023 14:50:34 -0500 |
| parents | 970cd6d2b8ea |
| children | d787f7282ea3 |
line wrap: on
line diff
--- a/roundup/password.py Fri Feb 24 23:47:28 2023 -0500 +++ b/roundup/password.py Sat Feb 25 14:50:34 2023 -0500 @@ -29,7 +29,7 @@ import roundup.anypy.random_ as random_ from roundup.anypy.strings import us2s, b2s, s2b - +from roundup.exceptions import RoundupException try: with warnings.catch_warnings(): @@ -41,6 +41,8 @@ _bempty = b"" _bjoin = _bempty.join +class ConfigNotSet(RoundupException): + pass def bchr(c): if bytes == str: @@ -190,7 +192,37 @@ if config: rounds = config.PASSWORD_PBKDF2_DEFAULT_ROUNDS else: - rounds = 2000000 + import os + import sys + if ("pytest" in sys.modules and + "PYTEST_CURRENT_TEST" in os.environ): + # Set rounds to 1000 if no config is passed and + # we are running within a pytest test. Using + # actual 2M production values makes testing + # increase from 12 minutes to 1 hour in CI. + rounds = 1000 + else: + import logging + # Log and abort. Initialize rounds and log (which + # will probably be ignored) with traceback in case + # ConfigNotSet exception is removed in the + # future. + rounds = 2000000 + logger = logging.getLogger('roundup') + if sys.version_info[0] > 2: + logger.critical( + "encodePassword called without config.", + stack_info = True) + else: + import inspect, traceback + where = inspect.currentframe() + trace = traceback.format_stack(where) + logger.critical( + "encodePassword called without config. %s", + trace[:-1] + ) + raise ConfigNotSet("encodePassword called without config.") + if rounds < 1000: raise PasswordValueError("invalid PBKDF2 hash (rounds too low)") raw_digest = pbkdf2(plaintext, raw_salt, rounds, 20)
