|
17 | 17 |
|
18 | 18 | import khiops.core as kh |
19 | 19 | import khiops.core.internals.filesystems as fs |
| 20 | +from khiops import tools |
20 | 21 | from khiops.core.exceptions import KhiopsEnvironmentError |
21 | 22 | from khiops.core.internals.runner import KhiopsLocalRunner |
22 | 23 | from khiops.extras.docker import KhiopsDockerRunner |
|
30 | 31 | class KhiopsRunnerEnvironmentTests(unittest.TestCase): |
31 | 32 | """Test that runners in different environments work""" |
32 | 33 |
|
| 34 | + @unittest.skipIf( |
| 35 | + os.environ.get("SKIP_EXPENSIVE_TESTS", "false").lower() == "true", |
| 36 | + "Skipping expensive test", |
| 37 | + ) |
| 38 | + def test_samples_are_downloaded_according_to_the_runner_setting(self): |
| 39 | + """Test that samples are downloaded to the runner samples directory""" |
| 40 | + |
| 41 | + # Save initial state |
| 42 | + initial_runner = kh.get_runner() |
| 43 | + initial_khiops_samples_dir = os.environ.get("KHIOPS_SAMPLES_DIR") |
| 44 | + |
| 45 | + # Test that default samples download location is consistent with the |
| 46 | + # KhiopsLocalRunner samples directory |
| 47 | + with tempfile.TemporaryDirectory() as tmp_samples_dir: |
| 48 | + |
| 49 | + # Set environment variable to the temporary samples dir |
| 50 | + os.environ["KHIOPS_SAMPLES_DIR"] = tmp_samples_dir |
| 51 | + |
| 52 | + # Create test runner to update samples dir to tmp_samples_dir, |
| 53 | + # according to the newly-set environment variable |
| 54 | + test_runner = KhiopsLocalRunner() |
| 55 | + |
| 56 | + # Set current runner to the test runner |
| 57 | + kh.set_runner(test_runner) |
| 58 | + |
| 59 | + # Check that samples are not in tmp_samples_dir |
| 60 | + try: |
| 61 | + self.assert_samples_dir_integrity(tmp_samples_dir) |
| 62 | + except AssertionError: |
| 63 | + pass |
| 64 | + |
| 65 | + # Fail if the samples directory is populated (it should be empty) |
| 66 | + else: |
| 67 | + return False |
| 68 | + |
| 69 | + # Download samples into existing, but empty, tmp_samples_dir |
| 70 | + # Check that samples have been downloaded to tmp_samples_dir |
| 71 | + tools.download_datasets(force_overwrite=True) |
| 72 | + self.assert_samples_dir_integrity(tmp_samples_dir) |
| 73 | + |
| 74 | + # Remove KHIOPS_SAMPLES_DIR |
| 75 | + # Create test runner to update samples dir to the default runner samples |
| 76 | + # dir, following the deletion of the KHIOPS_SAMPLES_DIR environment |
| 77 | + # variable |
| 78 | + # Set current runner to the test runner |
| 79 | + del os.environ["KHIOPS_SAMPLES_DIR"] |
| 80 | + test_runner = KhiopsLocalRunner() |
| 81 | + kh.set_runner(test_runner) |
| 82 | + |
| 83 | + # Get the default runner samples dir |
| 84 | + default_runner_samples_dir = kh.get_samples_dir() |
| 85 | + |
| 86 | + # Move existing default runner samples dir contents to temporary directory |
| 87 | + if os.path.isdir(default_runner_samples_dir): |
| 88 | + tmp_initial_samples_dir = tempfile.mkdtemp() |
| 89 | + shutil.copytree( |
| 90 | + default_runner_samples_dir, |
| 91 | + tmp_initial_samples_dir, |
| 92 | + dirs_exist_ok=True, |
| 93 | + ) |
| 94 | + shutil.rmtree(default_runner_samples_dir) |
| 95 | + else: |
| 96 | + tmp_initial_samples_dir = None |
| 97 | + |
| 98 | + # Check that the samples are not present in the default runner |
| 99 | + # samples dir |
| 100 | + try: |
| 101 | + self.assert_samples_dir_integrity(default_runner_samples_dir) |
| 102 | + except AssertionError: |
| 103 | + pass |
| 104 | + |
| 105 | + # Fail if the samples directory is populated (it should not exist) |
| 106 | + else: |
| 107 | + return False |
| 108 | + |
| 109 | + # Download datasets to the default runner samples dir (which |
| 110 | + # should be created on this occasion) |
| 111 | + # Default samples dir does not exist anymore |
| 112 | + # Check that the default samples dir is populated |
| 113 | + tools.download_datasets() |
| 114 | + self.assert_samples_dir_integrity(default_runner_samples_dir) |
| 115 | + |
| 116 | + # Clean-up default samples dir |
| 117 | + shutil.rmtree(default_runner_samples_dir) |
| 118 | + |
| 119 | + # Restore initial state: |
| 120 | + # - initial samples dir contents if previously present |
| 121 | + # - initial KHIOPS_SAMPLES_DIR if set |
| 122 | + # - initial runner |
| 123 | + if tmp_initial_samples_dir is not None and os.path.isdir( |
| 124 | + tmp_initial_samples_dir |
| 125 | + ): |
| 126 | + shutil.copytree( |
| 127 | + tmp_initial_samples_dir, |
| 128 | + default_runner_samples_dir, |
| 129 | + dirs_exist_ok=True, |
| 130 | + ) |
| 131 | + |
| 132 | + # Remove temporary directory |
| 133 | + shutil.rmtree(tmp_initial_samples_dir) |
| 134 | + if initial_khiops_samples_dir is not None: |
| 135 | + os.environ["KHIOPS_SAMPLES_DIR"] = initial_khiops_samples_dir |
| 136 | + kh.set_runner(initial_runner) |
| 137 | + |
| 138 | + def assert_samples_dir_integrity(self, samples_dir): |
| 139 | + """Checks that the samples dir has the expected structure""" |
| 140 | + expected_dataset_names = [ |
| 141 | + "Accidents", |
| 142 | + "AccidentsSummary", |
| 143 | + "Adult", |
| 144 | + "Customer", |
| 145 | + "CustomerExtended", |
| 146 | + "Iris", |
| 147 | + "Letter", |
| 148 | + "Mushroom", |
| 149 | + "NegativeAirlineTweets", |
| 150 | + "SpliceJunction", |
| 151 | + "Vehicle", |
| 152 | + "WineReviews", |
| 153 | + ] |
| 154 | + self.assertTrue(os.path.isdir(samples_dir)) |
| 155 | + for ds_name in expected_dataset_names: |
| 156 | + self.assertIn(ds_name, os.listdir(samples_dir)) |
| 157 | + |
33 | 158 | @unittest.skipIf( |
34 | 159 | platform.system() != "Linux", "Skipping test for non-Linux platform" |
35 | 160 | ) |
|
0 commit comments