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.1 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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_condition_exception(self):
  60. """Test the exception of validate condition."""
  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. # test the ProfilerOpTypeException
  68. condition_list = [{'op_type': "xxx"}, {}]
  69. exception_message = "The op_type in search_condition error, The op_type must in " \
  70. "['aicpu_type','aicpu_detail', 'aicore_type', 'aicore_detail', "\
  71. "'gpu_op_type', 'gpu_op_info', 'gpu_cuda_activity']"
  72. for condition in condition_list:
  73. with pytest.raises(ProfilerOpTypeException) as exc_info:
  74. validate_condition(condition)
  75. assert exc_info.value.error_code == '50546183'
  76. assert exc_info.value.message == exception_message
  77. # test the ProfilerGroupConditionException
  78. condition_list = [
  79. {
  80. 'op_type': 'aicpu_type',
  81. 'group_condition': 0
  82. },
  83. {
  84. 'op_type': 'aicpu_type',
  85. 'group_condition': {'limit': True}
  86. },
  87. {
  88. 'op_type': 'aicpu_type',
  89. 'group_condition': {'limit': 0}
  90. },
  91. {
  92. 'op_type': 'aicpu_type',
  93. 'group_condition': {'offset': True}
  94. },
  95. {
  96. 'op_type': 'aicpu_type',
  97. 'group_condition': {'offset': -1}
  98. },
  99. {
  100. 'op_type': 'aicpu_type',
  101. 'group_condition': {'offset': 10000000}
  102. },
  103. ]
  104. exception_message_list = [
  105. "The group condition must be dict.",
  106. "The limit must be int.",
  107. "The limit must in [1, 100].",
  108. "The offset must be int.",
  109. "The offset must ge 0.",
  110. "The offset must le 1000000."
  111. ]
  112. exception_message_list = [
  113. 'The group_condition in search_condition error, ' + message
  114. for message in exception_message_list
  115. ]
  116. for idx, condition in enumerate(condition_list):
  117. with pytest.raises(ProfilerGroupConditionException) as exc_info:
  118. validate_condition(condition)
  119. assert exc_info.value.error_code == '50546184'
  120. assert exc_info.value.message == exception_message_list[idx]
  121. @pytest.mark.level0
  122. @pytest.mark.env_single
  123. @pytest.mark.platform_x86_cpu
  124. @pytest.mark.platform_arm_ascend_training
  125. @pytest.mark.platform_x86_gpu_training
  126. @pytest.mark.platform_x86_ascend_training
  127. def test_validate_minddata_pipeline_condition(self):
  128. """Test the validate minddata pipeline condition of normal input."""
  129. filter_condition_list = [
  130. {
  131. 'op_id': {
  132. 'in': [1, 2]
  133. }
  134. },
  135. {
  136. 'op_type': {
  137. 'in': ['add', 'conv2d']
  138. }
  139. },
  140. {
  141. 'is_display_op_detail': True
  142. }
  143. ]
  144. for filter_condition in filter_condition_list:
  145. condition = {
  146. 'device_id': '0',
  147. 'op_type': 'aicpu_type',
  148. 'filter_condition': filter_condition,
  149. 'group_condition': {
  150. 'limit': 1,
  151. 'offset': 1
  152. },
  153. 'sort_condition': {
  154. 'name': 'op_type',
  155. 'type': 'ascending'
  156. }
  157. }
  158. validate_minddata_pipeline_condition(condition)
  159. @pytest.mark.level0
  160. @pytest.mark.env_single
  161. @pytest.mark.platform_x86_cpu
  162. @pytest.mark.platform_arm_ascend_training
  163. @pytest.mark.platform_x86_gpu_training
  164. @pytest.mark.platform_x86_ascend_training
  165. def test_validate_minddata_pipeline_condition_exception(self):
  166. """Test the exception of validate minddata pipeline condition."""
  167. condition_list = [
  168. {
  169. 'filter_condition': {
  170. 'op_id': 0
  171. }
  172. },
  173. {
  174. 'filter_condition': {
  175. 'op_id': {
  176. 'in': ['0']
  177. }
  178. }
  179. }
  180. ]
  181. exception_message_list = [
  182. 'The filter_condition in search_condition error, '
  183. 'The filter condition value must be dict.',
  184. 'The filter_condition in search_condition error, '
  185. 'The item in filter value must be int.'
  186. ]
  187. for idx, condition in enumerate(condition_list):
  188. with pytest.raises(ProfilerFilterConditionException) as exc_info:
  189. validate_minddata_pipeline_condition(condition)
  190. assert exc_info.value.error_code == '50546186'
  191. assert exc_info.value.message == exception_message_list[idx]
  192. @pytest.mark.level0
  193. @pytest.mark.env_single
  194. @pytest.mark.platform_x86_cpu
  195. @pytest.mark.platform_arm_ascend_training
  196. @pytest.mark.platform_x86_gpu_training
  197. @pytest.mark.platform_x86_ascend_training
  198. def test_validate_and_set_job_id_env(self):
  199. """Test the validate and set job id env."""
  200. job_id = '256'
  201. validate_and_set_job_id_env(job_id)