Commit 733df700 authored by Tom Pillot's avatar Tom Pillot
Browse files

Fix tests

parent f4c98e2c
Pipeline #10431 passed with stage
in 1 minute and 19 seconds
......@@ -168,7 +168,10 @@ RAINBOARD_GITS = RAINBOARD_DATA / 'repositories'
RAINBOARD_RPKG = RAINBOARD_DATA / 'robotpkg'
PRIVATE_REGISTRY = 'gitlab.laas.fr:4567'
PUBLIC_REGISTRY = 'memmos.laas.fr:5000'
GITHUB_USER = 'hrp2-14'
if os.getenv('GITLAB_PIPELINE_TOKEN'): # Inside gitlab pipeline
GITHUB_USER = 'hrp2-14'
else:
GITHUB_USER = 'Ozon2'
GITHUB_WEBHOOK_KEY = os.environ['GITHUB_WEBHOOK_KEY']
GITLAB_WEBHOOK_KEY = os.environ['GITLAB_WEBHOOK_KEY']
......
......@@ -2,7 +2,6 @@ import hmac
from hashlib import sha1
from os import getenv
import git
from autoslug.utils import slugify
from django.conf import settings
from django.test import TestCase
......@@ -19,8 +18,8 @@ class GhTests(TestCase):
super(GhTests, cls).setUpClass()
if getenv('GITLAB_PIPELINE_TOKEN'): # Inside gitlab pipeline
cls.namespace, _ = Namespace.objects.get_or_create(name='gsaurel',
slug_gitlab='gsaurel',
slug_github='hrp2-14')
slug_gitlab=slugify('gsaurel'),
slug_github=slugify('hrp2-14'))
cls.project, _ = Project.objects.get_or_create(name='rainboard-tests',
main_namespace=Namespace.objects.get(name='gsaurel'),
main_forge=Forge.objects.get(name='Gitlab'))
......@@ -34,37 +33,41 @@ class GhTests(TestCase):
cls.github = cls.project.github()
cls.gitlab = cls.project.gitlab()
@classmethod
def tearDownClass(cls):
"""Force both repos to be synced after the tests."""
print('Syncing both repos after the tests ...')
def assertSync(self, branch):
"""Raise an exception if the branch is not synced between both repos."""
last_commit_github = self.github.get_branch(branch).commit.sha
last_commit_gitlab = self.gitlab.commits.list(ref_name=branch)[0].id
self.assertEqual(last_commit_github, last_commit_gitlab)
def sync(self):
"""Force both repos to be synced."""
for branch in ('master', 'devel'):
last_commit_github = cls.github.get_branch(branch).commit.sha
last_commit_gitlab = cls.gitlab.commits.list(ref_name=branch)[0].id
last_commit_github = self.github.get_branch(branch).commit.sha
last_commit_gitlab = self.gitlab.commits.list(ref_name=branch)[0].id
if last_commit_github != last_commit_gitlab:
print(f'{branch} is not synced, force pushing to github')
git_repo = cls.project.git()
with git_repo.config_writer() as cw:
cw.set_value('user', 'email', "you@example.com")
cw.set_value('user', 'name', "foo")
git_repo.git.reset('--hard')
git_repo.git.pull(f'gitlab/{cls.namespace.slug}', branch)
git_repo.git.reset('--hard', f'gitlab/{cls.namespace.slug}/{branch}')
try:
git_repo.git.push('-f', f'github/{cls.namespace.slug}', branch)
except git.GitCommandError:
print(f'Failed to push on github {branch}')
last_commit_github = cls.github.get_branch(branch).commit.sha
last_commit_gitlab = cls.gitlab.commits.list(ref_name=branch)[0].id
assert last_commit_github == last_commit_gitlab
print(f'sync: {branch} is not synced, force pushing commit {last_commit_gitlab} on github')
git_repo = self.project.git()
# Create the remotes if necessary
gl_remote_name = f'gitlab/{self.namespace.slug}'
gh_remote_name = f'github/{self.namespace.slug}'
if gl_remote_name not in git_repo.remotes:
git_repo.create_remote(gl_remote_name, url=self.project.remote_url_gitlab())
if gh_remote_name not in git_repo.remotes:
git_repo.create_remote(gh_remote_name, url=self.project.remote_url_github())
# Force push the latest gitlab commit on github
git_repo.remote(gl_remote_name).fetch()
git_repo.git.push('-f', gh_remote_name, f'{last_commit_gitlab}:{branch}')
self.assertSync(branch)
def gh_webhook_event(self, event, last_commit=''):
"""Simulate receiving an event from a github webhook."""
data = {
'repository': {
'name': 'example-adder',
'name': self.project.slug,
'owner': {
'name': self.namespace.slug_github
}
......@@ -88,7 +91,7 @@ class GhTests(TestCase):
"""Simulate receiving an event from a gitlab webhook."""
data = {
'repository': {
'name': 'example-adder',
'name': self.project.slug,
},
'project': {
'namespace': self.namespace.slug_gitlab
......@@ -154,6 +157,8 @@ class GhTests(TestCase):
def test_push_already_synced(self):
"""Test push when both repos are already synced."""
self.sync()
last_commit = self.github.get_branch('master').commit.sha
response = self.gh_webhook_event('push', last_commit)
self.assertEqual(response.status_code, 200)
......@@ -161,6 +166,8 @@ class GhTests(TestCase):
def test_push_github(self):
"""Test sync after pushing to github."""
self.sync()
last_commit = self.github.get_branch('master').commit.sha
file = self.github.get_contents('README.md')
self.github.update_file('README.md',
......@@ -181,10 +188,19 @@ class GhTests(TestCase):
def test_push_gitlab(self):
"""Test sync after pushing to gitlab."""
self.sync()
git_repo = self.project.git()
last_commit = self.gitlab.commits.list(ref_name='master')[0].id
file = self.gitlab.files.get(file_path='README.md', ref='master')
file.content = last_commit[:8]
file.save(branch='master', commit_message='Test push on gitlab')
# Push a new commit to gitlab
gl_remote_name = f'gitlab/{self.namespace.slug}'
if gl_remote_name not in git_repo.remotes:
git_repo.create_remote(gl_remote_name, url=self.project.remote_url_gitlab())
with open(str(self.project.git_path()) + '/README.md', 'w') as f:
f.write(last_commit[:8])
git_repo.index.add(['README.md'])
git_repo.index.commit('Test push on gitlab')
git_repo.git.push(gl_remote_name, 'master')
last_commit_gitlab = self.gitlab.commits.list(ref_name='master')[0].id
self.assertNotEqual(last_commit, last_commit_gitlab)
......
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