PageRenderTime 93ms CodeModel.GetById 5ms RepoModel.GetById 0ms app.codeStats 1ms

/test/python/WMCore_t/Services_t/WMStats_t/DataStructs_t/RequestInfoCollection_t.py

https://github.com/dmwm/WMCore
Python | 190 lines | 124 code | 34 blank | 32 comment | 5 complexity | 20399c10c68e13dbb34a013162618f8c MD5 | raw file
  1. #!/usr/bin/env python
  2. """
  3. Unittests for the RequestInfoCollection WMStats module
  4. """
  5. from __future__ import division, print_function
  6. import unittest
  7. from future.utils import viewvalues
  8. from Utils.PythonVersion import PY3
  9. from WMCore.Services.WMStats.DataStruct.RequestInfoCollection import (JobSummary, ProgressSummary,
  10. TaskInfo, RequestInfo)
  11. class DummyTask(object):
  12. """
  13. Dummy object carrying some needed attributes for TaskInfo
  14. """
  15. def __init__(self, reqName, taskName, taskType):
  16. self.requestName = reqName
  17. self.taskName = taskName
  18. self.taskType = taskType
  19. self.jobSummary = {}
  20. def setJobSummary(self, jobSum):
  21. self.jobSummary = jobSum
  22. class MyTestCase(unittest.TestCase):
  23. def setUp(self):
  24. if PY3:
  25. self.assertItemsEqual = self.assertCountEqual
  26. def testJobSummary(self):
  27. """some very basic unit tests for the JobSummary class"""
  28. jobSumKeys = ["success", "canceled", "transition", "queued",
  29. "submitted", "failure", "cooloff", "paused"]
  30. jsonStatusKeys = ["success", "failure", "cooloff", "running",
  31. "queued", "pending", "paused", "created"]
  32. # default object
  33. jobSumObj = JobSummary()
  34. self.assertItemsEqual(list(jobSumObj.jobStatus), jobSumKeys)
  35. self.assertItemsEqual(list(jobSumObj.getJSONStatus()), jsonStatusKeys)
  36. self.assertEqual(jobSumObj.getTotalJobs(), 0)
  37. self.assertEqual(sum(viewvalues(jobSumObj.getJSONStatus())), 0)
  38. # update with no content, thus, do nothing!
  39. jobSumObj.addJobStatusInfo({})
  40. self.assertItemsEqual(list(jobSumObj.jobStatus), jobSumKeys)
  41. self.assertItemsEqual(list(jobSumObj.getJSONStatus()), jsonStatusKeys)
  42. self.assertEqual(jobSumObj.getTotalJobs(), 0)
  43. self.assertEqual(sum(viewvalues(jobSumObj.getJSONStatus())), 0)
  44. # passing an invalid key/status
  45. jobSumObj.addJobStatusInfo({"bad_status_key": 10})
  46. self.assertItemsEqual(list(jobSumObj.jobStatus), jobSumKeys)
  47. self.assertEqual(jobSumObj.getTotalJobs(), 0)
  48. # updating a simple integer status
  49. jobSumObj.addJobStatusInfo({"success": 10})
  50. self.assertItemsEqual(list(jobSumObj.jobStatus), jobSumKeys)
  51. self.assertEqual(jobSumObj.getTotalJobs(), 10)
  52. self.assertEqual(jobSumObj.getSuccess(), 10)
  53. # getJSONStatus considers success jobs as success and as created,
  54. # thus double counting
  55. self.assertEqual(sum(viewvalues(jobSumObj.getJSONStatus())), 20)
  56. # updating a dictionary status
  57. jobSumObj.addJobStatusInfo({"submitted": {"pending": 2, "running": 4}})
  58. self.assertItemsEqual(list(jobSumObj.jobStatus), jobSumKeys)
  59. self.assertItemsEqual(list(jobSumObj.getJSONStatus()), jsonStatusKeys)
  60. self.assertEqual(jobSumObj.getTotalJobs(), 16)
  61. self.assertEqual(jobSumObj.getSuccess(), 10)
  62. # Submitted only considers first/retry states
  63. self.assertEqual(jobSumObj.getSubmitted(), 0)
  64. self.assertEqual(jobSumObj.getPending(), 2)
  65. self.assertEqual(jobSumObj.getRunning(), 4)
  66. self.assertEqual(sum(viewvalues(jobSumObj.getJSONStatus())), 32)
  67. def testProgressSummary(self):
  68. """some very basic unit tests for the ProgressSummary class"""
  69. progSumKeys = ["totalLumis", "events", "size"]
  70. # default object
  71. progSumObj = ProgressSummary()
  72. self.assertItemsEqual(list(progSumObj.getReport()), progSumKeys)
  73. self.assertEqual(sum(viewvalues(progSumObj.getReport())), 0)
  74. # update with no content, thus, do nothing!
  75. progSumObj.addProgressReport({})
  76. self.assertItemsEqual(list(progSumObj.getReport()), progSumKeys)
  77. self.assertEqual(sum(viewvalues(progSumObj.getReport())), 0)
  78. # passing an invalid key/status
  79. progSumObj.addProgressReport({"bad_status_key": 10})
  80. self.assertItemsEqual(list(progSumObj.getReport()), progSumKeys)
  81. self.assertEqual(sum(viewvalues(progSumObj.getReport())), 0)
  82. # now passing some valid information
  83. progSumObj.addProgressReport({"totalLumis": 10, "events": 1000})
  84. self.assertItemsEqual(list(progSumObj.getReport()), progSumKeys)
  85. self.assertEqual(sum(viewvalues(progSumObj.getReport())), 1010)
  86. def testTaskInfo(self):
  87. """some very basic unit tests for the TaskInfo class"""
  88. reqName = "test_request_name"
  89. taskName = "test_task_name"
  90. # default object
  91. taskSumObj = TaskInfo(reqName, taskName, {})
  92. self.assertEqual(taskSumObj.getRequestName(), reqName)
  93. self.assertEqual(taskSumObj.getTaskName(), taskName)
  94. self.assertEqual(taskSumObj.getTaskType(), "N/A")
  95. self.assertIsInstance(taskSumObj.getJobSummary(), JobSummary)
  96. self.assertFalse(taskSumObj.isTaskCompleted())
  97. # try to add an invalid task info object
  98. with self.assertRaises(Exception):
  99. taskSumObj.addTaskInfo("blah")
  100. # and again, with valid attributes but invalid values
  101. with self.assertRaises(Exception):
  102. dummyTask = DummyTask("req", "task", "type")
  103. taskSumObj.addTaskInfo(dummyTask)
  104. # now with all valid, but invalid JobSummary object
  105. dummyTask = DummyTask(reqName, taskName, "dummy_task_type")
  106. with self.assertRaises(AttributeError):
  107. taskSumObj.addTaskInfo(dummyTask)
  108. # now update it with some valid content
  109. dummyTask.setJobSummary(JobSummary())
  110. taskSumObj.addTaskInfo(dummyTask)
  111. self.assertFalse(taskSumObj.isTaskCompleted())
  112. # now complete this task
  113. # now update it with some valid content
  114. dummyTask.setJobSummary(JobSummary({"success": 10}))
  115. taskSumObj.addTaskInfo(dummyTask)
  116. self.assertTrue(taskSumObj.isTaskCompleted())
  117. def testRequestInfo(self):
  118. """some very basic unit tests for the RequestInfo class"""
  119. reqName = "test_request_name"
  120. taskName = "test_task_name"
  121. agentName = "test_agent_name"
  122. # default object
  123. defaultDict = {"RequestName": reqName,
  124. "AgentJobInfo": {}}
  125. reqInfoObj = RequestInfo(defaultDict)
  126. self.assertItemsEqual(reqInfoObj.getTasks(), {})
  127. self.assertEqual(reqInfoObj.getTotalTopLevelJobs(), "N/A")
  128. self.assertEqual(reqInfoObj.getTotalInputLumis(), "N/A")
  129. self.assertEqual(reqInfoObj.getTotalInputEvents(), "N/A")
  130. self.assertEqual(reqInfoObj.getTotalTopLevelJobsInWMBS(), 0)
  131. self.assertItemsEqual(reqInfoObj.getJobSummaryByAgent(), {})
  132. self.assertItemsEqual(reqInfoObj.getTasksByAgent(), {})
  133. self.assertFalse(reqInfoObj.isWorkflowFinished())
  134. # with some useful content now
  135. defaultDict = {"RequestName": reqName,
  136. "total_jobs": 10,
  137. "input_lumis": 100,
  138. "input_events": 1000,
  139. "AgentJobInfo": {agentName: {"status": {"success": 10, "inWMBS": 10},
  140. "tasks": {taskName: {"status": {"success": 10}}}}}}
  141. reqInfoObj = RequestInfo(defaultDict)
  142. self.assertItemsEqual(list(reqInfoObj.getTasks()), [taskName])
  143. self.assertEqual(reqInfoObj.getTotalTopLevelJobs(), 10)
  144. self.assertEqual(reqInfoObj.getTotalInputLumis(), 100)
  145. self.assertEqual(reqInfoObj.getTotalInputEvents(), 1000)
  146. self.assertEqual(reqInfoObj.getTotalTopLevelJobsInWMBS(), 10)
  147. self.assertIsInstance(reqInfoObj.getJobSummaryByAgent(), dict)
  148. self.assertTrue(agentName in reqInfoObj.getJobSummaryByAgent())
  149. self.assertIsInstance(reqInfoObj.getJobSummaryByAgent(agentName), JobSummary)
  150. self.assertIsInstance(reqInfoObj.getTasksByAgent(), dict)
  151. self.assertTrue(agentName in reqInfoObj.getTasksByAgent())
  152. self.assertIsInstance(reqInfoObj.getTasksByAgent(agentName), dict)
  153. self.assertTrue(taskName in reqInfoObj.getTasksByAgent(agentName))
  154. self.assertTrue(reqInfoObj.isWorkflowFinished())
  155. if __name__ == '__main__':
  156. unittest.main()