Repository: incubator-ariatosca Updated Branches: refs/heads/cli-tests f14efbf75 -> 56a4767e3
Refactor cli tests framework Project: http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/commit/56a4767e Tree: http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/tree/56a4767e Diff: http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/diff/56a4767e Branch: refs/heads/cli-tests Commit: 56a4767e3a22f5f9e5803f78231c27185c8974e0 Parents: f14efbf Author: Avia Efrat <[email protected]> Authored: Wed Apr 19 14:17:27 2017 +0300 Committer: Avia Efrat <[email protected]> Committed: Wed Apr 19 14:17:27 2017 +0300 ---------------------------------------------------------------------- aria/cli/commands/service_templates.py | 7 +- tests/cli/test_logs.py | 23 ----- tests/cli/test_node_templates.py | 56 +++++++----- tests/cli/test_nodes.py | 21 +++-- tests/cli/test_service_templates.py | 124 +++++++++++++++++++-------- tests/cli/test_services.py | 93 ++++++++++---------- tests/cli/utils.py | 128 +++------------------------- tests/mock/models.py | 69 +++++++++++---- tests/mock/topology.py | 8 +- tests/modeling/test_models.py | 5 +- 10 files changed, 262 insertions(+), 272 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/blob/56a4767e/aria/cli/commands/service_templates.py ---------------------------------------------------------------------- diff --git a/aria/cli/commands/service_templates.py b/aria/cli/commands/service_templates.py index 8e0e91c..8ee771f 100644 --- a/aria/cli/commands/service_templates.py +++ b/aria/cli/commands/service_templates.py @@ -65,9 +65,10 @@ def show(service_template_name, model_storage, logger): logger.info('{0}{1}'.format(service_template_dict['description'].encode('UTF-8') or '', os.linesep)) - logger.info('Existing services:') - logger.info('{0}{1}'.format([s['name'] for s in services], - os.linesep)) + if services: + logger.info('Existing services:') + logger.info('{0}{1}'.format([s['name'] for s in services], + os.linesep)) @service_templates.command(name='list', http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/blob/56a4767e/tests/cli/test_logs.py ---------------------------------------------------------------------- diff --git a/tests/cli/test_logs.py b/tests/cli/test_logs.py deleted file mode 100644 index ad0c4f1..0000000 --- a/tests/cli/test_logs.py +++ /dev/null @@ -1,23 +0,0 @@ -from aria.cli.env import _Environment -from tests.cli.base_test import TestCliBase, mock_storage - - -class TestLogsList(TestCliBase): - - def test_existing_logs(self, monkeypatch, mock_storage): - monkeypatch.setattr(_Environment, 'model_storage', mock_storage) - self.invoke('logs list exec_id') - - assert 'Listing logs for execution id exec_id' in self.logger_output_string - assert 'log_msg' in self.logger_output_string - assert 'No logs' not in self.logger_output_string - - def test_no_logs(self, monkeypatch, mock_object): - m = mock_object - m.log.list.return_value = [] - monkeypatch.setattr(_Environment, 'model_storage', m) - self.invoke('logs list exec_id') - - assert 'Listing logs for execution id exec_id' in self.logger_output_string - assert 'log_msg' not in self.logger_output_string - assert 'No logs' in self.logger_output_string http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/blob/56a4767e/tests/cli/test_node_templates.py ---------------------------------------------------------------------- diff --git a/tests/cli/test_node_templates.py b/tests/cli/test_node_templates.py index f0ad539..931c0a4 100644 --- a/tests/cli/test_node_templates.py +++ b/tests/cli/test_node_templates.py @@ -1,57 +1,66 @@ -from mock import ANY +from mock import ANY, MagicMock import pytest from aria.cli.env import _Environment from tests.cli.base_test import TestCliBase, mock_storage # pylint: disable=unused-import +from tests.mock.models import create_node_template_with_dependencies, NODE_NAME, \ + SERVICE_TEMPLATE_NAME, NODE_TEMPLATE_NAME class TestNodeTemplatesShow(TestCliBase): - def test_no_properties_no_nodes(self, monkeypatch, mock_storage): - + def test_header_strings(self, monkeypatch, mock_storage): monkeypatch.setattr(_Environment, 'model_storage', mock_storage) self.invoke('node_templates show 1') assert 'Showing node template 1' in self.logger_output_string assert 'Node template properties:' in self.logger_output_string + assert 'Nodes:' in self.logger_output_string + + def test_no_properties_no_nodes(self, monkeypatch, mock_storage): + + monkeypatch.setattr(_Environment, 'model_storage', mock_storage) + self.invoke('node_templates show 1') + assert 'No properties' in self.logger_output_string assert 'prop1' not in self.logger_output_string assert 'value1' not in self.logger_output_string assert 'No nodes' in self.logger_output_string - assert 'node1' not in self.logger_output_string + assert NODE_NAME not in self.logger_output_string def test_one_property_no_nodes(self, monkeypatch, mock_storage): monkeypatch.setattr(_Environment, 'model_storage', mock_storage) + m = MagicMock(return_value=create_node_template_with_dependencies(include_property=True)) + monkeypatch.setattr(mock_storage.node_template, 'get', m) self.invoke('node_templates show 2') - assert 'Showing node template 2' in self.logger_output_string - assert 'Node template properties:' in self.logger_output_string assert 'No properties' not in self.logger_output_string assert 'prop1' in self.logger_output_string and 'value1' in self.logger_output_string assert 'No nodes' in self.logger_output_string - assert 'node1' not in self.logger_output_string + assert NODE_NAME not in self.logger_output_string def test_no_properties_one_node(self, monkeypatch, mock_storage): monkeypatch.setattr(_Environment, 'model_storage', mock_storage) + m = MagicMock(return_value=create_node_template_with_dependencies(include_node=True)) + monkeypatch.setattr(mock_storage.node_template, 'get', m) self.invoke('node_templates show 3') - assert 'Showing node template 3' in self.logger_output_string - assert 'Node template properties:' in self.logger_output_string assert 'No properties' in self.logger_output_string assert 'prop1' not in self.logger_output_string assert 'value1' not in self.logger_output_string assert 'No nodes' not in self.logger_output_string - assert 'node1' in self.logger_output_string + assert NODE_NAME in self.logger_output_string def test_one_property_one_node(self, monkeypatch, mock_storage): monkeypatch.setattr(_Environment, 'model_storage', mock_storage) + m = MagicMock(return_value=create_node_template_with_dependencies(include_node=True, + include_property=True)) + monkeypatch.setattr(mock_storage.node_template, 'get', m) self.invoke('node_templates show 4') - assert 'Showing node template 4' in self.logger_output_string - assert 'Node template properties:' in self.logger_output_string assert 'No properties' not in self.logger_output_string assert 'prop1' in self.logger_output_string and 'value1' in self.logger_output_string assert 'No nodes' not in self.logger_output_string - assert 'node1' in self.logger_output_string + assert NODE_NAME in self.logger_output_string class TestNodeTemplatesList(TestCliBase): @@ -66,17 +75,20 @@ class TestNodeTemplatesList(TestCliBase): sort_by_in_output, order_in_output): monkeypatch.setattr(_Environment, 'model_storage', mock_storage) - self.invoke('node_templates list -t test_st{sort_by}{order}'.format(sort_by=sort_by, - order=order)) - assert 'Listing node templates for service template test_st...' in self.logger_output_string + self.invoke('node_templates list -t {service_template_name}{sort_by}{order}' + .format(service_template_name=SERVICE_TEMPLATE_NAME, + sort_by=sort_by, + order=order)) + assert 'Listing node templates for service template ' \ + '{name}...'.format(name=SERVICE_TEMPLATE_NAME) in self.logger_output_string assert 'Listing all node templates...' not in self.logger_output_string node_templates_list = mock_storage.node_template.list node_templates_list.assert_called_once_with(sort={sort_by_in_output: order_in_output}, filters={'service_template': ANY}) assert 'Node templates:' in self.logger_output_string - assert 'test_st' in self.logger_output_string - assert 'test_nt' in self.logger_output_string + assert SERVICE_TEMPLATE_NAME in self.logger_output_string + assert NODE_TEMPLATE_NAME in self.logger_output_string @pytest.mark.parametrize('sort_by, order, sort_by_in_output, order_in_output', [ ('', '', 'service_template_name', 'asc'), @@ -90,12 +102,12 @@ class TestNodeTemplatesList(TestCliBase): monkeypatch.setattr(_Environment, 'model_storage', mock_storage) self.invoke('node_templates list{sort_by}{order}'.format(sort_by=sort_by, order=order)) assert 'Listing all node templates...' in self.logger_output_string - assert 'Listing node templates for service template test_st...' not in \ - self.logger_output_string + assert 'Listing node templates for service template ' \ + '{name}...'.format(name=SERVICE_TEMPLATE_NAME) not in self.logger_output_string node_templates_list = mock_storage.node_template.list node_templates_list.assert_called_once_with(sort={sort_by_in_output: order_in_output}, filters={}) assert 'Node templates:' in self.logger_output_string - assert 'test_st' in self.logger_output_string - assert 'test_nt' in self.logger_output_string + assert SERVICE_TEMPLATE_NAME in self.logger_output_string + assert NODE_TEMPLATE_NAME in self.logger_output_string http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/blob/56a4767e/tests/cli/test_nodes.py ---------------------------------------------------------------------- diff --git a/tests/cli/test_nodes.py b/tests/cli/test_nodes.py index 9be97ca..e5a081c 100644 --- a/tests/cli/test_nodes.py +++ b/tests/cli/test_nodes.py @@ -1,19 +1,24 @@ import pytest -from mock import ANY +from mock import ANY, MagicMock from aria.cli.env import _Environment from tests.cli.base_test import TestCliBase, mock_storage # pylint: disable=unused-import +from tests.mock.models import create_node_with_dependencies class TestNodesShow(TestCliBase): - def test_no_attributes(self, monkeypatch, mock_storage): - + def test_header_strings(self, monkeypatch, mock_storage): monkeypatch.setattr(_Environment, 'model_storage', mock_storage) self.invoke('nodes show 1') assert 'Showing node 1' in self.logger_output_string assert 'Node:' in self.logger_output_string assert 'Node attributes:' in self.logger_output_string + + def test_no_attributes(self, monkeypatch, mock_storage): + + monkeypatch.setattr(_Environment, 'model_storage', mock_storage) + self.invoke('nodes show 2') assert 'No attributes' in self.logger_output_string assert 'attribute1' not in self.logger_output_string assert 'value1' not in self.logger_output_string @@ -21,12 +26,12 @@ class TestNodesShow(TestCliBase): def test_one_attribute(self, monkeypatch, mock_storage): monkeypatch.setattr(_Environment, 'model_storage', mock_storage) - self.invoke('nodes show 2') - assert 'Showing node 2' in self.logger_output_string - assert 'Node:' in self.logger_output_string - assert 'Node attributes:' in self.logger_output_string + m = MagicMock(return_value=create_node_with_dependencies(include_attribute=True)) + monkeypatch.setattr(mock_storage.node, 'get', m) + self.invoke('nodes show 3') assert 'No attributes' not in self.logger_output_string - assert 'attribute1' in self.logger_output_string and 'value1' in self.logger_output_string + assert 'attribute1' in self.logger_output_string + assert 'value1' in self.logger_output_string class TestNodesList(TestCliBase): http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/blob/56a4767e/tests/cli/test_service_templates.py ---------------------------------------------------------------------- diff --git a/tests/cli/test_service_templates.py b/tests/cli/test_service_templates.py index ef70c37..67ba567 100644 --- a/tests/cli/test_service_templates.py +++ b/tests/cli/test_service_templates.py @@ -1,4 +1,5 @@ import pytest +from mock import MagicMock from aria.cli import service_template_utils, csar from aria.cli.env import _Environment @@ -7,51 +8,76 @@ from aria.core import Core from aria.exceptions import AriaException from aria.storage import exceptions as storage_exceptions from tests.cli.base_test import TestCliBase, assert_exception_raised, raise_exception, mock_storage # pylint: disable=unused-import +from tests.mock.models import create_service_template, create_service, create_parameter, \ + SERVICE_TEMPLATE_NAME, SERVICE_NAME class TestServiceTemplatesShow(TestCliBase): - def test_show_no_services_no_description(self, monkeypatch, mock_storage): + def test_header_string(self, monkeypatch, mock_storage): monkeypatch.setattr(_Environment, 'model_storage', mock_storage) - self.invoke('service_templates show no_services_no_description') + self.invoke('service_templates show test_st') + assert 'Showing service template test_st...' in self.logger_output_string + + def test_no_services_no_description(self, monkeypatch, mock_storage): + + monkeypatch.setattr(_Environment, 'model_storage', mock_storage) + self.invoke('service_templates show test_st') - assert 'Showing service template no_services_no_description...' in self.logger_output_string assert 'Description:' not in self.logger_output_string - assert 'Existing services:\n[]' in self.logger_output_string + assert 'Existing services:' not in self.logger_output_string - def test_show_no_services_yes_description(self, monkeypatch, mock_storage): + def test_no_services_yes_description(self, monkeypatch, mock_storage): monkeypatch.setattr(_Environment, 'model_storage', mock_storage) - self.invoke('service_templates show no_services_yes_description') + st = create_service_template(description='test_description') + monkeypatch.setattr(mock_storage.service_template, 'get_by_name', + MagicMock(return_value=st)) - assert 'Showing service template no_services_yes_description...' in \ - self.logger_output_string - assert 'Description:\ntest_description' in self.logger_output_string - assert 'Existing services:\n[]' in self.logger_output_string + self.invoke('service_templates show test_st') + assert 'Description:' in self.logger_output_string + assert 'test_description' in self.logger_output_string + assert 'Existing services:' not in self.logger_output_string - def test_show_one_service_no_description(self, monkeypatch, mock_storage): + def test_one_service_no_description(self, monkeypatch, mock_storage): monkeypatch.setattr(_Environment, 'model_storage', mock_storage) - self.invoke('service_templates show one_service_no_description') + st = create_service_template() + st.services = [create_service(st)] + monkeypatch.setattr(mock_storage.service_template, 'get_by_name', + MagicMock(return_value=st)) + + self.invoke('service_templates show test_st') - assert 'Showing service template one_service_no_description...' in self.logger_output_string assert 'Description:' not in self.logger_output_string - assert "Existing services:\n['test_s']" in self.logger_output_string + assert 'Existing services:' in self.logger_output_string + assert SERVICE_NAME in self.logger_output_string - def test_show_one_service_yes_description(self, monkeypatch, mock_storage): + def test_one_service_yes_description(self, monkeypatch, mock_storage): monkeypatch.setattr(_Environment, 'model_storage', mock_storage) - self.invoke('service_templates show one_service_yes_description') + st = create_service_template(description='test_description') + st.services = [create_service(st)] + monkeypatch.setattr(mock_storage.service_template, 'get_by_name', + MagicMock(return_value=st)) + + self.invoke('service_templates show test_st') - assert 'Showing service template one_service_yes_description...' in \ - self.logger_output_string - assert 'Description:\ntest_description' in self.logger_output_string - assert "Existing services:\n['test_s']" in self.logger_output_string + assert 'Description:' in self.logger_output_string + assert 'test_description' in self.logger_output_string + assert 'Existing services:' in self.logger_output_string + assert 'test_s' in self.logger_output_string class TestServiceTemplatesList(TestCliBase): + def test_header_string(self, monkeypatch, mock_storage): + + monkeypatch.setattr(_Environment, 'model_storage', mock_storage) + self.invoke('service_templates list') + assert 'Listing all service templates...' in self.logger_output_string + @pytest.mark.parametrize('sort_by, order, sort_by_in_output, order_in_output', [ ('', '', 'created_at', 'asc'), ('', ' --descending', 'created_at', 'desc'), @@ -66,19 +92,24 @@ class TestServiceTemplatesList(TestCliBase): mock_storage.service_template.list.assert_called_with( sort={sort_by_in_output: order_in_output}) - assert 'Listing all service templates...' in self.logger_output_string - assert 'test_st' in self.logger_output_string + assert SERVICE_TEMPLATE_NAME in self.logger_output_string class TestServiceTemplatesStore(TestCliBase): + def test_header_string(self, monkeypatch, mock_storage): + + monkeypatch.setattr(_Environment, 'model_storage', mock_storage) + self.invoke('service_templates store stubpath test_st') + assert 'Storing service template test_st...' in self.logger_output_string + def test_store_no_exception(self, monkeypatch, mock_object): monkeypatch.setattr(Core, 'create_service_template', mock_object) monkeypatch.setattr(service_template_utils, 'get', mock_object) - self.invoke('service_templates store stubpath test_st') - assert 'Storing service template test_st...' in self.logger_output_string - assert 'Service template test_st stored' in self.logger_output_string + self.invoke('service_templates store stubpath {name}'.format(name=SERVICE_TEMPLATE_NAME)) + assert 'Service template {name} stored'.format(name=SERVICE_TEMPLATE_NAME) \ + in self.logger_output_string def test_store_raises_exception_resulting_from_name_uniqueness(self, monkeypatch, mock_object): @@ -93,7 +124,6 @@ class TestServiceTemplatesStore(TestCliBase): expected_exception=AriaCliError, expected_msg='Could not store service template `test_st`\n' 'There already a exists a service template with the same name') - assert 'Storing service template test_st...' in self.logger_output_string def test_store_raises_exception(self, monkeypatch, mock_object): @@ -105,18 +135,23 @@ class TestServiceTemplatesStore(TestCliBase): assert_exception_raised( self.invoke('service_templates store stubpath test_st'), expected_exception=AriaCliError) - assert 'Storing service template test_st...' in self.logger_output_string class TestServiceTemplatesDelete(TestCliBase): + def test_header_string(self, monkeypatch, mock_storage): + + monkeypatch.setattr(_Environment, 'model_storage', mock_storage) + self.invoke('service_templates delete test_st') + assert 'Deleting service template test_st...' in self.logger_output_string + def test_delete_no_exception(self, monkeypatch, mock_object): monkeypatch.setattr(_Environment, 'model_storage', mock_object) monkeypatch.setattr(Core, 'delete_service_template', mock_object) - self.invoke('service_templates delete test_st') - assert 'Deleting service template test_st...' in self.logger_output_string - assert 'Service template test_st deleted' in self.logger_output_string + self.invoke('service_templates delete {name}'.format(name=SERVICE_TEMPLATE_NAME)) + assert 'Service template {name} deleted'.format(name=SERVICE_TEMPLATE_NAME) \ + in self.logger_output_string def test_delete_raises_exception(self, monkeypatch, mock_object): @@ -129,31 +164,44 @@ class TestServiceTemplatesDelete(TestCliBase): self.invoke('service_templates delete test_st'), expected_exception=AriaCliError, expected_msg='') - assert 'Deleting service template test_st...' in self.logger_output_string class TestServiceTemplatesInputs(TestCliBase): + def test_header_string(self, monkeypatch, mock_storage): + + monkeypatch.setattr(_Environment, 'model_storage', mock_storage) + self.invoke('service_templates inputs test_st') + assert 'Showing inputs for service template test_st...' in self.logger_output_string + def test_inputs_existing_inputs(self, monkeypatch, mock_storage): monkeypatch.setattr(_Environment, 'model_storage', mock_storage) + input = create_parameter(name='input1', value='value1') + st = create_service_template(inputs={'input1': input}) + monkeypatch.setattr(mock_storage.service_template, 'get_by_name', + MagicMock(return_value=st)) + self.invoke('service_templates inputs with_inputs') - assert 'Showing inputs for service template with_inputs...' in self.logger_output_string assert 'input1' in self.logger_output_string and 'value1' in self.logger_output_string def test_inputs_no_inputs(self, monkeypatch, mock_storage): monkeypatch.setattr(_Environment, 'model_storage', mock_storage) self.invoke('service_templates inputs without_inputs') - assert 'Showing inputs for service template without_inputs...' in self.logger_output_string assert 'No inputs' in self.logger_output_string class TestServiceTemplatesValidate(TestCliBase): + def test_header_string(self, monkeypatch, mock_storage): + + monkeypatch.setattr(_Environment, 'model_storage', mock_storage) + self.invoke('service_templates validate stubpath') + assert 'Validating service template: stubpath' in self.logger_output_string + def test_validate_no_exception(self, monkeypatch, mock_object): monkeypatch.setattr(Core, 'validate_service_template', mock_object) monkeypatch.setattr(service_template_utils, 'get', mock_object) self.invoke('service_templates validate stubpath') - assert 'Validating service template: stubpath' in self.logger_output_string assert 'Service template validated successfully' in self.logger_output_string def test_validate_raises_exception(self, monkeypatch, mock_object): @@ -162,13 +210,17 @@ class TestServiceTemplatesValidate(TestCliBase): assert_exception_raised( self.invoke('service_templates validate stubpath'), expected_exception=AriaCliError) - assert 'Validating service template: stubpath' in self.logger_output_string class TestServiceTemplatesCreateArchive(TestCliBase): + def test_header_string(self, monkeypatch, mock_storage): + + monkeypatch.setattr(_Environment, 'model_storage', mock_storage) + self.invoke('service_templates create_archive stubpath stubdest') + assert 'Creating a csar archive' in self.logger_output_string + def test_create_archive_successful(self, monkeypatch, mock_object): monkeypatch.setattr(csar, 'write', mock_object) self.invoke('service_templates create_archive stubpath stubdest') - assert 'Creating a csar archive' in self.logger_output_string assert 'Csar archive created at stubdest' in self.logger_output_string http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/blob/56a4767e/tests/cli/test_services.py ---------------------------------------------------------------------- diff --git a/tests/cli/test_services.py b/tests/cli/test_services.py index 116e449..7e48593 100644 --- a/tests/cli/test_services.py +++ b/tests/cli/test_services.py @@ -1,5 +1,5 @@ import pytest -from mock import ANY +from mock import ANY, MagicMock from aria.cli.exceptions import AriaCliError from aria.cli.env import _Environment from aria.core import Core @@ -7,7 +7,7 @@ from aria.exceptions import (AriaException, DependentActiveExecutionsError, DependentAvailableNodesError) from aria.storage import exceptions as storage_exceptions from tests.cli.base_test import TestCliBase, raise_exception, assert_exception_raised, mock_storage #pylint: disable=unused-import -from tests.mock.models import create_service, create_service_template +from tests.mock.models import create_service_with_dependencies, SERVICE_TEMPLATE_NAME, SERVICE_NAME class TestServicesList(TestCliBase): @@ -18,8 +18,8 @@ class TestServicesList(TestCliBase): (' --sort-by name', '', 'name', 'asc'), (' --sort-by name', ' --descending', 'name', 'desc') ]) - def test_list_no_specified_service_template(self, monkeypatch, mock_storage, sort_by, order, - sort_by_in_output, order_in_output): + def test_no_specified_service_template(self, monkeypatch, mock_storage, sort_by, order, + sort_by_in_output, order_in_output): monkeypatch.setattr(_Environment, 'model_storage', mock_storage) self.invoke('services list{sort_by}{order}'.format(sort_by=sort_by, order=order)) @@ -29,8 +29,8 @@ class TestServicesList(TestCliBase): mock_storage.service.list.assert_called_once_with(sort={sort_by_in_output: order_in_output}, filters={}) assert 'Services:' in self.logger_output_string - assert 'test_st' in self.logger_output_string - assert 'test_s' in self.logger_output_string + assert SERVICE_TEMPLATE_NAME in self.logger_output_string + assert SERVICE_NAME in self.logger_output_string @pytest.mark.parametrize('sort_by, order, sort_by_in_output, order_in_output', [ ('', '', 'created_at', 'asc'), @@ -38,8 +38,8 @@ class TestServicesList(TestCliBase): (' --sort-by name', '', 'name', 'asc'), (' --sort-by name', ' --descending', 'name', 'desc') ]) - def test_list_specified_service_template(self, monkeypatch, mock_storage, sort_by, order, - sort_by_in_output, order_in_output): + def test_specified_service_template(self, monkeypatch, mock_storage, sort_by, order, + sort_by_in_output, order_in_output): monkeypatch.setattr(_Environment, 'model_storage', mock_storage) self.invoke('services list -t test_st{sort_by}{order}'.format(sort_by=sort_by, order=order)) @@ -49,27 +49,29 @@ class TestServicesList(TestCliBase): mock_storage.service.list.assert_called_once_with(sort={sort_by_in_output: order_in_output}, filters={'service_template': ANY}) assert 'Services:' in self.logger_output_string - assert 'test_st' in self.logger_output_string - assert 'test_s' in self.logger_output_string + assert SERVICE_TEMPLATE_NAME in self.logger_output_string + assert SERVICE_NAME in self.logger_output_string class TestServicesCreate(TestCliBase): - def test_create_no_exception(self, monkeypatch, mock_object): + def test_header_string(self, monkeypatch, mock_storage): + monkeypatch.setattr(_Environment, 'model_storage', mock_storage) + self.invoke('services create -t test_st test_s') + assert 'Creating new service from service template test_st...' in self.logger_output_string - monkeypatch.setattr(_Environment, 'model_storage', mock_object) + def test_no_exception(self, monkeypatch, mock_storage): - test_st = create_service_template('test_st') - mock_object.return_value = create_service(test_st, 'test_s') - monkeypatch.setattr(Core, 'create_service', mock_object) - self.invoke('services create -t test_st test_s') + monkeypatch.setattr(_Environment, 'model_storage', mock_storage) - assert 'Creating new service from service template test_st...' in self.logger_output_string + m = MagicMock(return_value=create_service_with_dependencies()) + monkeypatch.setattr(Core, 'create_service', m) + self.invoke('services create -t test_st test_s') assert "Service created. The service's name is test_s" in self.logger_output_string - def test_store_raises_storage_error_resulting_from_name_uniqueness(self, monkeypatch, - mock_object): - monkeypatch.setattr(_Environment, 'model_storage', mock_object) + def test_raises_storage_error_resulting_from_name_uniqueness(self, monkeypatch, + mock_storage): + monkeypatch.setattr(_Environment, 'model_storage', mock_storage) monkeypatch.setattr(Core, 'create_service', raise_exception(storage_exceptions.NotFoundError, @@ -80,10 +82,9 @@ class TestServicesCreate(TestCliBase): expected_msg='Could not store service `test_s`\n' 'There already a exists a service with the same name') - assert 'Creating new service from service template test_st...' in self.logger_output_string assert "Service created. The service's name is test_s" not in self.logger_output_string - def test_store_raises_other_storage_error(self, monkeypatch, mock_object): + def test_raises_other_storage_error(self, monkeypatch, mock_object): monkeypatch.setattr(_Environment, 'model_storage', mock_object) monkeypatch.setattr(Core, 'create_service', @@ -93,10 +94,9 @@ class TestServicesCreate(TestCliBase): self.invoke('services create -t test_st test_s'), expected_exception=AriaCliError) - assert 'Creating new service from service template test_st...' in self.logger_output_string assert "Service created. The service's name is test_s" not in self.logger_output_string - def test_store_raises_aria_exception(self, monkeypatch, mock_storage): + def test_raises_aria_exception(self, monkeypatch, mock_storage): monkeypatch.setattr(_Environment, 'model_storage', mock_storage) monkeypatch.setattr(Core, 'create_service', @@ -107,62 +107,67 @@ class TestServicesCreate(TestCliBase): expected_exception=AriaCliError, expected_msg='error creating service `test_s`') - assert 'Creating new service from service template with_inputs...' in \ - self.logger_output_string assert 'error creating service `test_s`' in self.logger_output_string - assert 'input1' in self.logger_output_string and 'value1' in self.logger_output_string assert "Service created. The service's name is test_s" not in self.logger_output_string class TestServicesDelete(TestCliBase): - def test_delete_no_exception(self, monkeypatch, mock_object): + def test_header_string(self, monkeypatch, mock_storage): + monkeypatch.setattr(_Environment, 'model_storage', mock_storage) + self.invoke('services delete test_s') + assert 'Deleting service test_s...' in self.logger_output_string - monkeypatch.setattr(_Environment, 'model_storage', mock_object) + def test_delete_no_exception(self, monkeypatch, mock_storage, mock_object): + + monkeypatch.setattr(_Environment, 'model_storage', mock_storage) monkeypatch.setattr(Core, 'delete_service', mock_object) self.invoke('services delete test_s') - assert 'Deleting service test_s...' in self.logger_output_string assert 'Service test_s deleted' in self.logger_output_string def test_delete_active_execution_error(self, monkeypatch, mock_storage): monkeypatch.setattr(_Environment, 'model_storage', mock_storage) + mock_service_with_execution = \ + MagicMock(return_value=create_service_with_dependencies(include_execution=True)) + monkeypatch.setattr(mock_storage.service, 'get', mock_service_with_execution) assert_exception_raised( - self.invoke('services delete service_with_active_executions'), + self.invoke('services delete test_s'), expected_exception=DependentActiveExecutionsError, - expected_msg="Can't delete service test_s - there is an active " - "execution for this service. Active execution id: 1" - ) - assert 'Deleting service service_with_active_executions...' in self.logger_output_string + expected_msg="Can't delete service {name} - there is an active execution " + "for this service. Active execution id: 1".format(name=SERVICE_NAME)) def test_delete_available_nodes_error(self, monkeypatch, mock_storage): monkeypatch.setattr(_Environment, 'model_storage', mock_storage) assert_exception_raised( - self.invoke('services delete service_with_available_nodes'), + self.invoke('services delete test_s'), expected_exception=DependentAvailableNodesError, - expected_msg="Can't delete service test_s - " - "there are available nodes for this service. Available node ids: 1" + expected_msg="Can't delete service {name} - there are available nodes " + "for this service. Available node ids: 1".format(name=SERVICE_NAME) ) - assert 'Deleting service service_with_available_nodes...' in self.logger_output_string def test_delete_available_nodes_error_with_force(self, monkeypatch, mock_storage): monkeypatch.setattr(_Environment, 'model_storage', mock_storage) self.invoke('services delete service_with_available_nodes --force') assert mock_storage.service.delete.call_count == 1 - assert 'Deleting service service_with_available_nodes...' in self.logger_output_string assert 'Service service_with_available_nodes deleted' in self.logger_output_string + class TestServicesOutputs(TestCliBase): pass class TestServicesInputs(TestCliBase): + def test_header_string(self, monkeypatch, mock_storage): + monkeypatch.setattr(_Environment, 'model_storage', mock_storage) + self.invoke('services inputs test_s') + assert 'Showing inputs for service test_s...' in self.logger_output_string + def test_inputs_no_inputs(self, monkeypatch, mock_storage): monkeypatch.setattr(_Environment, 'model_storage', mock_storage) self.invoke('services inputs service_with_no_inputs') - assert 'Showing inputs for service service_with_no_inputs...' in self.logger_output_string assert 'No inputs' in self.logger_output_string assert 'input1' not in self.logger_output_string assert 'value1' not in self.logger_output_string @@ -170,9 +175,11 @@ class TestServicesInputs(TestCliBase): def test_inputs_one_input(self, monkeypatch, mock_storage): monkeypatch.setattr(_Environment, 'model_storage', mock_storage) - self.invoke('services inputs service_with_one_input') + s = create_service_with_dependencies(include_input=True) + monkeypatch.setattr(mock_storage.service, 'get_by_name', MagicMock(return_value=s)) + + self.invoke('services inputs test_s') - assert 'Showing inputs for service service_with_one_input...' in self.logger_output_string assert 'input1' in self.logger_output_string assert 'value1' in self.logger_output_string assert 'No inputs' not in self.logger_output_string http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/blob/56a4767e/tests/cli/utils.py ---------------------------------------------------------------------- diff --git a/tests/cli/utils.py b/tests/cli/utils.py index b1537c5..e33203e 100644 --- a/tests/cli/utils.py +++ b/tests/cli/utils.py @@ -1,5 +1,4 @@ import logging -from aria.modeling import models from mock import MagicMock from tests.mock import models as mock_models @@ -51,135 +50,36 @@ class MockStorage(object): self.service = MockServiceStorage() self.node_template = MockNodeTemplateStorage() self.node = MockNodeStorage() - self.log = MockLogStorage() class MockServiceTemplateStorage(object): def __init__(self): - self.list = MagicMock(return_value=[mock_models.create_service_template('test_st')]) - - @staticmethod - def get_by_name(name): - st = mock_models.create_service_template('test_st') - if name == 'no_services_no_description': - pass - elif name == 'no_services_yes_description': - st.description = 'test_description' - elif name == 'one_service_no_description': - service = mock_models.create_service(st, 'test_s') - st.services = [service] - elif name == 'one_service_yes_description': - service = mock_models.create_service(st, 'test_s') - st.description = 'test_description' - st.services = [service] - elif name == 'with_inputs': - input = mock_models.create_parameter(name='input1', value='value1') - st.inputs = {'input1': input} - elif name == 'without_inputs': - st.inputs = {} - elif name == 'one_service': - service = mock_models.create_service(st, 'test_s') - st.services = [service] - return st + self.list = MagicMock(return_value=[mock_models.create_service_template()]) + self.get_by_name = MagicMock(return_value=mock_models.create_service_template()) class MockServiceStorage(object): def __init__(self): - self.st = mock_models.create_service_template('test_st') - self.list = MagicMock(return_value=[mock_models.create_service(self.st, 'test_s')]) - self.delete = MagicMock() - @staticmethod - def get(id): - test_st = mock_models.create_service_template('test_st') - test_s = mock_models.create_service(test_st, 'test_s') - if id == '1': - execution = mock_models.create_execution(test_s, status=models.Execution.STARTED) - execution.id = '1' - test_s.executions = [execution] - elif id == '2': - node_template = mock_models.create_node_template(service_template=test_st) - node = mock_models.create_node(name='test_node', - dependency_node_template=node_template, - service=test_s, - state=models.Node.STARTED) - node.id = '1' - return test_s - - @staticmethod - def get_by_name(name): - test_st = mock_models.create_service_template('test_st') - test_s = mock_models.create_service(test_st, 'test_s') - if name == 'service_with_active_executions': - m = MagicMock() - m.id = '1' - return m - elif name == 'service_with_available_nodes': - m = MagicMock() - m.id = '2' - return m - elif name == 'service_with_no_inputs': - pass - elif name == 'service_with_one_input': - input = mock_models.create_parameter(name='input1', value='value1') - test_s.inputs = {'input1': input} - - return test_s + self.s = mock_models.create_service_with_dependencies() + + self.list = MagicMock(return_value=[self.s]) + self.create = MagicMock(return_value=self.s) + self.get = MagicMock( + return_value=mock_models.create_service_with_dependencies(include_node=True)) + self.get_by_name = MagicMock(return_value=self.s) + self.delete = MagicMock() class MockNodeTemplateStorage(object): - def __init__(self): - self.st = mock_models.create_service_template('test_st') - self.list = MagicMock(return_value=[mock_models.create_node_template(self.st, 'test_nt')]) - - - @staticmethod - def get(id): - st = mock_models.create_service_template('test_st') - s = mock_models.create_service(st, 'test_s') - nt = mock_models.create_node_template(service_template=st, name='test_nt') - if id == '1': - pass - elif id == '2': - prop1 = mock_models.create_parameter('prop1', 'value1') - nt.properties = {'prop1': prop1} - elif id == '3': - mock_models.create_node('node1', nt, s) - elif id == '4': - prop1 = mock_models.create_parameter('prop1', 'value1') - nt.properties = {'prop1': prop1} - mock_models.create_node('node1', nt, s) - return nt + self.get = MagicMock(return_value=mock_models.create_node_template_with_dependencies()) + self.list = MagicMock(return_value=[mock_models.create_node_template_with_dependencies()]) class MockNodeStorage(object): - - def __init__(self): - self.st = mock_models.create_service_template('test_st') - self.s = mock_models.create_service(self.st, 'test_s') - self.nt = mock_models.create_node_template(service_template=self.st, name='test_nt') - self.list = MagicMock(return_value=[mock_models.create_node('test_n', self.nt, self.s)]) - - @staticmethod - def get(id): - st = mock_models.create_service_template('test_st') - s = mock_models.create_service(st, 'test_s') - nt = mock_models.create_node_template(service_template=st, name='test_nt') - n = mock_models.create_node('test_n', nt, s) - if id == '1': - pass - elif id == '2': - n.runtime_properties = {'attribute1': 'value1'} - return n - - -class MockLogStorage(object): - def __init__(self): - st = mock_models.create_service_template('test_st') - s = mock_models.create_service(st, 'test_s') - execution = mock_models.create_execution(s) - self.list = MagicMock(return_value=[mock_models.create_log(execution)]) + self.get = MagicMock(return_value=mock_models.create_node_with_dependencies()) + self.list = MagicMock(return_value=[mock_models.create_node_with_dependencies()]) http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/blob/56a4767e/tests/mock/models.py ---------------------------------------------------------------------- diff --git a/tests/mock/models.py b/tests/mock/models.py index 0e6bbe4..a434a2d 100644 --- a/tests/mock/models.py +++ b/tests/mock/models.py @@ -37,10 +37,11 @@ from aria.orchestrator.workflows.builtin.workflows import ( NORMATIVE_REMOVE_SOURCE ) -SERVICE_NAME = 'test_service_name' -SERVICE_TEMPLATE_NAME = 'test_service_template_name' +SERVICE_TEMPLATE_NAME = 'test_service_template' +SERVICE_NAME = 'test_service1' NODE_TEMPLATE_NAME = 'test_node_template' -WORKFLOW_NAME = 'test_workflow_name' +NODE_NAME = 'test_node1' +WORKFLOW_NAME = 'test_workflow' TASK_RETRY_INTERVAL = 1 TASK_MAX_ATTEMPTS = 1 @@ -50,11 +51,13 @@ DEPENDENT_NODE_TEMPLATE_NAME = 'dependent_node_template' DEPENDENT_NODE_NAME = 'dependent_node' -def create_service_template(name=SERVICE_TEMPLATE_NAME): +def create_service_template(name=SERVICE_TEMPLATE_NAME, description=None, inputs=None): now = datetime.now() + inputs = inputs or {} return models.ServiceTemplate( name=name, - description=None, + description=description, + inputs=inputs, created_at=now, updated_at=now, main_file_name='main_file_name', @@ -68,10 +71,12 @@ def create_service_template(name=SERVICE_TEMPLATE_NAME): ) -def create_service(service_template, name=SERVICE_NAME): +def create_service(service_template, name=SERVICE_NAME, inputs=None): now = datetime.utcnow() + inputs = inputs or {} return models.Service( name=name, + inputs=inputs, service_template=service_template, description='', created_at=now, @@ -81,6 +86,46 @@ def create_service(service_template, name=SERVICE_NAME): ) +def create_service_with_dependencies(include_execution=False, + include_input=False, + include_node=False): + service_template = create_service_template() + service = create_service(service_template=service_template) + if include_execution: + execution = create_execution(service=service, status=models.Execution.STARTED) + service.executions = [execution] + execution.id = '1' + if include_input: + input = create_parameter(name='input1', value='value1') + service.inputs = {'input1': input} + if include_node: + node_template = create_node_template(service_template=service_template) + node = create_node(node_template, service, state=models.Node.STARTED) + node.id = '1' + return service + + +def create_node_template_with_dependencies(include_node=False, include_property=False): + service_template = create_service_template() + node_template = create_node_template(service_template=service_template) + if include_node: + service = create_service(service_template=service_template) + create_node(dependency_node_template=node_template, service=service) + if include_property: + node_template.properties = {'prop1': create_parameter(name='prop1', value='value1')} + return node_template + + +def create_node_with_dependencies(include_attribute=False): + + node_template = create_node_template_with_dependencies() + node_template.service_template.services[0] = create_service(node_template.service_template) + node = create_node(node_template, node_template.service_template.services[0]) + if include_attribute: + node.runtime_properties = {'attribute1': 'value1'} + return node + + def create_node_template(service_template, name=NODE_TEMPLATE_NAME, type=models.Type(variant='node', name='test_node_type'), @@ -141,10 +186,9 @@ def create_dependent_node_template( ) -def create_node(name, dependency_node_template, service, state=models.Node.INITIAL, +def create_node(dependency_node_template, service, name=NODE_NAME, state=models.Node.INITIAL, runtime_properties=None): runtime_properties = runtime_properties or {} - # tmp_runtime_properties = {'ip': '1.1.1.1'} node = models.Node( name=name, type=dependency_node_template.type, @@ -244,15 +288,6 @@ def create_parameter(name, value): return p.wrap(name, value) -def create_log(execution, msg='log_msg', level=0, created_at=datetime.utcnow()): - - return models.Log( - execution=execution, - msg=msg, - level=level, - created_at=created_at) - - def _dictify(item): return dict(((item.name, item),)) http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/blob/56a4767e/tests/mock/topology.py ---------------------------------------------------------------------- diff --git a/tests/mock/topology.py b/tests/mock/topology.py index e5b4e01..bfb7b4e 100644 --- a/tests/mock/topology.py +++ b/tests/mock/topology.py @@ -33,7 +33,7 @@ def create_simple_topology_single_node(model_storage, create_operation): ) node_template.interface_templates[interface_template.name] = interface_template # pylint: disable=unsubscriptable-object - node = models.create_node(models.DEPENDENCY_NODE_NAME, node_template, service) + node = models.create_node(node_template, service, name=models.DEPENDENCY_NODE_NAME) interface = models.create_interface( service, 'Standard', 'create', @@ -59,9 +59,9 @@ def create_simple_topology_two_nodes(model_storage): dependency_node_template) dependency_node = models.create_node( - models.DEPENDENCY_NODE_NAME, dependency_node_template, service) + dependency_node_template, service, models.DEPENDENCY_NODE_NAME) dependent_node = models.create_node( - models.DEPENDENT_NODE_NAME, dependent_node_template, service) + dependent_node_template, service, models.DEPENDENT_NODE_NAME) dependent_node.outbound_relationships.append(models.create_relationship( # pylint: disable=no-member source=dependent_node, @@ -86,7 +86,7 @@ def create_simple_topology_three_nodes(model_storage): service = model_storage.service.get(service_id) third_node_template = models.create_dependency_node_template( service.service_template, name='another_dependency_node_template') - third_node = models.create_node('another_dependency_node', third_node_template, service) + third_node = models.create_node(third_node_template, service, 'another_dependency_node') new_relationship = models.create_relationship( source=model_storage.node.get_by_name(models.DEPENDENT_NODE_NAME), target=third_node, http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/blob/56a4767e/tests/modeling/test_models.py ---------------------------------------------------------------------- diff --git a/tests/modeling/test_models.py b/tests/modeling/test_models.py index d91249a..d64cdba 100644 --- a/tests/modeling/test_models.py +++ b/tests/modeling/test_models.py @@ -100,12 +100,13 @@ def _nodes_storage(): service = storage.service.get_by_name(mock.models.SERVICE_NAME) dependency_node_template = storage.node_template.get_by_name( mock.models.DEPENDENCY_NODE_TEMPLATE_NAME) - mock.models.create_node(mock.models.DEPENDENCY_NODE_NAME, dependency_node_template, service) + mock.models.create_node(dependency_node_template, service, + name=mock.models.DEPENDENCY_NODE_NAME) dependent_node_template = mock.models.create_dependent_node_template(service.service_template, dependency_node_template) - mock.models.create_node(mock.models.DEPENDENT_NODE_NAME, dependent_node_template, service) + mock.models.create_node(dependent_node_template, service, name=mock.models.DEPENDENT_NODE_NAME) storage.service.update(service) return storage
