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

Improve REST API testing system #4403

Merged
merged 40 commits into from
Mar 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
8e660af
Models: correct parsing point values from JSON type string
Mar 1, 2022
7ed7080
Create test db with loaddata functionality
Mar 1, 2022
062159d
Update JSON assets
Mar 1, 2022
655f9de
Test Jobs: add order independence for shapes
Mar 2, 2022
de70fde
Conftest: close contections for cvat db before creating test db
Mar 2, 2022
7b08c12
Pretty JSON format for data.json
Mar 3, 2022
d2452a2
Delete sql dump file
Mar 3, 2022
4ab4ccc
Reorganize assets directory
Mar 3, 2022
1548df1
Update README
Mar 3, 2022
5e1e741
Remove 1 space
Mar 3, 2022
4b9541a
README: fix typo
Mar 3, 2022
22cf2cf
Conftest: add immediate exit on any error during db restoring
Mar 4, 2022
1759104
cvat_data: delete cache and logs
Mar 4, 2022
f72694f
JSON assets: reduce size of task1
Mar 5, 2022
19e7fc9
cvat_data: reduce size of task1
Mar 5, 2022
0422bf0
data.json: reduce size of task1
Mar 5, 2022
9057719
test tasks: update test
Mar 5, 2022
53f4b8c
tesk integrite: fix regexp
Mar 5, 2022
9e61bd6
JSON assets:reduce size of task3
Mar 5, 2022
0c869c2
cvat_data:reduce size of task3
Mar 5, 2022
c3d028f
data.json:reduce size of task3
Mar 5, 2022
7b2e2fc
JSON assets: reduce size of task4
Mar 5, 2022
d71c5af
cvat_data: reduce size of task4
Mar 5, 2022
254eaab
data.json: reduce size of task4
Mar 5, 2022
8431e21
Add cvat_data in unpacked form
Mar 8, 2022
f052732
Conftest: restore data volumes in unpacked form
Mar 8, 2022
7ca1c79
Restore by only one command
Mar 8, 2022
54378be
Delete cvat_data.tar.bz2
Mar 9, 2022
c48925f
Update README
Mar 9, 2022
0103b74
README: add troubleshooting section
Mar 9, 2022
0b6a464
Use SQL restore script instead of sh
Mar 10, 2022
9966e45
Return to zip format for cvat_data
Mar 14, 2022
cc95cdf
Update README
Mar 14, 2022
c7a38f3
Update README
Mar 14, 2022
e2ae5db
Merge develop into the sk/improve-testing-system
Mar 14, 2022
a906b75
Resolve conflicts
Mar 14, 2022
f930798
Fix conflicts
Mar 14, 2022
5626446
Fix README
Mar 15, 2022
8bdcf99
Update README
Mar 15, 2022
55cf081
Update README
Mar 15, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions cvat/apps/engine/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -532,6 +532,8 @@ class FloatArrayField(models.TextField):
def from_db_value(self, value, expression, connection):
if not value:
return value
if value.startswith('[') and value.endswith(']'):
value = value[1:-1]
return [float(v) for v in value.split(self.separator)]

def to_python(self, value):
Expand Down
102 changes: 92 additions & 10 deletions tests/rest_api/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ procedure to add them:
1. Backup DB and data volume using commands below
1. Don't forget to dump new objects into corresponding json files inside
assets directory
1. Commit cvat_data.tar.bz2 and cvat_db.sql into git. Be sure that they are
small enough: ~200K-400K together.
1. Commit cvat_data.tar.bz2 and data.json into git. Be sure that they are
small enough: ~300K together.

It is recommended to use dummy and tiny images. You can generate them using
Pillow library. See a sample code below:
Expand All @@ -63,10 +63,13 @@ for i, color in enumerate(colormap):
To backup DB and data volume, please use commands below.

```console
docker exec cvat_db pg_dump -c -Fp -U root -d cvat > assets/cvat_db/cvat_db.sql
docker run --rm --volumes-from cvat ubuntu tar -cjv /home/django/data > assets/cvat_data.tar.bz2
docker exec cvat python manage.py dumpdata --indent 2 > assets/cvat_db/data.json
docker exec cvat tar -cjv /home/django/data > assets/cvat_db/cvat_data.tar.bz2
```

> Note: if you won't be use --indent options or will be use with other value
> it potentially will lead to problems with merging of this file with other branch.

## How to update *.json files in the assets directory?

If you have updated the test database and want to update the assets/*.json
Expand All @@ -81,19 +84,38 @@ python utils/dump_objects.py
To restore DB and data volume, please use commands below.

```console
cat assets/cvat_db/cvat_db.sql | docker exec -i cvat_db psql -U root -d cvat
cat assets/cvat_data.tar.bz2 | docker run --rm -i --volumes-from cvat ubuntu tar -xj --strip 3 -C /home/django/data
cat assets/cvat_db/data.json | docker exec -i cvat python manage.py --format=json loaddata -
cat assets/cvat_db/cvat_data.tar.bz2 | docker exec -i cvat tar --strip 3 -C /home/django/data/ -xj
```

## Assets directory structure

Assets directory has two parts:

- `cvat_db` directory --- this directory contains all necessary files for
successful restoring of test db
- `cvat_data.tar.bz2` --- archieve with data volumes;
- `data.json` --- file required for DB restoring.
Contains all information about test db;
- `restore.sql` --- SQL script for creating copy of database and
killing connection for `cvat` database.
Script should be run with varialbe declaration:
```
# create database <new> with template <existing>
psql -U root -d postgres -v from=<existing> -v to=<new> restore.sql
```
- `*.json` files --- these file contains all necessary data for getting
expected results from HTTP responses

## FAQ

1. How to merge two DB dumps?

It can be critical if several developers add new tests in parallel. But if
you have json description of all objects together with cvat_db.sql, it will
be possible to recreate them manually.
In common case it should be easy just to merge two JSON files.
But in the case when a simple merge fails, you have to first merge
the branches, then re-create the changes that you made.

1. How to upgrade cvat_data.tar.bz2 and cvat_db.sql?
1. How to upgrade cvat_data.tar.bz2 and data.json?

After every commit which changes the layout of DB and data directory it is
possible to break these files. But failed tests should be a clear indicator
Expand All @@ -111,3 +133,63 @@ cat assets/cvat_data.tar.bz2 | docker run --rm -i --volumes-from cvat ubuntu tar
Since some tests change the database, these tests may be dependent on each
other, so in current implementation we avoid such problem by restoring
the database after each test function (see `conftest.py`)

1. Which user should be selected to create new resources in test DB?

If for your test it's no matter what user should send a request,
then better to choose `admin1` user for creating new resource.

## Troubleshooting

1. If your test session was exit with message:
```
_pytest.outcomes.Exit: Command failed: ... Add `-s` option to see more details.
```
Rerun tests to see error messages:
```
pytest ./tests/rest_api -s
```

1. If your tests was failed due to date field incompatibility and you have
error message like this:
```
assert {'values_chan...34.908528Z'}}} == {}
E Left contains 1 more item:
E {'values_changed': {"root['results'][0]['updated_date']": {'new_value': '2022-03-05T08:52:34.908000Z',
E 'old_value': '2022-03-05T08:52:34.908528Z'}}}
E Use -v to get the full diff
```
Just dump JSON assets with:
```
python3 tests/rest_api/utils/dump_objests.py
```

1. If your test infrastructure has been corrupted and you have errors during db restoring.
You should to create (or recreate) `cvat` database:
```
docker exec cvat_db dropdb --if-exists cvat
docker exec cvat_db createdb cvat
docker exec cvat python manage.py migrate
```

1. Perform migrate when some relation does not exists. Example of error message:
```
django.db.utils.ProgrammingError: Problem installing fixture '/data.json': Could not load admin.LogEntry(pk=1): relation "django_admin_log" does not exist`
```
Solution:
```
docker exec cvat python manage.py migrate
```

1. If for some reason you need to recreate cvat database, but using `dropdb`
you have error message:
```
ERROR: database "cvat" is being accessed by other users
DETAIL: There are 1 other session(s) using the database.
```
In this case you should terminate all existent connections for cvat database,
you can perform it with command:
```
docker exec cvat_db psql -U root -d postgres -v from=cvat -v to=test_db -f restore.sql
```

Loading