Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Start linting the test #1710

Merged
merged 10 commits into from
Dec 10, 2021
6 changes: 3 additions & 3 deletions docs/CONTRIBUTORS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,9 @@ Individual tests can also be executed. Optional '-v' flags can be added to
increase log level up to DEBUG ('-vvvv').
::

$ python3 test_updater.py # run a specific test file
$ python3 test_updater.py TestUpdater.test_4_refresh # run a specific test
$ python3 test_updater.py -vvvv TestUpdater.test_4_refresh # run test with DEBUG log level
$ python3 test_updater_ng.py # run a specific test file
$ python3 test_updater_ng.py TestUpdater.test_refresh_and_download # run a specific test
$ python3 test_updater_ng.py -vvvv TestUpdater.test_refresh_and_download # run test with DEBUG log level


All of the log levels and the corresponding options that could be used for testing are:
Expand Down
9 changes: 9 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,17 @@ build-backend = "setuptools.build_meta"
# Read more here: https://black.readthedocs.io/en/stable/usage_and_configuration/the_basics.html#configuration-via-a-file
[tool.black]
line-length=80
# TODO: remove "excludes" after deleting old test files
exclude="tests/.*old.py"

# Isort section
# Read more here: https://pycqa.github.io/isort/docs/configuration/config_files.html
[tool.isort]
profile="black"
line_length=80
known_first_party = ["tuf"]
# TODO: remove "skip_glob" after deleting old test files
skip_glob="*old.py"

# Pylint section

Expand Down Expand Up @@ -55,6 +59,8 @@ module-rgx="^(_?[a-z][a-z0-9_]*|__init__)$"
no-docstring-rgx="(__.*__|main|test.*|.*test|.*Test)$"
variable-rgx="^[a-z][a-z0-9_]*$"
docstring-min-length=10
# TODO: remove "ignore-patterns" after deleting old test files
ignore-patterns=".*_old.py"

[tool.pylint.logging]
logging-format-style="old"
Expand All @@ -76,6 +82,9 @@ strict_equality = "True"
disallow_untyped_defs = "True"
disallow_untyped_calls = "True"
show_error_codes = "True"
disable_error_code = ["attr-defined"]
# TODO: remove "exclude" after deleting old test files
exclude=".*_old.py"

[[tool.mypy.overrides]]
module = [
Expand Down
1 change: 1 addition & 0 deletions requirements-test.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ pylint
mypy
bandit
types-requests
types-python-dateutil
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
See LICENSE-MIT.txt OR LICENSE-APACHE.txt for licensing information.

<Purpose>
Generate a pre-fabricated set of metadata files for 'test_developer_tool.py'
test cases.
Generate a pre-fabricated set of metadata files for
'test_developer_tool_old.py' test cases.
"""

import shutil
Expand Down
18 changes: 8 additions & 10 deletions tests/repository_simulator.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ def fetch(self, url: str) -> Iterator[bytes]:
role = ver_and_name
version = None

yield self._fetch_metadata(role, version)
yield self.fetch_metadata(role, version)
elif path.startswith("/targets/"):
# figure out target path and hash prefix
target_path = path[len("/targets/") :]
Expand All @@ -228,11 +228,11 @@ def fetch(self, url: str) -> Iterator[bytes]:
prefix, _, filename = prefixed_filename.partition(".")
target_path = f"{dir_parts}{sep}{filename}"

yield self._fetch_target(target_path, prefix)
yield self.fetch_target(target_path, prefix)
else:
raise FetcherHTTPError(f"Unknown path '{path}'", 404)

def _fetch_target(
def fetch_target(
self, target_path: str, target_hash: Optional[str]
) -> bytes:
"""Return data for 'target_path', checking 'target_hash' if it is given.
Expand All @@ -253,9 +253,7 @@ def _fetch_target(
logger.debug("fetched target %s", target_path)
return repo_target.data

def _fetch_metadata(
self, role: str, version: Optional[int] = None
) -> bytes:
def fetch_metadata(self, role: str, version: Optional[int] = None) -> bytes:
"""Return signed metadata for 'role', using 'version' if it is given.

If version is None, non-versioned metadata is being requested.
Expand Down Expand Up @@ -298,7 +296,7 @@ def _fetch_metadata(
def _compute_hashes_and_length(
self, role: str
) -> Tuple[Dict[str, str], int]:
data = self._fetch_metadata(role)
data = self.fetch_metadata(role)
digest_object = sslib_hash.digest(sslib_hash.DEFAULT_HASH_ALGORITHM)
digest_object.update(data)
hashes = {sslib_hash.DEFAULT_HASH_ALGORITHM: digest_object.hexdigest()}
Expand Down Expand Up @@ -392,12 +390,12 @@ def write(self) -> None:

for ver in range(1, len(self.signed_roots) + 1):
with open(os.path.join(dest_dir, f"{ver}.root.json"), "wb") as f:
f.write(self._fetch_metadata(Root.type, ver))
f.write(self.fetch_metadata(Root.type, ver))

for role in [Timestamp.type, Snapshot.type, Targets.type]:
with open(os.path.join(dest_dir, f"{role}.json"), "wb") as f:
f.write(self._fetch_metadata(role))
f.write(self.fetch_metadata(role))

for role in self.md_delegates:
with open(os.path.join(dest_dir, f"{role}.json"), "wb") as f:
f.write(self._fetch_metadata(role))
f.write(self.fetch_metadata(role))
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

"""
<Program>
simple_https_server.py
simple_https_server_old.py

<Author>
Vladimir Diaz.
Expand Down Expand Up @@ -53,7 +53,7 @@
print(port_message)

if len(sys.argv) > 1 and certfile != sys.argv[1]:
print('simple_https_server: cert file was not found: ' + sys.argv[1] +
print('simple_https_server_old: cert file was not found: ' + sys.argv[1] +
'; using default: ' + certfile + " certfile")

httpd.serve_forever()
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

"""
<Program Name>
slow_retrieval_server.py
slow_retrieval_server_old.py

<Author>
Konstantin Andrianov.
Expand All @@ -18,7 +18,7 @@

<Purpose>
Server that throttles data by sending one byte at a time (specified time
interval 'DELAY'). The server is used in 'test_slow_retrieval_attack.py'.
interval 'DELAY'). The server is used in 'test_slow_retrieval_attack_old.py'.
"""

import os
Expand Down
9 changes: 6 additions & 3 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
from tests import utils
from tuf import exceptions
from tuf.api.metadata import (
TOP_LEVEL_ROLE_NAMES,
DelegatedRole,
Delegations,
Key,
Expand Down Expand Up @@ -136,7 +137,7 @@ def test_compact_json(self) -> None:
)

def test_read_write_read_compare(self) -> None:
for metadata in [Root.type, Snapshot.type, Timestamp.type, Targets.type]:
for metadata in TOP_LEVEL_ROLE_NAMES:
path = os.path.join(self.repo_dir, "metadata", metadata + ".json")
md_obj = Metadata.from_file(path)

Expand All @@ -148,7 +149,7 @@ def test_read_write_read_compare(self) -> None:
os.remove(path_2)

def test_to_from_bytes(self) -> None:
for metadata in [Root.type, Snapshot.type, Timestamp.type, Targets.type]:
for metadata in TOP_LEVEL_ROLE_NAMES:
path = os.path.join(self.repo_dir, "metadata", metadata + ".json")
with open(path, "rb") as f:
metadata_bytes = f.read()
Expand Down Expand Up @@ -710,7 +711,9 @@ def test_targetfile_from_file(self) -> None:

def test_targetfile_from_data(self) -> None:
data = b"Inline test content"
target_file_path = os.path.join(self.repo_dir, Targets.type, "file1.txt")
target_file_path = os.path.join(
self.repo_dir, Targets.type, "file1.txt"
)

# Test with a valid hash algorithm
targetfile_from_data = TargetFile.from_data(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

"""
<Program Name>
test_arbitrary_package_attack.py
test_arbitrary_package_attack_old.py

<Author>
Konstantin Andrianov.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

"""
<Program Name>
test_developer_tool.py.
test_developer_tool_old.py.

<Authors>
Santiago Torres Arias <torresariass@gmail.com>
Expand Down
12 changes: 6 additions & 6 deletions tests/test_download.py → tests/test_download_old.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

"""
<Program>
test_download.py
test_download_old.py

<Author>
Konstantin Andrianov.
Expand All @@ -19,7 +19,7 @@
<Purpose>
Unit test for 'download.py'.

NOTE: Make sure test_download.py is ran in 'tuf/tests/' directory.
NOTE: Make sure test_download_old.py is ran in 'tuf/tests/' directory.
Otherwise, module that launches simple server would not be found.

TODO: Adopt the environment variable management from test_proxy_use.py here.
Expand Down Expand Up @@ -274,16 +274,16 @@ def test_https_connection(self):


good_https_server_handler = utils.TestServerProcess(log=logger,
server='simple_https_server.py',
server='simple_https_server_old.py',
extra_cmd_args=[good_cert_fname])
good2_https_server_handler = utils.TestServerProcess(log=logger,
server='simple_https_server.py',
server='simple_https_server_old.py',
extra_cmd_args=[good2_cert_fname])
bad_https_server_handler = utils.TestServerProcess(log=logger,
server='simple_https_server.py',
server='simple_https_server_old.py',
extra_cmd_args=[bad_cert_fname])
expd_https_server_handler = utils.TestServerProcess(log=logger,
server='simple_https_server.py',
server='simple_https_server_old.py',
extra_cmd_args=[expired_cert_fname])

suffix = '/' + os.path.basename(target_filepath)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

"""
<Program Name>
test_endless_data_attack.py
test_endless_data_attack_old.py

<Author>
Konstantin Andrianov.
Expand Down
19 changes: 14 additions & 5 deletions tests/test_examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,13 @@
import glob
import os
import shutil
import sys
import tempfile
import unittest
from pathlib import Path
from typing import ClassVar, List

from tests import utils


class TestRepoExamples(unittest.TestCase):
Expand All @@ -20,26 +24,30 @@ class TestRepoExamples(unittest.TestCase):

"""

repo_examples_dir: ClassVar[Path]

@classmethod
def setUpClass(cls):
def setUpClass(cls) -> None:
"""Locate and cache 'repo_example' dir."""
base = Path(__file__).resolve().parents[1]
cls.repo_examples_dir = base / "examples" / "repo_example"

def setUp(self):
def setUp(self) -> None:
"""Create and change into test dir.
NOTE: Test scripts are expected to create dirs/files in new CWD."""
self.original_cwd = os.getcwd()
self.base_test_dir = os.path.realpath(tempfile.mkdtemp())
os.chdir(self.base_test_dir)

def tearDown(self):
def tearDown(self) -> None:
"""Change back to original dir and remove test dir, which may contain
dirs/files the test created at test-time CWD."""
os.chdir(self.original_cwd)
shutil.rmtree(self.base_test_dir)

def _run_script_and_assert_files(self, script_name, filenames_created):
def _run_script_and_assert_files(
self, script_name: str, filenames_created: List[str]
) -> None:
"""Run script in 'repo_example' dir and assert that it created the
files corresponding to the passed filenames inside a 'tmp*' test dir at
CWD."""
Expand All @@ -63,7 +71,7 @@ def _run_script_and_assert_files(self, script_name, filenames_created):
metadata_path.exists(), f"missing '{metadata_path}' file"
)

def test_basic_repo(self):
def test_basic_repo(self) -> None:
"""Run 'basic_repo.py' and assert creation of metadata files."""
self._run_script_and_assert_files(
"basic_repo.py",
Expand All @@ -81,4 +89,5 @@ def test_basic_repo(self):


if __name__ == "__main__":
utils.configure_test_logging(sys.argv)
unittest.main()
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

"""
<Program Name>
test_extraneous_dependencies_attack.py
test_extraneous_dependencies_attack_old.py

<Author>
Zane Fisher.
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion tests/test_formats.py → tests/test_formats_old.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

"""
<Program Name>
test_formats.py
test_formats_old.py

<Author>
Vladimir Diaz <vladimir.v.diaz@gmail.com>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

"""
<Program Name>
test_indefinite_freeze_attack.py
test_indefinite_freeze_attack_old.py

<Author>
Konstantin Andrianov.
Expand Down Expand Up @@ -36,7 +36,6 @@
metadata without the client being aware.
"""

import datetime
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why this change in an "_old.py" file?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just saw it was unused and decided to remove it, but I can return it if it looks strange.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤷 Probably not worth the effort. :) Let's leave it there.

import os
import time
import tempfile
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

"""
<Program Name>
test_key_revocation_integration.py
test_key_revocation_integration_old.py

<Author>
Vladimir Diaz.
Expand All @@ -18,7 +18,7 @@

<Purpose>
Integration test that verifies top-level roles are updated after all of their
keys have been revoked. There are unit tests in 'test_repository_tool.py'
keys have been revoked. There are unit tests in 'test_repository_tool_old.py'
that verify key and role revocation of specific roles, but these should be
expanded to verify key revocations over the span of multiple snapshots of the
repository.
Expand Down
2 changes: 1 addition & 1 deletion tests/test_keydb.py → tests/test_keydb_old.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

"""
<Program Name>
test_keydb.py
test_keydb_old.py

<Author>
Vladimir Diaz <vladimir.v.diaz@gmail.com>
Expand Down
2 changes: 1 addition & 1 deletion tests/test_log.py → tests/test_log_old.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

"""
<Program Name>
test_log.py
test_log_old.py

<Authors>
Vladimir Diaz <vladimir.v.diaz@gmail.com>
Expand Down