From 1b6a93d93fe6ac41eb221dc59c2171e9bdd578e3 Mon Sep 17 00:00:00 2001 From: paweldenst Date: Tue, 25 Oct 2022 11:37:03 +0000 Subject: Run Python 3.11 tests This commit deletes unnecessary comments Issue-ID: INT-2168 Signed-off-by: paweldenst Change-Id: I08b738d3a9aebbdf5d305a118a4d7b7e64d686a5 --- .gitlab-ci.yml | 8 ++++++++ src/onapsdk/sdc/service.py | 17 ++++++++--------- src/onapsdk/utils/configuration.py | 5 ----- src/onapsdk/utils/headers_creator.py | 2 +- src/onapsdk/utils/tosca_file_handler.py | 4 ++-- tests/test_configuration.py | 4 ---- tests/test_service.py | 13 +++++++------ tox.ini | 5 ++++- 8 files changed, 30 insertions(+), 28 deletions(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 5ed14e3..84ae83b 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -102,6 +102,14 @@ image: python:3.10 <<: *integration_tests + bandit: + image: python:3.10 + stage: linting + script: + - pip install -r requirements.txt + - pip3 install bandit + - bandit -r src/onapsdk + pages: stage: deploy image: diff --git a/src/onapsdk/sdc/service.py b/src/onapsdk/sdc/service.py index c866fa4..9bd92f0 100644 --- a/src/onapsdk/sdc/service.py +++ b/src/onapsdk/sdc/service.py @@ -31,8 +31,7 @@ from onapsdk.exceptions import (ParameterError, RequestError, ResourceNotFound, from onapsdk.sdc.category_management import ServiceCategory from onapsdk.sdc.properties import NestedInput, Property from onapsdk.sdc.sdc_resource import SdcResource -from onapsdk.utils.configuration import (components_needing_distribution, - tosca_path) +from onapsdk.utils.configuration import components_needing_distribution from onapsdk.utils.headers_creator import headers_sdc_creator, headers_sdc_artifact_upload from onapsdk.utils.jinja import jinja_env @@ -591,7 +590,7 @@ class Service(SdcResource): # pylint: disable=too-many-instance-attributes, too const.DISTRIBUTE, headers=headers) - def get_tosca(self) -> None: + def get_tosca(self, paths) -> None: """Get Service tosca files and save it.""" url = "{}/services/{}/toscaModel".format(self._base_url(), self.identifier) @@ -603,17 +602,17 @@ class Service(SdcResource): # pylint: disable=too-many-instance-attributes, too url, headers=headers) if result: - self._create_tosca_file(result) + self._create_tosca_file(paths, result) - def _create_tosca_file(self, result: Response) -> None: + def _create_tosca_file(self, paths, result: Response) -> None: """Create Service Tosca files from HTTP response.""" csar_filename = "service-{}-csar.csar".format(self.name) - makedirs(tosca_path(), exist_ok=True) - with open((tosca_path() + csar_filename), 'wb') as csar_file: + makedirs(paths, exist_ok=True) + with open((paths + csar_filename), 'wb') as csar_file: for chunk in result.iter_content(chunk_size=128): csar_file.write(chunk) try: - self._unzip_csar_file(tosca_path() + csar_filename, + self._unzip_csar_file(paths + csar_filename, self._write_csar_file) except BadZipFile as exc: self._logger.exception(exc) @@ -795,7 +794,7 @@ class Service(SdcResource): # pylint: disable=too-many-instance-attributes, too def _write_csar_file(service_template: str, template_file: TextIOWrapper) -> None: """Write service temple into a file.""" - with open(tosca_path() + service_template[12:], 'wb') as file: + with open(service_template[12:], 'wb') as file: file.write(template_file.read()) # _service_template is not used but function generation is generic diff --git a/src/onapsdk/utils/configuration.py b/src/onapsdk/utils/configuration.py index 89bee5a..487a472 100644 --- a/src/onapsdk/utils/configuration.py +++ b/src/onapsdk/utils/configuration.py @@ -15,11 +15,6 @@ from typing import List -def tosca_path() -> str: - """Return tosca file paths.""" - return '/tmp/tosca_files/' - - def components_needing_distribution() -> List[str]: """Return the list of components needing distribution.""" return ["SO", "sdnc", "aai"] diff --git a/src/onapsdk/utils/headers_creator.py b/src/onapsdk/utils/headers_creator.py index adb0609..ae03a38 100644 --- a/src/onapsdk/utils/headers_creator.py +++ b/src/onapsdk/utils/headers_creator.py @@ -223,7 +223,7 @@ def headers_sdc_artifact_upload(base_header: Dict[str, str], data: str): headers["Accept"] = "application/json, text/plain, */*" headers["Accept-Encoding"] = "gzip, deflate, br" headers["Content-Type"] = "application/json; charset=UTF-8" - md5_content = hashlib.md5(data.encode('UTF-8')).hexdigest() + md5_content = hashlib.new('md5', data.encode('UTF-8'), usedforsecurity=False).hexdigest() content = base64.b64encode(md5_content.encode('ascii')).decode('UTF-8') headers["Content-MD5"] = content return headers diff --git a/src/onapsdk/utils/tosca_file_handler.py b/src/onapsdk/utils/tosca_file_handler.py index 921b868..1d1918b 100644 --- a/src/onapsdk/utils/tosca_file_handler.py +++ b/src/onapsdk/utils/tosca_file_handler.py @@ -14,7 +14,7 @@ # limitations under the License. import json import string -import random +import secrets from typing import Dict, List from onapsdk.exceptions import ValidationError @@ -103,4 +103,4 @@ def random_string_generator(size=6, str: a sequence of random characters """ - return ''.join(random.choice(chars) for _ in range(size)) + return ''.join(secrets.choice(chars) for _ in range(size)) diff --git a/tests/test_configuration.py b/tests/test_configuration.py index 70ae14a..d35776c 100644 --- a/tests/test_configuration.py +++ b/tests/test_configuration.py @@ -12,12 +12,8 @@ # See the License for the specific language governing permissions and # limitations under the License. -from onapsdk.utils.configuration import tosca_path from onapsdk.utils.configuration import components_needing_distribution -def test_tosca_path(): - assert tosca_path() == "/tmp/tosca_files/" - def test_components_needing_distribution(): assert "SO" in components_needing_distribution() assert "sdnc" in components_needing_distribution() diff --git a/tests/test_service.py b/tests/test_service.py index e486833..7606ed2 100755 --- a/tests/test_service.py +++ b/tests/test_service.py @@ -18,10 +18,8 @@ from pathlib import Path from unittest import mock from unittest.mock import MagicMock, PropertyMock import shutil - import oyaml as yaml import pytest - import onapsdk.constants as const from onapsdk.exceptions import ParameterError, RequestError, ResourceNotFound, StatusError, ValidationError from onapsdk.sdc.category_management import ServiceCategory @@ -513,7 +511,7 @@ def test_get_tosca_no_result(mock_send): mock_send.return_value = {} svc = Service() svc.identifier = "12" - svc.get_tosca() + svc.get_tosca('/tmp/tosca_files') headers = headers_sdc_creator(svc.headers) headers['Accept'] = 'application/octet-stream' mock_send.assert_called_once_with( @@ -533,7 +531,8 @@ def test_get_tosca_bad_csart(requests_mock): requests_mock.get( 'https://sdc.api.be.simpledemo.onap.org:30204/sdc/v1/catalog/services/12/toscaModel', content=file_content) - svc.get_tosca() + svc.get_tosca('directory') + assert not path.exists('/tmp/tosca_files') def test_get_tosca_result(requests_mock): @@ -546,7 +545,9 @@ def test_get_tosca_result(requests_mock): content=file_content) svc = Service() svc.identifier = "12" - svc.get_tosca() + svc.get_tosca('directory') + assert not path.exists('/tmp/tosca_files') + def test_get_tosca_result_no_service_in_csar(requests_mock): if path.exists('/tmp/tosca_files'): @@ -559,7 +560,7 @@ def test_get_tosca_result_no_service_in_csar(requests_mock): svc = Service() svc.identifier = "12" with pytest.raises(ValidationError): - svc.get_tosca() + svc.get_tosca('directory') @mock.patch.object(Service, 'send_message_json') def test_distributed_api_error(mock_send): diff --git a/tox.ini b/tox.ini index 40c3a5a..4da867f 100644 --- a/tox.ini +++ b/tox.ini @@ -1,5 +1,5 @@ [tox] -envlist = py37,py38,py39,py310,pylint,pydocstyle +envlist = py37,py38,py39,py310,py311,pylint,pydocstyle,bandit [testenv] commands = pytest tests/ @@ -12,3 +12,6 @@ basepython = python3.8 [testenv:pydocstyle] commands = pydocstyle src/ + +[testenv:bandit] +commands = bandit -r src/onapsdk -- cgit 1.2.3-korg