test_functions.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. import re
  2. import pytest
  3. from render import Render
  4. @pytest.fixture
  5. def mock_values():
  6. return {
  7. "images": {
  8. "test_image": {
  9. "repository": "nginx",
  10. "tag": "latest",
  11. }
  12. },
  13. }
  14. def test_funcs(mock_values):
  15. mock_values["ix_volumes"] = {"test": "/mnt/test123"}
  16. render = Render(mock_values)
  17. c1 = render.add_container("test_container", "test_image")
  18. c1.healthcheck.disable()
  19. tests = [
  20. {"func": "auto_cast", "values": ["1"], "expected": 1},
  21. {"func": "auto_cast", "values": ["TrUe"], "expected": True},
  22. {"func": "auto_cast", "values": ["FaLsE"], "expected": False},
  23. {"func": "auto_cast", "values": ["0.2"], "expected": 0.2},
  24. {"func": "auto_cast", "values": [True], "expected": True},
  25. {"func": "basic_auth_header", "values": ["my_user", "my_pass"], "expected": "Basic bXlfdXNlcjpteV9wYXNz"},
  26. {"func": "basic_auth", "values": ["my_user", "my_pass"], "expected": "bXlfdXNlcjpteV9wYXNz"},
  27. {
  28. "func": "bcrypt_hash",
  29. "values": ["my_pass"],
  30. "expect_regex": r"^\$2b\$12\$[a-zA-Z0-9-_\.\/]+$",
  31. },
  32. {"func": "camel_case", "values": ["my_user"], "expected": "My_User"},
  33. {"func": "copy_dict", "values": [{"a": 1}], "expected": {"a": 1}},
  34. {"func": "fail", "values": ["my_message"], "expect_raise": True},
  35. {
  36. "func": "htpasswd",
  37. "values": ["my_user", "my_pass"],
  38. "expect_regex": r"^my_user:\$2b\$12\$[a-zA-Z0-9-_\.\/]+$",
  39. },
  40. {"func": "is_boolean", "values": ["true"], "expected": True},
  41. {"func": "is_boolean", "values": ["false"], "expected": True},
  42. {"func": "is_number", "values": ["1"], "expected": True},
  43. {"func": "is_number", "values": ["1.1"], "expected": True},
  44. {"func": "match_regex", "values": ["value", "^[a-zA-Z0-9]+$"], "expected": True},
  45. {"func": "match_regex", "values": ["value", "^[0-9]+$"], "expected": False},
  46. {"func": "merge_dicts", "values": [{"a": 1}, {"b": 2}], "expected": {"a": 1, "b": 2}},
  47. {"func": "must_match_regex", "values": ["my_user", "^[0-9]$"], "expect_raise": True},
  48. {"func": "must_match_regex", "values": ["1", "^[0-9]$"], "expected": "1"},
  49. {"func": "secure_string", "values": [10], "expect_regex": r"^[a-zA-Z0-9-_]+$"},
  50. {"func": "disallow_chars", "values": ["my_user", ["$", "@"], "my_key"], "expected": "my_user"},
  51. {"func": "disallow_chars", "values": ["my_user$", ["$", "@"], "my_key"], "expect_raise": True},
  52. {
  53. "func": "get_host_path",
  54. "values": [{"type": "host_path", "host_path_config": {"path": "/mnt/test"}}],
  55. "expected": "/mnt/test",
  56. },
  57. {
  58. "func": "get_host_path",
  59. "values": [{"type": "ix_volume", "ix_volume_config": {"dataset_name": "test"}}],
  60. "expected": "/mnt/test123",
  61. },
  62. {"func": "or_default", "values": [None, 1], "expected": 1},
  63. {"func": "or_default", "values": [1, None], "expected": 1},
  64. {"func": "or_default", "values": [False, 1], "expected": 1},
  65. {"func": "or_default", "values": [True, 1], "expected": True},
  66. {"func": "temp_config", "values": [""], "expect_raise": True},
  67. {
  68. "func": "temp_config",
  69. "values": ["test"],
  70. "expected": {"type": "temporary", "volume_config": {"volume_name": "test"}},
  71. },
  72. {"func": "require_unique", "values": [["a=1", "b=2", "c"], "values.key", "="], "expected": None},
  73. {
  74. "func": "require_unique",
  75. "values": [["a=1", "b=2", "b=3"], "values.key", "="],
  76. "expect_raise": True,
  77. },
  78. {
  79. "func": "require_no_reserved",
  80. "values": [["a=1", "b=2", "c"], "values.key", ["d"], "="],
  81. "expected": None,
  82. },
  83. {
  84. "func": "require_no_reserved",
  85. "values": [["a=1", "b=2", "c"], "values.key", ["a"], "="],
  86. "expect_raise": True,
  87. },
  88. {
  89. "func": "require_no_reserved",
  90. "values": [["a=1", "b=2", "c"], "values.key", ["b"], "=", True],
  91. "expect_raise": True,
  92. },
  93. {
  94. "func": "url_encode",
  95. "values": ["7V!@@%%63r@a5#e!2X9!68g4b"],
  96. "expected": "7V%21%40%40%25%2563r%40a5%23e%212X9%2168g4b",
  97. },
  98. {
  99. "func": "url_to_dict",
  100. "values": ["192.168.1.1:8080"],
  101. "expected": {"host": "192.168.1.1", "port": 8080},
  102. },
  103. {
  104. "func": "url_to_dict",
  105. "values": ["[::]:8080"],
  106. "expected": {"host": "::", "port": 8080},
  107. },
  108. {
  109. "func": "url_to_dict",
  110. "values": ["[::]:8080", True],
  111. "expected": {"host": "[::]", "port": 8080, "host_no_brackets": "::"},
  112. },
  113. ]
  114. for test in tests:
  115. print(test["func"], test)
  116. func = render.funcs[test["func"]]
  117. if test.get("expect_raise", False):
  118. with pytest.raises(Exception):
  119. func(*test["values"])
  120. elif test.get("expect_regex"):
  121. r = func(*test["values"])
  122. assert re.match(test["expect_regex"], r) is not None
  123. else:
  124. r = func(*test["values"])
  125. assert r == test["expected"]