Skip to content

Commit

Permalink
task: add test for tests-invoke with issues
Browse files Browse the repository at this point in the history
  • Loading branch information
jelly authored and martinpitt committed Sep 1, 2023
1 parent 14b13cb commit a4fda94
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
37 changes: 36 additions & 1 deletion task/test-tests-scan
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import tempfile
import unittest

from lib.constants import BOTS_DIR
from task import github
from task.test_mock_server import MockHandler, MockServer

ADDRESS = ("127.0.0.7", 9898)
Expand Down Expand Up @@ -69,11 +70,20 @@ class Handler(MockHandler):
self.replyJson([])
elif self.path.startswith('/repos/project/repo/pulls'):
self.replyJson([self.server.data['/repos/project/repo/pulls/1']])
elif self.path.endswith("/issues"):
issues = self.server.data['issues']
self.replyJson(issues)
else:
self.send_error(404, 'Mock Not Found: ' + self.path)

def do_POST(self):
self.send_error(405, 'Method not allowed: ' + self.path)
if self.path.startswith("/repos/cockpit-project/cockpit/issues"):
content_len = int(self.headers.get('content-length'))
data = json.loads(self.rfile.read(content_len).decode('utf-8'))
self.server.data['issues'] = [data]
self.replyJson(data)
else:
self.send_error(405, 'Method not allowed: ' + self.path)


class TestTestsScan(unittest.TestCase):
Expand Down Expand Up @@ -206,6 +216,31 @@ class TestTestsScan(unittest.TestCase):
self.assertEqual(output.strip(), expected_output)
self.assertIsNone(stderr)

def test_tests_invoke(self):
repo = "cockpit-project/cockpit"
args = ["--revision", self.revision, "--repo", repo]
script = os.path.join(BOTS_DIR, "tests-invoke")
with tempfile.TemporaryDirectory() as tempdir:
testdir = f"{tempdir}/.cockpit-ci"
os.makedirs(testdir)
with open(f"{testdir}/run", "w") as fp:
fp.write("#!/bin/bash\nexit 1")
os.system(f"chmod +x {testdir}/run")
proc = subprocess.Popen([script, *args], stdout=subprocess.PIPE, universal_newlines=True,
env={"GITHUB_API": f"http://{ADDRESS[0]}:{ADDRESS[1]}", "TEST_INVOKE_SLEEP": "1"},
cwd=tempdir)
output, stderr = proc.communicate()
api = github.GitHub(f"http://{ADDRESS[0]}:{ADDRESS[1]}/")
issues = api.get("issues")

self.assertEqual(output, "")
self.assertEqual(proc.returncode, 1)
self.assertEqual(len(issues), 1)
self.assertEqual(issues[0]['title'], "Nightly tests did not succeed")
self.assertEqual(issues[0]['body'], f"Tests failed on {self.revision}")
self.assertEqual(issues[0]['labels'], ["nightly"])
self.assertIsNone(stderr)


if __name__ == '__main__':
unittest.main()
2 changes: 1 addition & 1 deletion tests-invoke
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ def main():
p.terminate()
p.wait(timeout=60)
else:
time.sleep(60)
time.sleep(int(os.getenv("TEST_INVOKE_SLEEP", "60")))
return_code = p.returncode
sys.stderr.write("Test run finished, return code: {0}\n".format(return_code))
if not opts.pull_number and return_code != 0:
Expand Down

0 comments on commit a4fda94

Please sign in to comment.