Commit 3248a533 authored by Xavier Thompson's avatar Xavier Thompson

recipe/promise_plugin: Fix tests

parent 5a565f05
Pipeline #17833 passed with stage
in 0 seconds
...@@ -91,7 +91,7 @@ class Recipe(GenericBaseRecipe): ...@@ -91,7 +91,7 @@ class Recipe(GenericBaseRecipe):
if klass == 'RunPromise': if klass == 'RunPromise':
klass = None klass = None
elif klass and not isPythonName(klass): elif klass and not isPythonName(klass):
raise UserError("%r is not valid class name" % klass) raise UserError("%r is not a valid class name" % klass)
if bool(module) == bool(filepath): if bool(module) == bool(filepath):
raise UserError("Either 'module' or 'file' is required but not both") raise UserError("Either 'module' or 'file' is required but not both")
......
import os, shutil, tempfile, unittest import os, shutil, tempfile, unittest
from slapos.recipe import promise_plugin from slapos.recipe import promise_plugin
from slapos.test.utils import makeRecipe from slapos.test.utils import makeRecipe
from zc.buildout import UserError
from pprint import pformat from pprint import pformat
import stat, json import stat, json
import six import six
...@@ -10,7 +11,7 @@ class TestPromisePlugin(unittest.TestCase): ...@@ -10,7 +11,7 @@ class TestPromisePlugin(unittest.TestCase):
def setUp(self): def setUp(self):
self.tmp = tempfile.mkdtemp() self.tmp = tempfile.mkdtemp()
self.output = os.path.join(self.tmp, 'output.py') self.output = os.path.join(self.tmp, 'output.py')
self.options = options = { self.options = {
'output': self.output, 'output': self.output,
'eggs': 'slapos.cookbook' 'eggs': 'slapos.cookbook'
} }
...@@ -18,9 +19,16 @@ class TestPromisePlugin(unittest.TestCase): ...@@ -18,9 +19,16 @@ class TestPromisePlugin(unittest.TestCase):
def tearDown(self): def tearDown(self):
shutil.rmtree(self.tmp) shutil.rmtree(self.tmp)
def test_parameters(self): def makeRecipe(self):
self.options['mode'] = '0644' return makeRecipe(
self.options['import'] = 'slapos.promise.plugin.check_site_available' promise_plugin.Recipe,
options=self.options,
name='plugin')
def installRecipe(self):
self.makeRecipe().install()
def setConfig(self):
self.options['config-param1'] = "YY^@12" self.options['config-param1'] = "YY^@12"
self.options['config-param2'] = "23'91'" self.options['config-param2'] = "23'91'"
self.options['config-param3'] = None self.options['config-param3'] = None
...@@ -28,88 +36,102 @@ class TestPromisePlugin(unittest.TestCase): ...@@ -28,88 +36,102 @@ class TestPromisePlugin(unittest.TestCase):
in multi line in multi line
123444 123444
""" """
recipe = makeRecipe(
promise_plugin.Recipe,
options=self.options,
name='plugin')
recipe.install()
def assertOutput(self, *expect):
self.assertTrue(os.path.exists(self.output)) self.assertTrue(os.path.exists(self.output))
with open(self.output, 'r') as f: with open(self.output, 'r') as f:
content = f.read() content = f.read()
self.assertIn("from slapos.promise.plugin.check_site_available import RunPromise", content) for s in expect:
self.assertEqual(stat.S_IMODE(os.stat(self.output).st_mode), int('644', 8)) self.assertIn(s, content)
expected_dict = dict( def assertConfig(self):
param1=self.options['config-param1'], items = self.options.items()
param2=self.options['config-param2'], expect = {k[7:] : v for k, v in items if k.startswith('config-')}
param3=self.options['config-param3'], self.assertOutput("extra_config_dict = %s" % pformat(expect, indent=2))
param4=self.options['config-param4'],
) def assertEmptyConfig(self):
self.assertIn('extra_config_dict = %s' % pformat(expected_dict, indent=2), content) self.assertOutput("extra_config_dict = %s" % ('{}' if six.PY3 else '{ }'))
def test_no_module_set(self): def test_module(self):
recipe = makeRecipe( self.options['module'] = 'slapos.promise.plugin.check_site_available'
promise_plugin.Recipe, self.installRecipe()
options=self.options, self.assertOutput("from %s import RunPromise" % self.options['module'])
name='plugin') self.assertEmptyConfig()
with self.assertRaises(KeyError):
recipe.install() def test_file(self):
self.options['file'] = __file__
def test_default(self): self.installRecipe()
self.options['import'] = 'slapos.promise.plugin.check_site_available' self.assertOutput("exec(_(%r))" % self.options['file'])
recipe = makeRecipe( self.assertEmptyConfig()
promise_plugin.Recipe,
options=self.options, def test_module_and_parameters(self):
name='plugin') self.options['module'] = 'slapos.promise.plugin.check_site_available'
recipe.install() self.setConfig()
self.installRecipe()
self.assertTrue(os.path.exists(self.output)) self.assertOutput("from %s import RunPromise" % self.options['module'])
self.assertConfig()
def test_file_and_parameters(self):
self.options['file'] = __file__
self.setConfig()
self.installRecipe()
self.assertOutput("exec(_(%r))" % self.options['file'])
self.assertConfig()
def test_mode(self):
self.options['mode'] = '0644'
self.options['module'] = 'slapos.promise.plugin.check_site_available'
self.installRecipe()
self.assertEqual(stat.S_IMODE(os.stat(self.output).st_mode), int('644', 8)) self.assertEqual(stat.S_IMODE(os.stat(self.output).st_mode), int('644', 8))
with open(self.output) as f:
content = f.read()
self.assertIn("from slapos.promise.plugin.check_site_available import RunPromise", content)
self.assertIn('extra_config_dict = %s' % ('{}' if six.PY3 else '{ }'), content)
def test_module_and_class(self):
self.options['module'] = m = 'slapos.promise.plugin.check_site_available'
self.options['class'] = 'MyPromise'
self.installRecipe()
self.assertOutput("from %s import MyPromise as RunPromise" % m)
def test_file_and_class(self):
self.options['file'] = __file__
self.options['class'] = 'MyPromise'
self.installRecipe()
self.assertOutput("exec(_(%r))\n\nRunPromise = MyPromise" % __file__)
def test_no_module_or_file(self):
with self.assertRaises(UserError) as p:
self.makeRecipe()
msg = str(p.exception)
self.assertEqual(msg, "Either 'module' or 'file' is required but not both")
def test_module_and_file(self):
self.options['module'] = 'slapos.promise.plugin.check_site_available'
self.options['file'] = __file__
self.test_no_module_or_file()
def test_bad_parameters(self): def test_bad_parameters(self):
self.options['import'] = 'slapos.promise.plugin.check_site_available' self.options['module'] = 'slapos.promise.plugin.check_site_available'
self.options['config-param1; print "toto"'] = """#xxxx"\nimport os; os.stat(f)""" self.options.update((
self.options['config-param2\n@domething'] = '"#$$*PPP\n\n p = 2*5; print "result is %s" % p' ('config-param1; print "toto"', '#xxxx"\nimport os; os.stat(f)'),
recipe = makeRecipe( ('config-param2\n@domething', '"#$$*PPP\np = 2*5; print "result=%s" % p')
promise_plugin.Recipe, ))
options=self.options, self.installRecipe()
name='plugin') self.assertOutput(
recipe.install() r"""'param1; print "toto"': '#xxxx"\nimport os; os.stat(f)',""",
r"""'param2\n@domething': '"#$$*PPP\np = 2*5; print "result=%s" % p'"""
self.assertTrue(os.path.exists(self.output)) )
with open(self.output) as f:
content = f.read()
expected_param1 = r"""'param1; print "toto"': '#xxxx"\nimport os; os.stat(f)',"""
expected_param2 = r"""'param2\n@domething': '"#$$*PPP\n\n p = 2*5; print "result is %s" % p'"""
self.assertIn(expected_param1, content)
self.assertIn(expected_param2, content)
def test_bad_module_path(self):
self.options['import'] = 'slapos.promise.plugin.check_site_available; print "toto"'
recipe = makeRecipe(
promise_plugin.Recipe,
options=self.options,
name='plugin')
with self.assertRaises(ValueError) as p:
recipe.install()
self.assertEqual(str(p.exception), "Import path %r is not a valid" % self.options['import'])
def test_bad_content(self):
self.options['content'] = 'from slapos.plugin.check_site_available import toto; print "toto"'
recipe = makeRecipe(
promise_plugin.Recipe,
options=self.options,
name='plugin')
with self.assertRaises(ValueError) as p:
recipe.install()
self.assertEqual(str(p.exception), "Promise content %r is not valid" % self.options['content'])
def test_bad_module(self):
self.options['module'] = 'slapos.promise.plugin.check_site_available; print "toto"'
with self.assertRaises(UserError) as p:
self.makeRecipe()
self.assertEqual(str(p.exception), "%r is not a valid module name" % self.options['module'])
def test_bad_file(self):
self.options['file'] = 'print "toto"'
self.installRecipe()
self.assertOutput(r"""exec(_('print "toto"'))""")
def test_bad_class(self):
self.options['class'] = 'MyPromise; print "toto"'
with self.assertRaises(UserError) as p:
self.makeRecipe()
self.assertEqual(str(p.exception), "%r is not a valid class name" % self.options['class'])
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment