Merge ~cjwatson/launchpad:archive-auth-memcache-expire-time into launchpad:master

Proposed by Colin Watson
Status: Merged
Approved by: Colin Watson
Approved revision: 9da2845b8bcf49e96dd437a0424a94def0ca3bbb
Merge reported by: Otto Co-Pilot
Merged at revision: not available
Proposed branch: ~cjwatson/launchpad:archive-auth-memcache-expire-time
Merge into: launchpad:master
Diff against target: 69 lines (+31/-3)
3 files modified
lib/lp/services/memcache/testing.py (+7/-2)
lib/lp/services/memcache/tests/test_testing.py (+23/-0)
lib/lp/soyuz/wsgi/archiveauth.py (+1/-1)
Reviewer Review Type Date Requested Status
Jürgen Gmach Approve
Review via email: mp+412092@code.launchpad.net

Commit message

Fix wsgi-archive-auth's expiry handling for pymemcache

Description of the change

I adjusted `MemcacheFixture` to apply the same type check that `pymemcache` does, in order to catch any other stragglers.

To post a comment you must log in.
Revision history for this message
Jürgen Gmach (jugmac00) wrote :

LGTM - what do you think about adding a test which "proves" our fixture acts like the real thing and throws MemcacheIllegalInputError when you try to pass in a float?

review: Approve
Revision history for this message
Colin Watson (cjwatson) wrote :

Good idea - done.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1diff --git a/lib/lp/services/memcache/testing.py b/lib/lp/services/memcache/testing.py
2index 14b6ca5..307d92f 100644
3--- a/lib/lp/services/memcache/testing.py
4+++ b/lib/lp/services/memcache/testing.py
5@@ -8,6 +8,7 @@ __all__ = [
6 import time as _time
7
8 import fixtures
9+from pymemcache.exceptions import MemcacheIllegalInputError
10
11 from lp.services.memcache.client import MemcacheClient
12 from lp.services.memcache.interfaces import IMemcacheClient
13@@ -36,8 +37,12 @@ class MemcacheFixture(fixtures.Fixture, MemcacheClient):
14 # absolute epoch-seconds, and tells them apart using a magic
15 # threshold. See memcached/memcached.c:realtime.
16 MONTH_IN_SECONDS = 60 * 60 * 24 * 30
17- if expire and expire <= MONTH_IN_SECONDS:
18- expire = _time.time() + expire
19+ if expire:
20+ if not isinstance(expire, int):
21+ raise MemcacheIllegalInputError(
22+ "expire must be integer, got bad value: %r" % expire)
23+ if expire <= MONTH_IN_SECONDS:
24+ expire = int(_time.time()) + expire
25 self._cache[key] = (val, expire)
26 return 1
27
28diff --git a/lib/lp/services/memcache/tests/test_testing.py b/lib/lp/services/memcache/tests/test_testing.py
29new file mode 100644
30index 0000000..9bff933
31--- /dev/null
32+++ b/lib/lp/services/memcache/tests/test_testing.py
33@@ -0,0 +1,23 @@
34+# Copyright 2021 Canonical Ltd. This software is licensed under the
35+# GNU Affero General Public License version 3 (see the file LICENSE).
36+
37+"""MemcacheFixture tests."""
38+
39+from pymemcache.exceptions import MemcacheIllegalInputError
40+
41+from lp.services.memcache.testing import MemcacheFixture
42+from lp.testing import TestCase
43+from lp.testing.layers import BaseLayer
44+
45+
46+class TestMemcacheFixture(TestCase):
47+ layer = BaseLayer
48+
49+ def setUp(self):
50+ super().setUp()
51+ self.client = self.useFixture(MemcacheFixture())
52+
53+ def test_set_expire_requires_integer(self):
54+ self.assertRaises(
55+ MemcacheIllegalInputError,
56+ self.client.set, "key", "value", expire=0.5)
57diff --git a/lib/lp/soyuz/wsgi/archiveauth.py b/lib/lp/soyuz/wsgi/archiveauth.py
58index dc0dece..b02c49b 100644
59--- a/lib/lp/soyuz/wsgi/archiveauth.py
60+++ b/lib/lp/soyuz/wsgi/archiveauth.py
61@@ -92,7 +92,7 @@ def check_password(environ, user, password):
62 proxy.checkArchiveAuthToken(archive_reference, user, password)
63 # Cache positive responses for a minute to reduce database load.
64 _memcache_client.set(
65- memcache_key, _crypt_sha256(password), time.time() + 60)
66+ memcache_key, _crypt_sha256(password), int(time.time()) + 60)
67 _log(environ, "%s@%s: Authorized.", user, archive_reference)
68 return True
69 except Fault as e:

Subscribers

People subscribed via source and target branches

to status/vote changes: