From: Lovett, Trevor Date: Fri, 6 Sep 2019 20:34:12 +0000 (-0500) Subject: Enforce black code format via pre-commit hook X-Git-Tag: 6.0.0~49 X-Git-Url: https://gerrit.onap.org/r/gitweb?p=vvp%2Fvalidation-scripts.git;a=commitdiff_plain;h=1bc097aa957e560147b4d9af49d25e69a6692702 Enforce black code format via pre-commit hook Issue-ID: VVP-203 Change-Id: If0de5b4bc1be0c9514decea1f4ff6f5ec79dc41b Signed-off-by: Lovett, Trevor --- diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 0000000..31154e2 --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1,41 @@ +# ============LICENSE_START==================================================== +# org.onap.vvp/validation-scripts +# =================================================================== +# Copyright © 2019 AT&T Intellectual Property. All rights reserved. +# =================================================================== +# +# Unless otherwise specified, all software contained herein is licensed +# under the Apache License, Version 2.0 (the "License"); +# you may not use this software except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +# +# +# Unless otherwise specified, all documentation contained herein is licensed +# under the Creative Commons License, Attribution 4.0 Intl. (the "License"); +# you may not use this documentation except in compliance with the License. +# You may obtain a copy of the License at +# +# https://creativecommons.org/licenses/by/4.0/ +# +# Unless required by applicable law or agreed to in writing, documentation +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +# ============LICENSE_END============================================ +repos: +- repo: https://github.com/ambv/black + rev: stable + hooks: + - id: black + language_version: python3.6 diff --git a/checks.py b/checks.py index 4431d26..4e5fb13 100644 --- a/checks.py +++ b/checks.py @@ -40,7 +40,7 @@ import csv import io import json import os -import subprocess #nosec +import subprocess # nosec import sys import pytest @@ -156,13 +156,19 @@ def check_requirements_up_to_date(): def check_app_tests_pass(): - return run_pytest("tests", "--self-test", - msg="app_tests failed. Run pytest app_tests and fix errors.") + return run_pytest( + "tests", + "--self-test", + msg="app_tests failed. Run pytest app_tests and fix errors.", + ) def check_self_test_pass(): - return run_pytest("tests", "--self-test", - msg="self-test failed. Run pytest --self-test and fix errors.") + return run_pytest( + "tests", + "--self-test", + msg="self-test failed. Run pytest --self-test and fix errors.", + ) def check_testable_requirements_are_mapped(): @@ -186,12 +192,12 @@ def check_flake8_passes(): def check_bandit_passes(): - result = subprocess.run( #nosec - ["bandit", "-c", "bandit.yaml", "-r", ".", "-x", "./.tox/**"], #nosec - encoding="utf-8", #nosec - stdout=subprocess.PIPE, #nosec - stderr=subprocess.PIPE, #nosec - ) #nosec + result = subprocess.run( # nosec + ["bandit", "-c", "bandit.yaml", "-r", ".", "-x", "./.tox/**"], # nosec + encoding="utf-8", # nosec + stdout=subprocess.PIPE, # nosec + stderr=subprocess.PIPE, # nosec + ) # nosec msgs = result.stdout.split("\n") if result.returncode != 0 else [] return ["bandit errors detected:"] + [f" {e}" for e in msgs] if msgs else [] diff --git a/ice_validator/app_tests/preload_tests/test_grapi.py b/ice_validator/app_tests/preload_tests/test_grapi.py index eea1a67..7f2361f 100644 --- a/ice_validator/app_tests/preload_tests/test_grapi.py +++ b/ice_validator/app_tests/preload_tests/test_grapi.py @@ -247,5 +247,6 @@ def test_preload_env_population_missing_value(preload): base_path = THIS_DIR / "sample_env/preloads/grapi/base_incomplete.json" data = load_json(base_path) vnf_name = data["input"]["preload-vf-module-topology-information"][ - "vnf-topology-identifier-structure"]["vnf-name"] + "vnf-topology-identifier-structure" + ]["vnf-name"] assert vnf_name == "VALUE FOR: vnf_name" diff --git a/ice_validator/preload/environment.py b/ice_validator/preload/environment.py index 5d69a99..3570dc5 100644 --- a/ice_validator/preload/environment.py +++ b/ice_validator/preload/environment.py @@ -175,7 +175,6 @@ class CloudServiceArchive: class PreloadEnvironment: - def __init__(self, env_dir, parent=None): self.base_dir = Path(env_dir) self.parent = parent diff --git a/ice_validator/preload/generator.py b/ice_validator/preload/generator.py index 456174a..6c0d4b8 100644 --- a/ice_validator/preload/generator.py +++ b/ice_validator/preload/generator.py @@ -52,7 +52,7 @@ def represent_ordered_dict(dumper, data): value.append((node_key, node_value)) - return yaml.nodes.MappingNode(u'tag:yaml.org,2002:map', value) + return yaml.nodes.MappingNode(u"tag:yaml.org,2002:map", value) def get_json_template(template_dir, template_name): diff --git a/ice_validator/preload_vnfapi/vnfapi_generator.py b/ice_validator/preload_vnfapi/vnfapi_generator.py index dce1789..87a8408 100644 --- a/ice_validator/preload_vnfapi/vnfapi_generator.py +++ b/ice_validator/preload_vnfapi/vnfapi_generator.py @@ -89,7 +89,9 @@ class VnfApiPreloadGenerator(AbstractPreloadGenerator): self._add_parameters(preload, vnf_module) def _add_vnf_metadata(self, preload): - vnf_meta = preload["input"]["vnf-topology-information"]["vnf-topology-identifier"] + vnf_meta = preload["input"]["vnf-topology-information"][ + "vnf-topology-identifier" + ] vnf_meta["vnf-name"] = self.replace("vnf_name") vnf_meta["generic-vnf-type"] = self.replace( "vnf-type", diff --git a/ice_validator/tests/conftest.py b/ice_validator/tests/conftest.py index 9868067..ecaf662 100644 --- a/ice_validator/tests/conftest.py +++ b/ice_validator/tests/conftest.py @@ -833,7 +833,7 @@ def pytest_addoption(parser): "--env-directory", dest="env_dir", action="store", - help="optional directory of .env files for preload generation" + help="optional directory of .env files for preload generation", ) parser.addoption( @@ -843,7 +843,7 @@ def pytest_addoption(parser): help=( "Preload format to create (multiple allowed). If not provided " "then all available formats will be created: {}" - ).format(", ".join(get_generator_plugin_names())) + ).format(", ".join(get_generator_plugin_names())), ) diff --git a/ice_validator/tests/helpers.py b/ice_validator/tests/helpers.py index fefa8de..6266f08 100644 --- a/ice_validator/tests/helpers.py +++ b/ice_validator/tests/helpers.py @@ -393,7 +393,10 @@ def unzip(zip_path, target_dir): :param zip_path: path to valid zip file :param target_dir: directory to unzip zip_path """ - check(zipfile.is_zipfile(zip_path), "{} is not a valid zipfile or does not exist".format(zip_path)) + check( + zipfile.is_zipfile(zip_path), + "{} is not a valid zipfile or does not exist".format(zip_path), + ) archive = zipfile.ZipFile(zip_path) if not os.path.exists(target_dir): os.makedirs(target_dir, exist_ok=True) diff --git a/ice_validator/tests/structures.py b/ice_validator/tests/structures.py index 12bfc63..0223bc3 100644 --- a/ice_validator/tests/structures.py +++ b/ice_validator/tests/structures.py @@ -495,7 +495,10 @@ class NeutronPortProcessor(HeatProcessor): An OS::Nova:Port with the property binding:vnic_type """ resource_properties = nested_dict.get(resource, "properties", default={}) - if nested_dict.get(resource, "type") == cls.resource_type and resource_properties.get("binding:vnic_type", "") == "direct": + if ( + nested_dict.get(resource, "type") == cls.resource_type + and resource_properties.get("binding:vnic_type", "") == "direct" + ): return True return False @@ -785,10 +788,12 @@ class Resource(object): try: return int(count_value) except (ValueError, TypeError): - print(( - "WARNING: Invalid value for count parameter {}. Expected " - "an integer, but got {}. Defaulting to 1" - ).format(count_param, count_value)) + print( + ( + "WARNING: Invalid value for count parameter {}. Expected " + "an integer, but got {}. Defaulting to 1" + ).format(count_param, count_value) + ) return 1 @property diff --git a/ice_validator/tests/test_contrail_instance_ip_parameters.py b/ice_validator/tests/test_contrail_instance_ip_parameters.py index 312c8e9..4071f66 100644 --- a/ice_validator/tests/test_contrail_instance_ip_parameters.py +++ b/ice_validator/tests/test_contrail_instance_ip_parameters.py @@ -100,13 +100,13 @@ sid_regx_dict = { "string": { "readable": "{network-role}_subnet_id or {network-role}_v6_subnet_id", "machine": RE_EXTERNAL_PARAM_SID, - }, + } }, "internal": { "string": { "readable": "int_{network-role}_subnet_id or int_{network-role}_v6_subnet_id", "machine": RE_INTERNAL_PARAM_SID, - }, + } }, "parameter_to_resource_comparisons": ["network_role"], } @@ -114,22 +114,46 @@ sid_regx_dict = { @validates("R-100000", "R-100010", "R-100030", "R-100050", "R-100070") def test_contrail_external_instance_ip_address_parameter(yaml_file): - check_parameter_format(yaml_file, iip_regx_dict, "external", ContrailV2InstanceIpProcessor, "instance_ip_address") + check_parameter_format( + yaml_file, + iip_regx_dict, + "external", + ContrailV2InstanceIpProcessor, + "instance_ip_address", + ) @validates("R-100000", "R-100090", "R-100110", "R-100130", "R-100150") def test_contrail_internal_instance_ip_address_parameter(yaml_file): - check_parameter_format(yaml_file, iip_regx_dict, "internal", ContrailV2InstanceIpProcessor, "instance_ip_address") + check_parameter_format( + yaml_file, + iip_regx_dict, + "internal", + ContrailV2InstanceIpProcessor, + "instance_ip_address", + ) @validates("R-100190", "R-100200", "R-100220") def test_contrail_external_instance_subnet_id_parameter(yaml_file): - check_parameter_format(yaml_file, sid_regx_dict, "external", ContrailV2InstanceIpProcessor, "subnet_uuid") + check_parameter_format( + yaml_file, + sid_regx_dict, + "external", + ContrailV2InstanceIpProcessor, + "subnet_uuid", + ) @validates("R-100190", "R-100240", "R-100260") def test_contrail_internal_instance_subnet_id_parameter(yaml_file): - check_parameter_format(yaml_file, sid_regx_dict, "internal", ContrailV2InstanceIpProcessor, "subnet_uuid") + check_parameter_format( + yaml_file, + sid_regx_dict, + "internal", + ContrailV2InstanceIpProcessor, + "subnet_uuid", + ) @validates("R-100240", "R-100260") @@ -150,10 +174,12 @@ def test_contrail_incremental_module_internal_subnet_usage(yaml_files): if not subnet_param: continue if subnet_param not in base_outputs: - errors.append(( - "Resource ({}) is designated as an internal IP, but its " - "subnet_uuid parameter ({}) does not refer to subnet in " - "this template nor is it defined in the output section " - "of the base module ({})" - ).format(r_id, subnet_param, os.path.basename(base_path))) + errors.append( + ( + "Resource ({}) is designated as an internal IP, but its " + "subnet_uuid parameter ({}) does not refer to subnet in " + "this template nor is it defined in the output section " + "of the base module ({})" + ).format(r_id, subnet_param, os.path.basename(base_path)) + ) assert not errors, ". ".join(errors) diff --git a/ice_validator/tests/test_contrail_resource_id.py b/ice_validator/tests/test_contrail_resource_id.py index 06b41b4..dbbedbb 100644 --- a/ice_validator/tests/test_contrail_resource_id.py +++ b/ice_validator/tests/test_contrail_resource_id.py @@ -139,9 +139,7 @@ def test_contrail_servicehealthcheck_resource_id(yaml_file): **MUST** contain the ``{vm-type}``. """ - run_test( - yaml_file, ContrailV2ServiceHealthCheckProcessor, get_vm_types, "vm_type" - ) + run_test(yaml_file, ContrailV2ServiceHealthCheckProcessor, get_vm_types, "vm_type") @validates("R-16437") diff --git a/ice_validator/tests/test_environment_file_parameters.py b/ice_validator/tests/test_environment_file_parameters.py index 69485bc..9744b49 100644 --- a/ice_validator/tests/test_environment_file_parameters.py +++ b/ice_validator/tests/test_environment_file_parameters.py @@ -234,8 +234,9 @@ def get_preload_excluded_parameters(yaml_file, persistent_only=False, env_spec=N for spec in specs: if persistent_only and not spec.get("persistent"): continue - results.extend(get_template_parameters(yaml_file, resource_type, - spec, all_resources)) + results.extend( + get_template_parameters(yaml_file, resource_type, spec, all_resources) + ) return {item["param"] for item in results} diff --git a/ice_validator/tests/test_fixed_ips_include_vm_type_network_role.py b/ice_validator/tests/test_fixed_ips_include_vm_type_network_role.py index 2ce1bcf..feb48e5 100644 --- a/ice_validator/tests/test_fixed_ips_include_vm_type_network_role.py +++ b/ice_validator/tests/test_fixed_ips_include_vm_type_network_role.py @@ -87,9 +87,23 @@ fip_regx_dict = { @validates("R-40971", "R-35735", "R-23503", "R-71577", "R-04697", "R-34037") def test_external_fip_format(yaml_file): - check_parameter_format(yaml_file, fip_regx_dict, "external", NeutronPortProcessor, "fixed_ips", "ip_address") + check_parameter_format( + yaml_file, + fip_regx_dict, + "external", + NeutronPortProcessor, + "fixed_ips", + "ip_address", + ) @validates("R-27818", "R-29765", "R-85235", "R-78380", "R-34037") def test_internal_fip_format(yaml_file): - check_parameter_format(yaml_file, fip_regx_dict, "internal", NeutronPortProcessor, "fixed_ips", "ip_address") + check_parameter_format( + yaml_file, + fip_regx_dict, + "internal", + NeutronPortProcessor, + "fixed_ips", + "ip_address", + ) diff --git a/ice_validator/tests/test_nested_parameters.py b/ice_validator/tests/test_nested_parameters.py index 47c4d90..97d2f76 100644 --- a/ice_validator/tests/test_nested_parameters.py +++ b/ice_validator/tests/test_nested_parameters.py @@ -81,7 +81,12 @@ def check_nested_parameter_doesnt_change(yaml_file, nresource_type, *nprops): r = Resource(resource_id=resource_id, resource=resource) properties = r.get_nested_properties() resources = r.get_nested_yaml(base_dir).get("resources", {}) - for nrid, nresource_dict in resources.items(): # iterate through nested file until found target r type + for ( + nrid, + nresource_dict, + ) in ( + resources.items() + ): # iterate through nested file until found target r type if ( nresource_dict.get("type") @@ -89,10 +94,19 @@ def check_nested_parameter_doesnt_change(yaml_file, nresource_type, *nprops): ): continue - for nparam in prop_iterator(nresource_dict, *nprops): # get iterator of all target parameters - if nparam and "get_param" in nparam: # iterator yields None if parameter isn't found + for nparam in prop_iterator( + nresource_dict, *nprops + ): # get iterator of all target parameters + if ( + nparam and "get_param" in nparam + ): # iterator yields None if parameter isn't found nparam = nparam.get("get_param") - for k1, v1 in properties.items(): # found nparam, now comparing to parent template + for ( + k1, + v1, + ) in ( + properties.items() + ): # found nparam, now comparing to parent template if isinstance(v1, dict) and "get_param" in v1: parameter = v1.get("get_param") # k1: nested resource parameter definition @@ -105,7 +119,9 @@ def check_nested_parameter_doesnt_change(yaml_file, nresource_type, *nprops): if isinstance(parameter, list): parameter = parameter[0] - if k1 != nparam: # we only care about the parameter we found in nested template + if ( + k1 != nparam + ): # we only care about the parameter we found in nested template continue if k1 != parameter: @@ -129,6 +145,7 @@ def check_nested_parameter_doesnt_change(yaml_file, nresource_type, *nprops): # def test_parameter_name_doesnt_change_in_nested_template(yaml_file): # check_nested_parameter_doesnt_change(yaml_file) + @validates("R-708564") def test_server_name_parameter_name_doesnt_change_in_nested_template(heat_template): check_nested_parameter_doesnt_change(heat_template, "OS::Nova::Server", "name") @@ -257,7 +274,7 @@ def test_vmi_aap_parameter_name_doesnt_change_in_nested_template(heat_template): "virtual_machine_interface_allowed_address_pairs", "virtual_machine_interface_allowed_address_pairs_allowed_address_pair", "virtual_machine_interface_allowed_address_pairs_allowed_address_pair_ip", - "virtual_machine_interface_allowed_address_pairs_allowed_address_pair_ip_ip_prefix" + "virtual_machine_interface_allowed_address_pairs_allowed_address_pair_ip_ip_prefix", ) diff --git a/ice_validator/tests/test_non_server_name.py b/ice_validator/tests/test_non_server_name.py index 7139c9b..9361389 100644 --- a/ice_validator/tests/test_non_server_name.py +++ b/ice_validator/tests/test_non_server_name.py @@ -151,7 +151,9 @@ def test_non_server_name(yaml_file): def test_non_server_name_unique(heat_template): """Test name has unique value """ - list_nest = nested_files.get_list_of_nested_files(heat_template, os.path.dirname(heat_template)) + list_nest = nested_files.get_list_of_nested_files( + heat_template, os.path.dirname(heat_template) + ) list_nest.append(heat_template) non_servers = {} for yaml_file in list_nest: diff --git a/ice_validator/tests/utils/vm_types.py b/ice_validator/tests/utils/vm_types.py index ae14336..ae6d7ff 100644 --- a/ice_validator/tests/utils/vm_types.py +++ b/ice_validator/tests/utils/vm_types.py @@ -83,7 +83,11 @@ def get_vm_types_for_resource(resource): def is_nova_server(resource): - return "type" in resource and "properties" in resource and resource.get("type") == "OS::Nova::Server" + return ( + "type" in resource + and "properties" in resource + and resource.get("type") == "OS::Nova::Server" + ) def get_vm_type_for_nova_server(resource): diff --git a/install_win_deps.py b/install_win_deps.py index 01e9503..e7d9c20 100644 --- a/install_win_deps.py +++ b/install_win_deps.py @@ -36,7 +36,7 @@ # import os import platform -import subprocess #nosec +import subprocess # nosec import sys import tempfile from urllib import request @@ -44,12 +44,12 @@ from urllib import request PREBUILT_DOWNLOAD_SITE = "https://download.lfd.uci.edu/pythonlibs/n5jyqt7p/" PREBUILT_WIN_LIBS = [ "yappi-1.0-cp{python_version}-cp{python_version}m-{arch}.whl", - "setproctitle-1.1.10-cp{python_version}-cp{python_version}m-{arch}.whl" + "setproctitle-1.1.10-cp{python_version}-cp{python_version}m-{arch}.whl", ] def is_windows(): - return os.name == 'nt' + return os.name == "nt" def python_version(): @@ -62,7 +62,7 @@ def system_architecture(): def download_url(url): - resp = request.urlopen(url) #nosec + resp = request.urlopen(url) # nosec return resp.read() @@ -81,15 +81,16 @@ def install_prebuilt_binaries_on_windows(): return temp_dir = tempfile.mkdtemp() for lib in PREBUILT_WIN_LIBS: - filename = lib.format(python_version=python_version(), - arch=system_architecture()) + filename = lib.format( + python_version=python_version(), arch=system_architecture() + ) url = PREBUILT_DOWNLOAD_SITE + filename print(f"Downloading {url}") contents = download_url(url) file_path = os.path.join(temp_dir, filename) write_file(contents, file_path, mode="wb") print("Download complete. Installing dependency.") - subprocess.call(["pip", "install", file_path]) #nosec + subprocess.call(["pip", "install", file_path]) # nosec if __name__ == "__main__": diff --git a/requirements.txt b/requirements.txt index 09c73d1..203dc09 100644 --- a/requirements.txt +++ b/requirements.txt @@ -51,3 +51,5 @@ mock openstack-heat cached-property>=1.5,<1.6 bandit +black +pre-commit diff --git a/tests.md b/tests.md deleted file mode 100644 index 7f3c90d..0000000 --- a/tests.md +++ /dev/null @@ -1,3 +0,0 @@ -# Implemented Tests - -Reworking the documentation so removing these for now