diff --git a/app/inventory/views.py b/app/inventory/views.py index 5e9b998fe616965775d0a4d5bdd162fbfeaae71e..cd630f0969ff59abbfdbd6f73c49661043e20ec7 100644 --- a/app/inventory/views.py +++ b/app/inventory/views.py @@ -46,7 +46,7 @@ def list_items(): @login_required def _generate_excel_file(): task = current_user.launch_task( - "generate_items_excel_file", func="generate_items_excel_file", timeout=180 + "generate_items_excel_file", func="generate_items_excel_file", job_timeout=180 ) db.session.commit() return utils.redirect_to_job_status(task.id) diff --git a/tests/functional/conftest.py b/tests/functional/conftest.py index 7d89ab6be6f49e507a92449858b750c36dce1526..edfb19113679b2e82d186fe656c9026f8ccdec15 100644 --- a/tests/functional/conftest.py +++ b/tests/functional/conftest.py @@ -9,10 +9,12 @@ Pytest fixtures common to all functional tests. :license: BSD 2-Clause, see LICENSE for more details. """ +import redis import pytest import sqlalchemy as sa from pytest_factoryboy import register from flask_ldap3_login import AuthenticationResponse, AuthenticationResponseStatus +from rq import push_connection, pop_connection from app.factory import create_app from app.extensions import db as _db from app.models import SearchableMixin, Host, Item, AnsibleGroup @@ -45,6 +47,7 @@ def app(request): "TESTING": True, "WTF_CSRF_ENABLED": False, "SQLALCHEMY_DATABASE_URI": "postgresql://ics:icspwd@postgres/csentry_db_test", + "RQ_REDIS_URL": "redis://redis:6379/4", "ELASTICSEARCH_INDEX_SUFFIX": "-test", "ELASTICSEARCH_REFRESH": "true", "CACHE_TYPE": "null", @@ -135,8 +138,17 @@ def session(db, request): Host.create_index() AnsibleGroup.create_index() + # Setup RQ redis connection + redis_connection = redis.from_url(db.app.config["RQ_REDIS_URL"]) + redis_connection.flushdb() + push_connection(redis_connection) + yield session + # Clean RQ redis connnection + redis_connection.flushdb() + pop_connection() + # ELASTICSEARCH_INDEX_SUFFIX is set to "-test" # Delete all "*-test" indices after each test db.app.elasticsearch.indices.delete("*-test", ignore=404) diff --git a/tests/functional/test_tasks.py b/tests/functional/test_tasks.py new file mode 100644 index 0000000000000000000000000000000000000000..06c728088ae28092fa8a5dd2b4e61ad03e7248f5 --- /dev/null +++ b/tests/functional/test_tasks.py @@ -0,0 +1,24 @@ +import pytest + + +@pytest.mark.parametrize( + "name, func, input_kwargs, output_args", + [ + ("my task1", "my_func1", {}, ""), + ( + "my task2", + "my_func2", + {"arg1": "foo", "arg2": True}, + "arg1='foo', arg2=True", + ), + # job_timeout is used by enqueue for the job + ("another task", "func_to_run", {"job_timeout": 180}, ""), + # timeout is NOT used by enqueue for the job (deprecated in RQ >= 1.0) + # it's passed to the function + ("task4", "my_func4", {"timeout": 60}, "timeout=60"), + ], +) +def test_launch_task_kwargs(user, name, func, input_kwargs, output_args): + task = user.launch_task(name, func=func, **input_kwargs) + assert task.name == name + assert task.command == f"app.tasks.{func}({output_args})" diff --git a/tests/functional/test_web.py b/tests/functional/test_web.py index 7e4da7bc14a1c516e5f60e2c977fb3eb5e64314e..d7d46c59b22afb1338f8c96937dfa3656ee5cd85 100644 --- a/tests/functional/test_web.py +++ b/tests/functional/test_web.py @@ -1056,3 +1056,14 @@ def test_retrieve_groups(logged_client, ansible_group_factory): response = logged_client.post("/network/_retrieve_groups") groups = response.get_json()["data"] assert {group1.name, group2.name} == set(group["name"] for group in groups) + + +def test_generate_excel_file(logged_client): + response = logged_client.get("/inventory/items/_generate_excel_file") + assert response.status_code == 202 + assert "/status/" in response.headers["Location"] + job_id = response.headers["Location"].split("/")[-1] + task = models.Task.query.get(job_id) + assert task is not None + assert task.name == "generate_items_excel_file" + assert task.command == "app.tasks.generate_items_excel_file()"