Refactor tests: Consolidate env var tests into test__mtls_helper.py#1952
Refactor tests: Consolidate env var tests into test__mtls_helper.py#1952chalmerlowe wants to merge 1 commit intomainfrom
Conversation
Co-authored-by: chalmerlowe <7291104+chalmerlowe@users.noreply.github.com>
|
👋 Jules, reporting for duty! I'm here to lend a hand with this pull request. When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down. I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job! For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with New to Jules? Learn more at jules.google/docs. For security, I will only act on instructions from the user who triggered this task. |
Summary of ChangesHello @chalmerlowe, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request enhances the mTLS client certificate handling by introducing fallback mechanisms to Cloud SDK environment variables. It centralizes the decision-making logic for mTLS usage within Highlights
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request introduces fallback mechanisms for mTLS configuration using new Cloud SDK-related environment variables, centralizing logic and improving the project structure. The changes are well-tested, including a specific case for empty environment variables. I've provided a few suggestions to further improve code clarity and simplify the test setup for better maintainability.
| cert_path = getenv(environment_vars.GOOGLE_API_CERTIFICATE_CONFIG) | ||
| if cert_path is None: | ||
| cert_path = getenv( | ||
| environment_vars.CLOUDSDK_CONTEXT_AWARE_CERTIFICATE_CONFIG_FILE_PATH | ||
| ) |
There was a problem hiding this comment.
To make the code more concise, you can use the or operator to chain the getenv calls. This will achieve the same fallback logic in a single line.
| cert_path = getenv(environment_vars.GOOGLE_API_CERTIFICATE_CONFIG) | |
| if cert_path is None: | |
| cert_path = getenv( | |
| environment_vars.CLOUDSDK_CONTEXT_AWARE_CERTIFICATE_CONFIG_FILE_PATH | |
| ) | |
| cert_path = getenv( | |
| environment_vars.GOOGLE_API_CERTIFICATE_CONFIG | |
| ) or getenv( | |
| environment_vars.CLOUDSDK_CONTEXT_AWARE_CERTIFICATE_CONFIG_FILE_PATH | |
| ) |
| with mock.patch.dict(os.environ, { | ||
| environment_vars.CLOUDSDK_CONTEXT_AWARE_CERTIFICATE_CONFIG_FILE_PATH: cloudsdk_path | ||
| }): | ||
| if environment_vars.GOOGLE_API_CERTIFICATE_CONFIG in os.environ: | ||
| del os.environ[environment_vars.GOOGLE_API_CERTIFICATE_CONFIG] | ||
|
|
||
| with mock.patch("os.path.exists", return_value=True): | ||
| assert _mtls_helper._get_cert_config_path() == cloudsdk_path |
There was a problem hiding this comment.
To simplify the test setup and avoid modifying os.environ directly, you can use the clear=True argument in mock.patch.dict. This ensures the test runs in a clean environment, making the test more robust and readable.
| with mock.patch.dict(os.environ, { | |
| environment_vars.CLOUDSDK_CONTEXT_AWARE_CERTIFICATE_CONFIG_FILE_PATH: cloudsdk_path | |
| }): | |
| if environment_vars.GOOGLE_API_CERTIFICATE_CONFIG in os.environ: | |
| del os.environ[environment_vars.GOOGLE_API_CERTIFICATE_CONFIG] | |
| with mock.patch("os.path.exists", return_value=True): | |
| assert _mtls_helper._get_cert_config_path() == cloudsdk_path | |
| with mock.patch.dict(os.environ, { | |
| environment_vars.CLOUDSDK_CONTEXT_AWARE_CERTIFICATE_CONFIG_FILE_PATH: cloudsdk_path | |
| }, clear=True): | |
| with mock.patch("os.path.exists", return_value=True): | |
| assert _mtls_helper._get_cert_config_path() == cloudsdk_path |
| def test_use_client_cert_fallback(self): | ||
| # Fallback to CLOUDSDK_CONTEXT_AWARE_USE_CLIENT_CERTIFICATE if GOOGLE_API_USE_CLIENT_CERTIFICATE is unset | ||
| with mock.patch.dict(os.environ, { | ||
| environment_vars.CLOUDSDK_CONTEXT_AWARE_USE_CLIENT_CERTIFICATE: "true" | ||
| }): | ||
| # Ensure GOOGLE_API_USE_CLIENT_CERTIFICATE is not set | ||
| if environment_vars.GOOGLE_API_USE_CLIENT_CERTIFICATE in os.environ: | ||
| del os.environ[environment_vars.GOOGLE_API_USE_CLIENT_CERTIFICATE] | ||
| assert _mtls_helper.check_use_client_cert() is True | ||
|
|
||
| with mock.patch.dict(os.environ, { | ||
| environment_vars.CLOUDSDK_CONTEXT_AWARE_USE_CLIENT_CERTIFICATE: "false" | ||
| }): | ||
| if environment_vars.GOOGLE_API_USE_CLIENT_CERTIFICATE in os.environ: | ||
| del os.environ[environment_vars.GOOGLE_API_USE_CLIENT_CERTIFICATE] | ||
| assert _mtls_helper.check_use_client_cert() is False |
There was a problem hiding this comment.
To make the test setup cleaner and more reliable, you can use clear=True with mock.patch.dict. This will ensure that os.environ is cleared before applying the mocked values, removing the need to manually delete environment variables within the test.
def test_use_client_cert_fallback(self):
# Fallback to CLOUDSDK_CONTEXT_AWARE_USE_CLIENT_CERTIFICATE if GOOGLE_API_USE_CLIENT_CERTIFICATE is unset
with mock.patch.dict(os.environ, {
environment_vars.CLOUDSDK_CONTEXT_AWARE_USE_CLIENT_CERTIFICATE: "true"
}, clear=True):
assert _mtls_helper.check_use_client_cert() is True
with mock.patch.dict(os.environ, {
environment_vars.CLOUDSDK_CONTEXT_AWARE_USE_CLIENT_CERTIFICATE: "false"
}, clear=True):
assert _mtls_helper.check_use_client_cert() is False| with mock.patch.dict(os.environ, { | ||
| environment_vars.CLOUDSDK_CONTEXT_AWARE_CERTIFICATE_CONFIG_FILE_PATH: cloudsdk_path | ||
| }): | ||
| if environment_vars.GOOGLE_API_CERTIFICATE_CONFIG in os.environ: | ||
| del os.environ[environment_vars.GOOGLE_API_CERTIFICATE_CONFIG] | ||
| if environment_vars.GOOGLE_API_USE_CLIENT_CERTIFICATE in os.environ: | ||
| del os.environ[environment_vars.GOOGLE_API_USE_CLIENT_CERTIFICATE] | ||
| if environment_vars.CLOUDSDK_CONTEXT_AWARE_USE_CLIENT_CERTIFICATE in os.environ: | ||
| del os.environ[environment_vars.CLOUDSDK_CONTEXT_AWARE_USE_CLIENT_CERTIFICATE] | ||
|
|
||
| assert _mtls_helper.check_use_client_cert() is True |
There was a problem hiding this comment.
To simplify this test, you can use clear=True in mock.patch.dict. This will provide a clean environment for your test, removing the need to manually delete several environment variables and making the test's intent clearer.
with mock.patch.dict(os.environ, {
environment_vars.CLOUDSDK_CONTEXT_AWARE_CERTIFICATE_CONFIG_FILE_PATH: cloudsdk_path
}, clear=True):
assert _mtls_helper.check_use_client_cert() is True
Moved the new mTLS environment variable tests from
tests/transport/test_mtls_env_vars.pyintotests/transport/test__mtls_helper.pyto maintain a cleaner project structure and keep related tests together. Also added a specific test case to verify that an empty string forGOOGLE_API_USE_CLIENT_CERTIFICATEcorrectly prevents fallback to the Cloud SDK variable.PR created automatically by Jules for task 3158735014071139371 started by @chalmerlowe