def _validate_config_consistency(ctx: Context): """ Validate fingerprints for every agent component. :param ctx: the context :raise ValueError: if there is a missing configuration file. or if the configuration file is not valid. or if the fingerprints do not match """ packages_public_ids_to_types = dict([ *map(lambda x: (x, PackageType.PROTOCOL), ctx.agent_config.protocols), *map( lambda x: (x, PackageType.CONNECTION), ctx.agent_config.connections, ), *map(lambda x: (x, PackageType.SKILL), ctx.agent_config.skills), *map(lambda x: (x, PackageType.CONTRACT), ctx.agent_config.contracts), ]) # type: Dict[PublicId, PackageType] for public_id, item_type in packages_public_ids_to_types.items(): # find the configuration file. try: # either in vendor/ or in personal packages. # we give precedence to custom agent components (i.e. not vendorized). package_directory = Path(item_type.to_plural(), public_id.name) is_vendor = False if not package_directory.exists(): package_directory = Path("vendor", public_id.author, item_type.to_plural(), public_id.name) is_vendor = True # we fail if none of the two alternative works. enforce(package_directory.exists(), "Package directory does not exist!") loader = ConfigLoaders.from_package_type(item_type) config_file_name = _get_default_configuration_file_name_from_type( item_type) configuration_file_path = package_directory / config_file_name enforce( configuration_file_path.exists(), "Configuration file path does not exist!", ) except Exception: raise ValueError("Cannot find {}: '{}'".format( item_type.value, public_id)) # load the configuration file. try: package_configuration = loader.load( configuration_file_path.open("r")) except ValidationError as e: raise ValueError("{} configuration file not valid: {}".format( item_type.value.capitalize(), str(e))) _check_aea_version(package_configuration) _compare_fingerprints(package_configuration, package_directory, is_vendor, item_type)
def test_component_configuration_check_fingerprint_different_fingerprints_no_vendor(): """Test ComponentConfiguration.check_fingerprint when the fingerprints differ for a non-vendor package.""" config = ProtocolConfig("name", "author", "0.1.0") package_dir = Path("path", "to", "dir") error_regex = ( f"Fingerprints for package {package_dir} do not match:\nExpected: {dict()}\nActual: {dict(foo='bar')}\n" + "Please fingerprint the package before continuing: 'aea fingerprint protocol author/name:0.1.0" ) with pytest.raises(ValueError, match=error_regex): with mock.patch( "aea.configurations.base._compute_fingerprint", return_value={"foo": "bar"} ): _compare_fingerprints(config, package_dir, False, PackageType.PROTOCOL)
def test_component_configuration_check_fingerprint_different_fingerprints_vendor(): """Test ComponentConfiguration.check_fingerprint when the fingerprints differ for a vendor package.""" config = ProtocolConfig("name", "author", "0.1.0") package_dir = Path("path", "to", "dir") error_regex = ( f"Fingerprints for package {package_dir} do not match:\nExpected: {dict()}\nActual: {dict(foo='bar')}\n" + "Vendorized projects should not be tampered with, please revert any changes to protocol author/name:0.1.0" ) with pytest.raises(ValueError, match=error_regex): with mock.patch( "aea.configurations.base._compute_fingerprint", return_value={"foo": "bar"} ): _compare_fingerprints(config, package_dir, True, PackageType.PROTOCOL)