diff options
33 files changed, 392 insertions, 142 deletions
diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index a4929172..03420986 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -83,6 +83,15 @@ Specially, it is recommended to link each patch set with a JIRA issue. Put in commit message to create an automatic link. +Test Coverage +------------- + +The implementation, including new features and bug fix, **must** be covered by +unit test. The criteria for test coverage in QTIP project are as following: + +* >=80% coverage for each file +* >=90% overall coverage for whole project + Documentation ------------- diff --git a/qtip/base/__init__.py b/qtip/base/__init__.py index e69de29b..909703ed 100644 --- a/qtip/base/__init__.py +++ b/qtip/base/__init__.py @@ -0,0 +1,19 @@ +############################################################################## +# Copyright (c) 2017 ZTE Corp and others. +# +# All rights reserved. This program and the accompanying materials +# are made available under the terms of the Apache License, Version 2.0 +# which accompanies this distribution, and is available at +# http://www.apache.org/licenses/LICENSE-2.0 +############################################################################## + + +class BaseActor(object): + """abstract actor class""" + + def __init__(self, config, parent=None): + self._config = config + self._parent = parent + + def get_config(self, key, default=None): + return self._config.get(key, default) diff --git a/qtip/base/constant.py b/qtip/base/constant.py index ddd07e9d..09c635ac 100644 --- a/qtip/base/constant.py +++ b/qtip/base/constant.py @@ -34,6 +34,8 @@ class BaseProp(object): # content DESCRIPTION = 'description' + WORKLOADS = 'workloads' + TYPE = 'type' class SpecProp(BaseProp): @@ -45,29 +47,5 @@ class SpecProp(BaseProp): WORKLOADS = 'workloads' -class PlanProp(BaseProp): - # plan - INFO = 'info' - - FACILITY = 'facility' - ENGINEER = 'engineer' - - CONFIG = 'config' - - DRIVER = 'driver' - COLLECTOR = 'collector' - REPORTER = 'reporter' - - QPIS = 'QPIs' - - -class CollectorProp(BaseProp): - LOGS = 'logs' - FILENAME = 'filename' - GREP = 'grep' - REGEX = 'regex' - CAPTURE = 'capture' - - class ReporterBaseProp(BaseProp): TRANSFORMER = 'transformer' diff --git a/qtip/base/error.py b/qtip/base/error.py index 01a7f7a6..a055aa8d 100644 --- a/qtip/base/error.py +++ b/qtip/base/error.py @@ -8,22 +8,23 @@ ############################################################################## -class QtipError(Exception): +class BaseError(Exception): pass -class InvalidFormat(QtipError): - def __init__(self, filename): +class InvalidContent(BaseError): + def __init__(self, filename, excinfo=None): self.filename = filename + self.excinfo = excinfo -class NotFound(QtipError): +class NotFound(BaseError): def __init__(self, module, package='qtip'): self.package = package self.module = module -class ToBeDoneError(QtipError): +class ToBeDoneError(BaseError): """something still to be done""" def __init__(self, method, module): self.method = method diff --git a/qtip/cli/commands/cmd_metric.py b/qtip/cli/commands/cmd_metric.py index d2fbd58f..aa4df1f4 100644 --- a/qtip/cli/commands/cmd_metric.py +++ b/qtip/cli/commands/cmd_metric.py @@ -27,7 +27,7 @@ def cmd_list(ctx): pass -@cli.command('run', help='Run tests to collect Performance Metrics') +@cli.command('run', help='Run tests to run Performance Metrics') @click.argument('name') @pass_context def cmd_run(ctx, name): diff --git a/qtip/collector/__init__.py b/qtip/collector/__init__.py index e69de29b..cc957ba4 100644 --- a/qtip/collector/__init__.py +++ b/qtip/collector/__init__.py @@ -0,0 +1,25 @@ +############################################################################## +# Copyright (c) 2017 ZTE Corp and others. +# +# All rights reserved. This program and the accompanying materials +# are made available under the terms of the Apache License, Version 2.0 +# which accompanies this distribution, and is available at +# http://www.apache.org/licenses/LICENSE-2.0 +############################################################################## + + +from qtip.base.constant import BaseProp +from qtip.collector.parser.grep import GrepParser + + +class CollectorProp(BaseProp): + TYPE = 'type' + PARSERS = 'parsers' + PATHS = 'paths' + + +def load_parser(type_name): + if type_name == GrepParser.TYPE: + return GrepParser + else: + raise Exception("Invalid parser type: {}".format(type_name)) diff --git a/qtip/collector/logfile.py b/qtip/collector/logfile.py index 19780aaa..2c2e532f 100644 --- a/qtip/collector/logfile.py +++ b/qtip/collector/logfile.py @@ -7,32 +7,49 @@ # http://www.apache.org/licenses/LICENSE-2.0 ############################################################################## -from base import BaseCollector +from itertools import chain +from six.moves import reduce +import os -from qtip.base.constant import CollectorProp as CProp +from qtip.base import BaseActor +from qtip.collector import load_parser +from qtip.collector import CollectorProp as CProp from qtip.loader.file import FileLoader -class LogfileCollector(BaseCollector): - """collect performance metrics from log files""" +class LogItem(BaseActor): + def find(self, filename, paths=None): + return self._parent.find(filename, paths) - def __init__(self, config, paths=None): + +class LogfileCollector(BaseActor): + """run performance metrics from log files""" + TYPE = 'logfile' + LOGS = 'logs' + PATHS = 'paths' + + def __init__(self, config, parent=None): super(LogfileCollector, self).__init__(config) - self.loader = FileLoader('.', paths) - - def collect(self): - captured = {} - for item in self._config[CProp.LOGS]: - captured.update(self._parse_log(item)) - return captured - - def _parse_log(self, log_item): - captured = {} - # TODO(yujunz) select parser by name - if CProp.GREP in log_item: - for rule in log_item[CProp.GREP]: - captured.update(self._grep(log_item[CProp.FILENAME], rule)) - return captured - - def _grep(self, filename, rule): - return {} + self._parent = parent # plan + # TODO(yujunz) handle exception of invalid parent + dirname = os.path.dirname(self._parent.abspath) + paths = [os.path.join(dirname, p) for p in config.get(self.PATHS, [])] + self._loader = FileLoader('.', paths) + + def run(self): + collected = [] + for log_item_config in self._config[self.LOGS]: + log_item = LogItem(log_item_config, self) + matches = [load_parser(c[CProp.TYPE])(c, log_item).run() + for c in log_item.get_config(CProp.PARSERS)] + collected = chain(collected, reduce(chain, matches)) + return reduce(merge_matchobj_to_dict, collected, {'groups': (), 'groupdict': {}}) + + def find(self, filename, paths=None): + return self._loader.find(filename, paths) + + +def merge_matchobj_to_dict(d, m): + d['groups'] = chain(d['groups'], m.groups()) + d['groupdict'].update(m.groupdict()) + return d diff --git a/qtip/collector/parser/__init__.py b/qtip/collector/parser/__init__.py new file mode 100644 index 00000000..e69de29b --- /dev/null +++ b/qtip/collector/parser/__init__.py diff --git a/qtip/collector/parser/grep.py b/qtip/collector/parser/grep.py new file mode 100644 index 00000000..d7ada485 --- /dev/null +++ b/qtip/collector/parser/grep.py @@ -0,0 +1,33 @@ +############################################################################## +# Copyright (c) 2017 ZTE Corp and others. +# +# All rights reserved. This program and the accompanying materials +# are made available under the terms of the Apache License, Version 2.0 +# which accompanies this distribution, and is available at +# http://www.apache.org/licenses/LICENSE-2.0 +############################################################################## + + +import re + + +from qtip.base.constant import BaseProp +from qtip.base import BaseActor + + +class GrepProp(BaseProp): + FILENAME = 'filename' + REGEX = 'regex' + + +class GrepParser(BaseActor): + TYPE = 'grep' + + def run(self): + filename = self._parent.get_config(GrepProp.FILENAME) + return grep_in_file(self._parent.find(filename), self._config[GrepProp.REGEX]) + + +def grep_in_file(filename, regex): + with open(filename, 'r') as f: + return filter(lambda x: x is not None, [re.search(regex, line) for line in f]) diff --git a/qtip/loader/file.py b/qtip/loader/file.py index 00f94818..0ea4d5b6 100644 --- a/qtip/loader/file.py +++ b/qtip/loader/file.py @@ -25,12 +25,12 @@ class FileLoader(BaseLoader): _paths = [ROOT_DIR] def __init__(self, name, paths=None): - self._file = name - self._abspath = self._find(name, paths=paths) + self._filename = name + self.abspath = self.find(name, paths=paths) - def _find(self, name, paths=None): + def find(self, name, paths=None): """find a specification in searching paths""" - paths = self._paths if paths is None else paths + paths = [self.abspath] if paths is None else paths for p in paths: abspath = path.join(p, self.RELATIVE_PATH, name) if path.exists(abspath): @@ -47,4 +47,4 @@ class FileLoader(BaseLoader): item = cls(name, paths=paths) yield { BaseProp.NAME: name, - BaseProp.ABSPATH: item._abspath} + BaseProp.ABSPATH: item.abspath} diff --git a/qtip/loader/plan.py b/qtip/loader/plan.py index 6f1764e2..e15651a3 100644 --- a/qtip/loader/plan.py +++ b/qtip/loader/plan.py @@ -8,12 +8,21 @@ ############################################################################## -from qtip.base.constant import PlanProp +from qtip.base.constant import BaseProp +from qtip.collector import CollectorProp as CProp from qtip.collector.logfile import LogfileCollector from qtip.loader.yaml_file import YamlFileLoader from qtip.loader.qpi import QPISpec +# TODO(yujunz) more elegant way to load module dynamically +def load_collector(type_name): + if type_name == LogfileCollector.TYPE: + return LogfileCollector + else: + raise Exception("Invalid collector type: {}".format(type_name)) + + class Plan(YamlFileLoader): """ a benchmark plan is consist of configuration and a QPI list @@ -24,10 +33,26 @@ class Plan(YamlFileLoader): def __init__(self, name, paths=None): super(Plan, self).__init__(name, paths) + _config = self.content[PlanProp.CONFIG] + + self.collectors = [load_collector(c[CProp.TYPE])(c, self) + for c in _config[PlanProp.COLLECTORS]] + self.qpis = [QPISpec(qpi, paths=paths) for qpi in self.content[PlanProp.QPIS]] - self.info = self.content[PlanProp.INFO] - _config = self.content[PlanProp.CONFIG] - # TODO(yujunz) create collector by name - self.collector = LogfileCollector(_config[PlanProp.COLLECTOR], paths) + +class PlanProp(BaseProp): + # plan + INFO = 'info' + + FACILITY = 'facility' + ENGINEER = 'engineer' + + CONFIG = 'config' + + DRIVER = 'driver' + COLLECTORS = 'collectors' + REPORTER = 'reporter' + + QPIS = 'QPIs' diff --git a/qtip/loader/yaml_file.py b/qtip/loader/yaml_file.py index f1cd4614..8b78a47c 100644 --- a/qtip/loader/yaml_file.py +++ b/qtip/loader/yaml_file.py @@ -7,11 +7,10 @@ # http://www.apache.org/licenses/LICENSE-2.0 ############################################################################## -from collections import defaultdict from os import path import yaml -from qtip.base.error import InvalidFormat +from qtip.base.error import InvalidContent from qtip.base.constant import BaseProp from qtip.loader.file import FileLoader @@ -21,13 +20,11 @@ class YamlFileLoader(FileLoader): def __init__(self, name, paths=None): super(YamlFileLoader, self).__init__(name, paths) - content = defaultdict(lambda: None) + abspath = self.abspath - try: - content.update(yaml.safe_load(file(self._abspath))) - except yaml.YAMLError: - # TODO(yujunz) log yaml error - raise InvalidFormat(self._abspath) - - self.name = content[BaseProp.NAME] or path.splitext(name)[0] - self.content = content + with open(abspath, 'r') as stream: + content = yaml.safe_load(stream) + if not isinstance(content, dict): + raise InvalidContent(abspath) + self.content = content + self.name = content.get(BaseProp.NAME, path.splitext(name)[0]) diff --git a/qtip/runner/__init__.py b/qtip/runner/__init__.py index 1db8498e..79c38850 100644 --- a/qtip/runner/__init__.py +++ b/qtip/runner/__init__.py @@ -28,16 +28,16 @@ class Runner(object): if driver_name == 'random': self.driver = RandomDriver() else: - raise NotFound(driver_name, package=PkgName.DRIVER) + raise NotFound(driver_name, heystack=PkgName.DRIVER) if collector_name == 'stdout': self.collector = StdoutCollector() else: raise NotFound(collector_name, - package=PkgName.COLLECTOR) + heystack=PkgName.COLLECTOR) if reporter_name == 'console': self.reporter = ConsoleReporter() else: raise NotFound(reporter_name, - package=PkgName.REPORTER) + heystack=PkgName.REPORTER) diff --git a/requirements.txt b/requirements.txt index ec566b83..4e4700c0 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,14 +1,8 @@ -pyyaml==3.10 -paramiko==1.16.0 -python-neutronclient==2.6.0 -python-novaclient==2.28.1 -python-glanceclient==1.1.0 -python-cinderclient==1.4.0 -python-heatclient==0.6.0 -python-keystoneclient==1.6.0 -reportlab==3.0 -Flask==0.11.1 -Flask-RESTful==0.3.5 -flask-restful-swagger==0.19 -ansible==2.1.1.0 -numpy==1.11.3 +click +pyyaml +paramiko +Flask +Flask-RESTful +flask-restful-swagger +numpy +pbr diff --git a/test-requirements.txt b/test-requirements.txt index 31581503..a5080127 100644 --- a/test-requirements.txt +++ b/test-requirements.txt @@ -2,6 +2,10 @@ # of appearance. Changing the order has an impact on the overall integration # process, which may cause wedges in the gate later. +tox pytest +pytest-cov +coverage pykwalify mock +pip_check_reqs diff --git a/tests/conftest.py b/tests/conftest.py index 7acb75e6..32042f24 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -12,6 +12,7 @@ from os import path import pytest from qtip.loader.plan import Plan +from qtip.loader.plan import PlanProp @pytest.fixture(scope='session') @@ -26,4 +27,19 @@ def benchmarks_root(data_root): @pytest.fixture(scope='session') def plan(benchmarks_root): - return Plan('fake-plan.yaml', [benchmarks_root]) + return Plan('doctor.yaml', [benchmarks_root]) + + +@pytest.fixture(scope='session') +def plan_config(plan): + return plan.content[PlanProp.CONFIG] + + +@pytest.fixture(scope='session') +def collectors_config(plan_config): + return plan_config[PlanProp.COLLECTORS] + + +@pytest.fixture(scope='session') +def logfile_config(collectors_config): + return collectors_config[0] diff --git a/tests/data/benchmarks/QPI/fake-qpi.yaml b/tests/data/benchmarks/QPI/fake_qpi.yaml index aa1097f4..aa1097f4 100644 --- a/tests/data/benchmarks/QPI/fake-qpi.yaml +++ b/tests/data/benchmarks/QPI/fake_qpi.yaml diff --git a/tests/data/benchmarks/plan/doctor.yaml b/tests/data/benchmarks/plan/doctor.yaml index 6c95077b..f8dcf08d 100644 --- a/tests/data/benchmarks/plan/doctor.yaml +++ b/tests/data/benchmarks/plan/doctor.yaml @@ -4,34 +4,30 @@ info: facility: local engineer: local config: - driver: sample - collector: - - name: logfile + collectors: + - type: logfile + paths: + - '../../external/doctor-verify-apex-sample-master' logs: - filename: doctor_consumer.log - # 2016-12-28 03:16:05,630 consumer.py 26 INFO doctor consumer notified at 1482894965.63 - grep: - - regex: 'doctor consumer notified at \d+(\.\d+)?$' - capture: notified consumer + parsers: + - type: grep + regex: 'doctor consumer notified at (?P<notified>\d+(?:\.\d+)?)$' - filename: doctor_inspector.log - # 2016-12-28 03:16:05,299 inspector.py 76 INFO event posted at 1482894965.3 - # 2016-12-28 03:16:05,299 inspector.py 56 INFO doctor mark vm(<Server: doctor_vm1>) error at 1482894965.3 - # 2016-12-28 03:16:05,506 inspector.py 66 INFO doctor mark host(overcloud-novacompute-1.ool-virtual1) down at 1482894965.51 - grep: - - regex: 'event posted at \d+(\.\d+)?$' - capture: posted event - - regex: 'doctor mark vm\(.*\) error at \d+(\.\d+)?$' - capture: marked VM error - - regex: 'doctor mark host\(.*\) down at \d+(\.\d+)?$' - capture: marked host down + parsers: + - type: grep + regex: 'event posted at (?P<event_posted>\d+(?:\.\d+)?)$' + - type: grep + regex: 'doctor mark vm\(.*\) error at (?P<vm_error>\d+(?:\.\d+)?)$' + - type: grep + regex: 'doctor mark host\(.*\) down at (?P<host_down>\d+(?:\.\d+)?)$' - filename: disable_network.log - # doctor set host down at 1482894965.164096803 - grep: - - regex: 'doctor set host down at \d+(\.\d+)?$' - capture: set host down - reporter: - name: console - # transform collected data into timeline - transformer: timeline + parsers: + - type: grep + regex: 'doctor set host down at (?P<network_down>\d+(?:\.\d+)?)$' + reporters: + - type: console + # transform collected data into timeline + transformer: timeline QPIs: - - fake-qpi.yaml + - fake_qpi.yaml diff --git a/tests/data/benchmarks/plan/fake-plan.yaml b/tests/data/benchmarks/plan/fake-plan.yaml deleted file mode 100644 index 8887f66d..00000000 --- a/tests/data/benchmarks/plan/fake-plan.yaml +++ /dev/null @@ -1,10 +0,0 @@ -name: fake plan -description: fake benchmark plan for demonstration and testing -config: - facility: local - engineer: local - driver: sample - collector: logfile - reporter: console -QPIs: - - fake-qpi.yaml diff --git a/tests/data/fake.log b/tests/data/fake.log new file mode 100644 index 00000000..bab71e5a --- /dev/null +++ b/tests/data/fake.log @@ -0,0 +1,9 @@ +Lorem ipsum dolor sit amet, +consectetur adipiscing elit, +sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. + +Ut enim ad minim veniam, +quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. + +Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. +Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. diff --git a/tests/data/yaml/invalid.yaml b/tests/data/yaml/invalid.yaml new file mode 100644 index 00000000..22e31ed1 --- /dev/null +++ b/tests/data/yaml/invalid.yaml @@ -0,0 +1 @@ +invalid - yaml
\ No newline at end of file diff --git a/tests/data/yaml/with_name.yaml b/tests/data/yaml/with_name.yaml new file mode 100644 index 00000000..25f7f83d --- /dev/null +++ b/tests/data/yaml/with_name.yaml @@ -0,0 +1 @@ +name: name in content
\ No newline at end of file diff --git a/tests/data/yaml/without_name.yaml b/tests/data/yaml/without_name.yaml new file mode 100644 index 00000000..bd234bd4 --- /dev/null +++ b/tests/data/yaml/without_name.yaml @@ -0,0 +1 @@ +no_name: yaml file without name
\ No newline at end of file diff --git a/tests/unit/collector/__init__.py b/tests/unit/collector/__init__.py new file mode 100644 index 00000000..e69de29b --- /dev/null +++ b/tests/unit/collector/__init__.py diff --git a/tests/unit/collector/base_test.py b/tests/unit/collector/base_test.py new file mode 100644 index 00000000..17fe1af1 --- /dev/null +++ b/tests/unit/collector/base_test.py @@ -0,0 +1,18 @@ +############################################################################## +# Copyright (c) 2017 ZTE Corp and others. +# +# All rights reserved. This program and the accompanying materials +# are made available under the terms of the Apache License, Version 2.0 +# which accompanies this distribution, and is available at +# http://www.apache.org/licenses/LICENSE-2.0 +############################################################################## + + +from qtip.loader.plan import load_collector +from qtip.collector import CollectorProp as CProp + + +def test_load_collector(collectors_config): + for c in collectors_config: + collector = load_collector(c[CProp.TYPE]) + assert collector.TYPE == c[CProp.TYPE] diff --git a/tests/unit/collector/grep_test.py b/tests/unit/collector/grep_test.py new file mode 100644 index 00000000..e5d5f8c6 --- /dev/null +++ b/tests/unit/collector/grep_test.py @@ -0,0 +1,31 @@ +############################################################################## +# Copyright (c) 2017 ZTE Corp and others. +# +# All rights reserved. This program and the accompanying materials +# are made available under the terms of the Apache License, Version 2.0 +# which accompanies this distribution, and is available at +# http://www.apache.org/licenses/LICENSE-2.0 +############################################################################## + +import os +import pytest + +from qtip.collector.parser.grep import grep_in_file + + +@pytest.fixture +def logfile(data_root): + return os.path.join(data_root, 'fake.log') + + +@pytest.mark.parametrize("regex,expected", [ + ('not exist', []), + ('Lorem (\S+)', [{'groups': ('ipsum',), 'groupdict': {}}]), + ('nisi ut (?P<name>\S+)', [{'groups': ('aliquip',), 'groupdict': {'name': 'aliquip'}}]) +]) +def test_grep_in_file(logfile, regex, expected): + matches = grep_in_file(logfile, regex) + assert len(matches) == len(expected) + for i in range(len(matches)): + assert matches[i].groups() == expected[i]['groups'] + assert matches[i].groupdict() == expected[i]['groupdict'] diff --git a/tests/unit/collector/logfile_test.py b/tests/unit/collector/logfile_test.py new file mode 100644 index 00000000..a76aa3ee --- /dev/null +++ b/tests/unit/collector/logfile_test.py @@ -0,0 +1,33 @@ +############################################################################## +# Copyright (c) 2017 ZTE Corp and others. +# +# All rights reserved. This program and the accompanying materials +# are made available under the terms of the Apache License, Version 2.0 +# which accompanies this distribution, and is available at +# http://www.apache.org/licenses/LICENSE-2.0 +############################################################################## + +import pytest + +from qtip.collector.logfile import LogfileCollector + + +@pytest.fixture +def logfile_collector(logfile_config, plan): + return LogfileCollector(logfile_config, plan) + + +def test_run(logfile_collector): + collected = logfile_collector.run() + assert collected['groupdict'] == { + 'event_posted': '1482894965.3', + 'host_down': '1482894965.51', + 'network_down': '1482894965.164096803', + 'notified': '1482894965.63', + 'vm_error': '1482894965.3' + } + assert list(collected['groups']) == ['1482894965.63', + '1482894965.3', + '1482894965.3', + '1482894965.51', + '1482894965.164096803'] diff --git a/tests/unit/collector/transformer/__init__.py b/tests/unit/collector/transformer/__init__.py new file mode 100644 index 00000000..e69de29b --- /dev/null +++ b/tests/unit/collector/transformer/__init__.py diff --git a/qtip/collector/base.py b/tests/unit/collector/transformer/base_test.py index 2a25455c..45c1a186 100644 --- a/qtip/collector/base.py +++ b/tests/unit/collector/transformer/base_test.py @@ -7,8 +7,9 @@ # http://www.apache.org/licenses/LICENSE-2.0 ############################################################################## +from qtip.collector.transformer.base import BaseTransformer -class BaseCollector(object): - """performance metrics collector""" - def __init__(self, config): - self._config = config + +def test_base_transformer(): + bt = BaseTransformer() + assert isinstance(bt, BaseTransformer) diff --git a/tests/unit/loader/metric_test.py b/tests/unit/loader/metric_test.py index 91d0dd2c..26f144af 100644 --- a/tests/unit/loader/metric_test.py +++ b/tests/unit/loader/metric_test.py @@ -27,8 +27,8 @@ def init_test(metric_spec): in str(excinfo.value) -def list_all_test(): - metric_list = MetricSpec.list_all() +def list_all_test(benchmarks_root): + metric_list = MetricSpec.list_all(paths=[benchmarks_root]) assert len(list(metric_list)) is 6 for desc in metric_list: assert BaseProp.NAME in desc @@ -37,8 +37,8 @@ def list_all_test(): assert BaseProp.ABSPATH is not None -def content_test(metric): - content = metric.content +def content_test(metric_spec): + content = metric_spec.content assert BaseProp.NAME in content assert BaseProp.DESCRIPTION in content assert BaseProp.WORKLOADS in content diff --git a/tests/unit/loader/plan_test.py b/tests/unit/loader/plan_test.py index 32837f8f..4872b4cd 100644 --- a/tests/unit/loader/plan_test.py +++ b/tests/unit/loader/plan_test.py @@ -9,12 +9,15 @@ import pytest -from qtip.base.constant import PlanProp -from qtip.loader.plan import Plan, QPISpec +from qtip.collector.logfile import LogfileCollector +from qtip.loader.plan import load_collector +from qtip.loader.plan import Plan +from qtip.loader.plan import PlanProp +from qtip.loader.plan import QPISpec def test_init(plan): - assert plan.name == 'fake plan' + assert plan.name == 'doctor performance profiling' assert isinstance(plan.content, dict) for qpi in plan.qpis: assert isinstance(qpi, QPISpec) @@ -27,7 +30,7 @@ def test_init(plan): def test_list_all(benchmarks_root): plan_list = Plan.list_all(paths=[benchmarks_root]) - assert len(list(plan_list)) is 2 + assert len(list(plan_list)) is 1 for desc in plan_list: assert PlanProp.NAME in desc assert PlanProp.CONTENT in desc @@ -41,3 +44,7 @@ def test_content(plan): assert PlanProp.DESCRIPTION in content assert PlanProp.CONFIG in content assert PlanProp.QPIS in content + + +def test_load_collector(): + assert load_collector(LogfileCollector.TYPE) is LogfileCollector diff --git a/tests/unit/loader/yaml_file_test.py b/tests/unit/loader/yaml_file_test.py new file mode 100644 index 00000000..17836946 --- /dev/null +++ b/tests/unit/loader/yaml_file_test.py @@ -0,0 +1,33 @@ +############################################################################## +# Copyright (c) 2017 ZTE Corp and others. +# +# All rights reserved. This program and the accompanying materials +# are made available under the terms of the Apache License, Version 2.0 +# which accompanies this distribution, and is available at +# http://www.apache.org/licenses/LICENSE-2.0 +############################################################################## + +import os +import pytest + +from qtip.base.error import InvalidContent +from qtip.loader.yaml_file import YamlFileLoader + + +@pytest.fixture +def yaml_root(data_root): + return os.path.join(data_root, 'yaml') + + +@pytest.mark.parametrize('filename, expected', [ + ('with_name.yaml', 'name in content'), + ('without_name.yaml', 'without_name')]) +def test_init(yaml_root, filename, expected): + loader = YamlFileLoader(filename, [yaml_root]) + assert loader.name == expected + + +def test_invalid_content(yaml_root): + with pytest.raises(InvalidContent) as excinfo: + YamlFileLoader('invalid.yaml', [yaml_root]) + assert 'invalid.yaml' in excinfo.value.filename @@ -11,11 +11,12 @@ skipsdist = True usedevelop = True install_command = pip install -U {opts} {packages} deps = - -r{toxinidir}/requirements.txt - -r{toxinidir}/test-requirements.txt + -rrequirements.txt + -rtest-requirements.txt commands= py.test \ --basetemp={envtmpdir} \ + --cov qtip --cov-report term-missing --cov-report html \ {posargs} setenv= HOME = {envtmpdir} @@ -33,3 +34,13 @@ show-source = True ignore = E123,E125,H803,E501 builtins = _ exclude = build,dist,doc,legacy,.eggs,.git,.tox,.venv + +[testenv:reqs] +deps=-rtest-requirements.txt +commands= + pip-missing-reqs qtip + pip-extra-reqs qtip + +[pytest] +testpaths = tests +python_functions = *_test test_* |