Coverage for /pythoncovmergedfiles/medio/medio/src/airflow/tests/hooks/test_subprocess.py: 0%

39 statements  

« prev     ^ index     » next       coverage.py v7.0.1, created at 2022-12-25 06:11 +0000

1# 

2# Licensed to the Apache Software Foundation (ASF) under one 

3# or more contributor license agreements. See the NOTICE file 

4# distributed with this work for additional information 

5# regarding copyright ownership. The ASF licenses this file 

6# to you under the Apache License, Version 2.0 (the 

7# "License"); you may not use this file except in compliance 

8# with the License. You may obtain a copy of the License at 

9# 

10# http://www.apache.org/licenses/LICENSE-2.0 

11# 

12# Unless required by applicable law or agreed to in writing, 

13# software distributed under the License is distributed on an 

14# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 

15# KIND, either express or implied. See the License for the 

16# specific language governing permissions and limitations 

17# under the License. 

18from __future__ import annotations 

19 

20from pathlib import Path 

21from subprocess import PIPE, STDOUT 

22from tempfile import TemporaryDirectory 

23from unittest import mock 

24from unittest.mock import MagicMock 

25 

26import pytest 

27 

28from airflow.hooks.subprocess import SubprocessHook 

29 

30OS_ENV_KEY = "SUBPROCESS_ENV_TEST" 

31OS_ENV_VAL = "this-is-from-os-environ" 

32 

33 

34class TestSubprocessHook: 

35 @pytest.mark.parametrize( 

36 "env,expected", 

37 [ 

38 ({"ABC": "123", "AAA": "456"}, {"ABC": "123", "AAA": "456", OS_ENV_KEY: ""}), 

39 ({}, {OS_ENV_KEY: ""}), 

40 (None, {OS_ENV_KEY: OS_ENV_VAL}), 

41 ], 

42 ids=["with env", "empty env", "no env"], 

43 ) 

44 def test_env(self, env, expected): 

45 """ 

46 Test that env variables are exported correctly to the command environment. 

47 When ``env`` is ``None``, ``os.environ`` should be passed to ``Popen``. 

48 Otherwise, the variables in ``env`` should be available, and ``os.environ`` should not. 

49 """ 

50 hook = SubprocessHook() 

51 

52 def build_cmd(keys, filename): 

53 """ 

54 Produce bash command to echo env vars into filename. 

55 Will always echo the special test var named ``OS_ENV_KEY`` into the file to test whether 

56 ``os.environ`` is passed or not. 

57 """ 

58 return "\n".join(f"echo {k}=${k}>> {filename}" for k in [*keys, OS_ENV_KEY]) 

59 

60 with TemporaryDirectory() as tmp_dir, mock.patch.dict("os.environ", {OS_ENV_KEY: OS_ENV_VAL}): 

61 tmp_file = Path(tmp_dir, "test.txt") 

62 command = build_cmd(env and env.keys() or [], tmp_file.as_posix()) 

63 hook.run_command(command=["bash", "-c", command], env=env) 

64 actual = dict([x.split("=") for x in tmp_file.read_text().splitlines()]) 

65 assert actual == expected 

66 

67 @pytest.mark.parametrize( 

68 "val,expected", 

69 [ 

70 ("test-val", "test-val"), 

71 ("test-val\ntest-val\n", ""), 

72 ("test-val\ntest-val", "test-val"), 

73 ("", ""), 

74 ], 

75 ) 

76 def test_return_value(self, val, expected): 

77 hook = SubprocessHook() 

78 result = hook.run_command(command=["bash", "-c", f'echo "{val}"']) 

79 assert result.output == expected 

80 

81 @mock.patch.dict("os.environ", clear=True) 

82 @mock.patch( 

83 "airflow.hooks.subprocess.TemporaryDirectory", 

84 return_value=MagicMock(__enter__=MagicMock(return_value="/tmp/airflowtmpcatcat")), 

85 ) 

86 @mock.patch( 

87 "airflow.hooks.subprocess.Popen", 

88 return_value=MagicMock(stdout=MagicMock(readline=MagicMock(side_effect=StopIteration), returncode=0)), 

89 ) 

90 def test_should_exec_subprocess(self, mock_popen, mock_temporary_directory): 

91 hook = SubprocessHook() 

92 hook.run_command(command=["bash", "-c", 'echo "stdout"']) 

93 

94 mock_popen.assert_called_once_with( 

95 ["bash", "-c", 'echo "stdout"'], 

96 cwd="/tmp/airflowtmpcatcat", 

97 env={}, 

98 preexec_fn=mock.ANY, 

99 stderr=STDOUT, 

100 stdout=PIPE, 

101 ) 

102 

103 def test_task_decode(self): 

104 hook = SubprocessHook() 

105 command = ["bash", "-c", 'printf "This will cause a coding error \\xb1\\xa6\\x01\n"'] 

106 result = hook.run_command(command=command) 

107 assert result.exit_code == 0