def test_render_template(self): # render_template() should take a template string and substitue # its variables. pa = PowerAction(POWER_TYPE.WAKE_ON_LAN) template = ShellTemplate("template: {{mac}}") rendered = pa.render_template(template, mac="mymac") self.assertEqual("template: mymac", rendered)
def test_render_template_raises_PowerActionFail(self): # If not enough arguments are supplied to fill in template # variables then a PowerActionFail is raised. pa = PowerAction(POWER_TYPE.WAKE_ON_LAN) template_name = factory.getRandomString() template = ShellTemplate("template: {{mac}}", name=template_name) self.assertThat( lambda: pa.render_template(template), Raises( MatchesException( PowerActionFail, ".*name 'mac' is not defined at line \d+ column \d+ " "in file %s" % re.escape(template_name))))
def test_substitute_does_not_escape_safe_objects(self): # Substitutions will not be escaped if they're `safe` objects. template = ShellTemplate("{{safe(a)}}") expected = "$ ! ()" observed = template.substitute(a="$ ! ()") self.assertEqual(expected, observed)
def test_substitute_does_not_escape_safe(self): # Substitutions will not be escaped if they're marked with `safe`. template = ShellTemplate("{{a|safe}}") expected = "$ ! ()" observed = template.substitute(a="$ ! ()") self.assertEqual(expected, observed)
def test_substitute_escapes(self): # Substitutions are shell-escaped. template = ShellTemplate("{{a}}") expected = "'1 2 3'" observed = template.substitute(a="1 2 3") self.assertEqual(expected, observed)
def get_template(self): with open(self.path, "rb") as f: return ShellTemplate(f.read(), name=self.path)