Commit d99e2549 authored by Cédric de Saint Martin's avatar Cédric de Saint Martin

Add tests for recipe.

Move dcron test in dedicated directory/file.
Also move slapos/recipe/test to slapos/test.
Currently : only dcron test.
parent d8b2230f
......@@ -216,7 +216,7 @@ setup(name=name,
'kumo = slapos.recipe.nosqltestbed.kumo:KumoTestBed',
],
},
test_suite='slapos.recipe.test',
test_suite='slapos.test',
test_requires=[ 'jsonschema' ],
)
......@@ -26,11 +26,8 @@
##############################################################################
import os
if __name__ == '__main__': # Hack to easily run test below.
GenericBaseRecipe = object
else:
from slapos.recipe.librecipe import GenericBaseRecipe
from zc.buildout import UserError
from slapos.recipe.librecipe import GenericBaseRecipe
from zc.buildout import UserError
class Recipe(GenericBaseRecipe):
......@@ -131,41 +128,3 @@ def systemd_to_cron(spec):
raise ValueError
return ' '.join(spec)
def test(self):
def _(systemd, cron):
self.assertEqual(systemd_to_cron(systemd), cron)
_("Sat,Mon-Thu,Sun", "0 0 * * 0,1-4,6")
_("mon,sun *-* 2,1:23", "23 2,1 * * 0,1")
_("Wed, 17:48", "48 17 * * 3")
_("Wed-Sat,Tue 10-* 1:2", "2 1 * 10 2,3-6")
_("*-*-7 0:0:0", "0 0 7 * *")
_("10-15", "0 0 15 10 *")
_("monday *-12-* 17:00", "00 17 * 12 1")
_("12,14,13,12:20,10,30", "20,10,30 12,14,13,12 * * *") # TODO: sort
_("*-1/2-1,3 *:30", "30 * 1,3 1/2 *")
_("03-05 08:05", "05 08 05 03 *")
_("08:05:00", "05 08 * * *")
_("05:40", "40 05 * * *")
_("Sat,Sun 12-* 08:05", "05 08 * 12 0,6")
_("Sat,Sun 08:05", "05 08 * * 0,6")
def _(systemd):
self.assertRaises(Exception, systemd_to_cron, systemd)
_("test")
_("")
_("7")
_("121212:1:2")
_("Wed *-1")
_("08:05:40")
_("2003-03-05")
_("0-1"); _("13-1"); _("6/4-1"); _("5/8-1")
_("1-0"); _("1-32"); _("1-4/3"); _("1-14/18")
_("24:0");_("9/9:0"); _("8/16:0")
_("0:60"); _("0:22/22"); _("0:15/45")
if __name__ == '__main__':
import unittest
unittest.TextTestRunner().run(type('', (unittest.TestCase,), {
'runTest': test})())
import unittest
from slapos.recipe.dcron import systemd_to_cron
class TestDcron(unittest.TestCase):
def test(self):
def _(systemd, cron):
self.assertEqual(systemd_to_cron(systemd), cron)
_("Sat,Mon-Thu,Sun", "0 0 * * 0,1-4,6")
_("mon,sun *-* 2,1:23", "23 2,1 * * 0,1")
_("Wed, 17:48", "48 17 * * 3")
_("Wed-Sat,Tue 10-* 1:2", "2 1 * 10 2,3-6")
_("*-*-7 0:0:0", "0 0 7 * *")
_("10-15", "0 0 15 10 *")
_("monday *-12-* 17:00", "00 17 * 12 1")
_("12,14,13,12:20,10,30", "20,10,30 12,14,13,12 * * *") # TODO: sort
_("*-1/2-1,3 *:30", "30 * 1,3 1/2 *")
_("03-05 08:05", "05 08 05 03 *")
_("08:05:00", "05 08 * * *")
_("05:40", "40 05 * * *")
_("Sat,Sun 12-* 08:05", "05 08 * 12 0,6")
_("Sat,Sun 08:05", "05 08 * * 0,6")
def _(systemd):
self.assertRaises(Exception, systemd_to_cron, systemd)
_("test")
_("")
_("7")
_("121212:1:2")
_("Wed *-1")
_("08:05:40")
_("2003-03-05")
_("0-1"); _("13-1"); _("6/4-1"); _("5/8-1")
_("1-0"); _("1-32"); _("1-4/3"); _("1-14/18")
_("24:0");_("9/9:0"); _("8/16:0")
_("0:60"); _("0:22/22"); _("0:15/45")
......@@ -29,12 +29,12 @@ import unittest
import os
import glob
import json
import slapos.recipe.test
import slapos.test
import jsonschema
def getSchemaValidator(filename):
schema_json_file = "/".join(slapos.recipe.test.__file__.split("/")[:-1])
schema_json_file = "/".join(slapos.test.__file__.split("/")[:-1])
schema_json_file += "/%s" % filename
with open(schema_json_file, "r") as json_file:
json_dict = json.loads(json_file.read())
......@@ -51,7 +51,7 @@ def createTest(path, json_dict):
def generateSoftwareCfgTest():
json_dict = getSchemaValidator("schema.json")
base_path = "/".join(slapos.recipe.test.__file__.split("/")[:-4])
base_path = "/".join(slapos.test.__file__.split("/")[:-3])
for path in glob.glob("%s/software/*/software.cfg.json" % base_path):
test_name = "test_%s_software_cfg_json" % path.split("/")[-2]
setattr(TestJSONSchemaValidation, test_name , createTest(path, json_dict))
......@@ -59,7 +59,7 @@ def generateSoftwareCfgTest():
def generateJSONSchemaTest():
json_dict = getSchemaValidator("metaschema.json")
base_path = "/".join(slapos.recipe.test.__file__.split("/")[:-4])
base_path = "/".join(slapos.test.__file__.split("/")[:-3])
for path in glob.glob("%s/software/*/*schema.json" % base_path):
software_type = path.split("/")[-2]
filename = path.split("/")[-1].replace("-", "_").replace(".", "_")
......
import os
import sys
import unittest
def additional_tests():
  • @luke @rafael @alain.takoudjou I noticed some tests are running twice ( for example this slow test_generateCertificates ).

    I believe it's because of this function. Do you know more about this ?

  • at least with python setup.py test they are run twice. Maybe this depends on the testrunner used (py.test or nose are different for this I think). Maybe what we need is to put a __init__.py in test/recipe ?

Please register or sign in to reply
"""
Load all recipe tests. test file must be called or finished by "test_recipe.py".
"""
setup_file = sys.modules['__main__'].__file__
setup_directory = os.path.abspath(os.path.dirname(setup_file))
recipe_directory = os.path.join(setup_directory, 'slapos', 'test', 'recipe')
return unittest.defaultTestLoader.discover(recipe_directory)
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