ecerulm opened a new issue #18268:
URL: https://github.com/apache/airflow/issues/18268


   ### Apache Airflow version
   
   main (development)
   
   ### Operating System
   
   macOS Big Sur
   
   ### Versions of Apache Airflow Providers
   
   _No response_
   
   ### Deployment
   
   Other
   
   ### Deployment details
   
   _No response_
   
   ### What happened
   
   `breeze tests tests` fails at 
`tests/hooks/test_subprocess.py::TestSubprocessHook::test_env_1_empty_env` 
   
   This can be easily reproduced  with pytest directly just by including 2 
tests `TestSentryHook::test_add_tagging` and `tests/hooks/test_subprocesses.py`
   
       pytest tests/core/test_sentry.py::TestSentryHook::test_add_tagging 
tests/hooks/test_subprocess.py::TestSubprocessHook::test_env_1_empty_env
   
   
   The test fails because after `TestSentryHook::test_add_tagging`  the 
`subprocess.Popen` is monkey patched to sentry's own 
[`sentry_sdk.integrations.stdlib.Popen`](https://github.com/getsentry/sentry-python/blob/master/sentry_sdk/integrations/stdlib.py#L186)
 which behaves differently that the  python's built-in  
[`subprocess.Popen`](https://docs.python.org/3/library/subprocess.html#popen-constructor).
 sentry's version always adds the contents of `os.environ` to the environment 
of the subprocess while the original stdlib version will not. 
   
   
   The `test_env_1_empty_env` sole purpose seems to check that the `os.environ` 
is not passed  to the subprocess if `env=={}`  but unfortunately as soon as you 
enable sentry that is no longer true. 
   
   
   
     
   
   ```
   
====================================================================================================================
 FAILURES 
====================================================================================================================
   
____________________________________________________________________________________________________
 TestSubprocessHook.test_env_1_empty_env 
_____________________________________________________________________________________________________
   
   a = (<tests.hooks.test_subprocess.TestSubprocessHook 
testMethod=test_env_1_empty_env>,)
   
       @wraps(func)
       def standalone_func(*a):
   >       return func(*(a + p.args), **p.kwargs)
   
   
../../.pyenv/versions/3.8.8/envs/venv-airflow-38/lib/python3.8/site-packages/parameterized/parameterized.py:533:
   _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
_ _ _
   
   self = <tests.hooks.test_subprocess.TestSubprocessHook 
testMethod=test_env_1_empty_env>, name = 'empty env', env = {}, expected = 
{'SUBPROCESS_ENV_TEST': ''}
   
       @parameterized.expand(
           [
               ('with env', {'ABC': '123', 'AAA': '456'}, {'ABC': '123', 'AAA': 
'456', OS_ENV_KEY: ''}),
               ('empty env', {}, {OS_ENV_KEY: ''}),
               ('no env', None, {OS_ENV_KEY: OS_ENV_VAL}),
           ]
       )
       def test_env(self, name, env, expected):
           """
           Test that env variables are exported correctly to the command 
environment.
           When ``env`` is ``None``, ``os.environ`` should be passed to 
``Popen``.
           Otherwise, the variables in ``env`` should be available, and 
``os.environ`` should not.
           """
           hook = SubprocessHook()
   
           def build_cmd(keys, filename):
               """
               Produce bash command to echo env vars into filename.
               Will always echo the special test var named ``OS_ENV_KEY`` into 
the file to test whether
               ``os.environ`` is passed or not.
               """
               return '\n'.join(f"echo {k}=${k}>> {filename}" for k in [*keys, 
OS_ENV_KEY])
   
           with TemporaryDirectory() as tmp_dir, mock.patch.dict('os.environ', 
{OS_ENV_KEY: OS_ENV_VAL}):
               tmp_file = Path(tmp_dir, 'test.txt')
               command = build_cmd(env and env.keys() or [], 
tmp_file.as_posix())
               hook.run_command(command=['bash', '-c', command], env=env)
               actual = dict([x.split('=') for x in 
tmp_file.read_text().splitlines()])
   >           assert actual == expected
   E           AssertionError: assert {'SUBPROCESS_ENV_TEST': 
'this-is-from-os-environ'} == {'SUBPROCESS_ENV_TEST': ''}
   E             Differing items:
   E             {'SUBPROCESS_ENV_TEST': 'this-is-from-os-environ'} != 
{'SUBPROCESS_ENV_TEST': ''}
   E             Full diff:
   E             - {'SUBPROCESS_ENV_TEST': ''}
   E             + {'SUBPROCESS_ENV_TEST': 'this-is-from-os-environ'}
   
   tests/hooks/test_subprocess.py:62: AssertionError
   ```
   
   
   ### What you expected to happen
   
   I expect to be able to run `breeze tests tests` with no failing tests. 
   
   Maybe test_sentry should not be run by default. 
   
   I also expect/hope that enabling 
[`sentry_on`](https://airflow.apache.org/docs/apache-airflow/stable/configurations-ref.html#sentry-on)
 on `airflow.cfg` does not cause behavioural changes, it seems to me that 
`test_env_1_empty_env` was added to **explicitly check that the values of 
certain environment variables were not leaked** to subprocesses. But I they 
will be leaked as soon as `sentry_on=True`
    
   
   
   ### How to reproduce
   
   pyenv virtualenv 3.8.8 venv-airflow-38
   pyenv shell venv-airflow-38
   breeze initialize-local-virtualenv
   pytest tests/core/test_sentry.py::TestSentryHook::test_add_tagging 
tests/hooks/test_subprocess.py::TestSubprocessHook::test_env_1_empty_env
   
   ### Anything else
   
   _No response_
   
   ### Are you willing to submit PR?
   
   - [ ] Yes I am willing to submit a PR!
   
   ### Code of Conduct
   
   - [X] I agree to follow this project's [Code of 
Conduct](https://github.com/apache/airflow/blob/main/CODE_OF_CONDUCT.md)
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to