Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 38 additions & 11 deletions tests/test_remote_access.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,9 @@ def setUp(self):
)
self.print_test_title()

# Save the runner that can be modified by the test
self.initial_runner = kh.get_runner()

def tearDown(self):
# Cleanup the output dir (the files within and the folder)
if hasattr(self, "folder_name_to_clean_in_teardown"):
Expand All @@ -160,6 +163,21 @@ def tearDown(self):
)
fs.remove(self.folder_name_to_clean_in_teardown)

# Restore the environment variables that can be left tained
# after a test failure
if hasattr(self, "env_vars_to_restore"):
for var_name in self.env_vars_to_restore:
if (
self.env_vars_to_restore.get(var_name) is None
and os.environ.get(var_name) is not None
):
del os.environ[var_name]
elif self.env_vars_to_restore.get(var_name) is not None:
os.environ[var_name] = self.env_vars_to_restore[var_name]

# Restore the runner that can have been modified by the test
kh.set_runner(self.initial_runner)

def test_train_predictor_with_remote_access(self):
"""Test train_predictor with remote resources"""
iris_data_dir = fs.get_child_path(kh.get_runner().samples_dir, "Iris")
Expand Down Expand Up @@ -230,13 +248,27 @@ def test_train_predictor_fail_and_log_with_remote_access(self):
def test_samples_dir_inferred_from_remote_home(self):
"""Test samples_dir is correctly inferred using a remote path in HOME"""

# Save initial state
# This runner has remote paths (for root_temp_dir for example)
initial_runner = kh.get_runner()
initial_home = os.environ.get("HOME")
if self.remote_access_test_case() == "KhiopsDockerRunner":
self.skipTest(
f"current test {os.environ.get('PYTEST_CURRENT_TEST')}"
" is skipped for "
f"{self.remote_access_test_case()} test case"
)

# Set a remote path to HOME
os.environ["HOME"] = initial_runner.root_temp_dir
# Save the env vars in a dict that will be restored by tearDown
# even if the test fails
self.env_vars_to_restore = {}
for var_name in ("HOME", "KHIOPS_SAMPLES_DIR"):
print(f"Initial value of {var_name} = {os.environ.get(var_name)}")
self.env_vars_to_restore[var_name] = os.environ.get(var_name)

# The current runner kh.get_runner() has remote paths
# (in root_temp_dir attribute for example)
# Set this remote path to HOME
os.environ["HOME"] = kh.get_runner().root_temp_dir
# Delete KHIOPS_SAMPLES_DIR so that its value will be inferred using HOME
if os.environ.get("KHIOPS_SAMPLES_DIR") is not None:
del os.environ["KHIOPS_SAMPLES_DIR"]
test_runner = KhiopsLocalRunner()
kh.set_runner(test_runner)

Expand All @@ -249,11 +281,6 @@ def test_samples_dir_inferred_from_remote_home(self):
)
self.assertEqual(test_runner.samples_dir, expected_samples_dir)

# Restore initial state
if initial_home is not None:
os.environ["HOME"] = initial_home
kh.set_runner(initial_runner)


class KhiopsS3RemoteFileTests(KhiopsRemoteAccessTestsContainer.KhiopsRemoteAccessTests):
"""Integration tests with Amazon S3 filesystems"""
Expand Down
Loading