diff --git a/.travis.yml b/.travis.yml index bd7e31d..7d23149 100644 --- a/.travis.yml +++ b/.travis.yml @@ -2,8 +2,8 @@ language: python python: - "2.7" # command to install dependencies, e.g. pip install -r requirements.txt --use-mirrors -install: pip install --use-mirrors pystache nose argparse flake8 +install: pip install --use-mirrors -r requirements.txt -r test-requirements.txt # # command to run tests, e.g. python setup.py test -script: ./run_tests.sh +script: python setup.py testr notifications: irc: "irc.freenode.org#tripleo" diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..68c771a --- /dev/null +++ b/LICENSE @@ -0,0 +1,176 @@ + + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + diff --git a/os_config_applier/config_exception.py b/os_config_applier/config_exception.py index 722f035..672b819 100644 --- a/os_config_applier/config_exception.py +++ b/os_config_applier/config_exception.py @@ -1,2 +1,18 @@ +# Copyright (c) 2013 Hewlett-Packard Development Company, L.P. +# +# 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. + + class ConfigException(Exception): pass diff --git a/os_config_applier/os_config_applier.py b/os_config_applier/os_config_applier.py index 8e2b737..005ae37 100755 --- a/os_config_applier/os_config_applier.py +++ b/os_config_applier/os_config_applier.py @@ -1,15 +1,31 @@ -#!/usr/bin/env python +# Copyright (c) 2013 Hewlett-Packard Development Company, L.P. +# +# 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. + +from argparse import ArgumentParser import json import logging import os +import subprocess import sys -from argparse import ArgumentParser -from pystache.context import KeyNotFoundError -from subprocess import Popen, PIPE from tempfile import NamedTemporaryFile -from value_types import ensure_type + +from pystache.context import KeyNotFoundError + from config_exception import ConfigException from renderers import JsonRenderer +from value_types import ensure_type TEMPLATES_DIR = os.environ.get('OS_CONFIG_APPLIER_TEMPLATES', '/opt/stack/os-config-applier/templates') @@ -81,7 +97,10 @@ def render_moustache(text, config): def render_executable(path, config): - p = Popen([path], stdin=PIPE, stdout=PIPE, stderr=PIPE) + p = subprocess.Popen([path], + stdin=subprocess.PIPE, + stdout=subprocess.PIPE, + stderr=subprocess.PIPE) stdout, stderr = p.communicate(json.dumps(config)) p.wait() if p.returncode != 0: @@ -194,6 +213,3 @@ logger.setLevel(logging.INFO) add_handler(logger, logging.StreamHandler(sys.stdout)) if os.geteuid() == 0: add_handler(logger, logging.FileHandler('/var/log/os-config-applier.log')) - -if __name__ == '__main__': - main(sys.argv) diff --git a/os_config_applier/renderers.py b/os_config_applier/renderers.py index 16e980a..80c9492 100644 --- a/os_config_applier/renderers.py +++ b/os_config_applier/renderers.py @@ -1,3 +1,18 @@ +# Copyright (c) 2013 Hewlett-Packard Development Company, L.P. +# +# 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 json from pystache import Renderer diff --git a/tests/__init__.py b/os_config_applier/tests/__init__.py similarity index 100% rename from tests/__init__.py rename to os_config_applier/tests/__init__.py diff --git a/tests/templates/etc/glance/script.conf b/os_config_applier/tests/templates/etc/glance/script.conf similarity index 100% rename from tests/templates/etc/glance/script.conf rename to os_config_applier/tests/templates/etc/glance/script.conf diff --git a/tests/templates/etc/keystone/keystone.conf b/os_config_applier/tests/templates/etc/keystone/keystone.conf similarity index 100% rename from tests/templates/etc/keystone/keystone.conf rename to os_config_applier/tests/templates/etc/keystone/keystone.conf diff --git a/os_config_applier/tests/test_json_renderer.py b/os_config_applier/tests/test_json_renderer.py new file mode 100644 index 0000000..95d0079 --- /dev/null +++ b/os_config_applier/tests/test_json_renderer.py @@ -0,0 +1,38 @@ +# Copyright (c) 2013 Hewlett-Packard Development Company, L.P. +# +# 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 json + +import testtools +from testtools import content + +from os_config_applier.renderers import JsonRenderer + +TEST_JSON = '{"a":{"b":[1,2,3,"foo"],"c": "the quick brown fox"}}' + + +class JsonRendererTestCase(testtools.TestCase): + + def test_json_renderer(self): + context = json.loads(TEST_JSON) + x = JsonRenderer() + result = x.render('{{a.b}}', context) + self.addDetail('result', content.text_content(result)) + result_structure = json.loads(result) + desire_structure = json.loads('[1,2,3,"foo"]') + self.assertEqual(desire_structure, result_structure) + result = x.render('{{a.c}}', context) + self.addDetail('result', content.text_content(result)) + self.assertEqual(u'the quick brown fox', result) diff --git a/os_config_applier/tests/test_os_config_applier.py b/os_config_applier/tests/test_os_config_applier.py new file mode 100644 index 0000000..6242218 --- /dev/null +++ b/os_config_applier/tests/test_os_config_applier.py @@ -0,0 +1,196 @@ +# Copyright (c) 2013 Hewlett-Packard Development Company, L.P. +# +# 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 json +import os +import subprocess +import tempfile + +import fixtures +from pystache.context import KeyNotFoundError +import testtools + +from os_config_applier.config_exception import ConfigException +from os_config_applier import os_config_applier as oca + +# example template tree +TEMPLATES = os.path.join(os.path.dirname(__file__), 'templates') +TEMPLATE_PATHS = [ + "/etc/glance/script.conf", + "/etc/keystone/keystone.conf" +] + +# config for example tree +CONFIG = { + "x": "foo", + "database": { + "url": "sqlite:///blah" + } +} + +# config for example tree - with subhash +CONFIG_SUBHASH = { + "OpenStack::Config": { + "x": "foo", + "database": { + "url": "sqlite:///blah" + } + } +} + +# expected output for example tree +OUTPUT = { + "/etc/glance/script.conf": "foo\n", + "/etc/keystone/keystone.conf": "[foo]\ndatabase = sqlite:///blah\n" +} + + +def main_path(): + return ( + os.path.dirname(os.path.realpath(__file__)) + + '/../os_config_applier.py') + + +def template(relpath): + return os.path.join(TEMPLATES, relpath[1:]) + + +class TestRunOSConfigApplier(testtools.TestCase): + + def setUp(self): + super(TestRunOSConfigApplier, self).setUp() + self.useFixture(fixtures.NestedTempfile()) + self.stdout = self.useFixture(fixtures.StringStream('stdout')).stream + self.useFixture(fixtures.MonkeyPatch('sys.stdout', self.stdout)) + stderr = self.useFixture(fixtures.StringStream('stderr')).stream + self.useFixture(fixtures.MonkeyPatch('sys.stderr', stderr)) + fd, self.path = tempfile.mkstemp() + with os.fdopen(fd, 'w') as t: + t.write(json.dumps(CONFIG)) + t.flush() + + def test_print_key(self): + oca.main(['os-config-applier.py', '--metadata', self.path, '--key', + 'database.url', '--type', 'raw']) + self.stdout.seek(0) + self.assertEqual(CONFIG['database']['url'], + self.stdout.read().strip()) + + def test_print_key_missing(self): + self.assertRaises( + subprocess.CalledProcessError, + ['os-config-applier.py', '--metadata', self.path, '--key', + 'does.not.exist']) + + def test_print_key_wrong_type(self): + self.assertRaises( + subprocess.CalledProcessError, + ['os-config-applier.py', '--metadata', self.path, '--key', + 'x', '--type', 'int']) + + def test_print_templates(self): + oca.main(['os-config-applier', '--print-templates']) + self.stdout.seek(0) + self.assertEqual(self.stdout.read().strip(), oca.TEMPLATES_DIR) + + +class OSConfigApplierTestCase(testtools.TestCase): + + def setUp(self): + super(OSConfigApplierTestCase, self).setUp() + self.useFixture(fixtures.NestedTempfile()) + + def test_install_config(self): + fd, path = tempfile.mkstemp() + with os.fdopen(fd, 'w') as t: + t.write(json.dumps(CONFIG)) + t.flush() + tmpdir = tempfile.mkdtemp() + oca.install_config(path, TEMPLATES, tmpdir, False) + for path, contents in OUTPUT.items(): + full_path = os.path.join(tmpdir, path[1:]) + assert os.path.exists(full_path) + self.assertEqual(open(full_path).read(), contents) + + def test_install_config_subhash(self): + fd, tpath = tempfile.mkstemp() + with os.fdopen(fd, 'w') as t: + t.write(json.dumps(CONFIG_SUBHASH)) + t.flush() + tmpdir = tempfile.mkdtemp() + oca.install_config( + tpath, TEMPLATES, tmpdir, False, 'OpenStack::Config') + for path, contents in OUTPUT.items(): + full_path = os.path.join(tmpdir, path[1:]) + assert os.path.exists(full_path) + self.assertEqual(open(full_path).read(), contents) + + def test_build_tree(self): + self.assertEqual(oca.build_tree( + oca.template_paths(TEMPLATES), CONFIG), OUTPUT) + + def test_render_template(self): + # execute executable files, moustache non-executables + self.assertEqual(oca.render_template(template( + "/etc/glance/script.conf"), {"x": "abc"}), "abc\n") + self.assertRaises(ConfigException, oca.render_template, template( + "/etc/glance/script.conf"), {}) + + def test_render_moustache(self): + self.assertEqual(oca.render_moustache("ab{{x.a}}cd", { + "x": {"a": "123"}}), "ab123cd") + + def test_render_moustache_bad_key(self): + self.assertRaises(KeyNotFoundError, + oca.render_moustache, "{{badkey}}", {}) + + def test_render_executable(self): + params = {"x": "foo"} + self.assertEqual(oca.render_executable(template( + "/etc/glance/script.conf"), params), "foo\n") + + def test_render_executable_failure(self): + self.assertRaises( + ConfigException, + oca.render_executable, template("/etc/glance/script.conf"), {}) + + def test_template_paths(self): + expected = map(lambda p: (template(p), p), TEMPLATE_PATHS) + actual = oca.template_paths(TEMPLATES) + expected.sort(key=lambda tup: tup[1]) + actual.sort(key=lambda tup: tup[1]) + self.assertEqual(actual, expected) + + def test_read_config(self): + with tempfile.NamedTemporaryFile() as t: + d = {"a": {"b": ["c", "d"]}} + t.write(json.dumps(d)) + t.flush() + self.assertEqual(oca.read_config(t.name), d) + + def test_read_config_bad_json(self): + with tempfile.NamedTemporaryFile() as t: + t.write("{{{{") + t.flush() + self.assertRaises(ConfigException, oca.read_config, t.name) + + def test_read_config_no_file(self): + self.assertRaises(ConfigException, oca.read_config, "/nosuchfile") + + def test_strip_hash(self): + h = {'a': {'b': {'x': 'y'}}, "c": [1, 2, 3]} + self.assertEqual(oca.strip_hash(h, 'a.b'), {'x': 'y'}) + self.assertRaises(ConfigException, oca.strip_hash, h, 'a.nonexistent') + self.assertRaises(ConfigException, oca.strip_hash, h, 'a.c') diff --git a/os_config_applier/tests/test_value_type.py b/os_config_applier/tests/test_value_type.py new file mode 100644 index 0000000..8203033 --- /dev/null +++ b/os_config_applier/tests/test_value_type.py @@ -0,0 +1,34 @@ +# Copyright (c) 2013 Hewlett-Packard Development Company, L.P. +# +# 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 testtools + +from os_config_applier.config_exception import ConfigException +from os_config_applier.value_types import ensure_type + + +class ValueTypeTestCase(testtools.TestCase): + + def test_unknown_type(self): + self.assertRaises(ValueError, ensure_type, "foo", "badtype") + + def test_int(self): + self.assertEqual("123", ensure_type("123", "int")) + + def test_default(self): + self.assertEqual("foobar", ensure_type("foobar", "default")) + + def test_default_bad(self): + self.assertRaises(ConfigException, ensure_type, "foo\nbar", "default") diff --git a/os_config_applier/value_types.py b/os_config_applier/value_types.py index 7d41fde..6cb7cc5 100644 --- a/os_config_applier/value_types.py +++ b/os_config_applier/value_types.py @@ -1,4 +1,20 @@ +# Copyright (c) 2013 Hewlett-Packard Development Company, L.P. +# +# 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 re + from config_exception import ConfigException TYPES = { diff --git a/requirements.txt b/requirements.txt index 4283556..7bd15e8 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,2 +1,4 @@ anyjson +d2to1 +pbr pystache diff --git a/run_tests.sh b/run_tests.sh deleted file mode 100755 index 053148f..0000000 --- a/run_tests.sh +++ /dev/null @@ -1 +0,0 @@ -tox diff --git a/tests/json_renderer_tests.py b/tests/json_renderer_tests.py deleted file mode 100644 index e096a5d..0000000 --- a/tests/json_renderer_tests.py +++ /dev/null @@ -1,19 +0,0 @@ -import json - -from nose.tools import assert_equals -from os_config_applier.renderers import JsonRenderer - -TEST_JSON = '{"a":{"b":[1,2,3,"foo"],"c": "the quick brown fox"}}' - - -def test_json_renderer(): - context = json.loads(TEST_JSON) - x = JsonRenderer() - result = x.render('{{a.b}}', context) - print result - result_structure = json.loads(result) - desire_structure = json.loads('[1,2,3,"foo"]') - assert_equals(desire_structure, result_structure) - result = x.render('{{a.c}}', context) - print result - assert_equals(u'the quick brown fox', result) diff --git a/tests/os_config_applier_tests.py b/tests/os_config_applier_tests.py deleted file mode 100644 index 8efc404..0000000 --- a/tests/os_config_applier_tests.py +++ /dev/null @@ -1,191 +0,0 @@ -import json -import os -import sys -import subprocess -import tempfile -from StringIO import StringIO -from nose.tools import assert_equal, assert_equals, assert_raises, raises -from os_config_applier.config_exception import ConfigException -from os_config_applier.os_config_applier import ( - main, TEMPLATES_DIR, strip_hash, read_config, template_paths, - render_executable, render_template, render_moustache, install_config, - build_tree) - -# example template tree -TEMPLATES = os.path.join(os.path.dirname(__file__), 'templates') -TEMPLATE_PATHS = [ - "/etc/glance/script.conf", - "/etc/keystone/keystone.conf" -] - -# config for example tree -CONFIG = { - "x": "foo", - "database": { - "url": "sqlite:///blah" - } -} - -# config for example tree - with subhash -CONFIG_SUBHASH = { - "OpenStack::Config": { - "x": "foo", - "database": { - "url": "sqlite:///blah" - } - } -} - -# expected output for example tree -OUTPUT = { - "/etc/glance/script.conf": "foo\n", - "/etc/keystone/keystone.conf": "[foo]\ndatabase = sqlite:///blah\n" -} - - -def setup(): - pass - - -def teardown(): - pass - - -def main_path(): - return ( - os.path.dirname(os.path.realpath(__file__)) + - '/../os_config_applier/os_config_applier.py') - - -def template(relpath): - return os.path.join(TEMPLATES, relpath[1:]) - - -def test_install_config(): - t = tempfile.NamedTemporaryFile() - t.write(json.dumps(CONFIG)) - t.flush() - tmpdir = tempfile.mkdtemp() - install_config(t.name, TEMPLATES, tmpdir, False) - for path, contents in OUTPUT.items(): - full_path = os.path.join(tmpdir, path[1:]) - assert os.path.exists(full_path) - assert_equal(open(full_path).read(), contents) - - -def test_install_config_subhash(): - t = tempfile.NamedTemporaryFile() - t.write(json.dumps(CONFIG_SUBHASH)) - t.flush() - tmpdir = tempfile.mkdtemp() - install_config(t.name, TEMPLATES, tmpdir, False, 'OpenStack::Config') - for path, contents in OUTPUT.items(): - full_path = os.path.join(tmpdir, path[1:]) - assert os.path.exists(full_path) - assert_equal(open(full_path).read(), contents) - - -def test_print_key(): - t = tempfile.NamedTemporaryFile() - t.write(json.dumps(CONFIG)) - t.flush() - out = subprocess.check_output([main_path(), '--metadata', t.name, '--key', - 'database.url', '--type', 'raw'], - stderr=subprocess.STDOUT) - assert_equals(CONFIG['database']['url'], out.rstrip()) - - -@raises(subprocess.CalledProcessError) -def test_print_key_missing(): - t = tempfile.NamedTemporaryFile() - t.write(json.dumps(CONFIG)) - t.flush() - subprocess.check_output([main_path(), '--metadata', t.name, '--key', - 'does.not.exist'], stderr=subprocess.STDOUT) - - -@raises(subprocess.CalledProcessError) -def test_print_key_wrong_type(): - t = tempfile.NamedTemporaryFile() - t.write(json.dumps(CONFIG)) - t.flush() - subprocess.check_output([main_path(), '--metadata', t.name, '--key', - 'x', '--type', 'int'], stderr=subprocess.STDOUT) - - -def test_build_tree(): - assert_equals(build_tree(template_paths(TEMPLATES), CONFIG), OUTPUT) - - -def test_render_template(): - # execute executable files, moustache non-executables - assert render_template(template( - "/etc/glance/script.conf"), {"x": "abc"}) == "abc\n" - assert_raises(ConfigException, render_template, template( - "/etc/glance/script.conf"), {}) - - -def test_render_moustache(): - assert_equals(render_moustache("ab{{x.a}}cd", { - "x": {"a": "123"}}), "ab123cd") - - -@raises(Exception) -def test_render_moustache_bad_key(): - render_moustache("{{badkey}}", {}) - - -def test_render_executable(): - params = {"x": "foo"} - assert render_executable(template( - "/etc/glance/script.conf"), params) == "foo\n" - - -@raises(ConfigException) -def test_render_executable_failure(): - render_executable(template("/etc/glance/script.conf"), {}) - - -def test_template_paths(): - expected = map(lambda p: (template(p), p), TEMPLATE_PATHS) - actual = template_paths(TEMPLATES) - expected.sort(key=lambda tup: tup[1]) - actual.sort(key=lambda tup: tup[1]) - assert_equals(actual, expected) - - -def test_read_config(): - with tempfile.NamedTemporaryFile() as t: - d = {"a": {"b": ["c", "d"]}} - t.write(json.dumps(d)) - t.flush() - assert_equals(read_config(t.name), d) - - -@raises(ConfigException) -def test_read_config_bad_json(): - with tempfile.NamedTemporaryFile() as t: - t.write("{{{{") - t.flush() - read_config(t.name) - - -@raises(Exception) -def test_read_config_no_file(): - read_config("/nosuchfile") - - -def test_strip_hash(): - h = {'a': {'b': {'x': 'y'}}, "c": [1, 2, 3]} - assert_equals(strip_hash(h, 'a.b'), {'x': 'y'}) - assert_raises(ConfigException, strip_hash, h, 'a.nonexistent') - assert_raises(ConfigException, strip_hash, h, 'a.c') - - -def test_print_templates(): - save_stdout = sys.stdout - output = StringIO() - sys.stdout = output - main(['os-config-applier', '--print-templates']) - sys.stdout = save_stdout - assert_equals(output.getvalue().strip(), TEMPLATES_DIR) diff --git a/tests/value_type_tests.py b/tests/value_type_tests.py deleted file mode 100644 index 61772a8..0000000 --- a/tests/value_type_tests.py +++ /dev/null @@ -1,21 +0,0 @@ -from nose.tools import assert_equals, raises -from os_config_applier.config_exception import ConfigException -from os_config_applier.value_types import ensure_type - - -@raises(ValueError) -def test_unknown_type(): - ensure_type("foo", "badtype") - - -def test_int(): - assert_equals("123", ensure_type("123", "int")) - - -def test_defualt(): - assert_equals("foobar", ensure_type("foobar", "default")) - - -@raises(ConfigException) -def test_default_bad(): - ensure_type("foo\nbar", "default")