Commit 7aa30b44 authored by Emanuele Aina's avatar Emanuele Aina

test: Exercise simple LAVA submissions

Test a couple of LAVA submissions an verify that:
 * entries are created in the database
 * Phabricator gets updated
Signed-off-by: Emanuele Aina's avatarEmanuele Aina <emanuele.aina@collabora.com>
parent e6f32785
......@@ -12,15 +12,19 @@ import unittest
import flask_migrate
import lxml.etree
import phabricator
import pytest
from testing.postgresql import Postgresql
from unittest.mock import patch
# Import modules from the current and parent directory in case pytest is
# invoked in this folder or from the project root
sys.path += ['.', '..']
from testutils import open_relative, import_app_with_custom_db_instance
import models
import taskmanager
from testutils import open_relative, import_app_with_custom_db_instance, mock_phabricator_request
class FunctionalTestCase(unittest.TestCase):
def setUp(self):
......@@ -43,3 +47,74 @@ class FunctionalTestCase(unittest.TestCase):
contents = lxml.etree.HTML(response.data.decode(response.charset))
assert contents.xpath('head/title') != None
assert contents.xpath('body') != None
def test_lava_submit_single_job_succeeding(self):
client = self.app.test_client()
with open_relative('./test_data/job_data_1450933') as f:
data = json.load(f)
response = client.post('/lava/post', json=data)
assert response.status_code == 200
with self.app.app_context():
assert models.Job.query.count() == 1
job = models.Job.query.filter_by(lava_job_id=data['id']).one()
assert job
assert job.description == 'AppArmor common tests on 19.03 Minnowboard turbot using target OStree image 20190125.0'
assert job.exec_type == 'automated'
assert job.image_architecture == 'amd64'
assert job.image_build == '20190125.0'
assert job.image_deployment == 'ostree'
assert job.image_platform == 'uefi'
assert job.image_release == '19.03'
assert job.image_type == 'target'
assert job.image_url == 'https://images.apertis.org/daily/19.03/20190125.0/amd64/target/apertis_ostree_19.03-target-amd64-uefi_20190125.0.img.gz'
assert job.lava_actual_device_id == 'minnowboard-turbot-E3826-cbg-0'
assert job.lava_device_id == 'minnowboard-turbot-E3826'
assert job.lava_health == 'complete'
assert job.lava_job_id == 1450933
assert job.lava_state == 'finished'
assert job.lava_status == 'complete'
assert job.visibility == 'public'
print('job.testcases', job.testcases)
assert len(job.testcases) == 9
assert all(testcase.result == 'pass' for testcase in job.testcases)
assert job.testcases[0].lava_url == '/results/1450933/0_sanity-check'
assert job.testcases[0].path == 'test-cases/sanity-check.yaml'
assert job.testcases[0].result == 'pass'
@patch.dict('phabricator.ARCRC', {'hosts': {'http://localhost': {'token':'foo'}}})
def test_lava_submit_single_job_with_failures(self):
phabricator_calls = []
phabricator_request_mocker = mock_phabricator_request(phabricator_calls)
client = self.app.test_client()
with open_relative('./test_data/job_data_1377334') as f:
data = json.load(f)
with patch('phabricator.Resource._request', new=phabricator_request_mocker):
taskmanager.connect_to_phab(None)
response = client.post('/lava/post', json=data)
assert response.status_code == 200
edits = [call for call in phabricator_calls if call.name == 'maniphest.edit']
edits.sort(key=lambda i: i.transaction_value('title'))
assert len(edits) == 3
assert edits[0].transaction_value('title') == 'frome: 3_frome test failed'
assert edits[0].transaction_value('custom.apertis:lava:suite') == 'frome'
assert edits[0].transaction_value('custom.apertis:lava:case') == '3_frome'
assert edits[1].transaction_value('title') == 'ribchester: 6_ribchester test failed'
assert edits[2].transaction_value('title') == 'traprain: 7_traprain test failed'
with self.app.app_context():
assert models.Job.query.count() == 1
job = models.Job.query.filter_by(lava_job_id=data['id']).one()
assert job
assert job.image_architecture == 'amd64'
assert job.image_build == '20181207.0'
assert job.image_deployment == 'apt'
assert job.image_platform == 'sdk'
assert job.image_release == '19.03'
assert job.image_type == 'sdk'
assert job.image_url == 'https://images.apertis.org/daily/19.03/20181207.0/amd64/sdk/apertis_19.03-sdk-amd64-sdk_20181207.0.img.gz'
print('job.testcases', job.testcases)
assert len(job.testcases) == 8
assert len([testcase for testcase in job.testcases if testcase.result == 'fail']) == 3
assert job.testcases[3].path == 'modules/frome.yaml'
assert job.testcases[3].result == 'fail'
assert job.testcases[6].path == 'modules/ribchester.yaml'
assert job.testcases[6].result == 'fail'
......@@ -8,6 +8,10 @@
import importlib
import os
from dataclasses import dataclass
from unittest.mock import MagicMock
def open_relative(path, *args, **kwargs):
"""Resolve data files relative to this file"""
dir_path = os.path.dirname(os.path.realpath(__file__))
......@@ -26,3 +30,21 @@ def import_app_with_custom_db_instance(postgresql):
import webhook
importlib.reload(webhook) # ensure that it picks up the new settings
return webhook.app, webhook.db
@dataclass
class Call:
name: str
args: tuple
kwargs: dict
def transaction_value(self, transaction_type, **kwargs):
return next((t['value'] for t in self.kwargs['transactions'] if t['type'] == transaction_type), **kwargs)
def mock_phabricator_request(output_call_list):
class Stub:
def _request(self, **kwargs):
call = Call('{}.{}'.format(self.method, self.endpoint), (), kwargs)
print(call)
output_call_list.append(call)
return MagicMock()
return Stub._request
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