Start to test all json schema and files.
[slapos.git] / slapos / recipe / test / test_json_schema.py
1 ##############################################################################
2 #
3 # Copyright (c) 2010 Vifib SARL and Contributors. All Rights Reserved.
4 #
5 # WARNING: This program as such is intended to be used by professional
6 # programmers who take the whole responsibility of assessing all potential
7 # consequences resulting from its eventual inadequacies and bugs
8 # End users who are looking for a ready-to-use solution with commercial
9 # guarantees and support are strongly adviced to contract a Free Software
10 # Service Company
11 #
12 # This program is Free Software; you can redistribute it and/or
13 # modify it under the terms of the GNU General Public License
14 # as published by the Free Software Foundation; either version 3
15 # of the License, or (at your option) any later version.
16 #
17 # This program is distributed in the hope that it will be useful,
18 # but WITHOUT ANY WARRANTY; without even the implied warranty of
19 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 # GNU General Public License for more details.
21 #
22 # You should have received a copy of the GNU General Public License
23 # along with this program; if not, write to the Free Software
24 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25 #
26 ##############################################################################
27
28 import unittest
29 import os
30 import glob
31 import json
32 import slapos.recipe.test
33 import jsonschema
34
35
36 def getSchemaValidator(filename):
37 schema_json_file = "/".join(slapos.recipe.test.__file__.split("/")[:-1])
38 schema_json_file += "/%s" % filename
39 with open(schema_json_file, "r") as json_file:
40 json_dict = json.loads(json_file.read())
41 json_file.close()
42 return json_dict
43
44 def createTest(path, json_dict):
45 def run(self, *args, **kwargs):
46 with open(path, "r") as json_file:
47 self.assertEquals(jsonschema.validate(json.loads(json_file.read()), json_dict), None)
48 json_file.close()
49
50 return run
51
52 def generateSoftwareCfgTest():
53 json_dict = getSchemaValidator("schema.json")
54 base_path = "/".join(slapos.recipe.test.__file__.split("/")[:-4])
55 for path in glob.glob("%s/software/*/software.cfg.json" % base_path):
56 test_name = "test_%s_software_cfg_json" % path.split("/")[-2]
57 setattr(TestJSONSchemaValidation, test_name , createTest(path, json_dict))
58
59
60 def generateJSONSchemaTest():
61 json_dict = getSchemaValidator("metaschema.json")
62 base_path = "/".join(slapos.recipe.test.__file__.split("/")[:-4])
63 for path in glob.glob("%s/software/*/*schema.json" % base_path):
64 software_type = path.split("/")[-2]
65 filename = path.split("/")[-1].replace("-", "_").replace(".", "_")
66 test_name = "test_schema_%s_%s" % (software_type, filename)
67 setattr(TestJSONSchemaValidation, test_name , createTest(path, json_dict))
68
69 class TestJSONSchemaValidation(unittest.TestCase):
70 pass
71
72 generateSoftwareCfgTest()
73 generateJSONSchemaTest()
74
75 if __name__ == '__main__':
76 unittest.main()