test: Use TestNode datadir_path or chain_path where possible #27884

pull maflcko wants to merge 4 commits into bitcoin:master from maflcko:2306-fs-stuff- changing 43 files +149 −164
  1. maflcko commented at 10:31 AM on June 14, 2023: member

    It seems inconsistent, fragile and verbose to:

    • Call get_datadir_path to recreate the path that already exists as field in TestNode
    • Call os.path.join with the hardcoded chain name or self.chain to recreate the TestNode chain_path property
    • Sometimes even use the hardcoded node dir name ("node0")

    Fix all issues by using the TestNode properties.

  2. DrahtBot commented at 10:31 AM on June 14, 2023: contributor

    <!--e57a25ab6845829454e8d69fc972939a-->

    The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

    <!--021abf342d371248e50ceaed478a90ca-->

    Reviews

    See the guideline for information on the review process.

    Type Reviewers
    ACK theStack, willcl-ark

    If your review is incorrectly listed, please react with 👎 to this comment and the bot will ignore it on the next update.

    <!--174a7506f384e20aa4161008e828411d-->

    Conflicts

    Reviewers, this pull request conflicts with the following ones:

    • #27101 (Support JSON-RPC 2.0 when requested by client by pinheadmz)
    • #20892 (tests: Run both descriptor and legacy tests within a single test invocation by achow101)

    If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first.

  3. DrahtBot renamed this:
    test: Use TestNode datadir_path or chain_path where possible
    test: Use TestNode datadir_path or chain_path where possible
    on Jun 14, 2023
  4. DrahtBot added the label Tests on Jun 14, 2023
  5. maflcko force-pushed on Jun 14, 2023
  6. DrahtBot added the label CI failed on Jun 14, 2023
  7. maflcko force-pushed on Jun 14, 2023
  8. DrahtBot removed the label CI failed on Jun 14, 2023
  9. fanquake requested review from theStack on Jun 16, 2023
  10. fanquake requested review from willcl-ark on Jun 16, 2023
  11. willcl-ark commented at 11:45 AM on June 16, 2023: member

    Concept ACK.

    In addition to those highlighted by TheStack, there are also quite a few other places we could update to use this, e.g. some of these matches:

    $ git grep -n "os.path" test/functional | rg -e '.*\.chain*'
    test/functional/feature_addrman.py:56:        peers_dat = os.path.join(self.nodes[0].datadir, self.chain, "peers.dat")
    test/functional/feature_asmap.py:116:        self.datadir = os.path.join(self.node.datadir, self.chain)
    test/functional/feature_blocksdir.py:33:        assert os.path.isfile(os.path.join(blocksdir_path, self.chain, "blocks", "blk00000.dat"))
    test/functional/feature_blocksdir.py:34:        assert os.path.isdir(os.path.join(self.nodes[0].datadir, self.chain, "blocks", "index"))
    test/functional/feature_config_args.py:418:        assert os.path.exists(os.path.join(new_data_dir, self.chain, 'blocks'))
    test/functional/feature_config_args.py:424:        assert os.path.exists(os.path.join(new_data_dir_2, self.chain, 'blocks'))
    test/functional/feature_fee_estimation.py:323:        fee_dat = os.path.join(self.nodes[0].datadir, self.chain, "fee_estimates.dat")
    test/functional/feature_filelock.py:27:        datadir = os.path.join(self.nodes[0].datadir, self.chain)
    test/functional/feature_loadblock.py:40:        blocks_dir = os.path.join(data_dir, self.chain, "blocks")
    test/functional/feature_logging.py:19:        return os.path.join(self.nodes[0].datadir, self.chain, name)
    test/functional/feature_posix_fs_permissions.py:34:        datadir = os.path.join(self.nodes[0].datadir, self.chain)
    test/functional/feature_pruning.py:94:        self.prunedir = os.path.join(self.nodes[2].datadir, self.chain, 'blocks', '')
    test/functional/feature_pruning.py:293:            return os.path.isfile(os.path.join(self.nodes[node_number].datadir, self.chain, "blocks", f"blk{index:05}.dat"))
    test/functional/feature_reindex.py:42:        blk0 = os.path.join(self.nodes[0].datadir, self.nodes[0].chain, 'blocks', 'blk00000.dat')
    test/functional/feature_remove_pruned_files_on_startup.py:23:        blk0 = os.path.join(self.nodes[0].datadir, self.nodes[0].chain, 'blocks', 'blk00000.dat')
    test/functional/feature_remove_pruned_files_on_startup.py:24:        rev0 = os.path.join(self.nodes[0].datadir, self.nodes[0].chain, 'blocks', 'rev00000.dat')
    test/functional/feature_remove_pruned_files_on_startup.py:25:        blk1 = os.path.join(self.nodes[0].datadir, self.nodes[0].chain, 'blocks', 'blk00001.dat')
    test/functional/feature_remove_pruned_files_on_startup.py:26:        rev1 = os.path.join(self.nodes[0].datadir, self.nodes[0].chain, 'blocks', 'rev00001.dat')
    test/functional/feature_txindex_compatibility.py:53:        legacy_chain_dir = os.path.join(self.nodes[0].datadir, self.chain)
    test/functional/feature_txindex_compatibility.py:56:        migrate_chain_dir = os.path.join(self.nodes[2].datadir, self.chain)
    test/functional/feature_txindex_compatibility.py:67:        drop_index_chain_dir = os.path.join(self.nodes[1].datadir, self.chain)
    test/functional/interface_rpc.py:49:        assert_equal(info['logpath'], os.path.join(self.nodes[0].datadir, self.chain, 'debug.log'))
    test/functional/mempool_compatibility.py:58:        old_node_mempool = os.path.join(old_node.datadir, self.chain, 'mempool.dat')
    test/functional/mempool_compatibility.py:59:        new_node_mempool = os.path.join(new_node.datadir, self.chain, 'mempool.dat')
    test/functional/mempool_persist.py:146:        mempooldat0 = os.path.join(self.nodes[0].datadir, self.chain, 'mempool.dat')
    test/functional/mempool_persist.py:147:        mempooldat1 = os.path.join(self.nodes[1].datadir, self.chain, 'mempool.dat')
    test/functional/test_framework/test_framework.py:826:                return os.path.join(cache_node_dir, self.chain, *paths)
    test/functional/tool_wallet.py:404:        self.wallet_path = os.path.join(self.nodes[0].datadir, self.chain, 'wallets', self.default_wallet_name, self.wallet_data_filename)
    test/functional/wallet_backup.py:112:        os.remove(os.path.join(self.nodes[0].datadir, self.chain, 'wallets', self.default_wallet_name, self.wallet_data_filename))
    test/functional/wallet_backup.py:113:        os.remove(os.path.join(self.nodes[1].datadir, self.chain, 'wallets', self.default_wallet_name, self.wallet_data_filename))
    test/functional/wallet_backup.py:114:        os.remove(os.path.join(self.nodes[2].datadir, self.chain, 'wallets', self.default_wallet_name, self.wallet_data_filename))
    test/functional/wallet_backup.py:121:        not_created_wallet_file = os.path.join(node.datadir, self.chain, 'wallets', wallet_name)
    test/functional/wallet_backup.py:131:        not_created_wallet_file = os.path.join(node.datadir, self.chain, 'wallets', wallet_name)
    test/functional/wallet_backup.py:138:        wallet_file = os.path.join(node.datadir, self.chain, 'wallets', wallet_name)
    test/functional/wallet_backup.py:209:        assert os.path.exists(os.path.join(self.nodes[3].datadir, self.chain, 'wallets', "res0"))
    test/functional/wallet_backup.py:210:        assert os.path.exists(os.path.join(self.nodes[3].datadir, self.chain, 'wallets', "res1"))
    test/functional/wallet_backup.py:211:        assert os.path.exists(os.path.join(self.nodes[3].datadir, self.chain, 'wallets', "res2"))
    test/functional/wallet_backup.py:229:            shutil.rmtree(os.path.join(self.nodes[2].datadir, self.chain, 'blocks'))
    test/functional/wallet_backup.py:230:            shutil.rmtree(os.path.join(self.nodes[2].datadir, self.chain, 'chainstate'))
    test/functional/wallet_backup.py:251:            os.path.join(self.nodes[0].datadir, self.chain, 'wallets', self.default_wallet_name, self.wallet_data_filename),
    test/functional/wallet_backup.py:252:            os.path.join(self.nodes[0].datadir, self.chain, '.', 'wallets', self.default_wallet_name, self.wallet_data_filename),
    test/functional/wallet_backup.py:253:            os.path.join(self.nodes[0].datadir, self.chain, 'wallets', self.default_wallet_name),
    test/functional/wallet_backup.py:254:            os.path.join(self.nodes[0].datadir, self.chain, 'wallets')]
    test/functional/wallet_descriptor.py:237:        wallet_db = os.path.join(self.nodes[0].datadir, self.chain, "wallets", "crashme", self.wallet_data_filename)
    test/functional/wallet_hd.py:90:        shutil.rmtree(os.path.join(self.nodes[1].datadir, self.chain, "blocks"))
    test/functional/wallet_hd.py:91:        shutil.rmtree(os.path.join(self.nodes[1].datadir, self.chain, "chainstate"))
    test/functional/wallet_hd.py:94:            os.path.join(self.nodes[1].datadir, self.chain, 'wallets', self.default_wallet_name, self.wallet_data_filename),
    test/functional/wallet_hd.py:118:        shutil.rmtree(os.path.join(self.nodes[1].datadir, self.chain, "blocks"))
    test/functional/wallet_hd.py:119:        shutil.rmtree(os.path.join(self.nodes[1].datadir, self.chain, "chainstate"))
    test/functional/wallet_hd.py:122:            os.path.join(self.nodes[1].datadir, self.chain, "wallets", self.default_wallet_name, self.wallet_data_filename),
    test/functional/wallet_keypool_topup.py:36:        wallet_path = os.path.join(self.nodes[1].datadir, self.chain, "wallets", self.default_wallet_name, self.wallet_data_filename)
    test/functional/wallet_listtransactions.py:237:        wallet0 = os.path.join(self.nodes[0].datadir, self.chain, self.default_wallet_name, "wallet.dat")
    test/functional/wallet_listtransactions.py:238:        wallet2 = os.path.join(self.nodes[2].datadir, self.chain, self.default_wallet_name, "wallet.dat")
    test/functional/wallet_multiwallet.py:65:        data_dir = lambda *p: os.path.join(node.datadir, self.chain, *p)
    test/functional/wallet_pruning.py:109:        return os.path.isfile(os.path.join(self.nodes[1].datadir, self.chain, "blocks", f"blk{block_index:05}.dat"))
    test/functional/wallet_reorgsrestore.py:92:        shutil.copyfile(os.path.join(self.nodes[0].datadir, 'wallet.bak'), os.path.join(self.nodes[1].datadir, self.chain, self.default_wallet_name, self.wallet_data_filename))
    

    ...although, changing some of these occurences does require additional changes for example when watching logs.

    Are we looking to change all of these out and switch to the os.Path operator syntax throughout?

  12. maflcko force-pushed on Jun 16, 2023
  13. maflcko force-pushed on Jun 16, 2023
  14. maflcko force-pushed on Jun 16, 2023
  15. DrahtBot added the label CI failed on Jun 16, 2023
  16. maflcko commented at 4:26 PM on June 16, 2023: member

    $ git grep -n "os.path" test/functional | rg -e '.*\.chain*'

    Thanks, added a scripted-diff as second commit

    $ git grep join.*regtest ./test

    Thanks, modified the last commit

  17. maflcko force-pushed on Jun 16, 2023
  18. DrahtBot removed the label CI failed on Jun 16, 2023
  19. DrahtBot removed review request from willcl-ark on Jun 19, 2023
  20. willcl-ark commented at 12:47 PM on June 19, 2023: member

    Light crACK fa7f1251f8

    Looks good to me.

    There are still instances in tests (outside of test_framework/) where we are using os.path.join on self.datadir which we could also switch to using self.datadir_path and the / operator. Is that something that we are looking to do here or in the future?

  21. maflcko force-pushed on Jun 19, 2023
  22. maflcko commented at 1:32 PM on June 19, 2023: member

    Is that something that we are looking to do here or in the future?

    Thanks, fixed the remaining join calls that used self.chain on the datadir.

  23. theStack commented at 4:43 PM on June 19, 2023: contributor

    LGTM, found only one more instance where chain_path could be used:

    diff --git a/test/functional/feature_anchors.py b/test/functional/feature_anchors.py
    index 468ad1eafa..0961f21a40 100755
    --- a/test/functional/feature_anchors.py
    +++ b/test/functional/feature_anchors.py
    @@ -20,9 +20,7 @@ class AnchorsTest(BitcoinTestFramework):
             self.disable_autoconnect = False
    
         def run_test(self):
    -        node_anchors_path = os.path.join(
    -            self.nodes[0].datadir, "regtest", "anchors.dat"
    -        )
    +        node_anchors_path = self.nodes[0].chain_path / "anchors.dat"
    
             self.log.info("When node starts, check if anchors.dat doesn't exist")
             assert not os.path.exists(node_anchors_path)
    
    

    As a follow-up, a new TestNode property wallets_path might also make sense, to deduplicate even more?

  24. willcl-ark commented at 10:33 PM on June 19, 2023: member

    Same, looks good here. Will ack if you want to make the anchors change.

    Question: Now that we use more Paths, would there be appetite to replace many of the os.path.join() calls with the / operator? It looks like ~100 lines or thereabouts, and not any performance wins, nor perfectly scripted-diff-able; so quite a lot of code churn (and might be considered "many grains of sand", and not a nice fat "rock"). But would make the tests more readable IMO?

  25. maflcko force-pushed on Jun 20, 2023
  26. maflcko commented at 9:36 AM on June 20, 2023: member

    As a follow-up, a new TestNode property wallets_path might also make sense, to deduplicate even more?

    Thanks, added and updated the scripted-diff to avoid touching the same lines twice.

  27. test: Use wallet_dir lambda in wallet_multiwallet test where possible
    Seems odd to hardcode all parent directory names in the path for no good
    reason.
    
    Also, add wallet_path property to TestNode.
    
    Also, rework wallet_backup.py test for scripted-diff in the next commit.
    fa493fadfb
  28. scripted-diff: Use wallets_path and chain_path where possible
    Instead of passing the datadir and chain name to os.path.join, just use
    the existing properties, which are the same.
    
    -BEGIN VERIFY SCRIPT-
     sed -i --regexp-extended 's|\.datadir, self\.chain, .wallets.|.wallets_path|g' $(git grep -l '\.datadir, self\.chain,')
     sed -i --regexp-extended 's|\.datadir, self\.chain,|.chain_path,|g'            $(git grep -l '\.datadir, self\.chain,')
    -END VERIFY SCRIPT-
    fa41614a0a
  29. test: Allow pathlib.Path as RPC argument via authproxy
    Also, add datadir_path property to TestNode
    dddd89962b
  30. test: Use TestNode *_path properties where possible
    Seems odd to place the burden on test writers to hardcode the chain or
    datadir path for the nodes under test.
    aaaa3aefbd
  31. maflcko force-pushed on Jun 21, 2023
  32. fanquake requested review from willcl-ark on Jun 21, 2023
  33. fanquake requested review from theStack on Jun 21, 2023
  34. in test/functional/test_framework/authproxy.py:67 in dddd89962b outdated
      62 | @@ -62,6 +63,8 @@ def __init__(self, rpc_error, http_status=None):
      63 |  def EncodeDecimal(o):
      64 |      if isinstance(o, decimal.Decimal):
      65 |          return str(o)
      66 | +    if isinstance(o, pathlib.Path):
      67 | +        return str(o)
    


    theStack commented at 10:15 AM on June 22, 2023:

    nit (feel free to ignore for now, probably follow-up material): Now that this function is not handling decimals only anymore, I think it should be renamed from EncodeDecimal to something generic?


    maflcko commented at 11:04 AM on June 22, 2023:

    Yeah, it should have been called serialization_fallback? May do on the next push or leave for a follow-up.


    fanquake commented at 11:02 AM on June 30, 2023:

    See #28011.

  35. theStack approved
  36. theStack commented at 10:15 AM on June 22, 2023: contributor

    Code-review ACK aaaa3aefbdfca1c9243057eeefdc19940e60bf18 🌊

    Re-reviewed the changes since my previous review via $ git range-diff faf830e3...aaaa3aef

  37. maflcko commented at 7:56 AM on June 29, 2023: member

    Is this rfm or does it need more review?

  38. maflcko requested review from fanquake on Jun 29, 2023
  39. willcl-ark commented at 8:37 AM on June 29, 2023: member

    re-ACK aaaa3aefbdfca1c9243057eeefdc19940e60bf18

    LGTM with the wallets_path added.

  40. DrahtBot removed review request from willcl-ark on Jun 29, 2023
  41. fanquake merged this on Jun 29, 2023
  42. fanquake closed this on Jun 29, 2023

  43. maflcko deleted the branch on Jun 29, 2023
  44. sidhujag referenced this in commit d1997dbaeb on Jun 30, 2023
  45. bitcoin locked this on Jun 29, 2024


fanquake

Labels

github-metadata-mirror

This is a metadata mirror of the GitHub repository bitcoin/bitcoin. This site is not affiliated with GitHub. Content is generated from a GitHub metadata backup.
generated: 2026-04-24 09:14 UTC

This site is hosted by @0xB10C
More mirrored repositories can be found on mirror.b10c.me