Commit 7a2867ea authored by Georges Racinet's avatar Georges Racinet 🦑
Browse files

backups: factorized create/restore as Heptapod instance methods

As usual, this'll help keep duplication low and focus on the
actual testing being done.

--HG--
branch : heptapod-0-8
parent 52f23c344d22
......@@ -30,30 +30,12 @@ def test_backup_restore(test_project, tmpdir):
inherit=True, auto_publish='nothing').status_code == 204
heptapod = test_project.heptapod
# removing leftover backups so that the restore rake task knows
# which one to restore
# (using find in order not to rely on shell expansion for *.tar)
heptapod.run_shell(('find', '/var/opt/gitlab/backups',
'-name', '*.tar',
'-delete'))
heptapod.run_shell(('gitlab-rake', 'gitlab:backup:create'))
heptapod.backup_create()
# make sure we won't confuse a no-op with a success
test_project.api_destroy()
app_services = ('unicorn', 'sidekiq')
try:
for service in app_services:
heptapod.run_shell(('gitlab-ctl', 'stop', service))
heptapod.run_shell(('gitlab-rake', 'gitlab:backup:restore',
'force=yes'))
for service in app_services:
heptapod.run_shell(('gitlab-ctl', 'start', service))
heptapod.wait_startup()
with heptapod.backup_restore():
# let's control what GitLab really sees
branches = test_project.api_branches()
......@@ -80,15 +62,6 @@ def test_backup_restore(test_project, tmpdir):
clone = LocalRepo.clone(url, tmpdir.join('repo2'))
log = clone.hg('log', '-T', '{desc}:{phase}:{topic}\n')
assert log.splitlines() == ['Commit 1:draft:zetop', 'Commit 0:public:']
except Exception:
# these are idempotent
heptapod.run_shell(('gitlab-ctl', 'start', 'unicorn'))
heptapod.run_shell(('gitlab-ctl', 'start', 'sidekiq'))
# Worst case scenario, we lost all our data. We need to
# reprepare the server for subsequent tests
heptapod.prepare(heptapod.users['root']['password'])
raise
# Checking that configuration has been backuped
repo.hg('up', 'default')
......
import contextlib
from io import BytesIO
import logging
from pathlib import Path
......@@ -233,6 +234,12 @@ class Heptapod:
"for %r", route_path, self.__class__)
raise NotImplementedError('force_remove_route')
def backup_create(self):
raise NotImplementedError('backup_create')
def backup_restore(self):
raise NotImplementedError('backup_restore')
class DockerHeptapod(Heptapod):
......@@ -303,6 +310,52 @@ class DockerHeptapod(Heptapod):
" AND path='%s'" % (source_type, route_path)
])
def backup_create(self, clean_previous=True):
"""Create a new backup
:param bool clean_previous: if true, any previously existing backups
are removed. This is handy so that the restore rake task knows
which one to restore without any need to tell it.
"""
# using find in order not to rely on shell expansion for *.tar
self.run_shell(('find', '/var/opt/gitlab/backups',
'-name', '*.tar',
'-delete'))
self.run_shell(('gitlab-rake', 'gitlab:backup:create'))
@contextlib.contextmanager
def backup_restore(self):
"""Context manager for backups restoration.
This is a context manager as a way to provide resuming of the
tests session on errors, in a convenient way for the caller.
That means ensuring as much as possible that the server is running,
maybe wait again for it, reinitialize passwords and tokens…
"""
app_services = ('unicorn', 'sidekiq')
try:
for service in app_services:
self.run_shell(('gitlab-ctl', 'stop', service))
self.run_shell(('gitlab-rake', 'gitlab:backup:restore',
'force=yes'))
for service in app_services:
self.run_shell(('gitlab-ctl', 'start', service))
self.wait_startup()
yield
except Exception:
# these are idempotent
self.run_shell(('gitlab-ctl', 'start', 'unicorn'))
self.run_shell(('gitlab-ctl', 'start', 'sidekiq'))
# Worst case scenario, we lost all our data. We need to
# reprepare the server for subsequent tests
self.prepare(self.users['root']['password'])
raise
class SourceHeptapod(Heptapod):
"""An Heptapod server installed from source on the same system.
......
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