Skip to content

Commit

Permalink
Merge pull request #1463 from gst/fix_template_self_use
Browse files Browse the repository at this point in the history
Fix : Infinite recursion when template uses itself
  • Loading branch information
Seb-Solon committed Jan 19, 2015
2 parents 80be0ee + dc8ccf2 commit ce67983
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 2 deletions.
25 changes: 23 additions & 2 deletions shinken/objects/item.py
Original file line number Diff line number Diff line change
Expand Up @@ -709,6 +709,14 @@ def dump(self):
dmp[prop] = getattr(self, prop)
return dmp

def _get_name(self):
if hasattr(self, 'get_name'):
return self.get_name()
name = getattr(self, 'name', None)
host_name = getattr(self, 'host_name', None)
return '%s(host_name=%s)' % (name or 'no-name', host_name or '')



class Items(object):
def __init__(self, items, index_items=True):
Expand Down Expand Up @@ -992,14 +1000,27 @@ def get_all_tags(self, item):
all_tags.extend(self.get_all_tags(t))
return list(set(all_tags))


def linkify_item_templates(self, item):
tpls = []
tpl_names = item.get_templates()

for name in tpl_names:
t = self.find_tpl_by_name(name)
if t is not None:
tpls.append(t)
if t is None:
# TODO: Check if this should not be better to report as an error ?
self.configuration_warnings.append(
"%s %r use/inherit from an unknown template (%r) ! Imported from: %s" % (
type(item).__name__, item._get_name(), name, item.imported_from
))
else:
if t is item:
self.configuration_errors.append(
'%s %r use/inherits from itself ! Imported from: %s' % (
type(item).__name__, item._get_name(), item.imported_from
))
else:
tpls.append(t)
item.templates = tpls

# We will link all templates, and create the template
Expand Down
18 changes: 18 additions & 0 deletions test/bad_template.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@

from shinken_test import ShinkenTest


class TestConfig(ShinkenTest):

def setUp(self):
pass # force no setUp for this class.

def test_bad_template_use_itself(self):
self.setup_with_file('etc/bad_template_use_itself.cfg')
self.assertIn(u"Host u'bla' use/inherits from itself ! Imported from: etc/bad_template_use_itself.cfg:1",
self.conf.hosts.configuration_errors)

def test_bad_host_use_undefined_template(self):
self.setup_with_file('etc/bad_host_use_undefined_template.cfg')
self.assertIn(u"Host u'bla' use/inherit from an unknown template (u'undefined') ! Imported from: etc/bad_host_use_undefined_template.cfg:2",
self.conf.hosts.configuration_warnings)
5 changes: 5 additions & 0 deletions test/etc/bad_host_use_undefined_template.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

define host {
host_name bla
use undefined
}
5 changes: 5 additions & 0 deletions test/etc/bad_template_use_itself.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
define host {
name bla
use bla
register 0
}

0 comments on commit ce67983

Please sign in to comment.