diff --git a/cdist/core/cdist_object.py b/cdist/core/cdist_object.py index ba0fdf11..8c6ee9c9 100644 --- a/cdist/core/cdist_object.py +++ b/cdist/core/cdist_object.py @@ -158,12 +158,13 @@ class CdistObject(object): base_path = self.base_path type_path = self.cdist_type.base_path + object_marker = self.object_marker type_name, object_id = self.split_name(object_name) cdist_type = self.cdist_type.__class__(type_path, type_name) - return self.__class__(cdist_type, base_path, object_id=object_id) + return self.__class__(cdist_type, base_path, object_marker, object_id=object_id) def __repr__(self): return '' % self.name diff --git a/cdist/test/cdist_object/__init__.py b/cdist/test/cdist_object/__init__.py index 3c25a959..9c075acb 100644 --- a/cdist/test/cdist_object/__init__.py +++ b/cdist/test/cdist_object/__init__.py @@ -1,7 +1,7 @@ # -*- coding: utf-8 -*- # # 2010-2011 Steven Armstrong (steven-cdist at armstrong.cc) -# 2012 Nico Schottelius (nico-cdist at schottelius.org) +# 2012-2015 Nico Schottelius (nico-cdist at schottelius.org) # 2014 Daniel Heule (hda at sfs.biz) # # This file is part of cdist. @@ -23,6 +23,7 @@ import os import shutil +import tempfile from cdist import test from cdist import core @@ -32,37 +33,48 @@ import cdist import os.path as op my_dir = op.abspath(op.dirname(__file__)) fixtures = op.join(my_dir, 'fixtures') -object_base_path = op.join(fixtures, 'object') type_base_path = op.join(fixtures, 'type') +OBJECT_MARKER_NAME = '.cdist-pseudo-random' + +expected_object_names = sorted([ + '__first/child', + '__first/dog', + '__first/man', + '__first/woman', + '__second/on-the', + '__second/under-the', + '__third/moon']) + + class ObjectClassTestCase(test.CdistTestCase): def setUp(self): - self.expected_object_names = sorted([ - '__first/child', - '__first/dog', - '__first/man', - '__first/woman', - '__second/on-the', - '__second/under-the', - '__third/moon']) + + self.tempdir = tempfile.mkdtemp(prefix="cdist.test") + self.object_base_path = self.tempdir self.expected_objects = [] - for cdist_object_name in self.expected_object_names: + for cdist_object_name in expected_object_names: cdist_type, cdist_object_id = cdist_object_name.split("/", 1) - cdist_object = core.CdistObject(core.CdistType(type_base_path, cdist_type), object_base_path, cdist_object_id) + cdist_object = core.CdistObject(core.CdistType(type_base_path, cdist_type), self.object_base_path, + OBJECT_MARKER_NAME, cdist_object_id) + cdist_object.create() self.expected_objects.append(cdist_object) + def tearDown(self): + shutil.rmtree(self.tempdir) + def test_list_object_names(self): - found_object_names = sorted(list(core.CdistObject.list_object_names(object_base_path))) - self.assertEqual(found_object_names, self.expected_object_names) + found_object_names = sorted(list(core.CdistObject.list_object_names(self.object_base_path, OBJECT_MARKER_NAME))) + self.assertEqual(found_object_names, expected_object_names) def test_list_type_names(self): - type_names = list(cdist.core.CdistObject.list_type_names(object_base_path)) + type_names = list(cdist.core.CdistObject.list_type_names(self.object_base_path)) self.assertEqual(sorted(type_names), ['__first', '__second', '__third']) def test_list_objects(self): - found_objects = sorted(list(core.CdistObject.list_objects(object_base_path, type_base_path))) + found_objects = sorted(list(core.CdistObject.list_objects(self.object_base_path, type_base_path, OBJECT_MARKER_NAME))) self.assertEqual(found_objects, self.expected_objects) def test_create_singleton(self): @@ -77,41 +89,65 @@ class ObjectClassTestCase(test.CdistTestCase): self.expected_objects[0].object_from_name("__first") class ObjectIdTestCase(test.CdistTestCase): + + def setUp(self): + self.tempdir = tempfile.mkdtemp(prefix="cdist.test") + self.object_base_path = self.tempdir + + self.expected_objects = [] + for cdist_object_name in expected_object_names: + cdist_type, cdist_object_id = cdist_object_name.split("/", 1) + cdist_object = core.CdistObject(core.CdistType(type_base_path, cdist_type), self.object_base_path, + OBJECT_MARKER_NAME, cdist_object_id) + cdist_object.create() + self.expected_objects.append(cdist_object) + + def tearDown(self): + shutil.rmtree(self.tempdir) + def test_object_id_contains_double_slash(self): cdist_type = core.CdistType(type_base_path, '__third') illegal_object_id = '/object_id//may/not/contain/double/slash' with self.assertRaises(core.IllegalObjectIdError): - core.CdistObject(cdist_type, object_base_path, illegal_object_id) + core.CdistObject(cdist_type, self.object_base_path, OBJECT_MARKER_NAME, illegal_object_id) def test_object_id_contains_object_marker(self): cdist_type = core.CdistType(type_base_path, '__third') - illegal_object_id = 'object_id/may/not/contain/%s/anywhere' % core.OBJECT_MARKER + illegal_object_id = 'object_id/may/not/contain/%s/anywhere' % OBJECT_MARKER_NAME with self.assertRaises(core.IllegalObjectIdError): - core.CdistObject(cdist_type, object_base_path, illegal_object_id) + core.CdistObject(cdist_type, self.object_base_path, OBJECT_MARKER_NAME, illegal_object_id) def test_object_id_contains_object_marker_string(self): cdist_type = core.CdistType(type_base_path, '__third') - illegal_object_id = 'object_id/may/contain_%s_in_filename' % core.OBJECT_MARKER - core.CdistObject(cdist_type, object_base_path, illegal_object_id) + illegal_object_id = 'object_id/may/contain_%s_in_filename' % OBJECT_MARKER_NAME + core.CdistObject(cdist_type, self.object_base_path, OBJECT_MARKER_NAME, illegal_object_id) # if we get here, the test passed def test_object_id_contains_only_dot(self): cdist_type = core.CdistType(type_base_path, '__third') illegal_object_id = '.' with self.assertRaises(core.IllegalObjectIdError): - core.CdistObject(cdist_type, object_base_path, illegal_object_id) + core.CdistObject(cdist_type, self.object_base_path, OBJECT_MARKER_NAME, illegal_object_id) def test_object_id_on_singleton_type(self): cdist_type = core.CdistType(type_base_path, '__test_singleton') illegal_object_id = 'object_id' with self.assertRaises(core.IllegalObjectIdError): - core.CdistObject(cdist_type, object_base_path, illegal_object_id) + core.CdistObject(cdist_type, self.object_base_path, OBJECT_MARKER_NAME, illegal_object_id) class ObjectTestCase(test.CdistTestCase): def setUp(self): + self.tempdir = tempfile.mkdtemp(prefix="cdist.test") + self.object_base_path = self.tempdir + self.cdist_type = core.CdistType(type_base_path, '__third') - self.cdist_object = core.CdistObject(self.cdist_type, object_base_path, 'moon') + self.cdist_object = core.CdistObject(self.cdist_type, self.object_base_path, OBJECT_MARKER_NAME, 'moon') + self.cdist_object.create() + + self.cdist_object.parameters['planet'] = 'Saturn' + self.cdist_object.parameters['name'] = 'Prometheus' + def tearDown(self): self.cdist_object.prepared = False @@ -121,6 +157,8 @@ class ObjectTestCase(test.CdistTestCase): self.cdist_object.code_remote = '' self.cdist_object.state = '' + shutil.rmtree(self.tempdir) + def test_name(self): self.assertEqual(self.cdist_object.name, '__third/moon') @@ -128,22 +166,22 @@ class ObjectTestCase(test.CdistTestCase): self.assertEqual(self.cdist_object.object_id, 'moon') def test_path(self): - self.assertEqual(self.cdist_object.path, '__third/moon/.cdist') + self.assertEqual(self.cdist_object.path, "__third/moon/%s" % OBJECT_MARKER_NAME) def test_absolute_path(self): - self.assertEqual(self.cdist_object.absolute_path, os.path.join(object_base_path, '__third/moon/.cdist')) + self.assertEqual(self.cdist_object.absolute_path, os.path.join(self.object_base_path, "__third/moon/%s" % OBJECT_MARKER_NAME)) def test_code_local_path(self): - self.assertEqual(self.cdist_object.code_local_path, '__third/moon/.cdist/code-local') + self.assertEqual(self.cdist_object.code_local_path, "__third/moon/%s/code-local" % OBJECT_MARKER_NAME) def test_code_remote_path(self): - self.assertEqual(self.cdist_object.code_remote_path, '__third/moon/.cdist/code-remote') + self.assertEqual(self.cdist_object.code_remote_path, "__third/moon/%s/code-remote" % OBJECT_MARKER_NAME) def test_parameter_path(self): - self.assertEqual(self.cdist_object.parameter_path, '__third/moon/.cdist/parameter') + self.assertEqual(self.cdist_object.parameter_path, "__third/moon/%s/parameter" % OBJECT_MARKER_NAME) def test_explorer_path(self): - self.assertEqual(self.cdist_object.explorer_path, '__third/moon/.cdist/explorer') + self.assertEqual(self.cdist_object.explorer_path, "__third/moon/%s/explorer" % OBJECT_MARKER_NAME) def test_parameters(self): expected_parameters = {'planet': 'Saturn', 'name': 'Prometheus'} diff --git a/cdist/test/cdist_object/fixtures/object/__first/.keep b/cdist/test/cdist_object/fixtures/object/__first/.keep deleted file mode 100644 index e69de29b..00000000 diff --git a/cdist/test/cdist_object/fixtures/object/__first/child/.cdist/.keep b/cdist/test/cdist_object/fixtures/object/__first/child/.cdist/.keep deleted file mode 100644 index e69de29b..00000000 diff --git a/cdist/test/cdist_object/fixtures/object/__first/dog/.cdist/.keep b/cdist/test/cdist_object/fixtures/object/__first/dog/.cdist/.keep deleted file mode 100644 index e69de29b..00000000 diff --git a/cdist/test/cdist_object/fixtures/object/__first/man/.cdist/.keep b/cdist/test/cdist_object/fixtures/object/__first/man/.cdist/.keep deleted file mode 100644 index e69de29b..00000000 diff --git a/cdist/test/cdist_object/fixtures/object/__first/woman/.cdist/.keep b/cdist/test/cdist_object/fixtures/object/__first/woman/.cdist/.keep deleted file mode 100644 index e69de29b..00000000 diff --git a/cdist/test/cdist_object/fixtures/object/__second/.keep b/cdist/test/cdist_object/fixtures/object/__second/.keep deleted file mode 100644 index e69de29b..00000000 diff --git a/cdist/test/cdist_object/fixtures/object/__second/on-the/.cdist/.keep b/cdist/test/cdist_object/fixtures/object/__second/on-the/.cdist/.keep deleted file mode 100644 index e69de29b..00000000 diff --git a/cdist/test/cdist_object/fixtures/object/__second/under-the/.cdist/.keep b/cdist/test/cdist_object/fixtures/object/__second/under-the/.cdist/.keep deleted file mode 100644 index e69de29b..00000000 diff --git a/cdist/test/cdist_object/fixtures/object/__third/.keep b/cdist/test/cdist_object/fixtures/object/__third/.keep deleted file mode 100644 index e69de29b..00000000 diff --git a/cdist/test/cdist_object/fixtures/object/__third/moon/.cdist/.keep b/cdist/test/cdist_object/fixtures/object/__third/moon/.cdist/.keep deleted file mode 100644 index e69de29b..00000000 diff --git a/cdist/test/cdist_object/fixtures/object/__third/moon/.cdist/parameter/name b/cdist/test/cdist_object/fixtures/object/__third/moon/.cdist/parameter/name deleted file mode 100644 index 4129a761..00000000 --- a/cdist/test/cdist_object/fixtures/object/__third/moon/.cdist/parameter/name +++ /dev/null @@ -1 +0,0 @@ -Prometheus diff --git a/cdist/test/cdist_object/fixtures/object/__third/moon/.cdist/parameter/planet b/cdist/test/cdist_object/fixtures/object/__third/moon/.cdist/parameter/planet deleted file mode 100644 index 8e6ee422..00000000 --- a/cdist/test/cdist_object/fixtures/object/__third/moon/.cdist/parameter/planet +++ /dev/null @@ -1 +0,0 @@ -Saturn diff --git a/cdist/test/code/__init__.py b/cdist/test/code/__init__.py index 796e8a51..689fcff6 100644 --- a/cdist/test/code/__init__.py +++ b/cdist/test/code/__init__.py @@ -1,7 +1,7 @@ # -*- coding: utf-8 -*- # # 2011 Steven Armstrong (steven-cdist at armstrong.cc) -# 2012-2013 Nico Schottelius (nico-cdist at schottelius.org) +# 2012-2015 Nico Schottelius (nico-cdist at schottelius.org) # # This file is part of cdist. # @@ -61,7 +61,7 @@ class CodeTestCase(test.CdistTestCase): self.code = code.Code(self.target_host, self.local, self.remote) self.cdist_type = core.CdistType(self.local.type_path, '__dump_environment') - self.cdist_object = core.CdistObject(self.cdist_type, self.local.object_path, 'whatever') + self.cdist_object = core.CdistObject(self.cdist_type, self.local.object_path, 'whatever', self.local.object_marker_name) self.cdist_object.create() def tearDown(self): diff --git a/cdist/test/config/__init__.py b/cdist/test/config/__init__.py index 70501c89..452ce042 100644 --- a/cdist/test/config/__init__.py +++ b/cdist/test/config/__init__.py @@ -1,7 +1,7 @@ # -*- coding: utf-8 -*- # # 2010-2011 Steven Armstrong (steven-cdist at armstrong.cc) -# 2012-2013 Nico Schottelius (nico-cdist at schottelius.org) +# 2012-2015 Nico Schottelius (nico-cdist at schottelius.org) # 2014 Daniel Heule (hda at sfs.biz) # # This file is part of cdist. @@ -67,7 +67,7 @@ class ConfigRunTestCase(test.CdistTestCase): self.config = cdist.config.Config(self.local, self.remote) - self.objects = list(core.CdistObject.list_objects(object_base_path, type_base_path)) + self.objects = list(core.CdistObject.list_objects(object_base_path, type_base_path, self.local.object_marker_name)) self.object_index = dict((o.name, o) for o in self.objects) self.object_names = [o.name for o in self.objects] diff --git a/cdist/test/emulator/__init__.py b/cdist/test/emulator/__init__.py index 870d6245..62000931 100644 --- a/cdist/test/emulator/__init__.py +++ b/cdist/test/emulator/__init__.py @@ -57,10 +57,15 @@ class EmulatorTestCase(test.CdistTestCase): self.manifest = core.Manifest(self.target_host, self.local) self.env = self.manifest.env_initial_manifest(self.script) + self.env['__cdist_object_marker'] = self.local.object_marker_name def tearDown(self): shutil.rmtree(self.temp_dir) +# def test_missing_object_marker_variable(self): +# del self.env['__cdist_object_marker'] +# self.assertRaises(KeyError, emulator.Emulator, argv, env=self.env) + def test_nonexistent_type_exec(self): argv = ['__does-not-exist'] self.assertRaises(core.cdist_type.NoSuchTypeError, emulator.Emulator, argv, env=self.env) @@ -73,7 +78,7 @@ class EmulatorTestCase(test.CdistTestCase): def test_illegal_object_id_requirement(self): argv = ['__file', '/tmp/foobar'] - self.env['require'] = '__file/bad/id/with/.cdist/inside' + self.env['require'] = "__file/bad/id/with/%s/inside" % self.local.object_marker_name emu = emulator.Emulator(argv, env=self.env) self.assertRaises(core.IllegalObjectIdError, emu.run) @@ -118,10 +123,10 @@ class EmulatorTestCase(test.CdistTestCase): emu.run() # now load the objects and verify the require parameter of the objects cdist_type = core.CdistType(self.local.type_path, '__planet') - erde_object = core.CdistObject(cdist_type, self.local.object_path, 'erde') - mars_object = core.CdistObject(cdist_type, self.local.object_path, 'mars') + erde_object = core.CdistObject(cdist_type, self.local.object_path, self.local.object_marker_name, 'erde') + mars_object = core.CdistObject(cdist_type, self.local.object_path, self.local.object_marker_name, 'mars') cdist_type = core.CdistType(self.local.type_path, '__file') - file_object = core.CdistObject(cdist_type, self.local.object_path, '/tmp/cdisttest') + file_object = core.CdistObject(cdist_type, self.local.object_path, self.local.object_marker_name, '/tmp/cdisttest') # now test the recorded requirements self.assertTrue(len(erde_object.requirements) == 0) self.assertEqual(list(mars_object.requirements), ['__planet/erde']) @@ -150,7 +155,7 @@ class AutoRequireEmulatorTestCase(test.CdistTestCase): initial_manifest = os.path.join(self.local.manifest_path, "init") self.manifest.run_initial_manifest(initial_manifest) cdist_type = core.CdistType(self.local.type_path, '__saturn') - cdist_object = core.CdistObject(cdist_type, self.local.object_path) + cdist_object = core.CdistObject(cdist_type, self.local.object_path, self.local.object_marker_name, '') self.manifest.run_type_manifest(cdist_object) expected = ['__planet/Saturn', '__moon/Prometheus'] self.assertEqual(sorted(cdist_object.autorequire), sorted(expected)) @@ -172,6 +177,7 @@ class OverrideTestCase(test.CdistTestCase): self.manifest = core.Manifest(self.target_host, self.local) self.env = self.manifest.env_initial_manifest(self.script) + self.env['__cdist_object_marker'] = self.local.object_marker_name def tearDown(self): shutil.rmtree(self.temp_dir) @@ -211,6 +217,7 @@ class ArgumentsTestCase(test.CdistTestCase): self.manifest = core.Manifest(self.target_host, self.local) self.env = self.manifest.env_initial_manifest(self.script) + self.env['__cdist_object_marker'] = self.local.object_marker_name def tearDown(self): shutil.rmtree(self.temp_dir) @@ -222,7 +229,7 @@ class ArgumentsTestCase(test.CdistTestCase): emu.run() cdist_type = core.CdistType(self.local.type_path, '__arguments_with_dashes') - cdist_object = core.CdistObject(cdist_type, self.local.object_path, 'some-id') + cdist_object = core.CdistObject(cdist_type, self.local.object_path, self.local.object_marker_name, 'some-id') self.assertTrue('with-dash' in cdist_object.parameters) def test_boolean(self): @@ -234,7 +241,7 @@ class ArgumentsTestCase(test.CdistTestCase): emu.run() cdist_type = core.CdistType(self.local.type_path, type_name) - cdist_object = core.CdistObject(cdist_type, self.local.object_path, object_id) + cdist_object = core.CdistObject(cdist_type, self.local.object_path, self.local.object_marker_name, object_id) self.assertTrue('boolean1' in cdist_object.parameters) self.assertFalse('boolean2' in cdist_object.parameters) # empty file -> True @@ -242,17 +249,17 @@ class ArgumentsTestCase(test.CdistTestCase): def test_required_arguments(self): """check whether assigning required parameter works""" + type_name = '__arguments_required' object_id = 'some-id' value = 'some value' argv = [type_name, object_id, '--required1', value, '--required2', value] -# print(self.env) os.environ.update(self.env) emu = emulator.Emulator(argv) emu.run() cdist_type = core.CdistType(self.local.type_path, type_name) - cdist_object = core.CdistObject(cdist_type, self.local.object_path, object_id) + cdist_object = core.CdistObject(cdist_type, self.local.object_path, self.local.object_marker_name, object_id) self.assertTrue('required1' in cdist_object.parameters) self.assertTrue('required2' in cdist_object.parameters) self.assertEqual(cdist_object.parameters['required1'], value) @@ -278,7 +285,7 @@ class ArgumentsTestCase(test.CdistTestCase): emu.run() cdist_type = core.CdistType(self.local.type_path, type_name) - cdist_object = core.CdistObject(cdist_type, self.local.object_path, object_id) + cdist_object = core.CdistObject(cdist_type, self.local.object_path, self.local.object_marker_name, object_id) self.assertTrue('optional1' in cdist_object.parameters) self.assertFalse('optional2' in cdist_object.parameters) self.assertEqual(cdist_object.parameters['optional1'], value) @@ -293,7 +300,7 @@ class ArgumentsTestCase(test.CdistTestCase): emu.run() cdist_type = core.CdistType(self.local.type_path, type_name) - cdist_object = core.CdistObject(cdist_type, self.local.object_path, object_id) + cdist_object = core.CdistObject(cdist_type, self.local.object_path, self.local.object_marker_name, object_id) self.assertTrue('optional1' in cdist_object.parameters) self.assertFalse('optional2' in cdist_object.parameters) self.assertEqual(cdist_object.parameters['optional1'], value) @@ -346,7 +353,7 @@ class StdinTestCase(test.CdistTestCase): ###################################################################### # Create path where stdin should reside at cdist_type = core.CdistType(self.local.type_path, type_name) - cdist_object = core.CdistObject(cdist_type, self.local.object_path, object_id) + cdist_object = core.CdistObject(cdist_type, self.local.object_path, self.local.object_marker_name, object_id) stdin_out_path = os.path.join(cdist_object.absolute_path, 'stdin') ###################################################################### diff --git a/cdist/test/explorer/__init__.py b/cdist/test/explorer/__init__.py index 92ef75a3..335d0e32 100644 --- a/cdist/test/explorer/__init__.py +++ b/cdist/test/explorer/__init__.py @@ -127,7 +127,7 @@ class ExplorerClassTestCase(test.CdistTestCase): def test_transfer_object_parameters(self): cdist_type = core.CdistType(self.local.type_path, '__test_type') - cdist_object = core.CdistObject(cdist_type, self.local.object_path, 'whatever') + cdist_object = core.CdistObject(cdist_type, self.local.object_path, self.local.object_marker_name, 'whatever') cdist_object.create() cdist_object.parameters = {'first': 'first value', 'second': 'second value'} self.explorer.transfer_object_parameters(cdist_object) @@ -137,14 +137,14 @@ class ExplorerClassTestCase(test.CdistTestCase): def test_run_type_explorer(self): cdist_type = core.CdistType(self.local.type_path, '__test_type') - cdist_object = core.CdistObject(cdist_type, self.local.object_path, 'whatever') + cdist_object = core.CdistObject(cdist_type, self.local.object_path, self.local.object_marker_name, 'whatever') self.explorer.transfer_type_explorers(cdist_type) output = self.explorer.run_type_explorer('world', cdist_object) self.assertEqual(output, 'hello\n') def test_run_type_explorers(self): cdist_type = core.CdistType(self.local.type_path, '__test_type') - cdist_object = core.CdistObject(cdist_type, self.local.object_path, 'whatever') + cdist_object = core.CdistObject(cdist_type, self.local.object_path, self.local.object_marker_name, 'whatever') cdist_object.create() self.explorer.run_type_explorers(cdist_object) self.assertEqual(cdist_object.explorers, {'world': 'hello'}) diff --git a/cdist/test/manifest/__init__.py b/cdist/test/manifest/__init__.py index c375a80f..cc60c844 100644 --- a/cdist/test/manifest/__init__.py +++ b/cdist/test/manifest/__init__.py @@ -84,7 +84,7 @@ class ManifestTestCase(test.CdistTestCase): def test_type_manifest_environment(self): cdist_type = core.CdistType(self.local.type_path, '__dump_environment') - cdist_object = core.CdistObject(cdist_type, self.local.object_path, 'whatever') + cdist_object = core.CdistObject(cdist_type, self.local.object_path, self.local.object_marker_name, 'whatever') handle, output_file = self.mkstemp(dir=self.temp_dir) os.close(handle) os.environ['__cdist_test_out'] = output_file