Skip to content

Commit

Permalink
tests: Add logger config with fixture
Browse files Browse the repository at this point in the history
Pyln logger's configuration was unexpectedly being overwritten by the root logger during the autogenerated examples test, complicating error debugging.

We resolved this by introducing a pytest fixture to reapply the logger configuration before the tests executes.

Changelog-None.

Fixes: #8023
  • Loading branch information
ShahanaFarooqui authored and rustyrussell committed Jan 24, 2025
1 parent 32a991a commit 7460b7d
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ preventing accidental overwrites from unrelated tests.
where `n` can be any number of repetitions. OR by manually running the test multiple times with:

```bash
rm -rf /tmp/ltests* && make -s && VALGRIND=0 TIMEOUT=40 TEST_DEBUG=1 GENERATE_EXAMPLES=1 pytest -vvv tests/autogenerate-rpc-examples.py
rm -rf /tmp/ltests* && make -s && VALGRIND=0 TIMEOUT=40 TEST_DEBUG=1 GENERATE_EXAMPLES=1 pytest -vvv -s tests/autogenerate-rpc-examples.py
```

- Identify changing values, and add them to `REPLACE_RESPONSE_VALUES`:
Expand All @@ -117,7 +117,7 @@ environment variable with a comma-separated list of method names. Eg. `REGENERAT
only regenerate examples for the `getinfo` and `connect` RPCs.
2. To regenerate specific examples, set the REGENERATE environment variable:
```bash
REGENERATE='getinfo,connect' VALGRIND=0 TIMEOUT=10 TEST_DEBUG=1 GENERATE_EXAMPLES=1 pytest -vvv tests/autogenerate-rpc-examples.py
REGENERATE='getinfo,connect' VALGRIND=0 TIMEOUT=10 TEST_DEBUG=1 GENERATE_EXAMPLES=1 pytest -vvv -s tests/autogenerate-rpc-examples.py
```
3. Logs are saved in `tests/autogenerate-examples-status.log`, and JSON data is in `tests/autogenerate-examples.json`.
4. Run `make` after the script completes to ensure schema updates are applied in other places too, such as `...msggen/schema.json`.
Expand All @@ -127,4 +127,4 @@ REGENERATE='getinfo,connect' VALGRIND=0 TIMEOUT=10 TEST_DEBUG=1 GENERATE_EXAMPLE

1. Sip whenever you have an additional comma at the end of a sequence.
2. Sip whenever you omit a comma in a sequence because you cut & paste.
3. Skull whenever you wish JSON had comments.
3. Skull whenever you wish JSON had comments.
30 changes: 17 additions & 13 deletions tests/autogenerate-rpc-examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -297,15 +297,6 @@

if os.path.exists(LOG_FILE):
open(LOG_FILE, 'w').close()

logging.basicConfig(level=logging.INFO,
format='%(asctime)s - %(levelname)s - %(message)s',
datefmt='%H:%M:%S',
handlers=[
logging.FileHandler(LOG_FILE),
logging.StreamHandler()
])

logger = logging.getLogger(__name__)


Expand Down Expand Up @@ -2035,6 +2026,19 @@ def generate_list_examples(l1, l2, l3, c12, c23_2, inv_l31, inv_l32, offer_l23,
raise


@pytest.fixture(autouse=True)
def setup_logging():
global logger
logger.setLevel(logging.DEBUG)
formatter = logging.Formatter("%(asctime)s - %(levelname)s - %(message)s", "%H:%M:%S")
stream_handler = logging.StreamHandler()
stream_handler.setFormatter(formatter)
logger.addHandler(stream_handler)
file_handler = logging.FileHandler(LOG_FILE)
file_handler.setFormatter(formatter)
logger.addHandler(file_handler)


@unittest.skipIf(not GENERATE_EXAMPLES, 'Generates examples for doc/schema/lightning-*.json files.')
def test_generate_examples(node_factory, bitcoind, executor):
"""Re-generates examples for doc/schema/lightning-*.json files"""
Expand All @@ -2055,9 +2059,9 @@ def list_all_examples():
for node in ast.walk(tree):
if isinstance(node, ast.Call) and isinstance(node.func, ast.Name) and node.func.id == 'update_example':
for keyword in node.keywords:
if (keyword.arg == 'method' and isinstance(keyword.value, ast.Str)):
if keyword.value.s not in methods:
methods.append(keyword.value.s)
if (keyword.arg == 'method' and isinstance(keyword.value, ast.Constant)):
if keyword.value.value not in methods:
methods.append(keyword.value.value)
return methods
except Exception as e:
logger.error(f'Error in listing all examples: {e}')
Expand Down Expand Up @@ -2105,5 +2109,5 @@ def list_missing_examples():
update_examples_in_schema_files()
logger.info('All Done!!!')
except Exception as e:
logger.error(e)
logger.error(e, exc_info=True)
sys.exit(1)

0 comments on commit 7460b7d

Please sign in to comment.