From a946b5cf59c4e8d139f014f9166e44a0bfcb5d83 Mon Sep 17 00:00:00 2001 From: Steven Armstrong Date: Tue, 18 Oct 2011 14:23:06 +0200 Subject: [PATCH 1/9] verify manifest environment Signed-off-by: Steven Armstrong --- lib/cdist/test/manifest/__init__.py | 41 +++++++++++++++++++ .../fixtures/conf/manifest/dump_environment | 12 +++--- .../conf/type/__dump_environment/manifest | 19 +++++---- 3 files changed, 59 insertions(+), 13 deletions(-) diff --git a/lib/cdist/test/manifest/__init__.py b/lib/cdist/test/manifest/__init__.py index efda5b0a..f091e494 100644 --- a/lib/cdist/test/manifest/__init__.py +++ b/lib/cdist/test/manifest/__init__.py @@ -27,6 +27,8 @@ import shutil import string import random import logging +import io +import sys import cdist from cdist.exec import local @@ -48,6 +50,8 @@ class ManifestTestCase(unittest.TestCase): return tempfile.mkstemp(prefix='tmp.cdist.test.', **kwargs) def setUp(self): + self.orig_environ = os.environ + os.environ = os.environ.copy() self.temp_dir = self.mkdtemp() self.target_host = 'localhost' out_path = self.temp_dir @@ -58,17 +62,54 @@ class ManifestTestCase(unittest.TestCase): self.log = logging.getLogger(self.target_host) def tearDown(self): + os.environ = self.orig_environ shutil.rmtree(self.temp_dir) def test_initial_manifest_environment(self): initial_manifest = os.path.join(self.local.manifest_path, "dump_environment") + handle, output_file = self.mkstemp(dir=self.temp_dir) + os.environ['__cdist_test_out'] = output_file self.manifest.run_initial_manifest(initial_manifest) + with open(output_file, 'r') as fd: + output_string = fd.read() + print("output_string: %s" % output_string) + 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_path) + self.assertEqual(output_dict['__manifest'], self.local.manifest_path) + def test_type_manifest_environment(self): cdist_type = core.Type(self.local.type_path, '__dump_environment') cdist_object = core.Object(cdist_type, self.local.object_path, 'whatever') + handle, output_file = self.mkstemp(dir=self.temp_dir) + os.environ['__cdist_test_out'] = output_file self.manifest.run_type_manifest(cdist_object) + with open(output_file, 'r') as fd: + output_string = fd.read() + print("output_string: %s" % output_string) + 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_path) + self.assertEqual(output_dict['__type'], cdist_type.absolute_path) + self.assertEqual(output_dict['__object'], cdist_object.absolute_path) + self.assertEqual(output_dict['__self'], cdist_object.path) + self.assertEqual(output_dict['__object_id'], cdist_object.object_id) + self.assertEqual(output_dict['__object_fq'], cdist_object.path) + def test_debug_env_setup(self): self.log.setLevel(logging.DEBUG) manifest = cdist.core.manifest.Manifest(self.target_host, self.local) diff --git a/lib/cdist/test/manifest/fixtures/conf/manifest/dump_environment b/lib/cdist/test/manifest/fixtures/conf/manifest/dump_environment index 1abe7755..7ce983ab 100755 --- a/lib/cdist/test/manifest/fixtures/conf/manifest/dump_environment +++ b/lib/cdist/test/manifest/fixtures/conf/manifest/dump_environment @@ -1,7 +1,9 @@ #!/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" +cat > $__cdist_test_out << DONE +PATH: $PATH +__target_host: $__target_host +__global: $__global +__cdist_type_base_path: $__cdist_type_base_path +__manifest: $__manifest +DONE diff --git a/lib/cdist/test/manifest/fixtures/conf/type/__dump_environment/manifest b/lib/cdist/test/manifest/fixtures/conf/type/__dump_environment/manifest index 92f533a8..212de64d 100755 --- a/lib/cdist/test/manifest/fixtures/conf/type/__dump_environment/manifest +++ b/lib/cdist/test/manifest/fixtures/conf/type/__dump_environment/manifest @@ -1,10 +1,13 @@ #!/bin/sh -echo "PATH: $PATH" -echo "__target_host: $__target_host" -echo "__global: $__global" -echo "__cdist_type_base_path: $__cdist_type_base_path" -echo "__type: $__type" -echo "__object: $__object" -echo "__object_id: $__object_id" -echo "__object_fq: $__object_fq" +cat > $__cdist_test_out << DONE +PATH: $PATH +__target_host: $__target_host +__global: $__global +__cdist_type_base_path: $__cdist_type_base_path +__type: $__type +__self: $__self +__object: $__object +__object_id: $__object_id +__object_fq: $__object_fq +DONE From 1805c16fd4cd94372f6ec33466e5185a5ac56f83 Mon Sep 17 00:00:00 2001 From: Steven Armstrong Date: Tue, 18 Oct 2011 14:23:46 +0200 Subject: [PATCH 2/9] --debug Signed-off-by: Steven Armstrong --- lib/cdist/test/manifest/__init__.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/lib/cdist/test/manifest/__init__.py b/lib/cdist/test/manifest/__init__.py index f091e494..45902477 100644 --- a/lib/cdist/test/manifest/__init__.py +++ b/lib/cdist/test/manifest/__init__.py @@ -73,7 +73,6 @@ class ManifestTestCase(unittest.TestCase): with open(output_file, 'r') as fd: output_string = fd.read() - print("output_string: %s" % output_string) output_dict = {} for line in output_string.split('\n'): if line: @@ -94,7 +93,6 @@ class ManifestTestCase(unittest.TestCase): with open(output_file, 'r') as fd: output_string = fd.read() - print("output_string: %s" % output_string) output_dict = {} for line in output_string.split('\n'): if line: From e58b52592b36120a21969690a1da9f1350224eba Mon Sep 17 00:00:00 2001 From: Steven Armstrong Date: Tue, 18 Oct 2011 14:24:52 +0200 Subject: [PATCH 3/9] add missing environment variable __self Signed-off-by: Steven Armstrong --- lib/cdist/core/manifest.py | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/cdist/core/manifest.py b/lib/cdist/core/manifest.py index 50964fdc..84092892 100644 --- a/lib/cdist/core/manifest.py +++ b/lib/cdist/core/manifest.py @@ -92,6 +92,7 @@ class Manifest(object): env = os.environ.copy() env.update(self.env) env.update({ + '__self': cdist_object.path, '__object': cdist_object.absolute_path, '__object_id': cdist_object.object_id, '__object_fq': cdist_object.path, From af367a76f8f1c9c2ea838b5f8f28db3d6e4e2d6f Mon Sep 17 00:00:00 2001 From: Steven Armstrong Date: Tue, 18 Oct 2011 14:41:38 +0200 Subject: [PATCH 4/9] test for: .cdist may not be used in object_id Signed-off-by: Steven Armstrong --- lib/cdist/test/object/__init__.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/lib/cdist/test/object/__init__.py b/lib/cdist/test/object/__init__.py index 9a13f524..84d2cf8f 100644 --- a/lib/cdist/test/object/__init__.py +++ b/lib/cdist/test/object/__init__.py @@ -53,12 +53,18 @@ class ObjectClassTestCase(unittest.TestCase): class ObjectIdTestCase(unittest.TestCase): - def test_illegal_object_id(self): + def test_object_id_starts_with_slash(self): cdist_type = core.Type(type_base_path, '__third') illegal_object_id = '/object_id/may/not/start/with/slash' with self.assertRaises(core.IllegalObjectIdError): core.Object(cdist_type, object_base_path, illegal_object_id) + def test_object_id_contains_dotcdist(self): + cdist_type = core.Type(type_base_path, '__third') + illegal_object_id = 'object_id/may/not/contain/.cdist/anywhere' + with self.assertRaises(core.IllegalObjectIdError): + core.Object(cdist_type, object_base_path, illegal_object_id) + class ObjectTestCase(unittest.TestCase): From 030d5919e621cc696ba647d571a73373c3d92664 Mon Sep 17 00:00:00 2001 From: Steven Armstrong Date: Tue, 18 Oct 2011 14:42:57 +0200 Subject: [PATCH 5/9] raise exception if object_id contains .cdist Signed-off-by: Steven Armstrong --- lib/cdist/core/object.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/lib/cdist/core/object.py b/lib/cdist/core/object.py index eeb5799b..f22c206a 100644 --- a/lib/cdist/core/object.py +++ b/lib/cdist/core/object.py @@ -88,8 +88,11 @@ class Object(object): return self.__class__(self.type.__class__(type_path, type_name), object_path, object_id=object_id) def __init__(self, cdist_type, base_path, object_id=None): - if object_id and object_id.startswith('/'): - raise IllegalObjectIdError(object_id, 'object_id may not start with /') + if object_id: + if object_id.startswith('/'): + raise IllegalObjectIdError(object_id, 'object_id may not start with /') + if '.cdist' in object_id: + raise IllegalObjectIdError(object_id, 'object_id may not contain \'.cdist\'') self.type = cdist_type # instance of Type self.base_path = base_path self.object_id = object_id From 2a5465c03cac30523f8fa5159e738e655f8ebe66 Mon Sep 17 00:00:00 2001 From: Steven Armstrong Date: Tue, 18 Oct 2011 14:48:06 +0200 Subject: [PATCH 6/9] test that emulator fails if requirement has illegal object_id Signed-off-by: Steven Armstrong --- lib/cdist/test/emulator/__init__.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/lib/cdist/test/emulator/__init__.py b/lib/cdist/test/emulator/__init__.py index 66e7918a..ccdda265 100644 --- a/lib/cdist/test/emulator/__init__.py +++ b/lib/cdist/test/emulator/__init__.py @@ -73,3 +73,10 @@ class EmulatorTestCase(unittest.TestCase): os.environ['require'] = '__does-not-exist/some-id' emu = emulator.Emulator(argv) self.assertRaises(core.NoSuchTypeError, emu.run) + + def test_illegal_object_id_requirement(self): + argv = ['__file', '/tmp/foobar'] + os.environ.update(self.env) + os.environ['require'] = '__file/bad/id/with/.cdist/inside' + emu = emulator.Emulator(argv) + self.assertRaises(core.IllegalObjectIdError, emu.run) From 2246b7496ba8c21ecca5737dff5dbe0d963fe4c2 Mon Sep 17 00:00:00 2001 From: Steven Armstrong Date: Tue, 18 Oct 2011 14:50:28 +0200 Subject: [PATCH 7/9] fails if requirement has illegal object_id Signed-off-by: Steven Armstrong --- lib/cdist/emulator.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/lib/cdist/emulator.py b/lib/cdist/emulator.py index b5682342..10c2dbaa 100644 --- a/lib/cdist/emulator.py +++ b/lib/cdist/emulator.py @@ -158,9 +158,6 @@ class Emulator(object): requirement_type_name = requirement_parts[0] requirement_object_id = requirement_parts[1] - # Instantiate type which fails if type does not exist - requirement_type = core.Type(self.type_base_path, requirement_type_name) - # FIXME: Add support for omitted object id == singleton #if len(requirement_parts) == 1: #except IndexError: @@ -170,6 +167,11 @@ class Emulator(object): # Remove / if existent in object id requirement_object_id = requirement_object_id.lstrip('/') + # Instantiate type which fails if type does not exist + requirement_type = core.Type(self.type_base_path, requirement_type_name) + # Instantiate object which fails if the object_id is illegal + requirement_object = core.Object(requirement_type, self.object_base_path, requirement_object_id) + # Construct cleaned up requirement with only one / :-) requirement = requirement_type_name + '/' + requirement_object_id self.cdist_object.requirements.append(requirement) From 205cd7b28ad4396cafe59d4e492e8e9a2a4503aa Mon Sep 17 00:00:00 2001 From: Steven Armstrong Date: Tue, 18 Oct 2011 14:55:20 +0200 Subject: [PATCH 8/9] fix the test Signed-off-by: Steven Armstrong --- lib/cdist/test/manifest/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/cdist/test/manifest/__init__.py b/lib/cdist/test/manifest/__init__.py index 45902477..14d6be8a 100644 --- a/lib/cdist/test/manifest/__init__.py +++ b/lib/cdist/test/manifest/__init__.py @@ -104,7 +104,7 @@ class ManifestTestCase(unittest.TestCase): self.assertEqual(output_dict['__cdist_type_base_path'], self.local.type_path) self.assertEqual(output_dict['__type'], cdist_type.absolute_path) self.assertEqual(output_dict['__object'], cdist_object.absolute_path) - self.assertEqual(output_dict['__self'], cdist_object.path) + self.assertEqual(output_dict['__self'], cdist_object.name) self.assertEqual(output_dict['__object_id'], cdist_object.object_id) self.assertEqual(output_dict['__object_fq'], cdist_object.path) From d427e3f5856df6bb87e8f349c998805dcaf1845d Mon Sep 17 00:00:00 2001 From: Steven Armstrong Date: Tue, 18 Oct 2011 14:56:35 +0200 Subject: [PATCH 9/9] __self should not contain .cdist Signed-off-by: Steven Armstrong --- lib/cdist/core/manifest.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/cdist/core/manifest.py b/lib/cdist/core/manifest.py index 84092892..bae44ebc 100644 --- a/lib/cdist/core/manifest.py +++ b/lib/cdist/core/manifest.py @@ -92,10 +92,10 @@ class Manifest(object): env = os.environ.copy() env.update(self.env) env.update({ - '__self': cdist_object.path, + '__self': cdist_object.name, '__object': cdist_object.absolute_path, '__object_id': cdist_object.object_id, - '__object_fq': cdist_object.path, + '__object_fq': cdist_object.name, '__type': cdist_object.type.absolute_path, '__cdist_manifest': script, })