From a5018d965841fac9f7d5583b7e4be1f9959fb063 Mon Sep 17 00:00:00 2001 From: gryf Date: Fri, 1 Nov 2019 12:56:18 +0100 Subject: [PATCH] Add base class for tests. --- tests/base.py | 27 +++++++++++++++++++++++++++ tests/test_patchest_created.py | 31 ++++--------------------------- 2 files changed, 31 insertions(+), 27 deletions(-) create mode 100644 tests/base.py diff --git a/tests/base.py b/tests/base.py new file mode 100644 index 0000000..ab7b48e --- /dev/null +++ b/tests/base.py @@ -0,0 +1,27 @@ +import os +import signal +import subprocess +import time +import unittest + + +class BaseTestCase(unittest.TestCase): + def setUp(self): + if not all((os.path.exists('gerrit-server-key'), + os.path.exists('gerrit-access-key'))): + raise SystemError("No server key-pair or access key-pair found. " + "You'll need to copy them to the current " + "directory under name 'gerrit-server-key' and " + "'gerrit-server-key.pub' for server keyes and " + "'gerrit-access-key' and " + "'gerrit-access-key.pub' for access keyes. " + "Don't forget to run configured Jenkins!") + self.process = subprocess.Popen(['ferrit'], preexec_fn=os.setpgrp) + # give some time for processes to start, and jenkins to be ready for + # consume the data + time.sleep(3) + + def tearDown(self): + os.killpg(self.process.pid, signal.SIGTERM) + os.unlink('ferrit-http.log') + os.unlink('ferrit-ssh.log') diff --git a/tests/test_patchest_created.py b/tests/test_patchest_created.py index 921950b..6530d5d 100644 --- a/tests/test_patchest_created.py +++ b/tests/test_patchest_created.py @@ -1,38 +1,17 @@ import json import os -import signal -import subprocess import time -import unittest import requests +from tests import base -class TestPatchCreate(unittest.TestCase): - def setUp(self): - if not all((os.path.exists('gerrit-server-key'), - os.path.exists('gerrit-access-key'))): - raise SystemError("No server key-pair or access key-pair found. " - "You'll need to copy them to the current " - "directory under name 'gerrit-server-key' and " - "'gerrit-server-key.pub' for server keyes and " - "'gerrit-access-key' and " - "'gerrit-access-key.pub' for access keyes. " - "Don't forget to run configured Jenkins!") - self.process = subprocess.Popen(['ferrit'], preexec_fn=os.setpgrp) - time.sleep(5) # give some time to processes to start - def tearDown(self): - os.killpg(self.process.pid, signal.SIGTERM) - os.unlink('ferrit-http.log') - os.unlink('ferrit-ssh.log') +class TestPatchsetCreated(base.BaseTestCase): def test_send_patch(self): - counter = 15 - result = [] - pipe = subprocess.Popen('ssh -i gerrit-access-key localhost -p 2200 ' - 'gerrit stream-events'.split()) - time.sleep(3) + counter = 20 + result = None requests.post('http://localhost:8181/make/event', data='project=' 'example&branch=master&type=patchset-created') while counter: @@ -49,8 +28,6 @@ class TestPatchCreate(unittest.TestCase): else: break - pipe.terminate() - self.assertTrue(result) result = json.loads(result) self.assertEqual(result['labels']['Verified'], 1)