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
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()
with open(path) as cachef:
cached = json.load(cachef)
except Exception:"Cache file %r not available or not readable. "
"Heptapod instance info will be retrieved "
"or initialized", path)
for name, info in cached['users'].items():
self.users[name] = dict(name=name,
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.load_instance_cache()"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:
'-R', '[{host}]:{port}'.format(, port=self.ssh_port)))
self.update_instance_cache()"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:"%r: reusing existing access token.", self)
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