|
15 | 15 | import logging |
16 | 16 | import os |
17 | 17 | import shutil |
| 18 | +import uuid |
| 19 | +import json |
18 | 20 | import pytest |
19 | 21 | import tftest |
20 | 22 | from unittest.mock import patch, DEFAULT, Mock |
@@ -78,3 +80,99 @@ def test_no_use_cache(tf): |
78 | 80 | for _ in range(expected_call_count): |
79 | 81 | getattr(tf, method)(use_cache=False) |
80 | 82 | assert mock_execute_command.call_count == expected_call_count |
| 83 | + |
| 84 | + |
| 85 | +@pytest.mark.parametrize("tf", [True], indirect=True) |
| 86 | +def test_use_cache_with_same_tf_var_file(tf, tmp_path): |
| 87 | + tf_var_file_methods = ["plan", "apply", "destroy"] |
| 88 | + |
| 89 | + tf_vars_file = tmp_path / (str(uuid.uuid4()) + '.json') |
| 90 | + tf_vars_file.write_text(json.dumps({"foo": "old"})) |
| 91 | + |
| 92 | + for method in tf_var_file_methods: |
| 93 | + with patch.object(tf, 'execute_command', wraps=tf.execute_command) as mock_execute_command: |
| 94 | + for _ in range(2): |
| 95 | + getattr(tf, method)(use_cache=True, tf_var_file=tf_vars_file) |
| 96 | + |
| 97 | + assert mock_execute_command.call_count == 1 |
| 98 | + |
| 99 | + |
| 100 | +@pytest.mark.parametrize("tf", [True], indirect=True) |
| 101 | +def test_use_cache_with_new_tf_var_file(tf, tmp_path): |
| 102 | + tf_var_file_methods = ["plan", "apply", "destroy"] |
| 103 | + expected_call_count = 2 |
| 104 | + |
| 105 | + tf_vars_file = tmp_path / (str(uuid.uuid4()) + '.json') |
| 106 | + |
| 107 | + for method in tf_var_file_methods: |
| 108 | + tf_vars_file.write_text(json.dumps({"foo": "old"})) |
| 109 | + with patch.object(tf, 'execute_command', wraps=tf.execute_command) as mock_execute_command: |
| 110 | + for _ in range(expected_call_count): |
| 111 | + getattr(tf, method)(use_cache=True, tf_var_file=tf_vars_file) |
| 112 | + tf_vars_file.write_text(json.dumps({"foo": "new"})) |
| 113 | + |
| 114 | + assert mock_execute_command.call_count == expected_call_count |
| 115 | + |
| 116 | + |
| 117 | +@pytest.mark.parametrize("tf", [True], indirect=True) |
| 118 | +def test_use_cache_with_new_extra_files(tf, tmp_path): |
| 119 | + expected_call_count = 2 |
| 120 | + tf_vars_file = tmp_path / (str(uuid.uuid4()) + '.json') |
| 121 | + tf_vars_file.write_text(json.dumps({"foo": "old"})) |
| 122 | + |
| 123 | + with patch.object(tf, 'execute_command', wraps=tf.execute_command) as mock_execute_command: |
| 124 | + for _ in range(expected_call_count): |
| 125 | + tf.setup(use_cache=True, extra_files=[tf_vars_file]) |
| 126 | + tf_vars_file.write_text(json.dumps({"foo": "new"})) |
| 127 | + |
| 128 | + assert mock_execute_command.call_count == expected_call_count |
| 129 | + |
| 130 | + |
| 131 | +@pytest.mark.parametrize("tf", [True], indirect=True) |
| 132 | +def test_use_cache_with_same_extra_files(tf, tmp_path): |
| 133 | + tf_vars_file = tmp_path / (str(uuid.uuid4()) + '.json') |
| 134 | + tf_vars_file.write_text(json.dumps({"foo": "old"})) |
| 135 | + |
| 136 | + with patch.object(tf, 'execute_command', wraps=tf.execute_command) as mock_execute_command: |
| 137 | + for _ in range(2): |
| 138 | + tf.setup(use_cache=True, extra_files=[tf_vars_file]) |
| 139 | + |
| 140 | + assert mock_execute_command.call_count == 1 |
| 141 | + |
| 142 | + |
| 143 | +@pytest.mark.parametrize("tf", [True], indirect=True) |
| 144 | +def test_use_cache_with_new_env(tf): |
| 145 | + expected_call_count = 2 |
| 146 | + for method in cache_methods: |
| 147 | + with patch.object(tf, 'execute_command', wraps=tf.execute_command) as mock_execute_command: |
| 148 | + for _ in range(expected_call_count): |
| 149 | + getattr(tf, method)(use_cache=True) |
| 150 | + tf._env["foo"] = "bar" |
| 151 | + |
| 152 | + assert mock_execute_command.call_count == expected_call_count |
| 153 | + |
| 154 | + del tf._env["foo"] |
| 155 | + |
| 156 | + |
| 157 | +@pytest.fixture |
| 158 | +def dummy_tf_filepath(tf): |
| 159 | + filepath = os.path.join(tf.tfdir, "bar.txt") |
| 160 | + with open(filepath, "w") as f: |
| 161 | + f.write("old") |
| 162 | + |
| 163 | + yield filepath |
| 164 | + |
| 165 | + os.remove(filepath) |
| 166 | + |
| 167 | + |
| 168 | +@pytest.mark.parametrize("tf", [True], indirect=True) |
| 169 | +def test_use_cache_with_new_tf_content(tf, dummy_tf_filepath): |
| 170 | + expected_call_count = 2 |
| 171 | + for method in cache_methods: |
| 172 | + with patch.object(tf, 'execute_command', wraps=tf.execute_command) as mock_execute_command: |
| 173 | + for _ in range(expected_call_count): |
| 174 | + getattr(tf, method)(use_cache=True) |
| 175 | + with open(dummy_tf_filepath, "w") as f: |
| 176 | + f.write(str(uuid.uuid4())) |
| 177 | + |
| 178 | + assert mock_execute_command.call_count == expected_call_count |
0 commit comments