From 72345f551620019f13f1b5710e82dd145f73263c Mon Sep 17 00:00:00 2001 From: Darko Poljak Date: Thu, 30 Jun 2016 20:31:01 +0200 Subject: [PATCH] Make union of existing and new requirements instead of conflict error. --- cdist/emulator.py | 60 +++++++++++++++++++++------------ cdist/test/emulator/__init__.py | 43 ++++++++++++++++++++--- 2 files changed, 77 insertions(+), 26 deletions(-) diff --git a/cdist/emulator.py b/cdist/emulator.py index f5a9f645..d526bf77 100644 --- a/cdist/emulator.py +++ b/cdist/emulator.py @@ -201,6 +201,34 @@ class Emulator(object): except EnvironmentError as e: raise cdist.Error('Failed to read from stdin: %s' % e) + + def record_requirement(self, requirement): + """record requirement and return recorded requirement""" + + # Raises an error, if object cannot be created + try: + cdist_object = self.cdist_object.object_from_name(requirement) + except core.cdist_type.NoSuchTypeError as e: + self.log.error(("%s requires object %s, but type %s does not" + " exist. Defined at %s" % (self.cdist_object.name, + requirement, e.name, self.object_source))) + raise + except core.cdist_object.MissingObjectIdError as e: + self.log.error(("%s requires object %s without object id." + " Defined at %s" % (self.cdist_object.name, requirement, + self.object_source))) + raise + + self.log.debug("Recording requirement: %s", requirement) + + # Save the sanitised version, not the user supplied one + # (__file//bar => __file/bar) + # This ensures pattern matching is done against sanitised list + self.cdist_object.requirements.append(cdist_object.name) + + return cdist_object.name + + def record_requirements(self): """record requirements""" @@ -228,24 +256,8 @@ class Emulator(object): for requirement in requirements.split(" "): # Ignore empty fields - probably the only field anyway if len(requirement) == 0: continue - - # Raises an error, if object cannot be created - try: - cdist_object = self.cdist_object.object_from_name(requirement) - except core.cdist_type.NoSuchTypeError as e: - self.log.error("%s requires object %s, but type %s does not exist. Defined at %s" % (self.cdist_object.name, requirement, e.name, self.object_source)) - raise - except core.cdist_object.MissingObjectIdError as e: - self.log.error("%s requires object %s without object id. Defined at %s" % (self.cdist_object.name, requirement, self.object_source)) - raise - - self.log.debug("Recording requirement: %s", requirement) - - # Save the sanitised version, not the user supplied one - # (__file//bar => __file/bar) - # This ensures pattern matching is done against sanitised list - self.cdist_object.requirements.append(cdist_object.name) - reqs.add(cdist_object.name) + req = self.record_requirement(requirement) + reqs.add(req) if self._existing_reqs is not None: # if object exists then compare existing and new requirements self.log.debug("OBJ: {} {}".format(self.cdist_type, self.object_id)) @@ -253,15 +265,19 @@ class Emulator(object): self.log.debug("REQS: {}".format(reqs)) if self._existing_reqs != reqs: - errmsg = ("Object {} already exists with conflicting " - "requirements:\n{}: {}\n{}: {}".format( + dbgmsg = ("Object {} already exists with different " + "requirements:\n{}: {}\n{}: {}. Merging sets.".format( self.cdist_object.name, " ".join(self.cdist_object.source), self._existing_reqs, self.object_source, reqs)) - self.log.error(errmsg) - raise cdist.Error(errmsg) + self.log.debug(dbgmsg) + all_reqs = reqs | self._existing_reqs + self.log.debug("All requirements: {}".format(all_reqs)) + for x in all_reqs: + if not x in self.cdist_object.requirements: + self.record_requirement(x) def record_auto_requirements(self): diff --git a/cdist/test/emulator/__init__.py b/cdist/test/emulator/__init__.py index b91c1e8f..ee6c3cea 100644 --- a/cdist/test/emulator/__init__.py +++ b/cdist/test/emulator/__init__.py @@ -155,7 +155,7 @@ class EmulatorConflictingRequirementsTestCase(test.CdistTestCase): def tearDown(self): shutil.rmtree(self.temp_dir) - def test_object_conflicting_requirements_req_none(self): + def test_object_different_requirements_req_none(self): argv = ['__directory', 'spam'] emu = emulator.Emulator(argv, env=self.env) emu.run() @@ -167,9 +167,14 @@ class EmulatorConflictingRequirementsTestCase(test.CdistTestCase): if 'require' in self.env: del self.env['require'] emu = emulator.Emulator(argv, env=self.env) - self.assertRaises(cdist.Error, emu.run) + emu.run() - def test_object_conflicting_requirements_none_req(self): + cdist_type = core.CdistType(self.local.type_path, '__file') + cdist_object = core.CdistObject(cdist_type, self.local.object_path, self.local.object_marker_name, 'eggs') + reqs = set(('__directory/spam',)) + self.assertEqual(reqs, set(cdist_object.requirements)) + + def test_object_different_requirements_none_req(self): argv = ['__directory', 'spam'] emu = emulator.Emulator(argv, env=self.env) emu.run() @@ -181,7 +186,37 @@ class EmulatorConflictingRequirementsTestCase(test.CdistTestCase): argv = ['__file', 'eggs'] self.env['require'] = '__directory/spam' emu = emulator.Emulator(argv, env=self.env) - self.assertRaises(cdist.Error, emu.run) + emu.run() + + cdist_type = core.CdistType(self.local.type_path, '__file') + cdist_object = core.CdistObject(cdist_type, self.local.object_path, self.local.object_marker_name, 'eggs') + reqs = set(('__directory/spam',)) + self.assertEqual(reqs, set(cdist_object.requirements)) + + def test_object_different_requirements(self): + argv = ['__directory', 'spam'] + emu = emulator.Emulator(argv, env=self.env) + emu.run() + argv = ['__directory', 'spameggs'] + emu = emulator.Emulator(argv, env=self.env) + emu.run() + + argv = ['__file', 'eggs'] + if 'require' in self.env: + del self.env['require'] + self.env['require'] = '__directory/spam' + emu = emulator.Emulator(argv, env=self.env) + emu.run() + + argv = ['__file', 'eggs'] + self.env['require'] = '__directory/spameggs' + emu = emulator.Emulator(argv, env=self.env) + emu.run() + + cdist_type = core.CdistType(self.local.type_path, '__file') + cdist_object = core.CdistObject(cdist_type, self.local.object_path, self.local.object_marker_name, 'eggs') + reqs = set(('__directory/spam', '__directory/spameggs',)) + self.assertEqual(reqs, set(cdist_object.requirements)) class AutoRequireEmulatorTestCase(test.CdistTestCase):