test_github.py 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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 github resource plugin."""
  18. from unittest.mock import PropertyMock, patch
  19. import pytest
  20. from pydolphinscheduler.resources_plugin import GitHub
  21. from pydolphinscheduler.resources_plugin.base.git import GitFileInfo
  22. @pytest.mark.parametrize(
  23. "attr, expected",
  24. [
  25. (
  26. {
  27. "user": "apache",
  28. "repo_name": "dolphinscheduler",
  29. "file_path": "script/install.sh",
  30. "api": "https://api.github.com/repos/{user}/{repo_name}/contents/{file_path}",
  31. },
  32. "https://api.github.com/repos/apache/dolphinscheduler/contents/script/install.sh",
  33. ),
  34. ],
  35. )
  36. def test_github_build_req_api(attr, expected):
  37. """Test the build_req_api function of the github resource plug-in."""
  38. github = GitHub(prefix="prefix")
  39. assert expected == github.build_req_api(**attr)
  40. @pytest.mark.parametrize(
  41. "attr, expected",
  42. [
  43. (
  44. "https://github.com/apache/dolphinscheduler/blob/dev/script/install.sh",
  45. {
  46. "user": "apache",
  47. "repo_name": "dolphinscheduler",
  48. "branch": "dev",
  49. "file_path": "script/install.sh",
  50. },
  51. ),
  52. (
  53. "https://github.com/apache/dolphinscheduler/blob/master/pom.xml",
  54. {
  55. "user": "apache",
  56. "repo_name": "dolphinscheduler",
  57. "branch": "master",
  58. "file_path": "pom.xml",
  59. },
  60. ),
  61. (
  62. "https://github.com/apache/dolphinscheduler/blob/1.3.9-release/docker/build/startup.sh",
  63. {
  64. "user": "apache",
  65. "repo_name": "dolphinscheduler",
  66. "branch": "1.3.9-release",
  67. "file_path": "docker/build/startup.sh",
  68. },
  69. ),
  70. ],
  71. )
  72. def test_github_get_git_file_info(attr, expected):
  73. """Test the get_git_file_info function of the github resource plug-in."""
  74. github = GitHub(prefix="prefix")
  75. github.get_git_file_info(attr)
  76. assert expected == github._git_file_info.__dict__
  77. @pytest.mark.parametrize(
  78. "attr, expected",
  79. [
  80. (
  81. (
  82. {
  83. "user": "apache",
  84. "repo_name": "dolphinscheduler",
  85. "file_path": "docker/build/startup.sh",
  86. }
  87. ),
  88. "https://api.github.com/repos/apache/dolphinscheduler/contents/docker/build/startup.sh",
  89. ),
  90. (
  91. (
  92. {
  93. "user": "apache",
  94. "repo_name": "dolphinscheduler",
  95. "file_path": "pom.xml",
  96. }
  97. ),
  98. "https://api.github.com/repos/apache/dolphinscheduler/contents/pom.xml",
  99. ),
  100. (
  101. (
  102. {
  103. "user": "apache",
  104. "repo_name": "dolphinscheduler",
  105. "file_path": "script/create-dolphinscheduler.sh",
  106. }
  107. ),
  108. "https://api.github.com/repos/apache/dolphinscheduler/contents/script/create-dolphinscheduler.sh",
  109. ),
  110. ],
  111. )
  112. @patch(
  113. "pydolphinscheduler.resources_plugin.github.GitHub._git_file_info",
  114. new_callable=PropertyMock,
  115. )
  116. def test_github_get_req_url(m_git_file_info, attr, expected):
  117. """Test the get_req_url function of the github resource plug-in."""
  118. github = GitHub(prefix="prefix")
  119. m_git_file_info.return_value = GitFileInfo(**attr)
  120. assert expected == github.get_req_url()
  121. @pytest.mark.parametrize(
  122. "attr, expected",
  123. [
  124. (
  125. {
  126. "init": {"prefix": "prefix", "access_token": "access_token"},
  127. "file_path": "github_resource_plugin.sh",
  128. "file_content": "github resource plugin",
  129. },
  130. "github resource plugin",
  131. ),
  132. (
  133. {
  134. "init": {
  135. "prefix": "prefix",
  136. },
  137. "file_path": "github_resource_plugin.sh",
  138. "file_content": "github resource plugin",
  139. },
  140. "github resource plugin",
  141. ),
  142. ],
  143. )
  144. @patch("pydolphinscheduler.resources_plugin.github.GitHub.req")
  145. def test_github_read_file(m_req, attr, expected):
  146. """Test the read_file function of the github resource plug-in."""
  147. github = GitHub(**attr.get("init"))
  148. m_req.return_value = attr.get("file_content")
  149. assert expected == github.read_file(attr.get("file_path"))
  150. @pytest.mark.skip(reason="Lack of test environment, need stable repository")
  151. @pytest.mark.parametrize(
  152. "attr, expected",
  153. [
  154. (
  155. "https://github.com/apache/dolphinscheduler/blob/dev/lombok.config",
  156. "#\n"
  157. "# Licensed to the Apache Software Foundation (ASF) under one or more\n"
  158. "# contributor license agreements. See the NOTICE file distributed with\n"
  159. "# this work for additional information regarding copyright ownership.\n"
  160. "# The ASF licenses this file to You under the Apache License, Version 2.0\n"
  161. '# (the "License"); you may not use this file except in compliance with\n'
  162. "# the License. You may obtain a copy of the License at\n"
  163. "#\n"
  164. "# http://www.apache.org/licenses/LICENSE-2.0\n"
  165. "#\n"
  166. "# Unless required by applicable law or agreed to in writing, software\n"
  167. '# distributed under the License is distributed on an "AS IS" BASIS,\n'
  168. "# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n"
  169. "# See the License for the specific language governing permissions and\n"
  170. "# limitations under the License.\n"
  171. "#\n"
  172. "\n"
  173. "lombok.addLombokGeneratedAnnotation = true\n",
  174. ),
  175. ],
  176. )
  177. def test_github_req(attr, expected):
  178. """Test the req function of the github resource plug-in."""
  179. github = GitHub(
  180. prefix="prefix",
  181. )
  182. assert expected == github.req(attr)