/test/python/WMCore_t/Cache_t/WMConfigCache_t.py

https://github.com/PerilousApricot/WMCore · Python · 213 lines · 124 code · 37 blank · 52 comment · 3 complexity · ec36d124605e23bb1d73927e577aae39 MD5 · raw file

  1. #!/usr/bin/env python
  2. """
  3. _WMConfigCache_t_
  4. Test class for the WMConfigCache
  5. """
  6. import os
  7. import unittest
  8. import tempfile
  9. import subprocess
  10. from WMCore.Agent.Configuration import Configuration
  11. from WMCore.Cache.WMConfigCache import ConfigCache, ConfigCacheException
  12. from WMCore.WMBase import getTestBase
  13. from WMQuality.TestInitCouchApp import TestInitCouchApp
  14. class testWMConfigCache(unittest.TestCase):
  15. """
  16. _testWMConfigCache_
  17. Basic test class for configCache
  18. """
  19. def setUp(self):
  20. """
  21. _setUp_
  22. """
  23. self.testInit = TestInitCouchApp(__file__)
  24. self.testInit.setLogging()
  25. self.testInit.setupCouch("config_test", "GroupUser", "ConfigCache")
  26. self.testDir = self.testInit.generateWorkDir()
  27. return
  28. def tearDown(self):
  29. """
  30. _tearDown_
  31. Clear out the database.
  32. """
  33. self.testInit.delWorkDir()
  34. self.testInit.tearDownCouch()
  35. return
  36. def testA_basicConfig(self):
  37. """
  38. _basicConfig_
  39. Basic configCache stuff.
  40. """
  41. PSetTweak = "Hello, I am a PSetTweak. It's nice to meet you."
  42. configCache = ConfigCache(os.environ["COUCHURL"], couchDBName = 'config_test')
  43. configCache.createUserGroup(groupname = "testGroup", username = 'testOps')
  44. configCache.setPSetTweaks(PSetTweak = PSetTweak)
  45. configCache.save()
  46. configCache2 = ConfigCache(os.environ["COUCHURL"], couchDBName = 'config_test',
  47. id = configCache.getCouchID(),
  48. rev = configCache.getCouchRev())
  49. configCache2.loadByID(configCache.getCouchID())
  50. self.assertEqual(configCache2.getPSetTweaks(), PSetTweak)
  51. configCache2.delete()
  52. configCache3 = ConfigCache(os.environ["COUCHURL"], couchDBName = 'config_test',
  53. id = configCache.getCouchID(),
  54. rev = configCache.getCouchRev())
  55. testFlag = False
  56. # It should fail to load deleted documents
  57. try:
  58. configCache3.loadByID(configCache.getCouchID())
  59. except ConfigCacheException:
  60. testFlag = True
  61. self.assertTrue(testFlag)
  62. return
  63. def testB_addingConfigsAndTweaks(self):
  64. """
  65. _addingConfigsAndTweaks_
  66. Test adding config files and tweak files
  67. """
  68. PSetTweak = "Hello, I am a PSetTweak. It's nice to meet you."
  69. attach = "Hello, I am an attachment"
  70. configCache = ConfigCache(os.environ["COUCHURL"], couchDBName = 'config_test')
  71. configCache.createUserGroup(groupname = "testGroup", username = 'testOps')
  72. configCache.setPSetTweaks(PSetTweak = PSetTweak)
  73. configCache.attachments['attach1'] = attach
  74. psetPath = os.path.join(getTestBase(), "WMCore_t/Cache_t/PSet.txt")
  75. configCache.addConfig(newConfig = psetPath, psetHash = None)
  76. configCache.setLabel("sample-label")
  77. configCache.setDescription("describe this config here")
  78. configCache.save()
  79. configString1 = configCache.getConfig()
  80. configCache2 = ConfigCache(os.environ["COUCHURL"], couchDBName = 'config_test',
  81. id = configCache.getCouchID(),
  82. rev = configCache.getCouchRev())
  83. configCache2.loadByID(configCache.getCouchID())
  84. configString2 = configCache2.getConfig()
  85. self.assertEqual(configString1, configString2)
  86. self.assertEqual(configCache2.attachments.get('attach1', None), attach)
  87. configCache.delete()
  88. return
  89. def testC_testViews(self):
  90. """
  91. _testViews_
  92. Prototype test for what should be a lot of other tests.
  93. """
  94. PSetTweak = "Hello, I am a PSetTweak. It's nice to meet you."
  95. attach = "Hello, I am an attachment"
  96. configCache = ConfigCache(os.environ["COUCHURL"], couchDBName = 'config_test')
  97. configCache.createUserGroup(groupname = "testGroup", username = 'testOps')
  98. configCache.setPSetTweaks(PSetTweak = PSetTweak)
  99. configCache.attachments['attach1'] = attach
  100. configCache.document['md5_hash'] = "somemd5"
  101. psetPath = os.path.join(getTestBase(), "WMCore_t/Cache_t/PSet.txt")
  102. configCache.addConfig(newConfig = psetPath, psetHash = None)
  103. configCache.save()
  104. configCache2 = ConfigCache(os.environ["COUCHURL"], couchDBName = 'config_test')
  105. configCache2.document['md5_hash'] = configCache.document['md5_hash']
  106. configCache2.load()
  107. self.assertEqual(configCache2.attachments.get('attach1', None), attach)
  108. configCache2.delete()
  109. return
  110. def testD_LoadConfigCache(self):
  111. """
  112. _LoadConfigCache_
  113. Actually load the config cache using plain .load()
  114. Tests to make sure that if we pass in an id field it gets used to load configs
  115. """
  116. configCache = ConfigCache(os.environ["COUCHURL"], couchDBName = 'config_test')
  117. configCache.createUserGroup(groupname = "testGroup", username = 'testOps')
  118. configCache.setLabel("labelA")
  119. configCache.save()
  120. configCache2 = ConfigCache(os.environ["COUCHURL"], couchDBName = 'config_test',
  121. id = configCache.getCouchID(),
  122. rev = configCache.getCouchRev())
  123. configCache2.load()
  124. self.assertEqual(configCache2.document['owner'],
  125. {'group': 'testGroup', 'user': 'testOps'})
  126. self.assertEqual(configCache2.document['description'],
  127. {'config_desc': None, 'config_label': 'labelA'})
  128. return
  129. def testE_SaveConfigFileToDisk(self):
  130. """
  131. _SaveConfigFileToDisk_
  132. Check and see if we can save the config file attachment to disk
  133. """
  134. targetFile = os.path.join(self.testDir, 'configCache.test')
  135. configCache = ConfigCache(os.environ["COUCHURL"], couchDBName = 'config_test')
  136. configCache.createUserGroup(groupname = "testGroup", username = 'testOps')
  137. configCache.attachments['configFile'] = 'ThisIsAConfigFile'
  138. configCache.saveConfigToDisk(targetFile = targetFile)
  139. f = open(targetFile, 'r')
  140. content = f.read()
  141. f.close()
  142. self.assertEqual(content, configCache.getConfig())
  143. return
  144. def testListAllConfigs(self):
  145. """
  146. _testListAllConfigs_
  147. Verify that the list all configs method works correctly.
  148. """
  149. configCacheA = ConfigCache(os.environ["COUCHURL"], couchDBName = 'config_test')
  150. configCacheA.createUserGroup(groupname = "testGroup", username = 'testOps')
  151. configCacheA.setLabel("labelA")
  152. configCacheA.save()
  153. configCacheB = ConfigCache(os.environ["COUCHURL"], couchDBName = 'config_test')
  154. configCacheB.createUserGroup(groupname = "testGroup", username = 'testOps')
  155. configCacheB.setLabel("labelB")
  156. configCacheB.save()
  157. configs = configCacheA.listAllConfigsByLabel()
  158. self.assertEqual(len(configs.keys()), 2,
  159. "Error: There should be two configs")
  160. self.assertEqual(configs["labelA"], configCacheA.getCouchID(),
  161. "Error: Label A is wrong.")
  162. self.assertEqual(configs["labelB"], configCacheB.getCouchID(),
  163. "Error: Label B is wrong.")
  164. return
  165. if __name__ == "__main__":
  166. unittest.main()