PageRenderTime 71ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/test/python/WMCore_t/Configuration_t.py

https://github.com/PerilousApricot/WMCore
Python | 286 lines | 268 code | 10 blank | 8 comment | 0 complexity | b670c8df9778e134fd543cf2d02e0c6b MD5 | raw file
  1. #!/usr/bin/env python
  2. #pylint: disable-msg=E1101,C0103,R0902
  3. import unittest
  4. import os
  5. from WMCore.Configuration import ConfigSection
  6. from WMCore.Configuration import Configuration
  7. from WMCore.Configuration import loadConfigurationFile
  8. from WMCore.Configuration import saveConfigurationFile
  9. from WMQuality.TestInitCouchApp import TestInitCouchApp as TestInit
  10. class ConfigurationTest(unittest.TestCase):
  11. """
  12. test case for Configuration object
  13. """
  14. def setUp(self):
  15. """set up"""
  16. self.testInit = TestInit(__file__)
  17. self.testDir = self.testInit.generateWorkDir()
  18. self.normalSave = "%s/WMCore_Agent_Configuration_t_normal.py" % self.testDir
  19. self.docSave = "%s/WMCore_Agent_Configuration_t_documented.py" % self.testDir
  20. self.commentSave = "%s/WMCore_Agent_Configuration_t_commented.py" % self.testDir
  21. def tearDown(self):
  22. """clean up"""
  23. self.testInit.delWorkDir()
  24. def testA(self):
  25. """ctor"""
  26. try:
  27. config = Configuration()
  28. except Exception, ex:
  29. msg = "Failed to instantiate Configuration\n"
  30. msg += str(ex)
  31. self.fail(msg)
  32. def testB(self):
  33. """add settings"""
  34. config = Configuration()
  35. config.section_("Section1")
  36. section1 = getattr(config, "Section1", None)
  37. self.failUnless(section1 != None)
  38. config.section_("Section2")
  39. section2 = getattr(config, "Section2", None)
  40. self.failUnless(section2 != None)
  41. self.assertRaises(AttributeError, getattr, config, "Section3")
  42. # basic types
  43. config.Section1.Parameter1 = True
  44. config.Section1.Parameter2 = "string"
  45. config.Section1.Parameter3 = 123
  46. config.Section1.Parameter4 = 123.456
  47. self.assertEqual(config.Section1.Parameter1, True)
  48. self.assertEqual(config.Section1.Parameter2, "string")
  49. self.assertEqual(config.Section1.Parameter3, 123)
  50. self.assertEqual(config.Section1.Parameter4, 123.456)
  51. # dictionary format:
  52. try:
  53. section1Dict = config.Section1.dictionary_()
  54. except Exception, ex:
  55. msg = "Error converting section to dictionary:\n"
  56. msg += "%s\n" % str(ex)
  57. self.fail(msg)
  58. self.failUnless( section1Dict.has_key("Parameter1"))
  59. self.failUnless( section1Dict.has_key("Parameter2"))
  60. self.failUnless( section1Dict.has_key("Parameter3"))
  61. self.failUnless( section1Dict.has_key("Parameter4"))
  62. self.assertEqual(section1Dict['Parameter1'],
  63. config.Section1.Parameter1)
  64. self.assertEqual(section1Dict['Parameter2'],
  65. config.Section1.Parameter2)
  66. self.assertEqual(section1Dict['Parameter3'],
  67. config.Section1.Parameter3)
  68. self.assertEqual(section1Dict['Parameter4'],
  69. config.Section1.Parameter4)
  70. # compound types
  71. config.Section2.List = ["string", 123, 123.456, False]
  72. config.Section2.Dictionary = { "string" : "string",
  73. "int" : 123,
  74. "float" : 123.456,
  75. "bool" : False}
  76. config.Section2.Tuple = ("string", 123, 123.456, False)
  77. self.assertEqual(config.Section2.List,
  78. ["string", 123, 123.456, False])
  79. self.assertEqual(config.Section2.Tuple,
  80. ("string", 123, 123.456, False))
  81. class DummyObject:
  82. pass
  83. # unsupported parameter type
  84. self.assertRaises(
  85. RuntimeError, setattr,
  86. config.Section2, "BadObject", DummyObject())
  87. # unsupported data type in compound type
  88. badList = [ DummyObject(), DummyObject()]
  89. self.assertRaises(
  90. RuntimeError, setattr,
  91. config.Section2, "BadList", badList)
  92. badDict = { "dict" : {}, "list": [], "tuple" : () }
  93. self.assertRaises(
  94. RuntimeError, setattr,
  95. config.Section2, "BadDict", badDict)
  96. def testC(self):
  97. """add components"""
  98. config = Configuration()
  99. config.component_("Component1")
  100. config.component_("Component2")
  101. config.component_("Component3")
  102. comp1 = getattr(config, "Component1", None)
  103. self.failUnless(comp1 != None)
  104. comp2 = getattr(config, "Component2", None)
  105. self.failUnless(comp2 != None)
  106. def testD(self):
  107. """test documentation"""
  108. config = Configuration()
  109. config.section_("Section1")
  110. config.Section1.Parameter1 = True
  111. config.Section1.Parameter2 = "string"
  112. config.Section1.Parameter3 = 123
  113. config.Section1.Parameter4 = 123.456
  114. config.Section1.Parameter5 = {"test1" : "test2", "test3" : 123}
  115. config.Section1.document_("""This is Section1""")
  116. config.Section1.document_("""This is Section1.Parameter1""",
  117. "Parameter1")
  118. config.Section1.document_("""This is Section1.Parameter2""",
  119. "Parameter2")
  120. config.Section1.document_("""This is Section1.Parameter3\n with multiline comments""",
  121. "Parameter3")
  122. try:
  123. config.Section1.documentedString_()
  124. except Exception, ex:
  125. msg = "Error calling ConfigSection.documentedString_:\n"
  126. msg += "%s\n" % str(ex)
  127. self.fail(msg)
  128. try:
  129. config.Section1.commentedString_()
  130. except Exception, ex:
  131. msg = "Error calling ConfigSection.commentedString_:\n"
  132. msg += "%s\n" % str(ex)
  133. self.fail(msg)
  134. try:
  135. config.documentedString_()
  136. except Exception, ex:
  137. msg = "Error calling Configuration.documentedString_:\n"
  138. msg += "%s\n" % str(ex)
  139. self.fail(msg)
  140. try:
  141. config.commentedString_()
  142. except Exception, ex:
  143. msg = "Error calling Configuration.commentedString_:\n"
  144. msg += "%s\n" % str(ex)
  145. self.fail(msg)
  146. def testE(self):
  147. """test save/load """
  148. testValues = [
  149. "string", 123, 123.456,
  150. ["list", 789, 10.1 ],
  151. { "dict1" : "value", "dict2" : 10.0 }
  152. ]
  153. config = Configuration()
  154. for x in range(0, 5):
  155. config.section_("Section%s" % x)
  156. config.component_("Component%s" % x)
  157. sect = getattr(config, "Section%s" % x)
  158. comp = getattr(config, "Component%s" % x)
  159. sect.document_("This is Section%s" % x)
  160. comp.document_("This is Component%s" % x)
  161. for i in range(0, 5):
  162. setattr(comp, "Parameter%s" % i, testValues[i])
  163. setattr(sect, "Parameter%s" % i, testValues[i])
  164. comp.document_("This is Parameter%s" % i,
  165. "Parameter%s" %i)
  166. sect.document_("This is Parameter%s" %i,
  167. "Parameter%s" %i)
  168. stringSave = str(config)
  169. documentSave = config.documentedString_()
  170. commentSave = config.commentedString_()
  171. saveConfigurationFile(config, self.normalSave)
  172. saveConfigurationFile(config, self.docSave, document = True)
  173. saveConfigurationFile(config, self.commentSave, comment = True)
  174. plainConfig = loadConfigurationFile(self.normalSave)
  175. docConfig = loadConfigurationFile(self.docSave)
  176. commentConfig = loadConfigurationFile(self.commentSave)
  177. #print commentConfig.commentedString_()
  178. #print docConfig.documentedString_()
  179. #print docConfig.commentedString_()
  180. def testF(self):
  181. """
  182. Test internal functions pythonise_, listSections_
  183. """
  184. config = ConfigSection("config")
  185. config.section_("SectionA")
  186. config.section_("SectionB")
  187. config.SectionA.section_("Section1")
  188. config.SectionA.section_("Section2")
  189. config.SectionA.Section1.x = 100
  190. config.SectionA.Section1.y = 100
  191. pythonise = config.pythonise_()
  192. assert "config.section_('SectionA')" in pythonise, "Pythonise failed: Could not find SectionA"
  193. assert "config.SectionA.Section1.x = 100" in pythonise, "Pythonise failed: Could not find x"
  194. pythonise = config.SectionA.pythonise_()
  195. assert "SectionA.section_('Section1')" in pythonise, "Pythonise failed: Could not find Section1"
  196. assert "SectionA.Section1.x = 100" in pythonise, "Pythonise failed: Could not find x"
  197. self.assertEqual(config.listSections_(), ['SectionB', 'SectionA'])
  198. self.assertEqual(config.SectionA.listSections_(), ['Section2', 'Section1'])
  199. def testG_testStaticReferenceToConfigurationInstance(self):
  200. """
  201. test Configuration.getInstance() which returns reference
  202. to the Configuration object instance.
  203. """
  204. config = Configuration()
  205. instance = Configuration.getInstance()
  206. self.assertFalse(hasattr(instance, "testsection"))
  207. config.section_("testsection")
  208. self.assertTrue(hasattr(instance, "testsection"))
  209. config.testsection.var = 10
  210. self.assertEquals(instance.testsection.var, 10)
  211. def testH_ConfigSectionDictionariseInternalChildren(self):
  212. """
  213. The test checks if any item of the dictionary_whole_tree_()
  214. result is not unexpanded instance of ConfigSection.
  215. """
  216. config = ConfigSection("config")
  217. config.value1 = "MyValue1"
  218. config.section_("Task1")
  219. config.Task1.value2 = "MyValue2"
  220. config.Task1.section_("subSection")
  221. config.Task1.subSection.value3 = "MyValue3"
  222. d = config.dictionary_whole_tree_()
  223. for values in d.values():
  224. self.assertFalse(isinstance(values, ConfigSection))
  225. self.assertEqual(d["Task1"]["subSection"]["value3"], "MyValue3")
  226. if __name__ == '__main__':
  227. unittest.main()