From 5df8479c5a6e69a85c59a923a435003c060cba4b Mon Sep 17 00:00:00 2001 From: Steven Armstrong Date: Wed, 12 Oct 2011 17:20:47 +0200 Subject: [PATCH] implement Manifest and tests Signed-off-by: Steven Armstrong --- lib/cdist/core/manifest.py | 94 +++++++++++++++++++ lib/cdist/test/manifest/__init__.py | 87 +++++++++++++++++ .../fixtures/conf/manifest/dump_environment | 7 ++ .../test/manifest/fixtures/conf/manifest/init | 4 + .../manifest/fixtures/conf/type/__moon/.keep | 0 .../fixtures/conf/type/__moon/manifest | 8 ++ .../conf/type/__moon/parameter/optional | 1 + .../conf/type/__moon/parameter/required | 1 + .../fixtures/conf/type/__planet/.keep | 0 .../fixtures/conf/type/__planet/manifest | 8 ++ .../conf/type/__planet/parameter/optional | 1 + 11 files changed, 211 insertions(+) create mode 100644 lib/cdist/core/manifest.py create mode 100644 lib/cdist/test/manifest/__init__.py create mode 100755 lib/cdist/test/manifest/fixtures/conf/manifest/dump_environment create mode 100755 lib/cdist/test/manifest/fixtures/conf/manifest/init create mode 100644 lib/cdist/test/manifest/fixtures/conf/type/__moon/.keep create mode 100755 lib/cdist/test/manifest/fixtures/conf/type/__moon/manifest create mode 100644 lib/cdist/test/manifest/fixtures/conf/type/__moon/parameter/optional create mode 100644 lib/cdist/test/manifest/fixtures/conf/type/__moon/parameter/required create mode 100644 lib/cdist/test/manifest/fixtures/conf/type/__planet/.keep create mode 100755 lib/cdist/test/manifest/fixtures/conf/type/__planet/manifest create mode 100644 lib/cdist/test/manifest/fixtures/conf/type/__planet/parameter/optional diff --git a/lib/cdist/core/manifest.py b/lib/cdist/core/manifest.py new file mode 100644 index 00000000..8f9fc7ae --- /dev/null +++ b/lib/cdist/core/manifest.py @@ -0,0 +1,94 @@ +# -*- coding: utf-8 -*- +# +# 2011 Steven Armstrong (steven-cdist at armstrong.cc) +# 2011 Nico Schottelius (nico-cdist at schottelius.org) +# +# This file is part of cdist. +# +# cdist is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# cdist is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with cdist. If not, see . +# +# + +import logging +import os + +import cdist + +log = logging.getLogger(__name__) + + +''' +common: + runs only locally, does not need remote + + env: + PATH: prepend directory with type emulator symlinks == local.bin_path + __target_host: the target host we are working on + __global: full qualified path to the global output dir == local.out_path + __cdist_manifest: full qualified path of the manifest == script + __cdist_type_base_path: full qualified path to the directory where types are defined for use in type emulator + == local.type_base_path + +initial manifest is: + script: full qualified path to the initial manifest + + env: + __manifest: path to .../conf/manifest/ == local.manifest_path + + creates: new objects through type emulator + +type manifeste is: + script: full qualified path to the type manifest + + env: + __object: full qualified path to the object's dir + __object_id: the objects id + __object_fq: full qualified object id, iow: $type.name + / + object_id + __type: full qualified path to the type's dir + + creates: new objects through type emulator +''' + + +class Manifest(object): + """Represents a cdist manifest. + + """ + def __init__(self, target_host, local): + self.target_host = target_host + self.local = local + self.env = { + 'PATH': "%s:%s" % (self.local.bin_path, os.environ['PATH']), + '__target_host': self.target_host, + '__global': self.local.out_path, + '__cdist_type_base_path': self.local.type_base_path, # for use in type emulator + } + + def run_initial_manifest(self, script): + env = os.environ.copy() + env.update(self.env) + env['__manifest'] = self.local.manifest_path + return self.local.run_script(script, env=env) + + def run_type_manifest(self, cdist_object): + env = os.environ.copy() + env.update(self.env) + env.update({ + '__object': cdist_object.absolute_path, + '__object_id': cdist_object.object_id, + '__object_fq': cdist_object.path, + '__type': cdist_object.type.absolute_path, + }) + script = os.path.join(self.local.type_base_path, cdist_object.type.manifest_path) + return self.local.run_script(script, env=env) diff --git a/lib/cdist/test/manifest/__init__.py b/lib/cdist/test/manifest/__init__.py new file mode 100644 index 00000000..8fcb5d79 --- /dev/null +++ b/lib/cdist/test/manifest/__init__.py @@ -0,0 +1,87 @@ +# -*- coding: utf-8 -*- +# +# 2010-2011 Steven Armstrong (steven-cdist at armstrong.cc) +# +# This file is part of cdist. +# +# cdist is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# cdist is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with cdist. If not, see . +# +# + +import unittest +import os +import tempfile +import getpass +import shutil +import string +import random + +#import logging +#logging.basicConfig(level=logging.DEBUG, format='%(levelname)s: %(message)s') + +import cdist +from cdist.exec import local +from cdist import core +from cdist.core import manifest + +import os.path as op +my_dir = op.abspath(op.dirname(__file__)) +fixtures = op.join(my_dir, 'fixtures') +local_base_path = fixtures + + +class ManifestTestCase(unittest.TestCase): + + def mkdtemp(self, **kwargs): + return tempfile.mkdtemp(prefix='tmp.cdist.test.', **kwargs) + + def mkstemp(self, **kwargs): + return tempfile.mkstemp(prefix='tmp.cdist.test.', **kwargs) + + def setUp(self): + self.temp_dir = self.mkdtemp() + target_host = 'localhost' + out_path = self.temp_dir + self.local = local.Local(target_host, local_base_path, out_path) + self.local.create_directories() + self.local.link_emulator(cdist.test.cdist_exec_path) + self.manifest = manifest.Manifest(target_host, self.local) + + def tearDown(self): + #shutil.rmtree(self.temp_dir) + pass + + def test_initial_manifest_environment(self): + #initial_manifest = os.path.join(self.local.manifest_path, "init") + initial_manifest = os.path.join(self.local.manifest_path, "dump_environment") + output_string = self.manifest.run_initial_manifest(initial_manifest) + output_dict = {} + for line in output_string.split('\n'): + if line: + key,value = line.split(': ') + output_dict[key] = value + self.assertTrue(output_dict['PATH'].startswith(self.local.bin_path)) + self.assertEqual(output_dict['__target_host'], self.local.target_host) + self.assertEqual(output_dict['__global'], self.local.out_path) + self.assertEqual(output_dict['__cdist_type_base_path'], self.local.type_base_path) + self.assertEqual(output_dict['__manifest'], self.local.manifest_path) + + +# for line in output.split('\n'): +# print(line) + +# def test_type_manifest(self): +# cdist_type = core.Type(self.local.type_base_path, '__moon') +# cdist_object = core.Object(cdist_type, self.local.object_base_path, 'Saturn') +# self.manifest.run_type_manifest(cdist_object) diff --git a/lib/cdist/test/manifest/fixtures/conf/manifest/dump_environment b/lib/cdist/test/manifest/fixtures/conf/manifest/dump_environment new file mode 100755 index 00000000..1abe7755 --- /dev/null +++ b/lib/cdist/test/manifest/fixtures/conf/manifest/dump_environment @@ -0,0 +1,7 @@ +#!/bin/sh + +echo "PATH: $PATH" +echo "__target_host: $__target_host" +echo "__global: $__global" +echo "__cdist_type_base_path: $__cdist_type_base_path" +echo "__manifest: $__manifest" diff --git a/lib/cdist/test/manifest/fixtures/conf/manifest/init b/lib/cdist/test/manifest/fixtures/conf/manifest/init new file mode 100755 index 00000000..0bdb391a --- /dev/null +++ b/lib/cdist/test/manifest/fixtures/conf/manifest/init @@ -0,0 +1,4 @@ +#!/bin/sh + +__planet Saturn +__moon Prometheus --planet Saturn diff --git a/lib/cdist/test/manifest/fixtures/conf/type/__moon/.keep b/lib/cdist/test/manifest/fixtures/conf/type/__moon/.keep new file mode 100644 index 00000000..e69de29b diff --git a/lib/cdist/test/manifest/fixtures/conf/type/__moon/manifest b/lib/cdist/test/manifest/fixtures/conf/type/__moon/manifest new file mode 100755 index 00000000..362be5a1 --- /dev/null +++ b/lib/cdist/test/manifest/fixtures/conf/type/__moon/manifest @@ -0,0 +1,8 @@ +#!/bin/sh + +if [ -f "$__object/parameter/name" ]; then + name="(cat "$__object/parameter/name")" +else + name="$__object_id" + echo "$name" > "$__object/parameter/name" +fi diff --git a/lib/cdist/test/manifest/fixtures/conf/type/__moon/parameter/optional b/lib/cdist/test/manifest/fixtures/conf/type/__moon/parameter/optional new file mode 100644 index 00000000..f121bdbf --- /dev/null +++ b/lib/cdist/test/manifest/fixtures/conf/type/__moon/parameter/optional @@ -0,0 +1 @@ +name diff --git a/lib/cdist/test/manifest/fixtures/conf/type/__moon/parameter/required b/lib/cdist/test/manifest/fixtures/conf/type/__moon/parameter/required new file mode 100644 index 00000000..729a5167 --- /dev/null +++ b/lib/cdist/test/manifest/fixtures/conf/type/__moon/parameter/required @@ -0,0 +1 @@ +planet diff --git a/lib/cdist/test/manifest/fixtures/conf/type/__planet/.keep b/lib/cdist/test/manifest/fixtures/conf/type/__planet/.keep new file mode 100644 index 00000000..e69de29b diff --git a/lib/cdist/test/manifest/fixtures/conf/type/__planet/manifest b/lib/cdist/test/manifest/fixtures/conf/type/__planet/manifest new file mode 100755 index 00000000..362be5a1 --- /dev/null +++ b/lib/cdist/test/manifest/fixtures/conf/type/__planet/manifest @@ -0,0 +1,8 @@ +#!/bin/sh + +if [ -f "$__object/parameter/name" ]; then + name="(cat "$__object/parameter/name")" +else + name="$__object_id" + echo "$name" > "$__object/parameter/name" +fi diff --git a/lib/cdist/test/manifest/fixtures/conf/type/__planet/parameter/optional b/lib/cdist/test/manifest/fixtures/conf/type/__planet/parameter/optional new file mode 100644 index 00000000..f121bdbf --- /dev/null +++ b/lib/cdist/test/manifest/fixtures/conf/type/__planet/parameter/optional @@ -0,0 +1 @@ +name