Add molecule test utilities

Provide some functions that help with py.test style testing. This
includes checking for files & directories, and for INI files, validating
their contents.
This commit is contained in:
Mark Goddard 2018-02-20 11:01:36 +00:00
parent 318f73cc52
commit c310717065
2 changed files with 78 additions and 0 deletions

View File

View File

@ -0,0 +1,78 @@
# Copyright (c) 2018 StackHPC Ltd.
#
# 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 six
from six.moves import configparser
from six import StringIO
def test_file(host, path, owner='root', group='root'):
"""Test an expected file.
Validate that the file exists and has the correct ownership.
"""
f = host.file(path)
assert f.exists
assert f.is_file
assert owner == f.user
assert group == f.group
def test_ini_file(host, path, owner='root', group='root', expected=None):
"""Test an expected INI file.
Validate that the file exists, has the correct ownership, format and
expected contents.
:param expected: a dict of dicts providing config that should be present.
"""
test_file(host, path, owner, group)
sio = StringIO(host.file(path).content_string)
parser = configparser.RawConfigParser()
if six.PY3:
parser.read_file(sio)
else:
parser.readfp(sio)
if expected is None:
return
for exp_section_name, exp_section in expected.items():
assert parser.has_section(exp_section_name)
for exp_key, exp_value in exp_section.items():
assert parser.has_option(exp_section_name, exp_key)
assert parser.get(exp_section_name, exp_key) == exp_value
def test_directory(host, path, owner='root', group='root'):
"""Test an expected directory.
Validate that the directory exists and has the correct ownership.
"""
d = host.file(path)
assert d.exists
assert d.is_directory
assert owner == d.user
assert group == d.group
def test_path_absent(host, path):
"""Test a path expected to not exist."""
p = host.file(path)
assert not p.exists