aurweb/test
Kevin Morris a467b18474
Merge branch 'pu': pre-v6.0.0
Release v6.0.0 - Python

This documents UX and functional changes for the v6.0.0 aurweb release.
Following this release, we'll be working on a few very nice features
noted at the end of this article in Upcoming Work.

Preface
-------

This v6.0.0 release makes the long-awaited Python port official.

Along with the development of the python port, we have modified a
number of features. There have been some integral changes to how
package requests are dealt with, so _Trusted Users_ should read
the entirety of this document.

Legend
------

There are a few terms which I'd like to define to increase
understanding of these changes as they are listed:

- _self_
    - Refers to a user viewing or doing something regarding their own account
- _/pkgbase/{name}/{action}_
    - Refers to a POST action which can be triggered via the relevent package
      page at `/{pkgbase,packages}/{name}`.

Grouped changes explained in multiple items will always be prefixed with
the same letter surrounded by braces. Example:

- [A] Some feature that does something
- [A] The same feature where another thing has changed

Infrastructure
--------------

- Python packaging is now done with poetry.
- SQLite support has been removed. This was done because even though
  SQLAlchemy is an ORM, SQLite has quite a few SQL-server-like features
  missing both out of the box and integrally which force us to account
  for the different database types. We now only support mysql, and should
  be able to support postgresql without much effort in the future.
  Note: Users wishing to easily spin up a database quickly can use
  `docker-compose up -d mariadb` for a Docker-hosted mariadb service.
- An example systemd service has been included at `examples/aurweb.service`.
- Example wrappers to `aurweb-git-(auth|serve|update)` have been included
  at `examples/aurweb-git-(auth|serve|update).sh` and should be used to
  call these scripts when aurweb is installed into a poetry virtualenv.

HTML
----

- Pagers have all been modified. They still serve the same purpose, but
  they have slightly different display.
- Some markup and methods around the website has been changed for
  post requests, and some forms have been completely reworked.

Package Requests
----------------

- Normal users can now view and close their own requests
- [A] Requests can no longer be accepted through manual closures
- [A] Requests are now closed via their relevent actions
    - Deletion
        - Through `/packages` bulk delete action
        - Through `/pkgbase/{name}/delete`
    - Merge
        - Through `/pkgbase/{name}/merge`
    - Orphan
        - Through `/packages` bulk disown action
        - Through `/pkgbase/{name}/disown`
- Deletion and merge requests (and their closures) are now autogenerated
  if no pre-existing request exists. This was done to increase tracking of
  package modifications performed by those with access to do so (TUs).
- Deletion, merge and orphan request actions now close all (1 or more)
  requests pertaining to the action performed. This comes with the downside
  of multiple notifications sent out about a closure if more than one
  request (or no request) exists for them
- Merge actions now automatically reject other pre-existing merge requests
  with a mismatched `MergeBaseName` column when a merge action is performed
- The last `/requests` page no longer goes nowhere

Package Bulk Actions: /packages
-------------------------------

- The `Merge into` field has been removed. Merges now require being
  performed via the `/pkgbase/{name}/merge` action.

Package View
------------

- Some cached metadata is no longer cached (pkginfo). Previously,
  this was defaulted to a one day cache for some package information.
  If we need to bring this back, we can.

TU Proposals
------------

- A valid username is now required for any addition or removal of a TU.

RPC
---

- `type=get-comment-form` has been removed and is now located at
  `/pkgbase/{name}/comments/{id}/form`.
- Support for versions 1-4 have been removed.
- JSON key ordering is different than PHP's JSON.
- `type=search` performance is overall slightly worse than PHP's. This
  should not heavily affect users, as a 3,000 record query is returned
  in roughly 0.20ms from a local standpoint. We will be working on this
  in aim to push it over PHP.

Archives
--------

- Added metadata archive `packages-meta-v1.json.gz`.
- Added metadata archive `packages-meta-ext-v1.json.gz`.
    - Enable this by passing `--extended` to `aurweb-mkpkglists`.

Performance Changes
-------------------

As is expected from a complete rewrite of the website, performance
has changed across the board. In most places, Python's implementation
now performs better than the pre-existing PHP implementation, with the
exception of a few routes. Notably:

- `/` loads much quicker as it is now persistently cached forcibly
  for five minutes at a time.
- `/packages` search is much quicker.
- `/packages/{name}` view is slightly slower; we are no longer caching
  various pieces of package info for `cache_pkginfo_ttl`, which is
  defaulted to 86400 seconds, or one day.
- Request actions are slower due to the removal of the `via` parameter.
  We now query the database for requests related to the action based on
  the current state of the DB.
- `/rpc?type=info` queries are slightly quicker.
- `/rpc?type=search` queries of low result counts are quicker.
- `/rpc?type=search` queries of large result counts (> 2500) are slower.
    - We are not satisfied with this. We'll be working on pushing this
      over the edge along with the rest of the DB-intensive routes.
      However, the speed degredation is quite negligible for users'
      experience: 0.12ms PHP vs 0.15ms Python on a 3,000 record query
      on my local 4-core 8-thread system.

Upcoming Work
-------------

This release is the first major release of the Python implementation.
We have multiple tasks up for work immediately, which will bring us
a few more minor versions forward as they are completed.

- Update request and tu vote pagers
- Archive differentials
- Archive mimetypes
- (a) Git scripts to ORM conversion
- (a) Sharness removal
- Restriction of number of requests users can submit
2022-01-18 10:39:59 -08:00
..
scripts Makefile: run pytest units 2021-06-05 19:49:19 -07:00
__init__.py aurweb: Globalize a Translator instance, add more utility 2021-06-05 19:49:42 -07:00
conftest.py feat(testing.email): add Email.dump 2021-12-09 15:10:04 -08:00
Makefile change: set -v for sh tests 2021-11-22 10:22:32 -08:00
README.md feat: support LOG_CONFIG environment variable 2022-01-18 07:27:36 -08:00
setup.sh Merge branch 'pu': pre-v6.0.0 2022-01-18 10:39:59 -08:00
sharness.sh sharness: do not use spaces in trash directory 2021-07-27 16:43:16 -07:00
t1100-git-auth.t Makefile: run pytest units 2021-06-05 19:49:19 -07:00
t1200-git-serve.t Makefile: run pytest units 2021-06-05 19:49:19 -07:00
t1300-git-update.t Makefile: run pytest units 2021-06-05 19:49:19 -07:00
test_accepted_term.py housekeep(fastapi): rewrite test_accepted_term with fixtures 2021-12-01 00:30:18 -08:00
test_account_type.py change(aurweb): add parallel tests and improve aurweb.db 2021-11-17 01:34:59 -08:00
test_accounts_routes.py housekeep: centralize datetime generation 2022-01-18 07:31:04 -08:00
test_adduser.py feat: add aurweb-adduser console script 2022-01-08 13:40:38 -08:00
test_api_rate_limit.py change(aurweb): add parallel tests and improve aurweb.db 2021-11-17 01:34:59 -08:00
test_asgi.py feat(python): catch all exceptions thrown through fastapi route paths 2022-01-09 23:10:02 -08:00
test_aurblup.py fix(test_aurblup): use correct type hint for tmpdir 2021-12-01 00:33:31 -08:00
test_auth.py housekeep: centralize datetime generation 2022-01-18 07:31:04 -08:00
test_auth_routes.py housekeep: centralize datetime generation 2022-01-18 07:31:04 -08:00
test_ban.py housekeep(fastapi): rewrite test_ban with fixtures 2021-12-01 00:33:30 -08:00
test_cache.py change(aurweb): add parallel tests and improve aurweb.db 2021-11-17 01:34:59 -08:00
test_captcha.py change(aurweb): add parallel tests and improve aurweb.db 2021-11-17 01:34:59 -08:00
test_config.py feat(aurweb-config): add unset action and simplify 2021-11-27 22:34:15 -08:00
test_db.py change(aurweb): add parallel tests and improve aurweb.db 2021-11-17 01:34:59 -08:00
test_defaults.py feat(FastAPI): added /requests (get) route 2021-10-02 22:43:48 -07:00
test_dependency_type.py change(aurweb): add parallel tests and improve aurweb.db 2021-11-17 01:34:59 -08:00
test_email.py feat(testing.email): add Email.dump 2021-12-09 15:10:04 -08:00
test_exceptions.py feat(exceptions): add InvariantError 2021-12-09 15:10:06 -08:00
test_filelock.py fix(conftest): use synchronization locks for setup_database 2021-11-28 19:55:11 -08:00
test_filters.py housekeep: centralize datetime generation 2022-01-18 07:31:04 -08:00
test_group.py change(aurweb): add parallel tests and improve aurweb.db 2021-11-17 01:34:59 -08:00
test_homepage.py housekeep: centralize datetime generation 2022-01-18 07:31:04 -08:00
test_html.py test: add tests to check various 404 paths and 503 2022-01-03 18:22:10 -08:00
test_initdb.py change(aurweb): add parallel tests and improve aurweb.db 2021-11-17 01:34:59 -08:00
test_l10n.py housekeep: define filters in their own modules 2022-01-18 03:06:17 -08:00
test_license.py change(aurweb): add parallel tests and improve aurweb.db 2021-11-17 01:34:59 -08:00
test_logging.py fix(fastapi): centralize logging initialization 2021-10-21 10:40:52 -07:00
test_mkpkglists.py change(mkpkglists): converted to use aurweb.db ORM 2021-11-28 19:55:08 -08:00
test_notify.py housekeep: centralize datetime generation 2022-01-18 07:31:04 -08:00
test_official_provider.py change(aurweb): add parallel tests and improve aurweb.db 2021-11-17 01:34:59 -08:00
test_package.py housekeep(fastapi): rewrite test_package with fixtures 2021-12-01 00:33:29 -08:00
test_package_base.py housekeep(fastapi): rewrite test_package_base with fixtures 2021-12-01 00:30:21 -08:00
test_package_blacklist.py housekeep(fastapi): rewrite test_package_blacklist 2021-12-01 00:28:40 -08:00
test_package_comaintainer.py housekeep(fastapi): rewrite test_package_comaintainer with fixtures 2021-12-01 00:30:19 -08:00
test_package_comment.py housekeep(fastapi): rewrite test_package_comment with fixtures 2021-12-01 00:28:39 -08:00
test_package_dependency.py fix(models.package_dependency): add DepTypeID to PKs 2022-01-05 14:36:57 -08:00
test_package_group.py housekeep(fastapi): rewrite test_package_group with fixtures 2021-12-01 00:30:22 -08:00
test_package_keyword.py housekeep(fastapi): rewrite test_package_keyword with fixtures 2021-12-01 00:30:18 -08:00
test_package_license.py housekeep(fastapi): rewrite test_package_license with fixtures 2021-12-01 00:30:19 -08:00
test_package_notification.py housekeep(fastapi): rewrite test_package_notification with fixtures 2021-12-01 00:30:19 -08:00
test_package_relation.py housekeep(fastapi): rewrite test_package_relation with fixtures 2021-12-01 00:30:20 -08:00
test_package_request.py housekeep: centralize datetime generation 2022-01-18 07:31:04 -08:00
test_package_source.py housekeep(fastapi): rewrite test_package_source with fixtures 2021-12-01 00:30:22 -08:00
test_package_vote.py housekeep: centralize datetime generation 2022-01-18 07:31:04 -08:00
test_packages_routes.py housekeep: centralize datetime generation 2022-01-18 07:31:04 -08:00
test_packages_util.py housekeep: centralize datetime generation 2022-01-18 07:31:04 -08:00
test_pkgbase_routes.py housekeep: centralize datetime generation 2022-01-18 07:31:04 -08:00
test_pkgmaint.py housekeep: centralize datetime generation 2022-01-18 07:31:04 -08:00
test_popupdate.py change(aurweb): add parallel tests and improve aurweb.db 2021-11-17 01:34:59 -08:00
test_ratelimit.py change(aurweb): add parallel tests and improve aurweb.db 2021-11-17 01:34:59 -08:00
test_redis.py FastAPI: add redis integration 2021-08-17 19:58:28 -07:00
test_relation_type.py change(aurweb): add parallel tests and improve aurweb.db 2021-11-17 01:34:59 -08:00
test_rendercomment.py housekeep: centralize datetime generation 2022-01-18 07:31:04 -08:00
test_request_type.py change(aurweb): add parallel tests and improve aurweb.db 2021-11-17 01:34:59 -08:00
test_requests.py housekeep: centralize datetime generation 2022-01-18 07:31:04 -08:00
test_routes.py housekeep(fastapi): rewrite test_routes with fixtures 2021-12-01 00:30:21 -08:00
test_rpc.py housekeep: centralize datetime generation 2022-01-18 07:31:04 -08:00
test_rss.py housekeep: centralize datetime generation 2022-01-18 07:31:04 -08:00
test_session.py housekeep: centralize datetime generation 2022-01-18 07:31:04 -08:00
test_spawn.py feat: add test_spawn, an aurweb.spawn test 2021-11-20 15:47:25 -08:00
test_ssh_pub_key.py housekeep(fastapi): rewrite test_ssh_pub_key with fixtures 2021-12-01 00:30:18 -08:00
test_templates.py housekeep: move templates/partials/widgets/* to templates/partials/ 2022-01-18 08:33:00 -08:00
test_term.py change(aurweb): add parallel tests and improve aurweb.db 2021-11-17 01:34:59 -08:00
test_time.py add aurweb.time module 2021-06-05 20:11:17 -07:00
test_trusted_user_routes.py housekeep: centralize datetime generation 2022-01-18 07:31:04 -08:00
test_tu_vote.py housekeep: centralize datetime generation 2022-01-18 07:31:04 -08:00
test_tu_voteinfo.py housekeep: centralize datetime generation 2022-01-18 07:31:04 -08:00
test_tuvotereminder.py housekeep: centralize datetime generation 2022-01-18 07:31:04 -08:00
test_user.py change(models.user): can_edit_user should check account type id priority 2021-12-27 16:04:57 -08:00
test_usermaint.py housekeep: centralize datetime generation 2022-01-18 07:31:04 -08:00
test_util.py housekeep: define filters in their own modules 2022-01-18 03:06:17 -08:00

aurweb Test Collection

To run all tests, you may run make -C test sh and pytest within the project root:

$ make -C test sh    # Run Sharness tests.
$ poetry run pytest  # Run Pytest suites.

For more control, you may use the prove or pytest command, which receives a directory or a list of files to run, and produces a report.

Each test script is standalone, so you may run them individually. Some tests may receive command-line options to help debugging.

Dependencies

For all tests to run dependencies provided via poetry are required:

$ poetry install

Logging

Tests also require the logging.test.conf logging configuration file to be used. You can specify the LOG_CONFIG environment variable to override:

$ export LOG_CONFIG=logging.test.conf

logging.test.conf enables debug logging for the aurweb package, for which we run tests against.

Test Configuration

To perform any tests, we need to supply aurweb with a valid configuration. For development (and testing) purposes, an example conf/config.dev can be slightly modified.

Start off by copying config.dev to a new configuration.

$ cp -v conf/config.dev conf/config

First, we must tell aurweb where the root of our project lives by replacing YOUR_AUR_ROOT with the path to the aurweb repository.

$ sed -i "s;YOUR_AUR_ROOT;/path/to/aurweb;g" conf/config

Test Databases

Python tests create and drop hashed database names based on PYTEST_CURRENT_TEST. To run tests with a database, the database user must have privileges to create and drop their own databases. Typically, this is the root user, but can be configured for any other user:

GRANT ALL ON *.* TO 'user'@'localhost' WITH GRANT OPTION

The aurweb platform is intended to use the mysql backend, but the sqlite backend is still used for sharness tests. These tests will soon be replaced with pytest suites and sqlite removed.

After ensuring you've configured a test database, users can continue on to Running Tests.

Running tests

Makefile test targets: sh, clean.

Recommended method of running tests: pytest.

Legacy sharness tests: make -C test sh.

aurweb is currently going through a refactor where the majority of sharness tests have been replaced with pytest units. There are still a few sharness tests around, and they are required to gain as much coverage as possible over an entire test run. Users should be writing pytest units for any new features.

Run tests from the project root.

$ cd /path/to/aurweb

Ensure you have the proper AUR_CONFIG and LOG_CONFIG exported:

$ export AUR_CONFIG=conf/config
$ export LOG_CONFIG=logging.test.conf

To run sharness shell test suites (requires Arch Linux):

$ make -C test sh

To run pytest Python test suites:

# With poetry-installed aurweb
$ poetry run pytest

# With globally-installed aurweb
$ pytest

After tests are run, one can produce coverage reports.

# Print out a CLI coverage report.
$ coverage report

# Produce an HTML-based coverage report.
$ coverage html

Writing Python tests (current)

Almost all of our pytest suites use the database in some way. There are a few particular testing utilities in aurweb that one should keep aware of to aid testing code:

  • db_test pytest fixture
    • Prepares test databases for the module and cleans out database tables for each test function requiring this fixture.
  • aurweb.testing.requests.Request
    • A fake stripped down version of fastapi.Request that can be passed to any functions in our codebase which use fastapi.Request parameters.

Example code:

import pytest

from aurweb import db
from aurweb.models.account_type import USER_ID
from aurweb.models.user import User
from aurweb.testing.requests import Request

# We need to use the `db_test` fixture at some point
# during our test functions.
@pytest.fixture(autouse=True)
def setup(db_test: None) -> None:
    return

# Or... specify it in a dependency fixture.
@pytest.fixture
def user(db_test: None) -> User:
    with db.begin():
        user = db.create(User, Username="test",
                         Email="test@example.org",
                         Passwd="testPassword",
                         AccountTypeID=USER_ID)
    yield user

def test_user_login(user: User):
    assert isinstance(user, User) is True

    fake_request = Request()
    sid = user.login(fake_request, "testPassword")
    assert sid is not None

Writing Sharness tests (legacy)

Shell test scripts must follow the Test Anything Protocol specification: http://testanything.org/tap-specification.html

Python tests must be compatible with pytest and included in pytest test/ execution after setting up a configuration.

Tests must support being run from any directory. They may use $0 to determine their location. Python scripts should expect aurweb to be installed and importable without toying with os.path or PYTHONPATH.

Tests written in shell should use sharness. In general, new tests should be consistent with existing tests unless they have a good reason not to.

Debugging Sharness tests

By default, make -C test is quiet and does not print out verbose information about tests being run. If a test is failing, one can look into verbose details of sharness tests by executing them with the --verbose flag. Example: ./t1100_git_auth.t --verbose. This is particularly useful when tests happen to fail in a remote continuous integration environment, where the reader does not have complete access to the runner.