test_config.py 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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 command line interface subcommand `config`."""
  18. import os
  19. from pathlib import Path
  20. import pytest
  21. from pydolphinscheduler.cli.commands import cli
  22. from pydolphinscheduler.core.configuration import BUILD_IN_CONFIG_PATH, config_path
  23. from tests.testing.cli import CliTestWrapper
  24. from tests.testing.constants import DEV_MODE, ENV_PYDS_HOME
  25. from tests.testing.file import get_file_content
  26. config_file = "config.yaml"
  27. @pytest.fixture
  28. def teardown_file_env():
  29. """Util for deleting temp configuration file and pop env var after test finish."""
  30. yield
  31. config_file_path = config_path()
  32. if config_file_path.exists():
  33. config_file_path.unlink()
  34. # pop environment variable to keep test cases dependent
  35. os.environ.pop(ENV_PYDS_HOME, None)
  36. assert ENV_PYDS_HOME not in os.environ
  37. @pytest.mark.parametrize(
  38. "home",
  39. [
  40. None,
  41. "/tmp/pydolphinscheduler",
  42. "/tmp/test_abc",
  43. ],
  44. )
  45. def test_config_init(teardown_file_env, home):
  46. """Test command line interface `config --init`."""
  47. if home:
  48. os.environ[ENV_PYDS_HOME] = home
  49. elif DEV_MODE:
  50. pytest.skip(
  51. "Avoid delete ~/pydolphinscheduler/config.yaml by accident when test locally."
  52. )
  53. config_file_path = config_path()
  54. assert not config_file_path.exists()
  55. cli_test = CliTestWrapper(cli, ["config", "--init"])
  56. cli_test.assert_success()
  57. assert config_file_path.exists()
  58. assert get_file_content(config_file_path) == get_file_content(BUILD_IN_CONFIG_PATH)
  59. @pytest.mark.parametrize(
  60. "key, expect",
  61. [
  62. # We test each key in one single section
  63. ("java_gateway.address", "127.0.0.1"),
  64. ("default.user.name", "userPythonGateway"),
  65. ("default.workflow.project", "project-pydolphin"),
  66. ],
  67. )
  68. def test_config_get(teardown_file_env, key: str, expect: str):
  69. """Test command line interface `config --get XXX`."""
  70. os.environ[ENV_PYDS_HOME] = "/tmp/pydolphinscheduler"
  71. cli_test = CliTestWrapper(cli, ["config", "--init"])
  72. cli_test.assert_success()
  73. cli_test = CliTestWrapper(cli, ["config", "--get", key])
  74. cli_test.assert_success(output=f"{key} = {expect}", fuzzy=True)
  75. @pytest.mark.parametrize(
  76. "keys, expects",
  77. [
  78. # We test mix section keys
  79. (("java_gateway.address", "java_gateway.port"), ("127.0.0.1", "25333")),
  80. (
  81. ("java_gateway.auto_convert", "default.user.tenant"),
  82. ("True", "tenant_pydolphin"),
  83. ),
  84. (
  85. (
  86. "java_gateway.port",
  87. "default.user.state",
  88. "default.workflow.worker_group",
  89. ),
  90. ("25333", "1", "default"),
  91. ),
  92. ],
  93. )
  94. def test_config_get_multiple(teardown_file_env, keys: str, expects: str):
  95. """Test command line interface `config --get KEY1 --get KEY2 ...`."""
  96. os.environ[ENV_PYDS_HOME] = "/tmp/pydolphinscheduler"
  97. cli_test = CliTestWrapper(cli, ["config", "--init"])
  98. cli_test.assert_success()
  99. get_args = ["config"]
  100. for key in keys:
  101. get_args.append("--get")
  102. get_args.append(key)
  103. cli_test = CliTestWrapper(cli, get_args)
  104. for idx, expect in enumerate(expects):
  105. cli_test.assert_success(output=f"{keys[idx]} = {expect}", fuzzy=True)
  106. @pytest.mark.parametrize(
  107. "key, value",
  108. [
  109. # We test each key in one single section
  110. ("java_gateway.address", "127.1.1.1"),
  111. ("default.user.name", "editUserPythonGateway"),
  112. ("default.workflow.project", "edit-project-pydolphin"),
  113. ],
  114. )
  115. def test_config_set(teardown_file_env, key: str, value: str):
  116. """Test command line interface `config --set KEY VALUE`."""
  117. path = "/tmp/pydolphinscheduler"
  118. assert not Path(path).joinpath(config_file).exists()
  119. os.environ[ENV_PYDS_HOME] = path
  120. cli_test = CliTestWrapper(cli, ["config", "--init"])
  121. cli_test.assert_success()
  122. # Make sure value do not exists first
  123. cli_test = CliTestWrapper(cli, ["config", "--get", key])
  124. assert f"{key} = {value}" not in cli_test.result.output
  125. cli_test = CliTestWrapper(cli, ["config", "--set", key, value])
  126. cli_test.assert_success()
  127. cli_test = CliTestWrapper(cli, ["config", "--get", key])
  128. assert f"{key} = {value}" in cli_test.result.output
  129. @pytest.mark.parametrize(
  130. "keys, values",
  131. [
  132. # We test each key in mixture section
  133. (("java_gateway.address", "java_gateway.port"), ("127.1.1.1", "25444")),
  134. (
  135. ("java_gateway.auto_convert", "default.user.tenant"),
  136. ("False", "edit_tenant_pydolphin"),
  137. ),
  138. (
  139. (
  140. "java_gateway.port",
  141. "default.user.state",
  142. "default.workflow.worker_group",
  143. ),
  144. ("25555", "0", "not-default"),
  145. ),
  146. ],
  147. )
  148. def test_config_set_multiple(teardown_file_env, keys: str, values: str):
  149. """Test command line interface `config --set KEY1 VAL1 --set KEY2 VAL2`."""
  150. path = "/tmp/pydolphinscheduler"
  151. assert not Path(path).joinpath(config_file).exists()
  152. os.environ[ENV_PYDS_HOME] = path
  153. cli_test = CliTestWrapper(cli, ["config", "--init"])
  154. cli_test.assert_success()
  155. set_args = ["config"]
  156. for idx, key in enumerate(keys):
  157. # Make sure values do not exists first
  158. cli_test = CliTestWrapper(cli, ["config", "--get", key])
  159. assert f"{key} = {values[idx]}" not in cli_test.result.output
  160. set_args.append("--set")
  161. set_args.append(key)
  162. set_args.append(values[idx])
  163. cli_test = CliTestWrapper(cli, set_args)
  164. cli_test.assert_success()
  165. for idx, key in enumerate(keys):
  166. # Make sure values exists after `config --set` run
  167. cli_test = CliTestWrapper(cli, ["config", "--get", key])
  168. assert f"{key} = {values[idx]}" in cli_test.result.output