test_http.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. # Licensed to the Apache Software Foundation (ASF) under one
  2. # or more contributor license agreements. See the NOTICE file
  3. # distributed with this work for additional information
  4. # regarding copyright ownership. The ASF licenses this file
  5. # to you under the Apache License, Version 2.0 (the
  6. # "License"); you may not use this file except in compliance
  7. # with the License. You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing,
  12. # software distributed under the License is distributed on an
  13. # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  14. # KIND, either express or implied. See the License for the
  15. # specific language governing permissions and limitations
  16. # under the License.
  17. """Test Task HTTP."""
  18. from unittest.mock import patch
  19. import pytest
  20. from pydolphinscheduler.exceptions import PyDSParamException
  21. from pydolphinscheduler.tasks.http import Http, HttpCheckCondition, HttpMethod
  22. @pytest.mark.parametrize(
  23. "class_name, attrs",
  24. [
  25. (HttpMethod, ("GET", "POST", "HEAD", "PUT", "DELETE")),
  26. (
  27. HttpCheckCondition,
  28. (
  29. "STATUS_CODE_DEFAULT",
  30. "STATUS_CODE_CUSTOM",
  31. "BODY_CONTAINS",
  32. "BODY_NOT_CONTAINS",
  33. ),
  34. ),
  35. ],
  36. )
  37. def test_attr_exists(class_name, attrs):
  38. """Test weather class HttpMethod and HttpCheckCondition contain specific attribute."""
  39. assert all(hasattr(class_name, attr) for attr in attrs)
  40. @pytest.mark.parametrize(
  41. "attr, expect",
  42. [
  43. (
  44. {"url": "https://www.apache.org"},
  45. {
  46. "url": "https://www.apache.org",
  47. "httpMethod": "GET",
  48. "httpParams": [],
  49. "httpCheckCondition": "STATUS_CODE_DEFAULT",
  50. "condition": None,
  51. "connectTimeout": 60000,
  52. "socketTimeout": 60000,
  53. "localParams": [],
  54. "resourceList": [],
  55. "dependence": {},
  56. "waitStartTimeout": {},
  57. "conditionResult": {"successNode": [""], "failedNode": [""]},
  58. },
  59. )
  60. ],
  61. )
  62. @patch(
  63. "pydolphinscheduler.core.task.Task.gen_code_and_version",
  64. return_value=(123, 1),
  65. )
  66. def test_property_task_params(mock_code_version, attr, expect):
  67. """Test task http property."""
  68. task = Http("test-http-task-params", **attr)
  69. assert expect == task.task_params
  70. @pytest.mark.parametrize(
  71. "param",
  72. [
  73. {"http_method": "http_method"},
  74. {"http_check_condition": "http_check_condition"},
  75. {"http_check_condition": HttpCheckCondition.STATUS_CODE_CUSTOM},
  76. {
  77. "http_check_condition": HttpCheckCondition.STATUS_CODE_CUSTOM,
  78. "condition": None,
  79. },
  80. ],
  81. )
  82. @patch(
  83. "pydolphinscheduler.core.task.Task.gen_code_and_version",
  84. return_value=(123, 1),
  85. )
  86. def test_http_task_param_not_support_param(mock_code, param):
  87. """Test HttpTaskParams not support parameter."""
  88. url = "https://www.apache.org"
  89. with pytest.raises(PyDSParamException, match="Parameter .*?"):
  90. Http("test-no-supprot-param", url, **param)
  91. def test_http_get_define():
  92. """Test task HTTP function get_define."""
  93. code = 123
  94. version = 1
  95. name = "test_http_get_define"
  96. url = "https://www.apache.org"
  97. expect = {
  98. "code": code,
  99. "name": name,
  100. "version": 1,
  101. "description": None,
  102. "delayTime": 0,
  103. "taskType": "HTTP",
  104. "taskParams": {
  105. "localParams": [],
  106. "httpParams": [],
  107. "url": url,
  108. "httpMethod": "GET",
  109. "httpCheckCondition": "STATUS_CODE_DEFAULT",
  110. "condition": None,
  111. "connectTimeout": 60000,
  112. "socketTimeout": 60000,
  113. "dependence": {},
  114. "resourceList": [],
  115. "conditionResult": {"successNode": [""], "failedNode": [""]},
  116. "waitStartTimeout": {},
  117. },
  118. "flag": "YES",
  119. "taskPriority": "MEDIUM",
  120. "workerGroup": "default",
  121. "failRetryTimes": 0,
  122. "failRetryInterval": 1,
  123. "timeoutFlag": "CLOSE",
  124. "timeoutNotifyStrategy": None,
  125. "timeout": 0,
  126. }
  127. with patch(
  128. "pydolphinscheduler.core.task.Task.gen_code_and_version",
  129. return_value=(code, version),
  130. ):
  131. http = Http(name, url)
  132. assert http.get_define() == expect