Skip to content

Commit aa04b30

Browse files
authored
[ENH] replaced hardcoded test server admin key with env variable and secrets (openml#1568)
#### Metadata * Reference Issue: openml#1529 * New Tests Added: Yes * Documentation Updated: No * Change Log Entry: <!-- Short String, example: "Add new function `foo()` to module `bar`"; or "Fixes a bug with `bar`" --> #### Details this PR is made to remove the hardcoded test server admin key from the codebase and replace it with environment variable-based authentication. ## Summary - in `openml/config.py` Added a new environment variable constant for the test server admin key: ```python OPENML_TEST_SERVER_ADMIN_KEY_ENV_VAR = "OPENML_TEST_SERVER_ADMIN_KEY" ``` - Testing Base Class Updated in `openml/testing.py`. Modified the `TestBase` class to read the admin key from an environment variable instead of using a hardcoded value: **Before**: ```python admin_key = "abc" ``` **After**: ```python admin_key = os.environ.get(openml.config.OPENML_TEST_SERVER_ADMIN_KEY_ENV_VAR) ``` **Note**: - The admin key is now `None` by default when the environment variable is not set - Tests requiring the admin key will fail gracefully if the key is not available Also in the tests, Added `pytest.skipif` decorators to tests that require admin privileges in the following test files: #### `tests/test_openml/test_config.py` **Test**: `test_switch_to_example_configuration` **Added decorator**: ```python @pytest.mark.skipif( not os.environ.get(openml.config.OPENML_TEST_SERVER_ADMIN_KEY_ENV_VAR), reason="Test requires admin key. Set OPENML_TEST_SERVER_ADMIN_KEY environment variable.", ) ``` #### `tests/test_datasets/test_dataset_functions.py` **Test**: `test_data_status` **Added decorator**: ```python @pytest.mark.skipif( not os.environ.get(openml.config.OPENML_TEST_SERVER_ADMIN_KEY_ENV_VAR), reason="Test requires admin key. Set OPENML_TEST_SERVER_ADMIN_KEY environment variable.", ) ``` **Note**: - These tests will be automatically skipped if the admin key is not provided - Clear skip reason is displayed when tests are skipped - No failures or errors when running tests locally without the admin key ### 4. CI Configuration Update (`.github/workflows/test.yml`) Added the environment variable to all test execution steps in the GitHub Actions workflow: **Steps updated**: - Run tests on Ubuntu Test - Run tests on Ubuntu Production - Run tests on Windows **Added to each step**: ```yaml env: OPENML_TEST_SERVER_ADMIN_KEY: ${{ secrets.OPENML_TEST_SERVER_ADMIN_KEY }} ``` **Impact**: - CI will use the secret stored in GitHub repository settings - Tests requiring admin privileges will run in CI - The actual key value is never exposed in logs or code @PGijsbers this requires someone to put the admin key in the github secrets which would be a critical step. # Update on reviews the configurations should be done from openml config files in `./.openml/config` for directory level configurations, instead of the added responsibility of a new `.env` file and dependencies. in case of local testing the concerned tests would be skipped in case no key is provided.
1 parent 06ac6d0 commit aa04b30

File tree

6 files changed

+24
-2
lines changed

6 files changed

+24
-2
lines changed

.github/workflows/test.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,8 @@ jobs:
106106

107107
- name: Run tests on Ubuntu Test
108108
if: matrix.os == 'ubuntu-latest'
109+
env:
110+
OPENML_TEST_SERVER_ADMIN_KEY: ${{ secrets.OPENML_TEST_SERVER_ADMIN_KEY }}
109111
run: |
110112
if [ "${{ matrix.code-cov }}" = "true" ]; then
111113
codecov="--cov=openml --long --cov-report=xml"
@@ -121,6 +123,8 @@ jobs:
121123
122124
- name: Run tests on Ubuntu Production
123125
if: matrix.os == 'ubuntu-latest'
126+
env:
127+
OPENML_TEST_SERVER_ADMIN_KEY: ${{ secrets.OPENML_TEST_SERVER_ADMIN_KEY }}
124128
run: |
125129
if [ "${{ matrix.code-cov }}" = "true" ]; then
126130
codecov="--cov=openml --long --cov-report=xml"
@@ -136,6 +140,8 @@ jobs:
136140
137141
- name: Run tests on Windows
138142
if: matrix.os == 'windows-latest'
143+
env:
144+
OPENML_TEST_SERVER_ADMIN_KEY: ${{ secrets.OPENML_TEST_SERVER_ADMIN_KEY }}
139145
run: | # we need a separate step because of the bash-specific if-statement in the previous one.
140146
pytest -n 4 --durations=20 --dist load -sv --reruns 5 --reruns-delay 1 -m "not uses_test_server"
141147

CONTRIBUTING.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,17 @@ To test your new contribution, add [unit tests](https://github.com/openml/openml
9696
* Please ensure that the example is run on the test server by beginning with the call to `openml.config.start_using_configuration_for_example()`, which is done by default for tests derived from `TestBase`.
9797
* Add the `@pytest.mark.sklearn` marker to your unit tests if they have a dependency on scikit-learn.
9898
99+
#### Running Tests That Require Admin Privileges
100+
101+
Some tests require admin privileges on the test server and will be automatically skipped unless you provide an admin API key. For regular contributors, the tests will skip gracefully. For core contributors who need to run these tests locally, you can set up the key by exporting the variable as below before running the tests:
102+
103+
```bash
104+
# For windows
105+
$env:OPENML_TEST_SERVER_ADMIN_KEY = "admin-key"
106+
# For linux/mac
107+
export OPENML_TEST_SERVER_ADMIN_KEY="admin-key"
108+
```
109+
99110
### Pull Request Checklist
100111
101112
You can go to the `openml-python` GitHub repository to create the pull request by [comparing the branch](https://github.com/openml/openml-python/compare) from your fork with the `develop` branch of the `openml-python` repository. When creating a pull request, make sure to follow the comments and structured provided by the template on GitHub.

openml/config.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
OPENML_CACHE_DIR_ENV_VAR = "OPENML_CACHE_DIR"
2727
OPENML_SKIP_PARQUET_ENV_VAR = "OPENML_SKIP_PARQUET"
28+
OPENML_TEST_SERVER_ADMIN_KEY_ENV_VAR = "OPENML_TEST_SERVER_ADMIN_KEY"
2829
_TEST_SERVER_NORMAL_USER_KEY = "normaluser"
2930

3031

openml/testing.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ class TestBase(unittest.TestCase):
4848
}
4949
flow_name_tracker: ClassVar[list[str]] = []
5050
test_server = "https://test.openml.org/api/v1/xml"
51-
admin_key = "abc"
51+
admin_key = os.environ.get(openml.config.OPENML_TEST_SERVER_ADMIN_KEY_ENV_VAR)
5252
user_key = openml.config._TEST_SERVER_NORMAL_USER_KEY
5353

5454
# creating logger for tracking files uploaded to test server

tests/test_datasets/test_dataset_functions.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -599,6 +599,10 @@ def _assert_status_of_dataset(self, *, did: int, status: str):
599599
assert len(result) == 1
600600
assert result[did]["status"] == status
601601

602+
@pytest.mark.skipif(
603+
not os.environ.get(openml.config.OPENML_TEST_SERVER_ADMIN_KEY_ENV_VAR),
604+
reason="Test requires admin key. Set OPENML_TEST_SERVER_ADMIN_KEY environment variable.",
605+
)
602606
@pytest.mark.flaky()
603607
@pytest.mark.uses_test_server()
604608
def test_data_status(self):

tests/test_openml/test_config.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ class TestConfigurationForExamples(openml.testing.TestBase):
110110
def test_switch_to_example_configuration(self):
111111
"""Verifies the test configuration is loaded properly."""
112112
# Below is the default test key which would be used anyway, but just for clarity:
113-
openml.config.apikey = TestBase.admin_key
113+
openml.config.apikey = "any-api-key"
114114
openml.config.server = self.production_server
115115

116116
openml.config.start_using_configuration_for_example()

0 commit comments

Comments
 (0)