test: create assert_not_equal util #29500

pull kevkevinpal wants to merge 2 commits into bitcoin:master from kevkevinpal:testFrameworkUtilsOne changing 38 files +134 −80
  1. kevkevinpal commented at 2:55 am on February 28, 2024: contributor

    In the functional tests there are lots of cases where we assert != which we now swap with assert_not_equal to be more readable

    This is motivated/uses logic from this PR which was closed #28528 This partially helps #23119

    I’ve broken it up to just assert_not_equal to keep the PR smaller as suggested in #28528 (comment)

    I can create follow up PR’s if this is wanted

  2. DrahtBot commented at 2:55 am on February 28, 2024: contributor

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

    Code Coverage

    For detailed information about the code coverage, see the test coverage report.

    Reviews

    See the guideline for information on the review process.

    Type Reviewers
    Concept ACK theStack
    Approach ACK BrandonOdiwuor
    Stale ACK rkrux

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

    Conflicts

    Reviewers, this pull request conflicts with the following ones:

    • #29431 (test/BIP324: disconnection scenarios during v2 handshake by stratospher)
    • #28710 (Remove the legacy wallet and BDB dependency 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 added the label Tests on Feb 28, 2024
  4. axizqayum686 changes_requested
  5. axizqayum686 commented at 6:47 am on February 28, 2024: none
    .
  6. Empact commented at 7:03 am on February 28, 2024: member

    Would be nice to structure this as a scripted diff: one commit where you add the function and requires, another where you convert the assert !=s.

    That would ensure programmatically that you hit all of the cases.

  7. maflcko requested review from theStack on Feb 28, 2024
  8. kevkevinpal force-pushed on Mar 2, 2024
  9. kevkevinpal force-pushed on Mar 2, 2024
  10. DrahtBot added the label CI failed on Mar 2, 2024
  11. kevkevinpal force-pushed on Mar 2, 2024
  12. kevkevinpal commented at 4:15 pm on March 2, 2024: contributor

    added scripted diff in dc087f1 with the following script git grep -l "assert.*!=" ./test/functional

    I will have to amend the commit as I see more spots I need to modify with the assert_not_equal util

  13. kevkevinpal force-pushed on Mar 2, 2024
  14. kevkevinpal force-pushed on Mar 2, 2024
  15. kevkevinpal force-pushed on Mar 2, 2024
  16. kevkevinpal force-pushed on Mar 2, 2024
  17. kevkevinpal force-pushed on Mar 2, 2024
  18. kevkevinpal force-pushed on Mar 2, 2024
  19. theStack commented at 8:35 pm on March 2, 2024: contributor
    Concept ACK, thanks for picking this up.
  20. kevkevinpal force-pushed on Mar 2, 2024
  21. kevkevinpal force-pushed on Mar 2, 2024
  22. kevkevinpal force-pushed on Mar 2, 2024
  23. kevkevinpal force-pushed on Mar 2, 2024
  24. kevkevinpal force-pushed on Mar 2, 2024
  25. brunoerg commented at 2:48 pm on March 4, 2024: contributor

    There are some places you are using assert_not_equal but not importing it and other ones you’re importing it twice. Check CI.

     0Running Unit Tests for Test Framework Modules
     1Traceback (most recent call last):
     2  File "/ci_container_base/ci/scratch/build/bitcoin-x86_64-pc-linux-gnu/test/functional/test_runner.py", line 906, in <module>
     3    main()
     4  File "/ci_container_base/ci/scratch/build/bitcoin-x86_64-pc-linux-gnu/test/functional/test_runner.py", line 539, in main
     5    run_tests(
     6  File "/ci_container_base/ci/scratch/build/bitcoin-x86_64-pc-linux-gnu/test/functional/test_runner.py", line 580, in run_tests
     7    test_framework_tests.addTest(unittest.TestLoader().loadTestsFromName("test_framework.{}".format(module)))
     8                                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
     9  File "/usr/lib/python3.12/unittest/loader.py", line 137, in loadTestsFromName
    10    module = __import__(module_name)
    11             ^^^^^^^^^^^^^^^^^^^^^^^
    12  File "/ci_container_base/test/functional/test_framework/address.py", line 14, in <module>
    13    from .script import (
    14  File "/ci_container_base/test/functional/test_framework/script.py", line 14, in <module>
    15    from .key import TaggedHash, tweak_add_pubkey, compute_xonly_pubkey
    16  File "/ci_container_base/test/functional/test_framework/key.py", line 16, in <module>
    17    from test_framework.crypto import secp256k1
    18  File "/ci_container_base/test/functional/test_framework/crypto/secp256k1.py", line 316, in <module>
    19    G = GE.lift_x(0x79BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798)
    20        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    21  File "/ci_container_base/test/functional/test_framework/crypto/secp256k1.py", line 257, in lift_x
    22    y = (FE(x)**3 + 7).sqrt()
    23         ^^^^^
    24  File "/ci_container_base/test/functional/test_framework/crypto/secp256k1.py", line 41, in __init__
    25    assert_not_equal(den, 0)
    26    ^^^^^^^^^^^^^^^^
    27NameError: name 'assert_not_equal' is not defined
    
  26. in test/functional/p2p_blockfilters.py:24 in ad1e879351 outdated
    21@@ -21,6 +22,7 @@
    22 from test_framework.p2p import P2PInterface
    23 from test_framework.test_framework import BitcoinTestFramework
    24 from test_framework.util import (
    25+    assert_not_equal,
    


    brunoerg commented at 2:49 pm on March 4, 2024:
    Importing twice? (from util and messages)

    kevkevinpal commented at 3:20 am on March 5, 2024:

    hmm yea I was trying to use this command to find all the places but it seems to be a bit more complex than I initially thought

    0git grep -l "assert .*!=" -- ':(exclude)*script.py' ./test/functional | xargs sed -i -e "/assert .*!=/ s/assert /assert_not_equal(/g" -e "/assert_not_equal/ s/ !=/,/g" -e "/assert_not_equal/ s/$/)/g" -e "s/test_framework.util import (/test_framework.util import (\n    assert_not_equal,/g" -e "s/import assert_equal/(\n    assert_equal,\n    assert_not_equal,\n)/g"
    

    I might just manually add the imports and use the script to look for the assert ... != as suggested by @Empact

  27. kevkevinpal force-pushed on Mar 5, 2024
  28. kevkevinpal force-pushed on Mar 5, 2024
  29. kevkevinpal force-pushed on Mar 5, 2024
  30. kevkevinpal force-pushed on Mar 5, 2024
  31. kevkevinpal force-pushed on Mar 5, 2024
  32. kevkevinpal force-pushed on Mar 5, 2024
  33. kevkevinpal force-pushed on Mar 5, 2024
  34. fanquake commented at 11:05 am on March 5, 2024: member

    https://cirrus-ci.com/task/6627384883937280?logs=ci#L4021

     0 test  2024-03-05T05:30:24.555000Z TestFramework (ERROR): Unexpected exception caught during testing 
     1                                   Traceback (most recent call last):
     2                                     File "/ci_container_base/ci/scratch/build/bitcoin-x86_64-pc-linux-gnu/test/functional/test_framework/test_framework.py", line 131, in main
     3                                       self.run_test()
     4                                     File "/ci_container_base/ci/scratch/build/bitcoin-x86_64-pc-linux-gnu/test/functional/mempool_accept_v3.py", line 448, in run_test
     5                                       self.test_reorg_2child_rbf()
     6                                     File "/ci_container_base/ci/scratch/build/bitcoin-x86_64-pc-linux-gnu/test/functional/mempool_accept_v3.py", line 29, in wrapper
     7                                       func(self)
     8                                     File "/ci_container_base/ci/scratch/build/bitcoin-x86_64-pc-linux-gnu/test/functional/mempool_accept_v3.py", line 429, in test_reorg_2child_rbf
     9                                       assert_not_equal((child_1_conflict["txid"], child_1["txid"]))
    10                                   TypeError: assert_not_equal() missing 1 required positional argument: 'thing2'
    
  35. kevkevinpal force-pushed on Mar 6, 2024
  36. kevkevinpal force-pushed on Mar 6, 2024
  37. kevkevinpal force-pushed on Mar 6, 2024
  38. kevkevinpal force-pushed on Mar 6, 2024
  39. kevkevinpal force-pushed on Mar 6, 2024
  40. kevkevinpal force-pushed on Mar 6, 2024
  41. kevkevinpal commented at 3:22 pm on March 10, 2024: contributor
    not sure why this ci is failing ASan + LSan + UBSan + integer, no depends, USDT but the rest seem to be passing
  42. maflcko commented at 8:46 am on March 11, 2024: member

    not sure why this ci is failing ASan + LSan + UBSan + integer, no depends, USDT but the rest seem to be passing

    It is a race in the test logic between the python code and the net thread, IIUC. Please file a new issue or a new pull to fix it.

  43. kevkevinpal commented at 10:07 pm on March 12, 2024: contributor

    not sure why this ci is failing ASan + LSan + UBSan + integer, no depends, USDT but the rest seem to be passing

    It is a race in the test logic between the python code and the net thread, IIUC. Please file a new issue or a new pull to fix it.

    Opened a new issue for the CI error https://github.com/bitcoin/bitcoin/issues/29643

  44. DrahtBot removed the label CI failed on Apr 5, 2024
  45. BrandonOdiwuor commented at 2:28 pm on April 23, 2024: contributor

    Approach ACK

    Good job approaching this using Scripted diffs

  46. in test/functional/test_framework/test_node.py:640 in f9ba6fd46d outdated
    637@@ -638,7 +638,7 @@ def assert_start_raises_init_error(self, extra_args=None, expected_msg=None, mat
    638                 self.start(extra_args, stdout=log_stdout, stderr=log_stderr, *args, **kwargs)
    639                 ret = self.process.wait(timeout=self.rpc_timeout)
    640                 self.log.debug(self._node_msg(f'bitcoind exited with status {ret} during initialization'))
    641-                assert ret != 0  # Exit code must indicate failure
    642+                assert_not_equal(ret, 0  ) # Exit code must indicate failure
    


    rkrux commented at 1:57 pm on May 17, 2024:
    Extra spaces after 0?

    kevkevinpal commented at 0:27 am on May 22, 2024:
    not sure why it is doing that I will check the script to see why

    kevkevinpal commented at 2:26 am on May 22, 2024:

    fixed in 4358c6e

    thanks for the review!

  47. in test/functional/p2p_blockfilters.py:97 in f9ba6fd46d outdated
    93@@ -94,7 +94,7 @@ def run_test(self):
    94         self.nodes[0].syncwithvalidationinterfacequeue()
    95 
    96         main_block_hash = self.nodes[0].getblockhash(1000)
    97-        assert main_block_hash != stale_block_hash, "node 0 chain did not reorganize"
    98+        assert_not_equal(main_block_hash, stale_block_hash, "node 0 chain did not reorganize")
    


    rkrux commented at 2:04 pm on May 17, 2024:

    "node 0 chain did not reorganize" This seems to be the 3rd argument to the assert_not_equal() call, i.e. *args. Will this piece be kicked in then? any(thing1 == arg for arg in args)

    I don’t get why would we need to search for main_block_hash in the above error string.


    kevkevinpal commented at 0:28 am on May 22, 2024:
    that is strange I would think that the 3rd arg would make the tests fail in this case

    kevkevinpal commented at 2:19 am on May 22, 2024:

    I’ve updated the function to not accept the args param since it did not seem to be needed and instead allowed an optional 3rd param as the message

    updated in 4488120

  48. rkrux commented at 2:08 pm on May 17, 2024: none

    crACK f9ba6fd

    I’m unable to run make successfully because of the following error on my system. I’ve noticed this in another PR as well and it is due to lack of a commit that was merged in later. Asked couple questions.

    0/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/time.h:127:12: note: 'gmtime_r' declared here
    1struct tm *gmtime_r(const time_t * __restrict, struct tm * __restrict);
    
  49. DrahtBot requested review from BrandonOdiwuor on May 17, 2024
  50. rkrux commented at 2:08 pm on May 17, 2024: none
    @kevkevinpal Did you use a tool to generate the verification script?
  51. DrahtBot added the label Needs rebase on May 20, 2024
  52. in test/functional/feature_dbcrash.py:278 in f9ba6fd46d outdated
    274@@ -274,7 +275,7 @@ def run_test(self):
    275         self.log.info(f"Restarted nodes: {self.restart_counts}; crashes on restart: {self.crashed_on_restart}")
    276 
    277         # If no nodes were restarted, we didn't test anything.
    278-        assert self.restart_counts != [0, 0, 0]
    279+        assert_not_equal(self.restart_counts, [0, 0, 0])
    


    ajtowns commented at 1:11 pm on May 20, 2024:

    assert_equal is helpful because when a == 3 fails, it can be helpful to know what the value of a actually is without having to add debug code and rerun. If a != 3 fails, though, you already know that a == 3, so this doesn’t seem very helpful.

    There are cases where you’re comparing a != b and may not be sure which ones getting the wrong value where this could perhaps be helpful, but those seem pretty rare.

    Concept -0 for me.

  53. in test/functional/test_framework/util.py:78 in f9ba6fd46d outdated
    55@@ -56,6 +56,10 @@ def assert_equal(thing1, thing2, *args):
    56     if thing1 != thing2 or any(thing1 != arg for arg in args):
    57         raise AssertionError("not(%s)" % " == ".join(str(arg) for arg in (thing1, thing2) + args))
    58 
    59+def assert_not_equal(thing1, thing2, *args):
    60+    if thing1 == thing2 or any(thing1 == arg for arg in args):
    61+        raise AssertionError("%s" % " == ".join(str(arg) for arg in (thing1, thing2) + args))
    


    ajtowns commented at 1:19 pm on May 20, 2024:
    This error message doesn’t make sense when *args is non-empty: if you say assert_not_equal(a, 1, 2) and a is 1 or 2, it will report 1 == 1 == 2 or 2 == 1 == 2.

    kevkevinpal commented at 0:39 am on May 22, 2024:
    Yes that would not make sense let me see if I can improve the logging here

    kevkevinpal commented at 2:18 am on May 22, 2024:

    I’ve updated this to instead only take three arguments and the last one being an optional message since I did not see anywhere we were already asserting != in a chain.

    This should clear up the message and allow the one location we do use a message to be used

    updated in 4488120

  54. kevkevinpal force-pushed on May 22, 2024
  55. kevkevinpal commented at 0:29 am on May 22, 2024: contributor

    @kevkevinpal Did you use a tool to generate the verification script?

    No I did not I just manually used git grep and sed to create the script

  56. kevkevinpal force-pushed on May 22, 2024
  57. DrahtBot added the label CI failed on May 22, 2024
  58. DrahtBot commented at 0:38 am on May 22, 2024: contributor

    🚧 At least one of the CI tasks failed. Make sure to run all tests locally, according to the documentation.

    Possibly this is due to a silent merge conflict (the changes in this pull request being incompatible with the current code in the target branch). If so, make sure to rebase on the latest commit of the target branch.

    Leave a comment here, if you need help tracking down a confusing failure.

    Debug: https://github.com/bitcoin/bitcoin/runs/25253865668

  59. kevkevinpal commented at 0:38 am on May 22, 2024: contributor
    rebased to 4358c6e
  60. kevkevinpal force-pushed on May 22, 2024
  61. kevkevinpal force-pushed on May 22, 2024
  62. kevkevinpal force-pushed on May 22, 2024
  63. kevkevinpal force-pushed on May 22, 2024
  64. kevkevinpal force-pushed on May 22, 2024
  65. DrahtBot removed the label Needs rebase on May 22, 2024
  66. test: create assert_not_equal util and add to where imports are needed
    In the functional tests there are lots of cases where we assert != which
    this new util will replace, we also are adding the imports and the
    following commit will add the new assertion
    4488120ab7
  67. scripted-diff: tests replaced where != is used in functional tests to assert_not_equal
    -BEGIN VERIFY SCRIPT-
    git grep -l "assert .*!=" -- ':(exclude)*script.py' ./test/functional | xargs sed -i -e "/assert (.*!=.*)/ s/assert (/assert_not_equal(/g" -e "/assert (.*) !=/ s/assert /assert_not_equal(/g" -e "/assert [^(].*!=/ s/assert /assert_not_equal(/g" -e "/assert_not_equal(/ s/ !=/,/g" -e "/assert_not_equal(.*#/ s/  #/) #/g" -e "/assert_not_equal([^#]*.[^)]$/ s/$/)/g" -e "/assert_not_equal([^#]*.[(][)][)]$/ s/$/)/g" -e "/assert_not_equal([^#]*.[(][)]$/ s/$/)/g" -e "/assert_not_equal([^#]*([a-zA-Z=_]*)$/ s/$/)/g" -e "s/assert_not_equal(cblks/assert_not_equal((cblks/g" -e "/assert_not_equal((cblks/ s/)$/))/g" -e "s/assert_not_equal(amount/assert_not_equal((amount/g" -e "/assert_not_equal((amount/ s/None)$/None))/g"
    -END VERIFY SCRIPT-
    4358c6e722
  68. kevkevinpal force-pushed on May 22, 2024
  69. DrahtBot removed the label CI failed on May 22, 2024

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: 2024-07-01 10:13 UTC

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