diff --git a/elastic_recheck/tests/functional/test_queries.py b/elastic_recheck/tests/functional/test_queries.py index c63d4781..df0d2762 100644 --- a/elastic_recheck/tests/functional/test_queries.py +++ b/elastic_recheck/tests/functional/test_queries.py @@ -12,16 +12,13 @@ # License for the specific language governing permissions and limitations # under the License. -import os - import ConfigParser -from launchpadlib import launchpad +import pyelasticsearch from elastic_recheck import elasticRecheck +import elastic_recheck.query_builder as qb from elastic_recheck import tests -LPCACHEDIR = os.path.expanduser('~/.launchpadlib/cache') - class TestQueries(tests.TestCase): """Make sure queries are valid. @@ -38,44 +35,23 @@ class TestQueries(tests.TestCase): self.queries = config.get('gerrit', 'query_file') self.classifier = elasticRecheck.Classifier(self.queries) - def test_queries(self): + def test_elasticsearch_query(self): for x in self.classifier.queries: print("Looking for bug: https://bugs.launchpad.net/bugs/%s" % x['bug']) self.assertTrue( - (self._is_valid_ElasticSearch_query(x) or - self._is_valid_launchpad_bug(x['bug'])), + self._is_valid_ElasticSearch_query(x, x['bug']), ("Something is wrong with bug %s" % x['bug'])) - def _is_valid_ElasticSearch_query(self, x): - query = self.classifier._apply_template( - self.classifier.general_template, - x['query']) - results = self.classifier.es.search(query, size='10') - valid_query = int(results['hits']['total']) > 0 - if not valid_query: - print "Didn't find any hits for bug %s" % x['bug'] - return valid_query + def _is_valid_ElasticSearch_query(self, x, bug): + query = qb.generic(x['query']) + try: + results = self.classifier.es.search(query, size='10') + except pyelasticsearch.ElasticHttpError: + self.fail("Failure to process query for bug %s" % bug) - def _is_valid_launchpad_bug(self, bug): - lp = launchpad.Launchpad.login_anonymously('grabbing bugs', - 'production', - LPCACHEDIR) - openstack_group = lp.project_groups['openstack'] - openstack_projects = map(lambda project: project.name, - openstack_group.projects) - lp_bug = lp.bugs[bug] - bug_tasks = lp_bug.bug_tasks - bug_complete = map(lambda bug_task: bug_task.is_complete, bug_tasks) - projects = map(lambda bug_task: bug_task.bug_target_name, bug_tasks) - # Check if all open bug tasks are closed if is_complete is true - # for all tasks. - if len(bug_complete) != bug_complete.count(True): - print "bug %s is closed in launchpad" % bug - return False - # Check that all bug_tasks are targeted to OpenStack Projects - for project in projects: - if project not in openstack_projects: - print "bug target %s not an openstack project" % project - return False + valid_query = len(results) > 0 + if not valid_query: + print("Didn't find any hits for bug %s" % x['bug']) + # don't fail tests if no hits for a bug return True diff --git a/tox.ini b/tox.ini index ba62dd8b..589b2dda 100644 --- a/tox.ini +++ b/tox.ini @@ -15,6 +15,9 @@ commands = python setup.py testr --slowest --testr-args='{posargs} tests.unit' [testenv:functional] commands = python setup.py testr --slowest --testr-args='{posargs} tests.functional' +[testenv:queries] +commands = python setup.py testr --slowest --testr-args='{posargs} tests.functional.test_queries' + [testenv:pep8] commands = flake8