| 1 |
# -*- coding: utf-8 -*- |
| 2 |
""" |
| 3 |
Provides step definitions to perform tests with the Python logging subsystem. |
| 4 |
|
| 5 |
.. code-block: gherkin |
| 6 |
|
| 7 |
Given I create log records with: |
| 8 |
| category | level | message | |
| 9 |
| foo.bar | WARN | Hello LogRecord | |
| 10 |
| bar | CURRENT | Hello LogRecord | |
| 11 |
And I create a log record with: |
| 12 |
| category | level | message | |
| 13 |
| foo | ERROR | Hello Foo | |
| 14 |
Then the command output should contain the following log records: |
| 15 |
| category | level | message | |
| 16 |
| bar | CURRENT | xxx | |
| 17 |
Then the command output should not contain the following log records: |
| 18 |
| category | level | message | |
| 19 |
| bar | CURRENT | xxx | |
| 20 |
Then the file "behave.log" should contain the log records: |
| 21 |
| category | level | message | |
| 22 |
| bar | CURRENT | xxx | |
| 23 |
Then the file "behave.log" should not contain the log records: |
| 24 |
| category | level | message | |
| 25 |
| bar | CURRENT | xxx | |
| 26 |
|
| 27 |
Given I define the log record schema: |
| 28 |
| category | level | message | |
| 29 |
| root | INFO | Hello LogRecord | |
| 30 |
And I create log records with: |
| 31 |
| category | level | message | |
| 32 |
| foo.bar | INFO | Hello LogRecord | |
| 33 |
| bar | INFO | Hello LogRecord | |
| 34 |
Then the command output should contain log records from categories |
| 35 |
| category | |
| 36 |
| foo.bar | |
| 37 |
| bar | |
| 38 |
|
| 39 |
Given I use the log record configuration: |
| 40 |
| property | value | |
| 41 |
| format | LOG.%(levelname)-8s %(name)s %(message)s | |
| 42 |
| datefmt | | |
| 43 |
|
| 44 |
IDEA: |
| 45 |
|
| 46 |
.. code-block:: gherkin |
| 47 |
|
| 48 |
Given I capture log records |
| 49 |
When I create log records with: |
| 50 |
| category | level | message | |
| 51 |
| foo.bar | WARN | Hello LogRecord | |
| 52 |
Then the captured log should contain the following log records: |
| 53 |
| category | level | message | |
| 54 |
| bar | CURRENT | xxx | |
| 55 |
And the captured log should not contain the following log records: |
| 56 |
| category | level | message | |
| 57 |
| bar | CURRENT | xxx | |
| 58 |
""" |
| 59 |
|
| 60 |
from __future__ import absolute_import |
| 61 |
from behave import given, when, then, step |
| 62 |
from behave4cmd0.command_steps import \ |
| 63 |
step_file_should_contain_multiline_text, \ |
| 64 |
step_file_should_not_contain_multiline_text |
| 65 |
from behave.configuration import LogLevel |
| 66 |
from behave.log_capture import LoggingCapture |
| 67 |
import logging |
| 68 |
|
| 69 |
# ----------------------------------------------------------------------------- |
| 70 |
# STEP UTILS: |
| 71 |
# ----------------------------------------------------------------------------- |
| 72 |
def make_log_record(category, level, message): |
| 73 |
if category in ("root", "__ROOT__"): |
| 74 |
category = None |
| 75 |
logger = logging.getLogger(category) |
| 76 |
logger.log(level, message) |
| 77 |
|
| 78 |
def make_log_record_output(category, level, message, |
| 79 |
format=None, datefmt=None, **kwargs): |
| 80 |
""" |
| 81 |
Create the output for a log record, like performed by :mod:`logging` module. |
| 82 |
|
| 83 |
:param category: Name of the logger (as string or None). |
| 84 |
:param level: Log level (as number). |
| 85 |
:param message: Log message to use. |
| 86 |
:returns: Log record output (as string) |
| 87 |
""" |
| 88 |
if not category or (category == "__ROOT__"): |
| 89 |
category = "root" |
| 90 |
levelname = logging.getLevelName(level) |
| 91 |
record_data = dict(name=category, levelname=levelname, msg=message) |
| 92 |
record_data.update(kwargs) |
| 93 |
record = logging.makeLogRecord(record_data) |
| 94 |
formatter = logging.Formatter(format, datefmt=datefmt) |
| 95 |
return formatter.format(record) |
| 96 |
|
| 97 |
class LogRecordTable(object): |
| 98 |
|
| 99 |
@classmethod |
| 100 |
def make_output_for_row(cls, row, format=None, datefmt=None, **kwargs): |
| 101 |
category = row.get("category", None) |
| 102 |
level = LogLevel.parse_type(row.get("level", "INFO")) |
| 103 |
message = row.get("message", "__UNDEFINED__") |
| 104 |
return make_log_record_output(category, level, message, |
| 105 |
format, datefmt, **kwargs) |
| 106 |
|
| 107 |
@staticmethod |
| 108 |
def annotate_with_row_schema(table, row_schema): |
| 109 |
""" |
| 110 |
Annotate/extend a table of log-records with additional columns from |
| 111 |
the log-record schema if columns are missing. |
| 112 |
|
| 113 |
:param table: Table w/ log-records (as :class:`behave.model.Table`) |
| 114 |
:param row_schema: Log-record row schema (as dict). |
| 115 |
""" |
| 116 |
for column, value in row_schema.items(): |
| 117 |
if column not in table.headings: |
| 118 |
table.add_column(column, default_value=value) |
| 119 |
|
| 120 |
|
| 121 |
# ----------------------------------------------------------------------------- |
| 122 |
# STEP DEFINITIONS: |
| 123 |
# ----------------------------------------------------------------------------- |
| 124 |
# @step('I create log records for the following categories') |
| 125 |
# def step_I_create_logrecords_for_categories_with_text(context): |
| 126 |
# assert context.text is not None, "REQUIRE: context.text" |
| 127 |
# current_level = context.config.logging_level |
| 128 |
# categories = context.text.split() |
| 129 |
# for category_name in categories: |
| 130 |
# logger = logging.getLogger(category_name) |
| 131 |
# logger.log(current_level, "__LOG_RECORD__") |
| 132 |
|
| 133 |
@step('I create log records with') |
| 134 |
def step_I_create_logrecords_with_table(context): |
| 135 |
""" |
| 136 |
Step definition that creates one more log records by using a table. |
| 137 |
|
| 138 |
.. code-block: gherkin |
| 139 |
|
| 140 |
When I create log records with: |
| 141 |
| category | level | message | |
| 142 |
| foo | ERROR | Hello Foo | |
| 143 |
| foo.bar | WARN | Hello Foo.Bar | |
| 144 |
|
| 145 |
Table description |
| 146 |
------------------ |
| 147 |
|
| 148 |
| Column | Type | Required | Description | |
| 149 |
| category | string | yes | Category (or logger) to use. | |
| 150 |
| level | LogLevel | yes | Log level to use. | |
| 151 |
| message | string | yes | Log message to use. | |
| 152 |
|
| 153 |
.. code-block: python |
| 154 |
|
| 155 |
import logging |
| 156 |
from behave.configuration import LogLevel |
| 157 |
for row in table.rows: |
| 158 |
logger = logging.getLogger(row.category) |
| 159 |
level = LogLevel.parse_type(row.level) |
| 160 |
logger.log(level, row.message) |
| 161 |
""" |
| 162 |
assert context.table, "REQUIRE: context.table" |
| 163 |
context.table.require_columns(["category", "level", "message"]) |
| 164 |
for row in context.table.rows: |
| 165 |
category = row["category"] |
| 166 |
if category == "__ROOT__": |
| 167 |
category = None |
| 168 |
level = LogLevel.parse_type(row["level"]) |
| 169 |
message = row["message"] |
| 170 |
make_log_record(category, level, message) |
| 171 |
|
| 172 |
|
| 173 |
@step('I create a log record with') |
| 174 |
def step_I_create_logrecord_with_table(context): |
| 175 |
""" |
| 176 |
Create an log record by using a table to provide the parts. |
| 177 |
|
| 178 |
.. seealso: :func:`step_I_create_logrecords_with_table()` |
| 179 |
""" |
| 180 |
assert context.table, "REQUIRE: context.table" |
| 181 |
assert len(context.table.rows) == 1, "REQUIRE: table.row.size == 1" |
| 182 |
step_I_create_logrecords_with_table(context) |
| 183 |
|
| 184 |
@step('I define the log record schema') |
| 185 |
def step_I_define_logrecord_schema_with_table(context): |
| 186 |
assert context.table, "REQUIRE: context.table" |
| 187 |
context.table.require_columns(["category", "level", "message"]) |
| 188 |
assert len(context.table.rows) == 1, \ |
| 189 |
"REQUIRE: context.table.rows.size(%s) == 1" % (len(context.table.rows)) |
| 190 |
|
| 191 |
row = context.table.rows[0] |
| 192 |
row_schema = dict(category=row["category"], level=row["level"], |
| 193 |
message=row["message"]) |
| 194 |
context.log_record_row_schema = row_schema |
| 195 |
|
| 196 |
|
| 197 |
@then('the command output should contain the following log records') |
| 198 |
def step_command_output_should_contain_log_records(context): |
| 199 |
""" |
| 200 |
Verifies that the command output contains the specified log records |
| 201 |
(in any order). |
| 202 |
|
| 203 |
.. code-block: gherkin |
| 204 |
|
| 205 |
Then the command output should contain the following log records: |
| 206 |
| category | level | message | |
| 207 |
| bar | CURRENT | xxx | |
| 208 |
""" |
| 209 |
assert context.table, "REQUIRE: context.table" |
| 210 |
context.table.require_columns(["category", "level", "message"]) |
| 211 |
format = getattr(context, "log_record_format", context.config.logging_format) |
| 212 |
for row in context.table.rows: |
| 213 |
output = LogRecordTable.make_output_for_row(row, format) |
| 214 |
context.execute_steps(u''' |
| 215 |
Then the command output should contain: |
| 216 |
""" |
| 217 |
{expected_output} |
| 218 |
""" |
| 219 |
'''.format(expected_output=output)) |
| 220 |
|
| 221 |
|
| 222 |
@then('the command output should not contain the following log records') |
| 223 |
def step_command_output_should_not_contain_log_records(context): |
| 224 |
""" |
| 225 |
Verifies that the command output contains the specified log records |
| 226 |
(in any order). |
| 227 |
|
| 228 |
.. code-block: gherkin |
| 229 |
|
| 230 |
Then the command output should contain the following log records: |
| 231 |
| category | level | message | |
| 232 |
| bar | CURRENT | xxx | |
| 233 |
""" |
| 234 |
assert context.table, "REQUIRE: context.table" |
| 235 |
context.table.require_columns(["category", "level", "message"]) |
| 236 |
format = getattr(context, "log_record_format", context.config.logging_format) |
| 237 |
for row in context.table.rows: |
| 238 |
output = LogRecordTable.make_output_for_row(row, format) |
| 239 |
context.execute_steps(u''' |
| 240 |
Then the command output should not contain: |
| 241 |
""" |
| 242 |
{expected_output} |
| 243 |
""" |
| 244 |
'''.format(expected_output=output)) |
| 245 |
|
| 246 |
@then('the command output should contain the following log record') |
| 247 |
def step_command_output_should_contain_log_record(context): |
| 248 |
assert context.table, "REQUIRE: context.table" |
| 249 |
assert len(context.table.rows) == 1, "REQUIRE: table.row.size == 1" |
| 250 |
step_command_output_should_contain_log_records(context) |
| 251 |
|
| 252 |
|
| 253 |
@then('the command output should not contain the following log record') |
| 254 |
def step_command_output_should_not_contain_log_record(context): |
| 255 |
assert context.table, "REQUIRE: context.table" |
| 256 |
assert len(context.table.rows) == 1, "REQUIRE: table.row.size == 1" |
| 257 |
step_command_output_should_not_contain_log_records(context) |
| 258 |
|
| 259 |
@then('the command output should contain log records from categories') |
| 260 |
def step_command_output_should_contain_log_records_from_categories(context): |
| 261 |
""" |
| 262 |
Verifies that the command output contains the specified log records |
| 263 |
(in any order). |
| 264 |
|
| 265 |
.. code-block: gherkin |
| 266 |
|
| 267 |
Given I define a log record schema: |
| 268 |
| category | level | message | |
| 269 |
| root | ERROR | __LOG_MESSAGE__ | |
| 270 |
Then the command output should contain log records from categories: |
| 271 |
| category | |
| 272 |
| bar | |
| 273 |
""" |
| 274 |
assert context.table, "REQUIRE: context.table" |
| 275 |
context.table.require_column("category") |
| 276 |
record_schema = context.log_record_row_schema |
| 277 |
LogRecordTable.annotate_with_row_schema(context.table, record_schema) |
| 278 |
step_command_output_should_contain_log_records(context) |
| 279 |
context.table.remove_columns(["level", "message"]) |
| 280 |
|
| 281 |
|
| 282 |
@then('the command output should not contain log records from categories') |
| 283 |
def step_command_output_should_not_contain_log_records_from_categories(context): |
| 284 |
""" |
| 285 |
Verifies that the command output contains not log records from |
| 286 |
the provided log categories (in any order). |
| 287 |
|
| 288 |
.. code-block: gherkin |
| 289 |
|
| 290 |
Given I define the log record schema: |
| 291 |
| category | level | message | |
| 292 |
| root | ERROR | __LOG_MESSAGE__ | |
| 293 |
Then the command output should not contain log records from categories: |
| 294 |
| category | |
| 295 |
| bar | |
| 296 |
""" |
| 297 |
assert context.table, "REQUIRE: context.table" |
| 298 |
context.table.require_column("category") |
| 299 |
record_schema = context.log_record_row_schema |
| 300 |
LogRecordTable.annotate_with_row_schema(context.table, record_schema) |
| 301 |
step_command_output_should_not_contain_log_records(context) |
| 302 |
context.table.remove_columns(["level", "message"]) |
| 303 |
|
| 304 |
@then('the file "{filename}" should contain the log records') |
| 305 |
def step_file_should_contain_log_records(context, filename): |
| 306 |
""" |
| 307 |
Verifies that the command output contains the specified log records |
| 308 |
(in any order). |
| 309 |
|
| 310 |
.. code-block: gherkin |
| 311 |
|
| 312 |
Then the file "xxx.log" should contain the log records: |
| 313 |
| category | level | message | |
| 314 |
| bar | CURRENT | xxx | |
| 315 |
""" |
| 316 |
assert context.table, "REQUIRE: context.table" |
| 317 |
context.table.require_columns(["category", "level", "message"]) |
| 318 |
format = getattr(context, "log_record_format", context.config.logging_format) |
| 319 |
for row in context.table.rows: |
| 320 |
output = LogRecordTable.make_output_for_row(row, format) |
| 321 |
context.text = output |
| 322 |
step_file_should_contain_multiline_text(context, filename) |
| 323 |
|
| 324 |
@then('the file "{filename}" should not contain the log records') |
| 325 |
def step_file_should_not_contain_log_records(context, filename): |
| 326 |
""" |
| 327 |
Verifies that the command output contains the specified log records |
| 328 |
(in any order). |
| 329 |
|
| 330 |
.. code-block: gherkin |
| 331 |
|
| 332 |
Then the file "xxx.log" should not contain the log records: |
| 333 |
| category | level | message | |
| 334 |
| bar | CURRENT | xxx | |
| 335 |
""" |
| 336 |
assert context.table, "REQUIRE: context.table" |
| 337 |
context.table.require_columns(["category", "level", "message"]) |
| 338 |
format = getattr(context, "log_record_format", context.config.logging_format) |
| 339 |
for row in context.table.rows: |
| 340 |
output = LogRecordTable.make_output_for_row(row, format) |
| 341 |
context.text = output |
| 342 |
step_file_should_not_contain_multiline_text(context, filename) |
| 343 |
|
| 344 |
|
| 345 |
@step('I use "{log_record_format}" as log record format') |
| 346 |
def step_use_log_record_format_text(context, log_record_format): |
| 347 |
context.log_record_format = log_record_format |
| 348 |
|
| 349 |
@step('I use the log record configuration') |
| 350 |
def step_use_log_record_configuration(context): |
| 351 |
""" |
| 352 |
Define log record configuration parameters. |
| 353 |
|
| 354 |
.. code-block: gherkin |
| 355 |
|
| 356 |
Given I use the log record configuration: |
| 357 |
| property | value | |
| 358 |
| format | | |
| 359 |
| datefmt | | |
| 360 |
""" |
| 361 |
assert context.table, "REQUIRE: context.table" |
| 362 |
context.table.require_columns(["property", "value"]) |
| 363 |
for row in context.table.rows: |
| 364 |
property_name = row["property"] |
| 365 |
value = row["value"] |
| 366 |
if property_name == "format": |
| 367 |
context.log_record_format = value |
| 368 |
elif property_name == "datefmt": |
| 369 |
context.log_record_datefmt = value |
| 370 |
else: |
| 371 |
raise KeyError("Unknown property=%s" % property_name) |
| 372 |
|
| 373 |
|
| 374 |
# ----------------------------------------------------------------------------- |
| 375 |
# TODO: STEP DEFINITIONS: |
| 376 |
# ----------------------------------------------------------------------------- |
| 377 |
@step('I capture log records with level "{level}" or above') |
| 378 |
def step_I_capture_logrecords(context, level): |
| 379 |
raise NotImplementedError() |
| 380 |
|
| 381 |
|
| 382 |
@step('I capture log records') |
| 383 |
def step_I_capture_logrecords(context): |
| 384 |
""" |
| 385 |
|
| 386 |
.. code-block: gherkin |
| 387 |
Given I capture log records |
| 388 |
When I capture log records |
| 389 |
|
| 390 |
:param context: |
| 391 |
""" |
| 392 |
raise NotImplementedError() |
| 393 |
logcapture = getattr(context, "logcapture", None) |
| 394 |
if not logcapture: |
| 395 |
context.logcapture = LoggingCapture() |