Coverage for tests/cli/test_logs.py: 89%

34 statements  

« prev     ^ index     » next       coverage.py v7.17.3, created at 2026-11-17 21:20 +0000

1import logging 

2import time 

3from pathlib import Path 

4from unittest.mock import AsyncMock, MagicMock, patch 

5 

6import pytest 

7from typer.testing import CliRunner 

8 

9from faststream import FastStream 

10from faststream._internal.cli.main import cli as faststream_app 

11from faststream._internal.cli.utils.logs import get_log_level 

12from tests.cli import interfaces 

13from tests.marks import skip_windows 

14 

15 

16@pytest.mark.parametrize( 

17 ( 

18 "level", 

19 "expected_level", 

20 ), 

21 ( 

22 pytest.param("critical", logging.CRITICAL), 

23 pytest.param("fatal", logging.FATAL), 

24 pytest.param("error", logging.ERROR), 

25 pytest.param("warning", logging.WARNING), 

26 pytest.param("warn", logging.WARNING), 

27 pytest.param("info", logging.INFO), 

28 pytest.param("debug", logging.DEBUG), 

29 pytest.param("notset", logging.NOTSET), 

30 ), 

31) 

32def test_get_level(level: str, expected_level: int) -> None: 

33 assert get_log_level(level) == expected_level 

34 

35 

36@pytest.mark.slow() 

37@skip_windows 

38@pytest.mark.parametrize( 

39 ("log_config_file_name", "log_config"), 

40 ( 

41 pytest.param( 

42 "config.json", 

43 """ 

44 { 

45 "version": 1, 

46 "loggers": { 

47 "unique_logger_name": { 

48 "level": 42 

49 } 

50 } 

51 } 

52 """, 

53 id="json config", 

54 ), 

55 pytest.param( 

56 "config.toml", 

57 """ 

58 version = 1 

59 

60 [loggers.unique_logger_name] 

61 level = 42 

62 """, 

63 id="toml config", 

64 ), 

65 pytest.param( 

66 "config.yaml", 

67 """ 

68 version: 1 

69 loggers: 

70 unique_logger_name: 

71 level: 42 

72 """, 

73 id="yaml config", 

74 ), 

75 ), 

76) 

77def test_run_as_asgi_with_log_config( 

78 generate_template: interfaces.GenerateTemplateFactory, 

79 faststream_cli: interfaces.FastStreamCLIFactory, 

80 log_config_file_name: str, 

81 log_config: str, 

82) -> None: 

83 app_code = """ 

84 import logging 

85 

86 from faststream.asgi import AsgiFastStream 

87 from faststream.nats import NatsBroker 

88 

89 broker = NatsBroker() 

90 

91 app = AsgiFastStream(broker) 

92 

93 logger = logging.getLogger("faststream") 

94 

95 @app.on_startup 

96 def print_log_level() -> None: 

97 logger.critical(f"Current log level is {logging.getLogger('unique_logger_name').level}") 

98 """ 

99 with ( 

100 generate_template(app_code) as app_path, 

101 generate_template( 

102 log_config, 

103 filename=log_config_file_name, 

104 ) as log_config_file_path, 

105 faststream_cli( 

106 "faststream", 

107 "run", 

108 f"{app_path.stem}:app", 

109 "--log-config", 

110 str(log_config_file_path), 

111 ) as cli, 

112 ): 

113 assert cli.wait_for_stderr("Current log level is 42") 

114 

115 

116@pytest.mark.slow() 

117@skip_windows 

118@pytest.mark.flaky(reruns=3, reruns_delay=1) 

119def test_run_as_asgi_mp_with_log_level( 

120 generate_template: interfaces.GenerateTemplateFactory, 

121 faststream_cli: interfaces.FastStreamCLIFactory, 

122 tmp_path: Path, 

123) -> None: 

124 marker_file = tmp_path / "log_level" 

125 

126 app_code = """ 

127 import logging 

128 import os 

129 from pathlib import Path 

130 

131 from faststream.asgi import AsgiFastStream 

132 from faststream.nats import NatsBroker 

133 

134 app = AsgiFastStream(NatsBroker()) 

135 

136 @app.on_startup 

137 def write_log_level() -> None: 

138 level = logging.getLogger("uvicorn.asgi").level 

139 Path(os.environ["LOG_LEVEL_FILE"]).write_text(str(level)) 

140 """ 

141 log_level, numeric_log_level = "warn", 30 

142 

143 with ( 

144 generate_template(app_code) as app_path, 

145 faststream_cli( 

146 "faststream", 

147 "run", 

148 f"{app_path.stem}:app", 

149 "--workers", 

150 "2", 

151 "--log-level", 

152 log_level, 

153 extra_env={"LOG_LEVEL_FILE": str(marker_file)}, 

154 ), 

155 ): 

156 deadline = time.time() + 15 

157 while time.time() < deadline: 157 ↛ 163line 157 didn't jump to line 163 because the condition on line 157 was always true

158 if marker_file.exists() and (content := marker_file.read_text().strip()): 158 ↛ 161line 158 didn't jump to line 161 because the condition on line 158 was always true

159 assert content == str(numeric_log_level) 

160 return 

161 time.sleep(0.1) 

162 

163 msg = f"Marker file {marker_file} was never written" 

164 raise AssertionError(msg) 

165 

166 

167def test_run_with_log_level(runner: CliRunner) -> None: 

168 app = FastStream(MagicMock()) 

169 

170 with ( 

171 patch.object(app, "run", AsyncMock()), 

172 patch( 

173 "faststream._internal.cli.utils.imports._import_object_or_factory", 

174 return_value=(None, app), 

175 ), 

176 ): 

177 result = runner.invoke( 

178 faststream_app, 

179 ["run", "-l", "warning", "faststream:app"], 

180 ) 

181 

182 assert result.exit_code == 0, result.output 

183 

184 assert isinstance(app.logger, logging.Logger) 

185 assert app.logger.level == logging.WARNING 

186 

187 

188def test_run_with_wrong_log_level(runner: CliRunner) -> None: 

189 app = FastStream(MagicMock()) 

190 

191 with ( 

192 patch.object(app, "run", AsyncMock()), 

193 patch( 

194 "faststream._internal.cli.utils.imports._import_object_or_factory", 

195 return_value=(None, app), 

196 ), 

197 ): 

198 result = runner.invoke( 

199 faststream_app, 

200 ["run", "-l", "30", "faststream:app"], 

201 ) 

202 

203 assert result.exit_code == 2, result.output