Skip to content

Commit 931d788

Browse files
committed
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#
1 parent c29e91a commit 931d788

File tree

2 files changed

+45
-2
lines changed

2 files changed

+45
-2
lines changed

roundup/password.py

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
import roundup.anypy.random_ as random_
3030

3131
from roundup.anypy.strings import us2s, b2s, s2b
32-
32+
from roundup.exceptions import RoundupException
3333

3434
try:
3535
with warnings.catch_warnings():
@@ -41,6 +41,8 @@
4141
_bempty = b""
4242
_bjoin = _bempty.join
4343

44+
class ConfigNotSet(RoundupException):
45+
pass
4446

4547
def bchr(c):
4648
if bytes == str:
@@ -190,7 +192,37 @@ def encodePassword(plaintext, scheme, other=None, config=None):
190192
if config:
191193
rounds = config.PASSWORD_PBKDF2_DEFAULT_ROUNDS
192194
else:
193-
rounds = 2000000
195+
import os
196+
import sys
197+
if ("pytest" in sys.modules and
198+
"PYTEST_CURRENT_TEST" in os.environ):
199+
# Set rounds to 1000 if no config is passed and
200+
# we are running within a pytest test. Using
201+
# actual 2M production values makes testing
202+
# increase from 12 minutes to 1 hour in CI.
203+
rounds = 1000
204+
else:
205+
import logging
206+
# Log and abort. Initialize rounds and log (which
207+
# will probably be ignored) with traceback in case
208+
# ConfigNotSet exception is removed in the
209+
# future.
210+
rounds = 2000000
211+
logger = logging.getLogger('roundup')
212+
if sys.version_info[0] > 2:
213+
logger.critical(
214+
"encodePassword called without config.",
215+
stack_info = True)
216+
else:
217+
import inspect, traceback
218+
where = inspect.currentframe()
219+
trace = traceback.format_stack(where)
220+
logger.critical(
221+
"encodePassword called without config. %s",
222+
trace[:-1]
223+
)
224+
raise ConfigNotSet("encodePassword called without config.")
225+
194226
if rounds < 1000:
195227
raise PasswordValueError("invalid PBKDF2 hash (rounds too low)")
196228
raw_digest = pbkdf2(plaintext, raw_salt, rounds, 20)

test/test_security.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -432,4 +432,15 @@ def test_pbkdf2_migrate_rounds(self):
432432

433433
self.assertEqual(p.needs_migration(config=self.db.config), True)
434434

435+
def test_encodePasswordNoConfig(self):
436+
# should run cleanly as we are in a test.
437+
#
438+
p = roundup.password.encodePassword('sekrit', 'PBKDF2')
439+
440+
del(os.environ["PYTEST_CURRENT_TEST"])
441+
self.assertNotIn("PYTEST_CURRENT_TEST", os.environ)
442+
443+
with self.assertRaises(roundup.password.ConfigNotSet) as ctx:
444+
roundup.password.encodePassword('sekrit', 'PBKDF2')
445+
435446
# vim: set filetype=python sts=4 sw=4 et si :

0 commit comments

Comments
 (0)