Repository: incubator-ariatosca Updated Branches: refs/heads/ARIA-48-aria-cli 7ae1867ed -> 327cbd178
Mock storage and reroute logger Project: http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/commit/327cbd17 Tree: http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/tree/327cbd17 Diff: http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/diff/327cbd17 Branch: refs/heads/ARIA-48-aria-cli Commit: 327cbd1782bcb48aab570802d8e4879fa4c10312 Parents: 7ae1867 Author: Avia Efrat <[email protected]> Authored: Tue Apr 4 17:42:40 2017 +0300 Committer: Avia Efrat <[email protected]> Committed: Tue Apr 4 17:42:40 2017 +0300 ---------------------------------------------------------------------- tests/cli/__init__.py | 14 ++++++++++ tests/cli/base_test.py | 13 ++++++++++ tests/cli/runner.py | 12 +++++++++ tests/cli/test_service_templates.py | 27 ++++++++++++++++++++ tests/cli/utils.py | 44 ++++++++++++++++++++++++++++++++ 5 files changed, 110 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/blob/327cbd17/tests/cli/__init__.py ---------------------------------------------------------------------- diff --git a/tests/cli/__init__.py b/tests/cli/__init__.py new file mode 100644 index 0000000..13878a1 --- /dev/null +++ b/tests/cli/__init__.py @@ -0,0 +1,14 @@ +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You 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. \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/blob/327cbd17/tests/cli/base_test.py ---------------------------------------------------------------------- diff --git a/tests/cli/base_test.py b/tests/cli/base_test.py new file mode 100644 index 0000000..c8c574b --- /dev/null +++ b/tests/cli/base_test.py @@ -0,0 +1,13 @@ +from StringIO import StringIO + +import runner +from utils import setup_logger + + +class TestCliBase(object): + + logger_output = StringIO() + setup_logger(logger_name='aria.cli.main', output_stream=logger_output) + + def invoke(self, command): + return runner.invoke(command) http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/blob/327cbd17/tests/cli/runner.py ---------------------------------------------------------------------- diff --git a/tests/cli/runner.py b/tests/cli/runner.py new file mode 100644 index 0000000..26af20a --- /dev/null +++ b/tests/cli/runner.py @@ -0,0 +1,12 @@ +import aria.cli.commands as commands +import click.testing + + +def invoke(command): + # TODO handle verbosity later + command_string = ['service_templates', 'show', '1'] + command, sub, args = command_string[0], command_string[1], command_string[2:] + runner = click.testing.CliRunner() + outcome = runner.invoke(getattr( + getattr(commands, command), sub), args) + return outcome \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/blob/327cbd17/tests/cli/test_service_templates.py ---------------------------------------------------------------------- diff --git a/tests/cli/test_service_templates.py b/tests/cli/test_service_templates.py new file mode 100644 index 0000000..8e1fa65 --- /dev/null +++ b/tests/cli/test_service_templates.py @@ -0,0 +1,27 @@ +from aria.cli.env import Environment +from base_test import TestCliBase +from aria.modeling.models import ServiceTemplate + + +class MockStorage(object): + + def __init__(self): + self.service_template = MockServiceTemplateStorage() + + +class MockServiceTemplateStorage(object): + + def get(self, id_): + if id_ == '1': # a service-template with no description and no services. + st = ServiceTemplate() + st.name = 'test_st' + st.services = [] + return st + + +class TestServiceTemplatesShow(TestCliBase): + + def test_show_no_services_no_description(self, monkeypatch): + # reroute the logger to a special location, and check it's content. + monkeypatch.setattr(Environment, 'model_storage', MockStorage()) + outcome = self.invoke('service_templates show 1') http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/blob/327cbd17/tests/cli/utils.py ---------------------------------------------------------------------- diff --git a/tests/cli/utils.py b/tests/cli/utils.py new file mode 100644 index 0000000..fb3757a --- /dev/null +++ b/tests/cli/utils.py @@ -0,0 +1,44 @@ +import logging + + +def setup_logger(logger_name, + output_stream, + logger_level=logging.INFO, + handlers=None, + remove_existing_handlers=True, + logger_format=None): + """ + :param logger_name: Name of the logger. + :param logger_level: Level for the logger (not for specific handler). + :param handlers: An optional list of handlers (formatter will be + overridden); If None, only a StreamHandler for + sys.stdout will be used. + :param remove_existing_handlers: Determines whether to remove existing + handlers before adding new ones + :param logger_format: the format this logger will have. + :param propagate: propagate the message the parent logger. + :return: A logger instance. + :rtype: logging.Logger + """ + logger = logging.getLogger(logger_name) + + if logger_format is None: + # for easier parsing in tests, we prefer that the log will only include the message. + logger_format = '%(message)s' + + formatter = logging.Formatter(fmt=logger_format) + + if remove_existing_handlers: + for handler in logger.handlers: + logger.removeHandler(handler) + + if not handlers: + handler = logging.StreamHandler(output_stream) + handler.setLevel(logging.DEBUG) + handlers = [handler] + + for handler in handlers: + handler.setFormatter(formatter) + logger.addHandler(handler) + + logger.setLevel(logger_level)
