Skip to content

Commit e8df72f

Browse files
author
Thierry RAMORASOAVINA
committed
Fix remote access tests
1 parent d52d4e8 commit e8df72f

1 file changed

Lines changed: 36 additions & 12 deletions

File tree

tests/test_remote_access.py

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,9 @@ def setUp(self):
149149
)
150150
self.print_test_title()
151151

152+
# Save the runner that can be modified by the test
153+
self.initial_runner = kh.get_runner()
154+
152155
def tearDown(self):
153156
# Cleanup the output dir (the files within and the folder)
154157
if hasattr(self, "folder_name_to_clean_in_teardown"):
@@ -160,6 +163,21 @@ def tearDown(self):
160163
)
161164
fs.remove(self.folder_name_to_clean_in_teardown)
162165

166+
# Restore the environment variables that can be left tained
167+
# after a test failure
168+
if hasattr(self, "env_vars_to_restore"):
169+
for var_name in self.env_vars_to_restore:
170+
if (
171+
self.env_vars_to_restore.get(var_name) is None
172+
and os.environ.get(var_name) is not None
173+
):
174+
del os.environ[var_name]
175+
elif self.env_vars_to_restore.get(var_name) is not None:
176+
os.environ[var_name] = self.env_vars_to_restore[var_name]
177+
178+
# Restore the runner that can have been modified by the test
179+
kh.set_runner(self.initial_runner)
180+
163181
def test_train_predictor_with_remote_access(self):
164182
"""Test train_predictor with remote resources"""
165183
iris_data_dir = fs.get_child_path(kh.get_runner().samples_dir, "Iris")
@@ -227,16 +245,27 @@ def test_train_predictor_fail_and_log_with_remote_access(self):
227245
self.assertTrue(fs.exists(log_file_path), f"Path: {log_file_path}")
228246
fs.remove(log_file_path)
229247

248+
@unittest.skipIf(
249+
docker_runner_config_exists(),
250+
"Skip the remote path tests for docker runner",
251+
)
230252
def test_samples_dir_inferred_from_remote_home(self):
231253
"""Test samples_dir is correctly inferred using a remote path in HOME"""
232254

233-
# Save initial state
234-
# This runner has remote paths (for root_temp_dir for example)
235-
initial_runner = kh.get_runner()
236-
initial_home = os.environ.get("HOME")
237-
238-
# Set a remote path to HOME
239-
os.environ["HOME"] = initial_runner.root_temp_dir
255+
# Save the env vars in a dict that will be restored by tearDown
256+
# even if the test fails
257+
self.env_vars_to_restore = {}
258+
for var_name in ("HOME", "KHIOPS_SAMPLES_DIR"):
259+
print(f"Initial value of {var_name} = {os.environ.get(var_name)}")
260+
self.env_vars_to_restore[var_name] = os.environ.get(var_name)
261+
262+
# The current runner kh.get_runner() has remote paths
263+
# (in root_temp_dir attribute for example)
264+
# Set this remote path to HOME
265+
os.environ["HOME"] = kh.get_runner().root_temp_dir
266+
# Delete KHIOPS_SAMPLES_DIR so that its value will be inferred using HOME
267+
if os.environ.get("KHIOPS_SAMPLES_DIR") is not None:
268+
del os.environ["KHIOPS_SAMPLES_DIR"]
240269
test_runner = KhiopsLocalRunner()
241270
kh.set_runner(test_runner)
242271

@@ -249,11 +278,6 @@ def test_samples_dir_inferred_from_remote_home(self):
249278
)
250279
self.assertEqual(test_runner.samples_dir, expected_samples_dir)
251280

252-
# Restore initial state
253-
if initial_home is not None:
254-
os.environ["HOME"] = initial_home
255-
kh.set_runner(initial_runner)
256-
257281

258282
class KhiopsS3RemoteFileTests(KhiopsRemoteAccessTestsContainer.KhiopsRemoteAccessTests):
259283
"""Integration tests with Amazon S3 filesystems"""

0 commit comments

Comments
 (0)