fuel-ostf/fuel_plugin/testing/tests/unit/test_nose_discovery.py

254 lines
7.8 KiB
Python

# Copyright 2013 Mirantis, Inc.
#
# 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 random
from mock import Mock
from nose import case
from fuel_plugin.ostf_adapter.nose_plugin import nose_discovery
from fuel_plugin.ostf_adapter.nose_plugin import nose_utils
from fuel_plugin.ostf_adapter.storage import models
from fuel_plugin.testing.tests import base
TEST_PATH = 'fuel_plugin/testing/fixture/dummy_tests'
class TransactionBeginMock:
def __init__(inst, subtransactions):
pass
def __enter__(self):
pass
def __exit__(self, type, value, traceback):
pass
class TestNoseDiscovery(base.BaseUnitTest):
@classmethod
def setUpClass(cls):
session_mock = Mock()
session_mock.begin = TransactionBeginMock
nose_discovery.discovery(
path=TEST_PATH,
session=session_mock
)
cls.test_sets = [
el[0][0] for el in session_mock.merge.call_args_list
if isinstance(el[0][0], models.TestSet)
]
cls.tests = [
el[0][0] for el in session_mock.merge.call_args_list
if isinstance(el[0][0], models.Test)
]
def _find_needed_test(self, test_name):
return next(t for t in self.tests if t.name == test_name)
def _find_needed_test_set(self, test_set_id):
return next(t for t in self.test_sets if t.id == test_set_id)
def test_compare_release_versions(self):
def cmp_version(first, second):
if nose_utils._compare_release_versions(first, second):
return 1
else:
return -1
expected = [
'2014.2-6.0',
'2014.2.2-6.1',
'2015.1.0-7.0',
'liberty-8.0'
]
releases = expected[:]
random.shuffle(releases)
self.assertEqual(expected,
sorted(releases,
cmp=cmp_version))
def test_discovery(self):
expected = {
'test_sets_count': 10,
'tests_count': 30
}
self.assertTrue(
all(
[len(self.test_sets) == expected['test_sets_count'],
len(self.tests) == expected['tests_count']]
)
)
unique_test_sets = list(
set([testset.id for testset in self.test_sets])
)
unique_tests = list(set([test.name for test in self.tests]))
self.assertTrue(
all(
[len(unique_test_sets) == len(self.test_sets),
len(unique_tests) == len(self.tests)]
)
)
def test_get_proper_description(self):
expected = {
'title': 'fake empty test',
'name': ('fuel_plugin.testing.fixture.'
'dummy_tests.deployment_types_tests.'
'ha_deployment_test.HATest.test_ha_rhel_depl'),
'duration': '0sec',
'test_set_id': 'ha_deployment_test',
'deployment_tags': ['ha', 'rhel']
}
test = [t for t in self.tests if t.name == expected['name']][0]
self.assertTrue(
all(
[
expected[key] == getattr(test, key)
for key in expected.keys()
]
)
)
def test_discovery_tests_with_alternative_depl_tags(self):
expected = {
'testset': {
'id': 'alternative_depl_tags_test',
'deployment_tags': ['alternative | alternative_test']
},
'test': {
'name': ('fuel_plugin.testing.fixture.dummy_tests.'
'deployment_types_tests.alternative_depl_tags_test.'
'AlternativeDeplTagsTests.test_simple_fake_test'),
'deployment_tags': ['one_tag| another_tag', 'other_tag']
}
}
needed_testset = self._find_needed_test_set(expected['testset']['id'])
needed_test = self._find_needed_test(expected['test']['name'])
self.assertEqual(
needed_testset.deployment_tags,
expected['testset']['deployment_tags']
)
self.assertEqual(
needed_test.deployment_tags,
expected['test']['deployment_tags']
)
def test_if_test_belongs_to_test_set(self):
test_set_id = 'ha'
pass_checks = (
'test_ha_sth',
'test-ha-ha',
'test.ha.sahara',
'test.ha.sth',
)
fail_checks = (
'test_sahara',
'test.nonha.sth',
'test.nonha.sahara',
)
for test_id in pass_checks:
self.assertTrue(
nose_discovery.DiscoveryPlugin.test_belongs_to_testset(
test_id, test_set_id)
)
for test_id in fail_checks:
self.assertFalse(
nose_discovery.DiscoveryPlugin.test_belongs_to_testset(
test_id, test_set_id)
)
def test_release_version_attribute(self):
for test_entity in (self.tests, self.test_sets):
self.assertTrue(
all(
[hasattr(t, 'available_since_release')
for t in test_entity]
)
)
expected = {
'test_set': {
'id': 'test_versioning',
'available_since_release': '2015.2-6.0',
},
'tests': [
{'name': ('fuel_plugin.testing.fixture.dummy_tests.'
'test_versioning.TestVersioning.'
'test_simple_fake_first'),
'available_since_release': '2015.2-6.0', },
{'name': ('fuel_plugin.testing.fixture.dummy_tests.'
'test_versioning.TestVersioning.'
'test_simple_fake_second'),
'available_since_release': '2015.2-6.1', },
{'name': ('fuel_plugin.testing.fixture.dummy_tests.'
'test_versioning.TestVersioning.'
'test_simple_fake_alphabetic'),
'available_since_release': 'liberty-8.0', }
]
}
needed_test_set = self._find_needed_test_set(
expected['test_set']['id']
)
self.assertEqual(needed_test_set.available_since_release,
expected['test_set']['available_since_release'])
for test in expected['tests']:
needed_test = self._find_needed_test(test['name'])
self.assertEqual(needed_test.available_since_release,
test['available_since_release'])
def test_description_parsing(self):
test_obj = Mock(spec=case.Test(Mock()))
test_obj.test._testMethodDoc = """
Dummy Test
Available since release: 2014.2-6.1
Duration: 180 s.
Scenario:
1. Step 1
Deployment tags: Dummy Tags
"""
data = nose_utils.get_description(test_obj)
expected = {
'duration': '180 s.',
'title': '',
'deployment_tags': ['dummy tags'],
'available_since_release': '2014.2-6.1'
}
for key in expected:
self.assertEqual(data[key], expected[key])
self.assertNotIn('Duration', data['description'])