Commit 23a57aa7 authored by Georges Racinet's avatar Georges Racinet 🦑
Browse files

Heptapod: invalidate instance cache according to type and URL

With the new testing of production servers, the fixed user
names can differ from an instance (e.g. local, for tests development)
to another (actual production), leading to errors.

Invalidating based on URL only would not be enough for people that
actually work on the production mode – by testing it on a local
instance (typically provided by the HDK).

There already was a invalidation system for the token itself (test
and recreate if it does not work), but not for the mere existence
of an user in the registry.

--HG--
branch : stable
parent c7516a71cef9
...@@ -74,6 +74,17 @@ class Heptapod: ...@@ -74,6 +74,17 @@ class Heptapod:
default_group = None default_group = None
"""Group instance where to create projects in by default.""" """Group instance where to create projects in by default."""
instance_type = 'development'
"""The type of instance, meaning how these tests operate on it.
It is of course completely possible to treat a development instance
as if it were for production: that's what happens when developping the
production server tests, of course.
Treating a production instance as a development one is also technically
possible (strongly discouraged of course).
"""
def __init__(self, url, ssh_user, ssh_port, def __init__(self, url, ssh_user, ssh_port,
hg_native=False, hg_native=False,
reverse_call_host=None, reverse_call_host=None,
...@@ -216,6 +227,12 @@ class Heptapod: ...@@ -216,6 +227,12 @@ class Heptapod:
def load_instance_cache(self): def load_instance_cache(self):
path = self.instance_cache_file() path = self.instance_cache_file()
def invalidate_retry():
logger.warning("Removing cache file %r and starting afresh.", path)
os.unlink(path)
return self.load_instance_cache()
try: try:
with open(path) as cachef: with open(path) as cachef:
cached = json.load(cachef) cached = json.load(cachef)
...@@ -225,13 +242,32 @@ class Heptapod: ...@@ -225,13 +242,32 @@ class Heptapod:
"Heptapod instance info will be retrieved " "Heptapod instance info will be retrieved "
"or initialized", path) "or initialized", path)
else: else:
instance_type = cached.get('instance_type')
if instance_type != self.instance_type:
# None means before the introduction of this invalidation
# in case anyone wonders (not worth a specific message)
logger.warning(
"Cache file %r is for another instance type (%r) ",
path, instance_type)
return invalidate_retry()
url = cached.get('url')
# for now all development instances have the same two
# fixed test users (root and test_basic). There is already
# a token invalidation logic.
if instance_type == 'production' and url != self.url:
# None means before the introduction of this invalidation
# in case anyone wonders (not worth a specific message)
logger.warning(
"Cache file %r is for another instance (%r) ",
path, url)
return invalidate_retry()
for name, info in cached['users'].items(): for name, info in cached['users'].items():
if 'id' not in info: if 'id' not in info:
logger.warning("Cache file %r is from an earlier version " logger.warning("Cache file %r is from an earlier version "
"of heptapod-tests. " "of heptapod-tests. ", path)
"Removing to start afresh.", path) return invalidate_retry()
os.unlink(path)
return self.load_instance_cache()
self.users[name] = User(heptapod=self, self.users[name] = User(heptapod=self,
name=name, name=name,
id=info['id'], id=info['id'],
...@@ -241,7 +277,9 @@ class Heptapod: ...@@ -241,7 +277,9 @@ class Heptapod:
users = {user.name: dict(token=user.token, id=user.id) users = {user.name: dict(token=user.token, id=user.id)
for user in self.users.values()} for user in self.users.values()}
with open(self.instance_cache_file(), 'w') as cachef: with open(self.instance_cache_file(), 'w') as cachef:
json.dump(dict(users=users), cachef) json.dump(dict(url=self.url,
instance_type=self.instance_type,
users=users), cachef)
def prepare(self, root_password): def prepare(self, root_password):
"""Make all preparations for the Heptapod instance to be testable. """Make all preparations for the Heptapod instance to be testable.
...@@ -491,6 +529,8 @@ class ProductionHeptapod(Heptapod): ...@@ -491,6 +529,8 @@ class ProductionHeptapod(Heptapod):
and personal namespaces of a couple of users. and personal namespaces of a couple of users.
""" """
instance_type = 'production'
def __init__(self, group_owner_credentials, default_group_id, def __init__(self, group_owner_credentials, default_group_id,
**kw): **kw):
super(ProductionHeptapod, self).__init__(**kw) super(ProductionHeptapod, self).__init__(**kw)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment