test_file.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. """
  2. Tests for the file state
  3. """
  4. import pytest
  5. from tests.support.helpers import slowTest
  6. @pytest.fixture(scope="module")
  7. def pillar_tree(base_env_pillar_tree_root_dir, salt_minion, salt_call_cli):
  8. top_file = """
  9. base:
  10. '{}':
  11. - basic
  12. """.format(
  13. salt_minion.id
  14. )
  15. basic_pillar_file = """
  16. monty: python
  17. companions:
  18. three:
  19. - liz
  20. - jo
  21. - sarah jane
  22. """
  23. top_tempfile = pytest.helpers.temp_file(
  24. "top.sls", top_file, base_env_pillar_tree_root_dir
  25. )
  26. basic_tempfile = pytest.helpers.temp_file(
  27. "basic.sls", basic_pillar_file, base_env_pillar_tree_root_dir
  28. )
  29. try:
  30. with top_tempfile, basic_tempfile:
  31. ret = salt_call_cli.run("saltutil.refresh_pillar", wait=True)
  32. assert ret.exitcode == 0
  33. assert ret.json is True
  34. yield
  35. finally:
  36. # Refresh pillar again to cleaup the temp pillar
  37. ret = salt_call_cli.run("saltutil.refresh_pillar", wait=True)
  38. assert ret.exitcode == 0
  39. assert ret.json is True
  40. @slowTest
  41. def test_verify_ssl_skip_verify_false(salt_call_cli, tmpdir, ssl_webserver):
  42. """
  43. test verify_ssl when its False and True when managing
  44. a file with an https source and skip_verify is false.
  45. """
  46. web_file = ssl_webserver.url("this.txt")
  47. true_content = """
  48. test_verify_ssl:
  49. file.managed:
  50. - name: {}
  51. - source: {}
  52. - source_hash: {}
  53. """.format(
  54. tmpdir.join("test_verify_ssl_true.txt"), web_file, web_file + ".sha256"
  55. )
  56. false_content = true_content + " - verify_ssl: False"
  57. # test when verify_ssl is True
  58. with pytest.helpers.temp_state_file("verify_ssl.sls", true_content) as sfpath:
  59. ret = salt_call_cli.run("--local", "state.apply", "verify_ssl")
  60. assert ret.exitcode == 1
  61. assert (
  62. "SSL: CERTIFICATE_VERIFY_FAILED"
  63. in ret.json[next(iter(ret.json))]["comment"]
  64. )
  65. # test when verify_ssl is False
  66. with pytest.helpers.temp_state_file("verify_ssl.sls", false_content) as sfpath:
  67. ret = salt_call_cli.run("--local", "state.apply", "verify_ssl")
  68. assert ret.exitcode == 0
  69. assert ret.json[next(iter(ret.json))]["changes"] == {
  70. "diff": "New file",
  71. "mode": "0644",
  72. }
  73. @pytest.mark.windows_whitelisted
  74. def test_contents_pillar_with_pillar_list(
  75. salt_call_cli, pillar_tree, base_env_state_tree_root_dir, tmp_path
  76. ):
  77. """
  78. This tests for any regressions for this issue:
  79. https://github.com/saltstack/salt/issues/30934
  80. """
  81. target_path = tmp_path / "add-contents-pillar-target.txt"
  82. sls_name = "file-contents-pillar"
  83. sls_contents = """
  84. add_contents_pillar_sls:
  85. file.managed:
  86. - name: {}
  87. - contents_pillar: companions:three
  88. """.format(
  89. target_path
  90. )
  91. sls_tempfile = pytest.helpers.temp_file(
  92. "{}.sls".format(sls_name), sls_contents, base_env_state_tree_root_dir
  93. )
  94. with sls_tempfile:
  95. ret = salt_call_cli.run("state.sls", sls_name)
  96. assert ret.exitcode == 0
  97. assert ret.json
  98. state_run = next(iter(ret.json.values()))
  99. assert state_run["result"] is True
  100. # Check to make sure the file was created
  101. assert target_path.is_file()
  102. @pytest.mark.windows_whitelisted
  103. def test_managed_file_with_pillar_sls(
  104. salt_call_cli, pillar_tree, tmp_path, base_env_state_tree_root_dir
  105. ):
  106. """
  107. Test to ensure pillar data in sls file
  108. is rendered properly and file is created.
  109. """
  110. ret = salt_call_cli.run("pillar.get", "monty")
  111. assert ret.exitcode == 0
  112. assert ret.json
  113. target_path = tmp_path / "file-pillar-{}-target.txt".format(ret.json)
  114. sls_name = "file-pillar-get"
  115. sls_contents = (
  116. """
  117. {%- set filedir = '"""
  118. + str(tmp_path).replace("\\", "/")
  119. + """' %}
  120. {%- set filename = "file-pillar-{}-target.txt".format(salt["pillar.get"]("monty", "")) %}
  121. create-file:
  122. file.managed:
  123. - name: {{ filedir | path_join(filename) }}
  124. """
  125. )
  126. sls_tempfile = pytest.helpers.temp_file(
  127. "{}.sls".format(sls_name), sls_contents, base_env_state_tree_root_dir
  128. )
  129. with sls_tempfile:
  130. ret = salt_call_cli.run("state.sls", sls_name)
  131. assert ret.exitcode == 0
  132. assert ret.json
  133. state_run = next(iter(ret.json.values()))
  134. assert state_run["result"] is True
  135. # Check to make sure the file was created
  136. assert target_path.is_file()
  137. @pytest.mark.windows_whitelisted
  138. def test_issue_50221(
  139. salt_call_cli,
  140. pillar_tree,
  141. tmp_path,
  142. salt_minion,
  143. base_env_state_tree_root_dir,
  144. ext_pillar_file_tree_root_dir,
  145. ):
  146. expected_content = "abc\n\n\n"
  147. target_path = tmp_path / "issue-50221-target.txt"
  148. sls_name = "issue-50221"
  149. sls_contents = """
  150. {{ pillar["target-path"] }}:
  151. file.managed:
  152. - contents_pillar: issue-50221
  153. """
  154. sls_tempfile = pytest.helpers.temp_file(
  155. "{}.sls".format(sls_name), sls_contents, base_env_state_tree_root_dir
  156. )
  157. issue_50221_ext_pillar_tempfile = pytest.helpers.temp_file(
  158. "issue-50221",
  159. expected_content,
  160. ext_pillar_file_tree_root_dir / "hosts" / salt_minion.id,
  161. )
  162. with sls_tempfile, issue_50221_ext_pillar_tempfile:
  163. ret = salt_call_cli.run("pillar.get", "issue-50221")
  164. assert ret.exitcode == 0
  165. assert ret.json
  166. # The type of new line, ie, `\n` vs `\r\n` is not important
  167. assert ret.json.replace("\r\n", "\n") == expected_content
  168. ret = salt_call_cli.run(
  169. "state.apply", sls_name, pillar={"target-path": str(target_path)}
  170. )
  171. assert ret.exitcode == 0
  172. assert ret.json
  173. state_run = next(iter(ret.json.values()))
  174. assert state_run["result"] is True
  175. # Check to make sure the file was created
  176. assert target_path.is_file()
  177. # The type of new line, ie, `\n` vs `\r\n` is not important
  178. assert target_path.read_text().replace("\r\n", "\n") == expected_content