Convert test_classifier to be a unit test
This commit changes the tests in test_classifier to be unit tests by stubbing out anything that requires the network. It also moves the file from the functional test directory to the unit test directory. Change-Id: I59477ebce92f6115a07a563462c4f536a4694689
This commit is contained in:
@@ -1,42 +0,0 @@
|
||||
# All Rights Reserved.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
||||
# not use this file except in compliance with the License. You may obtain
|
||||
# a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
# License for the specific language governing permissions and limitations
|
||||
# under the License.
|
||||
|
||||
import testtools
|
||||
|
||||
from elastic_recheck import elasticRecheck
|
||||
|
||||
|
||||
class TestClassifier(testtools.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
super(TestClassifier, self).setUp()
|
||||
self.classifier = elasticRecheck.Classifier('queries.yaml')
|
||||
|
||||
def test_read_qeuries_file(self):
|
||||
self.assertNotEqual(self.classifier.queries, None)
|
||||
|
||||
def test_elasticSearch(self):
|
||||
query = self.classifier._apply_template(self.classifier.targeted_template,
|
||||
('@tags:"console.html" AND @message:"Finished: FAILURE"', '34825', '3'))
|
||||
results = self.classifier.es.search(query, size='10')
|
||||
self.assertTrue(int(results['hits']['total']) > 0, ("unable to find hit"))
|
||||
|
||||
def test_ready(self):
|
||||
self.assertTrue(self.classifier._is_ready('49282', '3',
|
||||
'BLAH http://logs.openstack.org/82/49282/3/gate/gate-tempest-devstack-vm-postgres-full/ffc0540'))
|
||||
|
||||
def test_classify(self):
|
||||
bug_numbers = self.classifier.classify('47463', '3',
|
||||
' blah http://logs.openstack.org/63/47463/3/gate/gate-tempest-devstack-vm-postgres-full/99bb8f6')
|
||||
self.assertEqual(bug_numbers, [1218391])
|
||||
87
elastic_recheck/tests/unit/test_classifier.py
Normal file
87
elastic_recheck/tests/unit/test_classifier.py
Normal file
@@ -0,0 +1,87 @@
|
||||
# All Rights Reserved.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
||||
# not use this file except in compliance with the License. You may obtain
|
||||
# a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
# License for the specific language governing permissions and limitations
|
||||
# under the License.
|
||||
|
||||
import yaml
|
||||
|
||||
from elastic_recheck import elasticRecheck
|
||||
from elastic_recheck import tests
|
||||
|
||||
|
||||
def fake_queries(filehandle):
|
||||
return [
|
||||
{'query': '@message:"fake query" AND @fields.filename:"fake"\n',
|
||||
'bug': 1226337},
|
||||
{'query': 'magic query',
|
||||
'bug': 1234567},
|
||||
{'query': '@message:"fake_query3" AND @fields.filename:"fake"\n',
|
||||
'bug': 1235437}]
|
||||
|
||||
|
||||
def _fake_search(query, size=None):
|
||||
files = ['console.html', 'logs/screen-n-api.txt',
|
||||
'logs/screen-n-cpu.txt', 'logs/screen-n-sch.txt',
|
||||
'logs/screen-c-api.txt', 'logs/screen-c-vol.txt',
|
||||
'logs/syslog.txt']
|
||||
file_list = []
|
||||
for f in files:
|
||||
file_list.append({'term': f})
|
||||
log_url = ('http://logs.openstack.org/57/51057/1/gate/gate-tempest-'
|
||||
'devstack-vm-full/f8965ee/console.html')
|
||||
hit_dict = {'_source': {'@fields': {'log_url': log_url}}}
|
||||
if 'magic query' in query['query']['query_string']['query']:
|
||||
fake_result = {'hits': {'total': 2, 'hits': 2},
|
||||
'facets': {'tag': {'terms': file_list}}}
|
||||
else:
|
||||
fake_result = {'hits': {'total': 2, 'hits': [hit_dict]},
|
||||
'facets': {'tag': {'terms': file_list}}}
|
||||
return fake_result
|
||||
|
||||
|
||||
def _fake_urls_match(comment, results):
|
||||
if results is 2:
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
|
||||
|
||||
def _fake_is_ready_urls_match(comment, results):
|
||||
return True
|
||||
|
||||
|
||||
def _fake_is_ready(change_number, patch_number, comment):
|
||||
return True
|
||||
|
||||
|
||||
class TestClassifier(tests.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
super(TestClassifier, self).setUp()
|
||||
self.stubs.Set(yaml, 'load', fake_queries)
|
||||
self.classifier = elasticRecheck.Classifier('queries.yaml')
|
||||
|
||||
def test_is_ready(self):
|
||||
self.stubs.Set(self.classifier.es, 'search', _fake_search)
|
||||
result = self.classifier._is_ready('49282', '3', 'BLAH http://logs.'
|
||||
'openstack.org/57/51057/1/gate/gate-tempest-devstack-vm-full'
|
||||
'/f8965ee')
|
||||
self.assertTrue(result)
|
||||
|
||||
def test_classify(self):
|
||||
self.stubs.Set(self.classifier.es, 'search', _fake_search)
|
||||
self.stubs.Set(self.classifier, '_urls_match', _fake_urls_match)
|
||||
self.stubs.Set(self.classifier, '_is_ready', _fake_is_ready)
|
||||
bug_numbers = self.classifier.classify('47463', '3',
|
||||
' blah http://logs.openstack.org/63/47463/3/gate/gate-tempest'
|
||||
'-devstack-vm-postgres-full/99bb8f6')
|
||||
self.assertEqual(bug_numbers, [1234567])
|
||||
Reference in New Issue
Block a user