Make union of existing and new requirements instead of conflict error.

This commit is contained in:
Darko Poljak 2016-06-30 20:31:01 +02:00
parent b04ab0b630
commit 72345f5516
2 changed files with 77 additions and 26 deletions

View file

@ -201,6 +201,34 @@ class Emulator(object):
except EnvironmentError as e: except EnvironmentError as e:
raise cdist.Error('Failed to read from stdin: %s' % 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): def record_requirements(self):
"""record requirements""" """record requirements"""
@ -228,24 +256,8 @@ class Emulator(object):
for requirement in requirements.split(" "): for requirement in requirements.split(" "):
# Ignore empty fields - probably the only field anyway # Ignore empty fields - probably the only field anyway
if len(requirement) == 0: continue if len(requirement) == 0: continue
req = self.record_requirement(requirement)
# Raises an error, if object cannot be created reqs.add(req)
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)
if self._existing_reqs is not None: if self._existing_reqs is not None:
# if object exists then compare existing and new requirements # if object exists then compare existing and new requirements
self.log.debug("OBJ: {} {}".format(self.cdist_type, self.object_id)) self.log.debug("OBJ: {} {}".format(self.cdist_type, self.object_id))
@ -253,15 +265,19 @@ class Emulator(object):
self.log.debug("REQS: {}".format(reqs)) self.log.debug("REQS: {}".format(reqs))
if self._existing_reqs != reqs: if self._existing_reqs != reqs:
errmsg = ("Object {} already exists with conflicting " dbgmsg = ("Object {} already exists with different "
"requirements:\n{}: {}\n{}: {}".format( "requirements:\n{}: {}\n{}: {}. Merging sets.".format(
self.cdist_object.name, self.cdist_object.name,
" ".join(self.cdist_object.source), " ".join(self.cdist_object.source),
self._existing_reqs, self._existing_reqs,
self.object_source, self.object_source,
reqs)) reqs))
self.log.error(errmsg) self.log.debug(dbgmsg)
raise cdist.Error(errmsg) 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): def record_auto_requirements(self):

View file

@ -155,7 +155,7 @@ class EmulatorConflictingRequirementsTestCase(test.CdistTestCase):
def tearDown(self): def tearDown(self):
shutil.rmtree(self.temp_dir) shutil.rmtree(self.temp_dir)
def test_object_conflicting_requirements_req_none(self): def test_object_different_requirements_req_none(self):
argv = ['__directory', 'spam'] argv = ['__directory', 'spam']
emu = emulator.Emulator(argv, env=self.env) emu = emulator.Emulator(argv, env=self.env)
emu.run() emu.run()
@ -167,9 +167,14 @@ class EmulatorConflictingRequirementsTestCase(test.CdistTestCase):
if 'require' in self.env: if 'require' in self.env:
del self.env['require'] del self.env['require']
emu = emulator.Emulator(argv, env=self.env) 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'] argv = ['__directory', 'spam']
emu = emulator.Emulator(argv, env=self.env) emu = emulator.Emulator(argv, env=self.env)
emu.run() emu.run()
@ -181,7 +186,37 @@ class EmulatorConflictingRequirementsTestCase(test.CdistTestCase):
argv = ['__file', 'eggs'] argv = ['__file', 'eggs']
self.env['require'] = '__directory/spam' self.env['require'] = '__directory/spam'
emu = emulator.Emulator(argv, env=self.env) 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): class AutoRequireEmulatorTestCase(test.CdistTestCase):