Browse Source

Merge "refactor: Use temp path fixture to automatically clean up"

Zuul 5 months ago
parent
commit
0fc18c6f19

+ 1
- 1
tests/unit/conftest.py View File

@@ -35,7 +35,7 @@ def restore_config():
35 35
         config.GLOBAL_CONTEXT = original_global_context
36 36
 
37 37
 
38
-@pytest.fixture(scope="class", autouse=True)
38
+@pytest.fixture(scope="module", autouse=True)
39 39
 def clean_temporary_git_repos():
40 40
     """Iterates through all temporarily created directories and deletes each
41 41
     one that was created for testing.

+ 0
- 2
tests/unit/engine/test_util_files.py View File

@@ -12,8 +12,6 @@
12 12
 # See the License for the specific language governing permissions and
13 13
 # limitations under the License.
14 14
 
15
-import tempfile
16
-
17 15
 from pegleg import config
18 16
 from pegleg.engine.util import files
19 17
 from tests.unit.fixtures import create_tmp_deployment_files

+ 3
- 4
tests/unit/engine/util/test_git.py View File

@@ -14,15 +14,14 @@
14 14
 
15 15
 import os
16 16
 import shutil
17
-import tempfile
18 17
 
19
-import fixtures
20 18
 from git import Repo
21 19
 import mock
22 20
 import pytest
23 21
 
24 22
 from pegleg.engine import exceptions
25 23
 from pegleg.engine.util import git
24
+from tests.unit.fixtures import temp_path
26 25
 from tests.unit import test_utils
27 26
 
28 27
 
@@ -511,8 +510,8 @@ def test_is_repository():
511 510
         subpath='deployment_files/site')
512 511
 
513 512
 
514
-def test_is_repository_negative():
515
-    assert not git.is_repository(tempfile.mkdtemp())
513
+def test_is_repository_negative(temp_path):
514
+    assert not git.is_repository(temp_path)
516 515
 
517 516
 
518 517
 @pytest.mark.skipif(

+ 1
- 1
tests/unit/fixtures.py View File

@@ -158,7 +158,7 @@ schema: pegleg/SiteDefinition/v1
158 158
 
159 159
 
160 160
 @pytest.fixture()
161
-def temp_clone_path():
161
+def temp_path():
162 162
     temp_folder = tempfile.mkdtemp()
163 163
     try:
164 164
         yield temp_folder

+ 20
- 31
tests/unit/test_cli.py View File

@@ -14,7 +14,6 @@
14 14
 
15 15
 import os
16 16
 import shutil
17
-import tempfile
18 17
 
19 18
 from click.testing import CliRunner
20 19
 import mock
@@ -24,7 +23,7 @@ from pegleg import cli
24 23
 from pegleg.engine import errorcodes
25 24
 from pegleg.engine.util import git
26 25
 from tests.unit import test_utils
27
-from tests.unit.fixtures import temp_clone_path
26
+from tests.unit.fixtures import temp_path
28 27
 
29 28
 
30 29
 @pytest.mark.skipif(
@@ -65,7 +64,7 @@ class TestSiteCLIOptions(BaseCLIActionTest):
65 64
     ### clone_path tests ###
66 65
 
67 66
     def test_list_sites_using_remote_repo_and_clone_path_option(
68
-            self, temp_clone_path):
67
+            self, temp_path):
69 68
         """Validates clone_path (-p) option is working properly with site list
70 69
         action when using remote repo. Verify that the repo was cloned in the
71 70
         clone_path
@@ -80,15 +79,15 @@ class TestSiteCLIOptions(BaseCLIActionTest):
80 79
 
81 80
         # Note that the -p option is used to specify the clone_folder
82 81
         site_list = self.runner.invoke(
83
-            cli.site, ['-p', temp_clone_path, '-r', repo_url, 'list'])
82
+            cli.site, ['-p', temp_path, '-r', repo_url, 'list'])
84 83
 
85 84
         assert site_list.exit_code == 0
86 85
         # Verify that the repo was cloned into the clone_path
87
-        assert os.path.exists(os.path.join(temp_clone_path, self.repo_name))
88
-        assert git.is_repository(os.path.join(temp_clone_path, self.repo_name))
86
+        assert os.path.exists(os.path.join(temp_path, self.repo_name))
87
+        assert git.is_repository(os.path.join(temp_path, self.repo_name))
89 88
 
90 89
     def test_list_sites_using_local_repo_and_clone_path_option(
91
-            self, temp_clone_path):
90
+            self, temp_path):
92 91
         """Validates clone_path (-p) option is working properly with site list
93 92
         action when using a local repo. Verify that the clone_path has NO
94 93
         effect when using a local repo
@@ -102,12 +101,11 @@ class TestSiteCLIOptions(BaseCLIActionTest):
102 101
 
103 102
         # Note that the -p option is used to specify the clone_folder
104 103
         site_list = self.runner.invoke(
105
-            cli.site, ['-p', temp_clone_path, '-r', repo_path, 'list'])
104
+            cli.site, ['-p', temp_path, '-r', repo_path, 'list'])
106 105
 
107 106
         assert site_list.exit_code == 0
108 107
         # Verify that passing in clone_path when using local repo has no effect
109
-        assert not os.path.exists(
110
-            os.path.join(temp_clone_path, self.repo_name))
108
+        assert not os.path.exists(os.path.join(temp_path, self.repo_name))
111 109
 
112 110
 
113 111
 class TestSiteCLIOptionsNegative(BaseCLIActionTest):
@@ -116,7 +114,7 @@ class TestSiteCLIOptionsNegative(BaseCLIActionTest):
116 114
     ### Negative clone_path tests ###
117 115
 
118 116
     def test_list_sites_using_remote_repo_and_reuse_clone_path_option(
119
-            self, temp_clone_path):
117
+            self, temp_path):
120 118
         """Validates clone_path (-p) option is working properly with site list
121 119
         action when using remote repo. Verify that the same repo can't be
122 120
         cloned in the same clone_path if it already exists
@@ -131,14 +129,14 @@ class TestSiteCLIOptionsNegative(BaseCLIActionTest):
131 129
 
132 130
         # Note that the -p option is used to specify the clone_folder
133 131
         site_list = self.runner.invoke(
134
-            cli.site, ['-p', temp_clone_path, '-r', repo_url, 'list'])
132
+            cli.site, ['-p', temp_path, '-r', repo_url, 'list'])
135 133
 
136
-        assert git.is_repository(os.path.join(temp_clone_path, self.repo_name))
134
+        assert git.is_repository(os.path.join(temp_path, self.repo_name))
137 135
 
138 136
         # Run site list for a second time to validate that the repo can't be
139 137
         # cloned twice in the same clone_path
140 138
         site_list = self.runner.invoke(
141
-            cli.site, ['-p', temp_clone_path, '-r', repo_url, 'list'])
139
+            cli.site, ['-p', temp_path, '-r', repo_url, 'list'])
142 140
 
143 141
         assert site_list.exit_code == 1
144 142
         msg = "The repository already exists in the given path. Either " \
@@ -146,23 +144,13 @@ class TestSiteCLIOptionsNegative(BaseCLIActionTest):
146 144
               "repository as the site repository (-r)."
147 145
         assert msg in site_list.output
148 146
 
149
-    @classmethod
150
-    def teardown_class(cls):
151
-        # Cleanup temporary Git repos.
152
-        root_tempdir = tempfile.gettempdir()
153
-        for tempdir in os.listdir(root_tempdir):
154
-            path = os.path.join(root_tempdir, tempdir)
155
-            if git.is_repository(path):
156
-                shutil.rmtree(path, ignore_errors=True)
157
-
158 147
 
159 148
 class TestSiteCliActions(BaseCLIActionTest):
160 149
     """Tests site-level CLI actions."""
161 150
 
162 151
     ### Collect tests ###
163 152
 
164
-    def _validate_collect_site_action(self, repo_path_or_url):
165
-        save_location = tempfile.mkdtemp()
153
+    def _validate_collect_site_action(self, repo_path_or_url, save_location):
166 154
         result = self.runner.invoke(cli.site, [
167 155
             '-r', repo_path_or_url, 'collect', self.site_name, '-s',
168 156
             save_location
@@ -176,7 +164,7 @@ class TestSiteCliActions(BaseCLIActionTest):
176 164
         # are written out to sensibly named files like airship-treasuremap.yaml
177 165
         assert collected_files[0] == ("%s.yaml" % self.repo_name)
178 166
 
179
-    def test_collect_using_remote_repo_url(self):
167
+    def test_collect_using_remote_repo_url(self, temp_path):
180 168
         """Validates collect action using a remote URL."""
181 169
         # Scenario:
182 170
         #
@@ -186,9 +174,10 @@ class TestSiteCliActions(BaseCLIActionTest):
186 174
 
187 175
         repo_url = 'https://github.com/openstack/%s@%s' % (self.repo_name,
188 176
                                                            self.repo_rev)
189
-        self._validate_collect_site_action(repo_url)
177
+        self._validate_collect_site_action(repo_url, temp_path)
190 178
 
191
-    def test_collect_using_remote_repo_url_ending_with_dot_git(self):
179
+    def test_collect_using_remote_repo_url_ending_with_dot_git(
180
+            self, temp_path):
192 181
         """Validates collect action using a remote URL ending in .git."""
193 182
         # Scenario:
194 183
         #
@@ -198,9 +187,9 @@ class TestSiteCliActions(BaseCLIActionTest):
198 187
 
199 188
         repo_url = 'https://github.com/openstack/%s@%s.git' % (self.repo_name,
200 189
                                                                self.repo_rev)
201
-        self._validate_collect_site_action(repo_url)
190
+        self._validate_collect_site_action(repo_url, temp_path)
202 191
 
203
-    def test_collect_using_local_path(self):
192
+    def test_collect_using_local_path(self, temp_path):
204 193
         """Validates collect action using a path to a local repo."""
205 194
         # Scenario:
206 195
         #
@@ -209,7 +198,7 @@ class TestSiteCliActions(BaseCLIActionTest):
209 198
         # 3) Check that expected file name is there
210 199
 
211 200
         repo_path = self.treasuremap_path
212
-        self._validate_collect_site_action(repo_path)
201
+        self._validate_collect_site_action(repo_path, temp_path)
213 202
 
214 203
     ### Lint tests ###
215 204
 

Loading…
Cancel
Save