You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

test_validator.py 8.4 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. # Copyright 2020 Huawei Technologies Co., Ltd
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. # ============================================================================
  15. """Test the validator module."""
  16. import pytest
  17. from mindinsight.profiler.common.exceptions.exceptions import \
  18. ProfilerParamTypeErrorException, ProfilerGroupConditionException, \
  19. ProfilerFilterConditionException, ProfilerOpTypeException
  20. from mindinsight.profiler.common.validator.validate import \
  21. validate_minddata_pipeline_condition, validate_condition, validate_and_set_job_id_env
  22. class TestValidate:
  23. """Test the function of validate."""
  24. @pytest.mark.level0
  25. @pytest.mark.env_single
  26. @pytest.mark.platform_x86_cpu
  27. @pytest.mark.platform_arm_ascend_training
  28. @pytest.mark.platform_x86_gpu_training
  29. @pytest.mark.platform_x86_ascend_training
  30. def test_validate_condition_normal(self):
  31. """Test the validate condition of normal input."""
  32. op_type_list = ['aicpu_type', 'aicpu_detail', 'aicore_type', 'aicore_detail',
  33. 'gpu_op_type', 'gpu_op_info', 'gpu_cuda_activity']
  34. sort_name_list = ['op_type', 'serial_number', 'op_type', 'op_name',
  35. 'op_type', 'op_side', 'name']
  36. for idx, op_type in enumerate(op_type_list):
  37. condition = {
  38. 'device_id': '0',
  39. 'op_type': op_type,
  40. 'filter_condition': {
  41. 'op_id': 0
  42. },
  43. 'group_condition': {
  44. 'limit': 1,
  45. 'offset': 1
  46. },
  47. 'sort_condition': {
  48. 'name': sort_name_list[idx],
  49. 'type': 'ascending'
  50. }
  51. }
  52. validate_condition(condition)
  53. @pytest.mark.level0
  54. @pytest.mark.env_single
  55. @pytest.mark.platform_x86_cpu
  56. @pytest.mark.platform_arm_ascend_training
  57. @pytest.mark.platform_x86_gpu_training
  58. @pytest.mark.platform_x86_ascend_training
  59. def test_validate_op_type_exception(self):
  60. """Test the operate type exception."""
  61. condition = "not a dict"
  62. exception_message = 'Param type error. Invalid search_condition type, it should be dict.'
  63. with pytest.raises(ProfilerParamTypeErrorException) as exc_info:
  64. validate_condition(condition)
  65. assert exc_info.value.error_code == '50546082'
  66. assert exc_info.value.message == exception_message
  67. condition_list = [{'op_type': "xxx"}, {}]
  68. exception_message = "The op_type in search_condition error, The op_type must in " \
  69. "['aicpu_type','aicpu_detail', 'aicore_type', 'aicore_detail', "\
  70. "'gpu_op_type', 'gpu_op_info', 'gpu_cuda_activity']"
  71. for condition in condition_list:
  72. with pytest.raises(ProfilerOpTypeException) as exc_info:
  73. validate_condition(condition)
  74. assert exc_info.value.error_code == '50546183'
  75. assert exc_info.value.message == exception_message
  76. @pytest.mark.level0
  77. @pytest.mark.env_single
  78. @pytest.mark.platform_x86_cpu
  79. @pytest.mark.platform_arm_ascend_training
  80. @pytest.mark.platform_x86_gpu_training
  81. @pytest.mark.platform_x86_ascend_training
  82. def test_validate_group_condition_exception(self):
  83. """test the group condition exception."""
  84. condition_list = [
  85. {
  86. 'op_type': 'aicpu_type',
  87. 'group_condition': 0
  88. },
  89. {
  90. 'op_type': 'aicpu_type',
  91. 'group_condition': {'limit': True}
  92. },
  93. {
  94. 'op_type': 'aicpu_type',
  95. 'group_condition': {'limit': 0}
  96. },
  97. {
  98. 'op_type': 'aicpu_type',
  99. 'group_condition': {'offset': True}
  100. },
  101. {
  102. 'op_type': 'aicpu_type',
  103. 'group_condition': {'offset': -1}
  104. },
  105. {
  106. 'op_type': 'aicpu_type',
  107. 'group_condition': {'offset': 10000000}
  108. },
  109. ]
  110. exception_message_list = [
  111. "The group condition must be dict.",
  112. "The limit must be int.",
  113. "The limit must in [1, 100].",
  114. "The offset must be int.",
  115. "The offset must ge 0.",
  116. "The offset must le 1000000."
  117. ]
  118. exception_message_list = [
  119. 'The group_condition in search_condition error, ' + message
  120. for message in exception_message_list
  121. ]
  122. for idx, condition in enumerate(condition_list):
  123. with pytest.raises(ProfilerGroupConditionException) as exc_info:
  124. validate_condition(condition)
  125. assert exc_info.value.error_code == '50546184'
  126. assert exc_info.value.message == exception_message_list[idx]
  127. @pytest.mark.level0
  128. @pytest.mark.env_single
  129. @pytest.mark.platform_x86_cpu
  130. @pytest.mark.platform_arm_ascend_training
  131. @pytest.mark.platform_x86_gpu_training
  132. @pytest.mark.platform_x86_ascend_training
  133. def test_validate_minddata_pipeline_condition(self):
  134. """Test the validate minddata pipeline condition of normal input."""
  135. filter_condition_list = [
  136. {
  137. 'op_id': {
  138. 'in': [1, 2]
  139. }
  140. },
  141. {
  142. 'op_type': {
  143. 'in': ['add', 'conv2d']
  144. }
  145. },
  146. {
  147. 'is_display_op_detail': True
  148. }
  149. ]
  150. for filter_condition in filter_condition_list:
  151. condition = {
  152. 'device_id': '0',
  153. 'op_type': 'aicpu_type',
  154. 'filter_condition': filter_condition,
  155. 'group_condition': {
  156. 'limit': 1,
  157. 'offset': 1
  158. },
  159. 'sort_condition': {
  160. 'name': 'op_type',
  161. 'type': 'ascending'
  162. }
  163. }
  164. validate_minddata_pipeline_condition(condition)
  165. @pytest.mark.level0
  166. @pytest.mark.env_single
  167. @pytest.mark.platform_x86_cpu
  168. @pytest.mark.platform_arm_ascend_training
  169. @pytest.mark.platform_x86_gpu_training
  170. @pytest.mark.platform_x86_ascend_training
  171. def test_validate_minddata_pipeline_condition_exception(self):
  172. """Test the exception of validate minddata pipeline condition."""
  173. condition_list = [
  174. {
  175. 'filter_condition': {
  176. 'op_id': 0
  177. }
  178. },
  179. {
  180. 'filter_condition': {
  181. 'op_id': {
  182. 'in': ['0']
  183. }
  184. }
  185. }
  186. ]
  187. exception_message_list = [
  188. 'The filter_condition in search_condition error, '
  189. 'The filter condition value must be dict.',
  190. 'The filter_condition in search_condition error, '
  191. 'The item in filter value must be int.'
  192. ]
  193. for idx, condition in enumerate(condition_list):
  194. with pytest.raises(ProfilerFilterConditionException) as exc_info:
  195. validate_minddata_pipeline_condition(condition)
  196. assert exc_info.value.error_code == '50546186'
  197. assert exc_info.value.message == exception_message_list[idx]
  198. @pytest.mark.level0
  199. @pytest.mark.env_single
  200. @pytest.mark.platform_x86_cpu
  201. @pytest.mark.platform_arm_ascend_training
  202. @pytest.mark.platform_x86_gpu_training
  203. @pytest.mark.platform_x86_ascend_training
  204. def test_validate_and_set_job_id_env(self):
  205. """Test the validate and set job id env."""
  206. job_id = '256'
  207. validate_and_set_job_id_env(job_id)