Commit 1a988f52 authored by Georges Racinet's avatar Georges Racinet 🦑
Browse files

Preparations: caching user access tokens

These can't be read with a webdriver, as it is a security feature
of GitLab not to display them, that's why we where creating new
ones for each session.

Storing them in a cache file will help making the session startup
time shorter. This is especially useful if launching a few tests
repeatedly or with the tests that have to redo the preparations after
an instance restart.
parent 952e922d9db5
......@@ -9,3 +9,5 @@ syntax: glob
*.\#*
*~
*.orig
*.cache
import contextlib
import json
from io import BytesIO
import logging
import os
......@@ -15,6 +16,7 @@ from urllib.parse import urlparse
from tests.utils.user import User
from tests.utils import docker
from tests.utils import session
from .constants import DATA_DIR
logger = logging.getLogger(__name__)
......@@ -157,6 +159,30 @@ class Heptapod:
self.dead = True
raise AssertionError(dead_msg)
def instance_cache_file(self):
return os.path.join(DATA_DIR, 'instance.cache')
def load_instance_cache(self):
path = self.instance_cache_file()
try:
with open(path) as cachef:
cached = json.load(cachef)
except Exception:
logger.info("Cache file %r not available or not readable. "
"Heptapod instance info will be retrieved "
"or initialized", path)
else:
for name, info in cached['users'].items():
self.users[name] = dict(name=name,
token=info['token'])
def update_instance_cache(self):
users = {name: dict(token=info['token'])
for name, info in self.users.items()}
with open(self.instance_cache_file(), 'w') as cachef:
json.dump(dict(users=users), cachef)
def prepare(self, root_password):
"""Make all preparations for the Heptapod instance to be testable.
......@@ -173,9 +199,11 @@ class Heptapod:
if self.dead is None:
self.wait_startup(
wait_after_first_response=self.wait_after_first_response)
self.load_instance_cache()
logger.info("Preparing root user.")
start = time.time()
root = self.users['root'] = {}
root = self.users.setdefault('root', {})
driver = self.new_webdriver()
# guaranteeing driver to be available for teardown as soon as created
root['webdriver'] = driver
......@@ -200,6 +228,7 @@ class Heptapod:
'ssh-keygen',
'-R', '[{host}]:{port}'.format(
host=self.host, port=self.ssh_port)))
self.update_instance_cache()
logger.info("All preparations done in %.2f seconds. "
"Proceeding with tests.", time.time() - start)
......
......@@ -142,6 +142,11 @@ class User(object):
def ensure_private_token(self, webdriver):
"""Generate a private token for already logged in user.
"""
resp = self.api_get()
if resp is not None and resp.status_code == 200:
logger.info("%r: reusing existing access token.", self)
return
heptapod = self.heptapod
start = time.time()
webdriver.get(heptapod.url + '/profile/personal_access_tokens')
......
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