diff --git a/fuel_plugin/testing/tests/unit/base.py b/fuel_plugin/testing/tests/base.py similarity index 68% rename from fuel_plugin/testing/tests/unit/base.py rename to fuel_plugin/testing/tests/base.py index 9866d3e5..88d94b2a 100644 --- a/fuel_plugin/testing/tests/unit/base.py +++ b/fuel_plugin/testing/tests/base.py @@ -1,3 +1,5 @@ +# -*- coding: utf-8 -*- + # Copyright 2013 Mirantis, Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); you may @@ -13,19 +15,110 @@ # under the License. -import unittest2 -from mock import patch, MagicMock +import mock from sqlalchemy import create_engine from sqlalchemy.orm import sessionmaker +import unittest2 from fuel_plugin.ostf_adapter import config +from fuel_plugin.ostf_adapter import mixins from fuel_plugin.ostf_adapter.nose_plugin.nose_discovery import discovery from fuel_plugin.ostf_adapter.storage import models -from fuel_plugin.ostf_adapter import mixins + TEST_PATH = 'fuel_plugin/testing/fixture/dummy_tests' +CLUSTERS = { + 1: { + 'cluster_meta': { + 'release_id': 1, + 'mode': 'ha' + }, + 'release_data': { + 'operating_system': 'rhel' + }, + 'cluster_attributes': { + 'editable': { + 'additional_components': {}, + 'common': {} + } + } + }, + 2: { + 'cluster_meta': { + 'release_id': 2, + 'mode': 'multinode', + }, + 'release_data': { + 'operating_system': 'ubuntu' + }, + 'cluster_attributes': { + 'editable': { + 'additional_components': {}, + 'common': {} + } + } + }, + 3: { + 'cluster_meta': { + 'release_id': 3, + 'mode': 'ha' + }, + 'release_data': { + 'operating_system': 'rhel' + }, + 'cluster_attributes': { + 'editable': { + 'additional_components': { + 'murano': { + 'value': True + }, + 'sahara': { + 'value': False + } + }, + 'common': {} + } + } + }, + 4: { + 'cluster_meta': { + 'release_id': 4, + 'mode': 'test_error' + }, + 'release_data': { + 'operating_system': 'none' + }, + 'cluster_attributes': { + 'editable': { + 'additional_components': {}, + 'common': {} + } + } + }, + 5: { + 'cluster_meta': { + 'release_id': 5, + 'mode': 'dependent_tests' + }, + 'release_data': { + 'operating_system': 'none' + }, + 'cluster_attributes': { + 'editable': { + 'additional_components': {}, + 'common': {} + } + } + } +} + + +class BaseUnitTest(unittest2.TestCase): + """Base class for all unit tests.""" + + class BaseWSGITest(unittest2.TestCase): @classmethod @@ -85,9 +178,9 @@ class BaseWSGITest(unittest2.TestCase): # mocking # request mocking - self.request_mock = MagicMock() + self.request_mock = mock.MagicMock() - self.request_patcher = patch( + self.request_patcher = mock.patch( 'fuel_plugin.ostf_adapter.wsgi.controllers.request', self.request_mock ) diff --git a/fuel_plugin/testing/tests/integration/__init__.py b/fuel_plugin/testing/tests/integration/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/fuel_plugin/testing/tests/unit/test_wsgi_controllers.py b/fuel_plugin/testing/tests/integration/test_wsgi_controllers.py similarity index 99% rename from fuel_plugin/testing/tests/unit/test_wsgi_controllers.py rename to fuel_plugin/testing/tests/integration/test_wsgi_controllers.py index 9bf7f368..f6d00b5a 100644 --- a/fuel_plugin/testing/tests/unit/test_wsgi_controllers.py +++ b/fuel_plugin/testing/tests/integration/test_wsgi_controllers.py @@ -18,7 +18,7 @@ from mock import patch, Mock from fuel_plugin.ostf_adapter.wsgi import controllers from fuel_plugin.ostf_adapter.storage import models -from fuel_plugin.testing.tests.unit import base +from fuel_plugin.testing.tests import base class TestTestsController(base.BaseWSGITest): diff --git a/fuel_plugin/testing/tests/unit/tests_wsgi_interface.py b/fuel_plugin/testing/tests/integration/tests_wsgi_interface.py similarity index 98% rename from fuel_plugin/testing/tests/unit/tests_wsgi_interface.py rename to fuel_plugin/testing/tests/integration/tests_wsgi_interface.py index a63d5e0f..5a132880 100644 --- a/fuel_plugin/testing/tests/unit/tests_wsgi_interface.py +++ b/fuel_plugin/testing/tests/integration/tests_wsgi_interface.py @@ -17,7 +17,7 @@ from mock import patch, MagicMock from webtest import TestApp from fuel_plugin.ostf_adapter.wsgi import app -from fuel_plugin.testing.tests.unit import base +from fuel_plugin.testing.tests import base class WsgiInterfaceTests(base.BaseWSGITest): diff --git a/fuel_plugin/testing/tests/unit/test_nose_discovery.py b/fuel_plugin/testing/tests/unit/test_nose_discovery.py index 66b3ba98..520e2430 100644 --- a/fuel_plugin/testing/tests/unit/test_nose_discovery.py +++ b/fuel_plugin/testing/tests/unit/test_nose_discovery.py @@ -13,9 +13,10 @@ # under the License. from mock import Mock -import unittest2 + from fuel_plugin.ostf_adapter.nose_plugin import nose_discovery from fuel_plugin.ostf_adapter.storage import models +from fuel_plugin.testing.tests import base TEST_PATH = 'fuel_plugin/testing/fixture/dummy_tests' @@ -31,7 +32,7 @@ class TransactionBeginMock: pass -class TestNoseDiscovery(unittest2.TestCase): +class TestNoseDiscovery(base.BaseUnitTest): @classmethod def setUpClass(cls): diff --git a/fuel_plugin/testing/tests/unit/test_results_logger.py b/fuel_plugin/testing/tests/unit/test_results_logger.py index 917b1a9b..8391be45 100644 --- a/fuel_plugin/testing/tests/unit/test_results_logger.py +++ b/fuel_plugin/testing/tests/unit/test_results_logger.py @@ -15,12 +15,13 @@ # under the License. import mock -from unittest2 import TestCase from fuel_plugin.ostf_adapter.logger import ResultsLogger +from fuel_plugin.testing.tests import base -class TestResultsLogger(TestCase): +@mock.patch.object(ResultsLogger, '_init_file_logger') +class TestResultsLogger(base.BaseUnitTest): def get_logger(self, **kwargs): options = { @@ -30,14 +31,14 @@ class TestResultsLogger(TestCase): options.update(kwargs) return ResultsLogger(**options) - def test_filename(self): + def test_filename(self, m_init_logger): logger = self.get_logger(testset='testset_name', cluster_id=99) expected = "cluster_99_testset_name.log" self.assertEqual(logger.filename, expected) - def test_log_format_on_success(self): + def test_log_format_on_success(self, m_init_logger): logger = self.get_logger() logger._logger = mock.Mock() @@ -48,7 +49,7 @@ class TestResultsLogger(TestCase): expected = 'SUCCESS Successful test (tests.successful.test) ' logger._logger.info.assert_called_once_with(expected) - def test_log_format_on_fail(self): + def test_log_format_on_fail(self, m_init_logger): logger = self.get_logger() logger._logger = mock.Mock() @@ -60,7 +61,7 @@ class TestResultsLogger(TestCase): 'Message after fail TRACEBACK') logger._logger.info.assert_called_once_with(expected) - def test_log_format_on_error(self): + def test_log_format_on_error(self, m_init_logger): logger = self.get_logger() logger._logger = mock.Mock() diff --git a/fuel_plugin/testing/tests/unit/test_support_utilities.py b/fuel_plugin/testing/tests/unit/test_support_utilities.py index e1746515..81152b99 100644 --- a/fuel_plugin/testing/tests/unit/test_support_utilities.py +++ b/fuel_plugin/testing/tests/unit/test_support_utilities.py @@ -12,13 +12,14 @@ # License for the specific language governing permissions and limitations # under the License. -import unittest +import requests_mock from fuel_plugin.ostf_adapter import config from fuel_plugin.ostf_adapter import mixins +from fuel_plugin.testing.tests import base -class TestDeplTagsGetter(unittest.TestCase): +class TestDeplTagsGetter(base.BaseUnitTest): def setUp(self): config.init_config([]) @@ -32,6 +33,14 @@ class TestDeplTagsGetter(unittest.TestCase): ) } - res = mixins._get_cluster_depl_tags(expected['cluster_id']) + with requests_mock.Mocker() as m: + cluster = base.CLUSTERS[expected['cluster_id']] + m.register_uri('GET', '/api/clusters/3', + json=cluster['cluster_meta']) + m.register_uri('GET', '/api/clusters/3/attributes', + json=cluster['cluster_attributes']) + m.register_uri('GET', '/api/releases/3', + json=cluster['release_data']) + res = mixins._get_cluster_depl_tags(expected['cluster_id']) self.assertEqual(res, expected['depl_tags']) diff --git a/test-requirements.txt b/test-requirements.txt index cacb966d..90706156 100644 --- a/test-requirements.txt +++ b/test-requirements.txt @@ -1,5 +1,6 @@ -r requirements.txt mock==1.0.1 +requests-mock>=0.5.1 flake8 tox>=1.7.1 coverage==3.6