Repository navigation
Conversation
Multi-region CMEK Spanner databases configure per-region Cloud KMS keys via EncryptionConfig.kms_key_names. Add spanner_create_database_with_mr_cmek with unit and live integration tests.
|
Here is the summary of changes. You are about to add 1 region tag.
This comment is generated by snippet-bot.
|
There was a problem hiding this comment.
Code Review
This pull request introduces a new script to create a multi-region Spanner database protected by regional KMS keys (CMEK), along with its corresponding unit and integration tests. The review feedback suggests improving the live test robustness by gracefully skipping the test if required environment variables are missing, and dynamically asserting the number of configured KMS keys instead of hardcoding the value.
| mr_instance = os.environ["SPANNER_MR_INSTANCE_ID"] | ||
| mr_keys = [k for k in os.environ["SPANNER_MR_KMS_KEY_NAMES"].split(",") if k] |
There was a problem hiding this comment.
If SPANNER_MR_INSTANCE_ID or SPANNER_MR_KMS_KEY_NAMES are not set in the environment, this test will fail with a KeyError. Since these are not checked in conftest.py, it is safer to retrieve them using os.environ.get() and skip the test if they are missing.
mr_instance = os.environ.get("SPANNER_MR_INSTANCE_ID")
mr_keys_raw = os.environ.get("SPANNER_MR_KMS_KEY_NAMES")
if not mr_instance or not mr_keys_raw:
pytest.skip(
"SPANNER_MR_INSTANCE_ID and SPANNER_MR_KMS_KEY_NAMES environment "
"variables are required for this test."
)
mr_keys = [k for k in mr_keys_raw.split(",") if k]There was a problem hiding this comment.
Good catch, done: the live test now checks for its required environment variables up front and skips with a clear message when one is missing, instead of failing with a KeyError (c4de0f2).
| out, err = capsys.readouterr() | ||
| assert err == "" | ||
| assert conftest.LIVE_PROJECT_ID not in out | ||
| assert "Configured KMS Keys: 3" in out |
There was a problem hiding this comment.
The assertion hardcodes 3 KMS keys, but the live test uses the keys from the SPANNER_MR_KMS_KEY_NAMES environment variable, which may contain a different number of keys depending on the test environment. Use len(mr_keys) to make the assertion dynamic and robust.
| assert "Configured KMS Keys: 3" in out | |
| assert f"Configured KMS Keys: {len(mr_keys)}" in out |
There was a problem hiding this comment.
Done: the assertion now uses the number of keys supplied to the test (c4de0f2).
…_mr_cmek Problem: multi-line SQL and DDL statements were written as adjacent string fragments. That style is easy to get wrong (a missing trailing space joins two SQL tokens) and is harder to read than the statement itself, and the samples in this set mixed it with triple-quoted SQL. Solution: write each multi-line statement as a single triple-quoted string, matching the layout already used by the other samples. Only whitespace between SQL tokens changes; the statements are otherwise identical. Files updated: - spanner/database_admin/src/spanner_database_admin/create_database_with_mr_cmek.py
Problem: review of this sample identified correctness and robustness improvements in the sample and its tests. Solution: apply the accepted review suggestions and extend the tests to cover the updated behavior. Details are in the review thread replies. Files updated: - spanner/database_admin/tests/create_database_with_mr_cmek_test.py
Description
Adds
spanner_create_database_with_mr_cmekcreating a multi-region Spanner database protected by regional Cloud KMS keys.Checklist
Testing
Compliance & Style
spanner/, which the default repository owners cover.Post-Approval Actions