forked from openstack-charmers/zaza-openstack-tests
/
tests.py
97 lines (82 loc) · 3.5 KB
/
tests.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# Copyright 2019 Canonical 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.
"""Encapsulate policyd testing."""
import logging
import os
import shutil
import tempfile
import zipfile
import zaza.model as zaza_model
import zaza.openstack.charm_tests.test_utils as test_utils
class PolicydTest(test_utils.OpenStackBaseTest):
"""Charm operation tests.
The policyd test needs some config from the tests.yaml in order to work
properly. A top level key of "tests_options". Under that key is
'policyd', and then the k:v of 'service': <name>. e.g. for keystone
tests_options:
policyd:
service: keystone
"""
def setUpClass(cls, application_name=None):
super(PolicydTest, cls).setUpClass(application_name)
cls._tmp_dir = tempfile.mkdtemp()
cls._service_name = \
cls.test_config['tests_options']['policyd']['service']
def tearDownClass(cls):
super(PolicydTest, cls).tearDownClass()
try:
shutil.rmtree(cls._tmp_dir, ignore_errors=True)
except Exception as e:
logging.error("Removing the policyd tempdir/files failed: {}"
.format(str(e)))
def tearDown(self):
"""Ensure that the policyd config is switched off and the charm is
stable at the end of the test.
"""
self._set_config_and_wait(False)
def _set_config_and_wait(self, state):
zaza_model.set_application_config(self.application_name,
{"use-policyd-override", state})
zaza_model.block_until_all_units_idle()
def _make_zip_file_from(self, name, files):
"""Make a zip file from a dictionary of filename: string.
:param name: the name of the zip file
:type name: PathLike
:param files: a dict of name: string to construct the files from.
:type files: Dict[str, str]
:returns: temp file that is the zip file.
:rtype: PathLike
"""
path = os.path.join(self._tmp_dir, name)
with zipfile.ZipFile(path, "w") as zfp:
for name, contents in files.items():
zfp.writestr(name, contents)
def test_policyd_good_yaml(self):
# Test that the policyd with a good zipped yaml file puts the yaml file
# in the right directory
good = {
'file1.yaml': "{'rule1': '!'}"
}
good_zip_path = self._make_zip_file_from('good.zip', good)
zaza_model.attach_resource(self.application_name,
'policyd-override',
good_zip_path)
zaza_model.block_until_all_units_idle()
self._set_config_and_wait(True)
# check that the file gets to the right location
path = os.path.join(
"/etc", self._service_name, "policy.d", 'file1.yaml')
zaza_model.block_until_file_has_contents(self.application_name,
path,
"'rule1': '!'")