Repository: bitcoin/bitcoin
File: test/functional/feature_asmap.py (Python)

Code before:
        self.log.info('Test bitcoind with -noasmap arg passed')
        self.stop_node(0)
        with self.node.assert_debug_log(['Using /16 prefix for IP bucketing']):
            self.start_node(0, ["-noasmap"])

    def test_asmap_with_absolute_path(self):
        self.log.info('Test bitcoind -asmap=<absolute path>')
        self.stop_node(0)
        filename = os.path.join(self.datadir, 'my-map-file.map')
        shutil.copyfile(self.asmap_raw, filename)
        with self.node.assert_debug_log(expected_messages(filename)):
            self.start_node(0, [f'-asmap={filename}'])
        os.remove(filename)

    def test_asmap_with_relative_path(self):
        self.log.info('Test bitcoind -asmap=<relative path>')
        self.stop_node(0)
        name = 'ASN_map'
        filename = os.path.join(self.datadir, name)
        shutil.copyfile(self.asmap_raw, filename)
        with self.node.assert_debug_log(expected_messages(filename)):
            self.start_node(0, [f'-asmap={name}'])
        os.remove(filename)

    def test_unspecified_asmap(self):
        msg = f"Error: Could not find asmap file \"{self.datadir}{os.sep}\""
        for arg in ['-asmap', '-asmap=']:
            self.log.info(f'Test bitcoind {arg} (and no filename specified)')
            self.stop_node(0)
            self.node.assert_start_raises_init_error(extra_args=[arg], expected_msg=msg)

    def test_asmap_interaction_with_addrman_containing_entries(self):
        self.log.info("Test bitcoind -asmap restart with addrman containing new and tried entries")
        self.stop_node(0)
        self.start_node(0, [f"-asmap={self.asmap_raw}", "-checkaddrman=1", "-test=addrman"])
        self.fill_addrman(node_id=0)
        self.restart_node(0, [f"-asmap={self.asmap_raw}", "-checkaddrman=1", "-test=addrman"])
        with self.node.assert_debug_log(
            expected_msgs=[
                "CheckAddrman: new 2, tried 2, total 4 started",
                "CheckAddrman: completed",
            ]
        ):
            self.node.getnodeaddresses()  # getnodeaddresses re-runs the addrman checks

    def test_asmap_with_missing_file(self):
        self.log.info('Test bitcoind -asmap with missing map file')
        self.stop_node(0)
        msg = f"Error: Could not find asmap file \"{self.datadir}{os.sep}missing\""
        self.node.assert_start_raises_init_error(extra_args=['-asmap=missing'], expected_msg=msg)


Review comment (refactor):
nit: I was going to say that the unspecified file and the missing file test could be deduplicated since they are currently similar but I think the better change would be to have an explicit error message for the empty `-asmap` arg.

Code after:
        self.log.info('Test bitcoind with -noasmap arg passed')
        self.stop_node(0)
        with self.node.assert_debug_log(['Using /16 prefix for IP bucketing']):
            self.start_node(0, ["-noasmap"])

    def test_asmap_with_absolute_path(self):
        self.log.info('Test bitcoind -asmap=<absolute path>')
        self.stop_node(0)
        filename = os.path.join(self.datadir, 'my-map-file.map')
        shutil.copyfile(self.asmap_raw, filename)
        with self.node.assert_debug_log(expected_messages(filename)):
            self.start_node(0, [f'-asmap={filename}'])
        os.remove(filename)

    def test_asmap_with_relative_path(self):
        self.log.info('Test bitcoind -asmap=<relative path>')
        self.stop_node(0)
        name = 'ASN_map'
        filename = os.path.join(self.datadir, name)
        shutil.copyfile(self.asmap_raw, filename)
        with self.node.assert_debug_log(expected_messages(filename)):
            self.start_node(0, [f'-asmap={name}'])
        os.remove(filename)

    def test_unspecified_asmap(self):
        msg = "Error: -asmap requires a file path. Use -asmap=<file>."
        for arg in ['-asmap', '-asmap=']:
            self.log.info(f'Test bitcoind {arg} (and no filename specified)')
            self.stop_node(0)
            self.node.assert_start_raises_init_error(extra_args=[arg], expected_msg=msg)

    def test_asmap_interaction_with_addrman_containing_entries(self):
        self.log.info("Test bitcoind -asmap restart with addrman containing new and tried entries")
        self.stop_node(0)
        self.start_node(0, [f"-asmap={self.asmap_raw}", "-checkaddrman=1", "-test=addrman"])
        self.fill_addrman(node_id=0)
        self.restart_node(0, [f"-asmap={self.asmap_raw}", "-checkaddrman=1", "-test=addrman"])
        with self.node.assert_debug_log(
            expected_msgs=[
                "CheckAddrman: new 2, tried 2, total 4 started",
                "CheckAddrman: completed",
            ]
        ):
            self.node.getnodeaddresses()  # getnodeaddresses re-runs the addrman checks

    def test_asmap_with_missing_file(self):
        self.log.info('Test bitcoind -asmap with missing map file')
        self.stop_node(0)
        msg = f"Error: Could not find asmap file \"{self.datadir}{os.sep}missing\""
        self.node.assert_start_raises_init_error(extra_args=['-asmap=missing'], expected_msg=msg)