Added functional test base
Functional cases can be described in yaml files in test/functional/cases folder Each case will be converted into test. Examples of cases included Change-Id: I60fc763c52ed4c25dfa7164c36c915497a430cfb
This commit is contained in:
parent
a71fe26c19
commit
fc20354fe0
|
@ -30,6 +30,8 @@ from muranopkgcheck.validators import VALIDATORS
|
|||
|
||||
LOG = log.getLogger(__name__)
|
||||
|
||||
error.register.E000(description='Check failed')
|
||||
|
||||
|
||||
@six.add_metaclass(abc.ABCMeta)
|
||||
class Formatter(object):
|
||||
|
@ -56,8 +58,11 @@ class PlainTextFormatter(Formatter):
|
|||
|
||||
class Manager(object):
|
||||
|
||||
def __init__(self, pkg_path, quiet_load=False):
|
||||
self.pkg = pkg_loader.load_package(pkg_path, quiet=quiet_load)
|
||||
def __init__(self, pkg_path, quiet_load=False, loader=None):
|
||||
if loader:
|
||||
self.pkg = loader(pkg_path)
|
||||
else:
|
||||
self.pkg = pkg_loader.load_package(pkg_path, quiet=quiet_load)
|
||||
self.validators = list(VALIDATORS)
|
||||
self.plugins = None
|
||||
|
||||
|
|
|
@ -0,0 +1,51 @@
|
|||
- case_no_errors
|
||||
-
|
||||
pkg:
|
||||
manifest.yaml:
|
||||
format: yaml
|
||||
content:
|
||||
Type: Library
|
||||
FullName: org.Test
|
||||
Description: Very important package
|
||||
Name: Test
|
||||
Classes:
|
||||
org.Test: Test.yaml
|
||||
LICENSE:
|
||||
format: raw
|
||||
content: LICENSE
|
||||
logo.png:
|
||||
format: raw
|
||||
content: logo
|
||||
Classes/Test.yaml:
|
||||
format: yaml
|
||||
content:
|
||||
Namespaces:
|
||||
=: org.Test
|
||||
Name: Test
|
||||
expected: []
|
||||
---
|
||||
- case_no_files
|
||||
-
|
||||
pkg:
|
||||
manifest.yaml:
|
||||
format: yaml
|
||||
content:
|
||||
Type: Application
|
||||
FullName: org.Test
|
||||
Description: Very important package
|
||||
Name: Test
|
||||
Classes:
|
||||
org.Test: Test.yaml
|
||||
expected:
|
||||
-
|
||||
code: E050
|
||||
msg: File "Test.yaml" is present in Manifest, but not in filesystem
|
||||
-
|
||||
code: W073
|
||||
msg: There is no UI file "ui.yaml"
|
||||
-
|
||||
code: W074
|
||||
msg: There is no Logo file "logo.png"
|
||||
-
|
||||
code: W121
|
||||
msg: Missing "LICENSE" in the package
|
|
@ -0,0 +1,95 @@
|
|||
# Copyright (c) 2016 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 io
|
||||
import os
|
||||
|
||||
import oslotest.base
|
||||
import six
|
||||
import testscenarios
|
||||
import yaml
|
||||
|
||||
from muranopkgcheck import consts
|
||||
from muranopkgcheck import manager
|
||||
from muranopkgcheck import pkg_loader
|
||||
|
||||
|
||||
class DictLoader(pkg_loader.BaseLoader):
|
||||
|
||||
@classmethod
|
||||
def _try_load(cls, pkg):
|
||||
if consts.MANIFEST_PATH in pkg:
|
||||
return cls(pkg)
|
||||
return None
|
||||
|
||||
def __init__(self, pkg):
|
||||
super(DictLoader, self).__init__('')
|
||||
self.pkg = pkg
|
||||
|
||||
def open_file(self, path, mode='r'):
|
||||
if self.pkg[path]['format'] == 'raw':
|
||||
sio = io.StringIO(six.text_type(self.pkg[path]['content']))
|
||||
setattr(sio, 'name', path)
|
||||
elif self.pkg[path]['format'] == 'yaml':
|
||||
content = yaml.safe_dump(self.pkg[path]['content'])
|
||||
sio = io.StringIO(six.text_type(content))
|
||||
setattr(sio, 'name', path)
|
||||
else:
|
||||
raise ValueError('Unknown type of content')
|
||||
return sio
|
||||
|
||||
def list_files(self, subdir=None):
|
||||
files = self.pkg.keys()
|
||||
if subdir is None:
|
||||
return files
|
||||
subdir_len = len(subdir)
|
||||
return [file_[subdir_len:].lstrip('/') for file_ in files
|
||||
if file_.startswith(subdir)]
|
||||
|
||||
def exists(self, name):
|
||||
return name in self.pkg
|
||||
|
||||
|
||||
class DictFormatter(manager.Formatter):
|
||||
|
||||
def format(self, error):
|
||||
return sorted([{'code': e.code, 'msg': e.message} for e in error],
|
||||
key=lambda item: item['code'])
|
||||
|
||||
|
||||
def load_cases():
|
||||
cases_path = os.path.join(os.path.dirname(os.path.abspath(__file__)),
|
||||
'cases')
|
||||
cases_files = [os.path.join(cases_path, f)for f in os.listdir(cases_path)
|
||||
if os.path.isfile(os.path.join(cases_path, f))]
|
||||
cases = []
|
||||
for cases_file in cases_files:
|
||||
with open(cases_file) as f:
|
||||
cases.extend(list(yaml.load_all(f)))
|
||||
return cases
|
||||
|
||||
cases = load_cases()
|
||||
|
||||
|
||||
class TestCase(testscenarios.WithScenarios, oslotest.base.BaseTestCase):
|
||||
|
||||
"""Test case base class for all unit tests."""
|
||||
|
||||
scenarios = cases
|
||||
|
||||
def test_foo(self):
|
||||
m = manager.Manager(self.pkg, loader=DictLoader)
|
||||
errors = m.validate()
|
||||
fmt = DictFormatter()
|
||||
self.assertEqual(self.expected, fmt.format(errors))
|
|
@ -26,9 +26,10 @@ class ManifestValidatorTests(helpers.BaseValidatorTestClass):
|
|||
self._oe_patcher = mock.patch('os.path.exists')
|
||||
self.exists = self._oe_patcher.start()
|
||||
self.exists.return_value = [True, True]
|
||||
self.loaded_package = mock.Mock()
|
||||
self.loaded_package.read.return_value.yaml.return_value = [
|
||||
{'Type': 'Application'}]
|
||||
self.loaded_package = mock.MagicMock()
|
||||
# self.loaded_package = mock.Mock()
|
||||
# self.loaded_package.read.return_value.yaml.return_value = [
|
||||
# {'Type': 'Application'}]
|
||||
self.mv = manifest.ManifestValidator(self.loaded_package)
|
||||
|
||||
def test_format_as_number(self):
|
||||
|
|
Loading…
Reference in New Issue