Fix and test environment dispatch tasks
This commit is contained in:
parent
6b7db2ca46
commit
1a9c34d856
@ -1,5 +1,7 @@
|
||||
from sqlalchemy import text
|
||||
from sqlalchemy.orm.exc import NoResultFound
|
||||
from sqlalchemy.orm import load_only
|
||||
from typing import List
|
||||
|
||||
from atst.database import db
|
||||
from atst.models import Environment, Application, Portfolio, TaskOrder, CLIN
|
||||
@ -107,35 +109,44 @@ class Environments(object):
|
||||
@classmethod
|
||||
def base_provision_query(cls, now):
|
||||
return (
|
||||
db.session.query(Environment.id)
|
||||
db.session.query(Environment)
|
||||
.join(Application)
|
||||
.join(Portfolio)
|
||||
.join(TaskOrder)
|
||||
.join(CLIN)
|
||||
.filter(CLIN.start_date <= now)
|
||||
.filter(CLIN.end_date > now)
|
||||
# select only these columns
|
||||
.options(load_only("id", "creator_id"))
|
||||
)
|
||||
|
||||
@classmethod
|
||||
def get_environments_pending_creation(cls, now) -> [str]:
|
||||
query = cls.base_provision_query(now).filter(Environment.cloud_id == None)
|
||||
return [environment_id for (environment_id,) in query.all()]
|
||||
def get_environments_pending_creation(cls, now) -> List[Environment]:
|
||||
"""
|
||||
Any environment with an active CLIN that doesn't yet have a `cloud_id`.
|
||||
"""
|
||||
return cls.base_provision_query(now).filter(Environment.cloud_id == None).all()
|
||||
|
||||
@classmethod
|
||||
def get_environments_pending_atat_user_creation(cls, now) -> [str]:
|
||||
query = (
|
||||
def get_environments_pending_atat_user_creation(cls, now) -> List[Environment]:
|
||||
"""
|
||||
Any environment with an active CLIN that has a cloud_id but no `root_user_info`.
|
||||
"""
|
||||
return (
|
||||
cls.base_provision_query(now)
|
||||
.filter(Environment.cloud_id != None)
|
||||
.filter(Environment.root_user_info == text("'null'"))
|
||||
)
|
||||
return [environment_id for (environment_id,) in query.all()]
|
||||
).all()
|
||||
|
||||
@classmethod
|
||||
def get_environments_pending_baseline_creation(cls, now) -> [str]:
|
||||
query = (
|
||||
def get_environments_pending_baseline_creation(cls, now) -> List[Environment]:
|
||||
"""
|
||||
Any environment with an active CLIN that has a `cloud_id` and `root_user_info`
|
||||
but no `baseline_info`.
|
||||
"""
|
||||
return (
|
||||
cls.base_provision_query(now)
|
||||
.filter(Environment.cloud_id != None)
|
||||
.filter(Environment.root_user_info != text("'null'"))
|
||||
.filter(Environment.baseline_info == text("'null'"))
|
||||
)
|
||||
return [environment_id for (environment_id,) in query.all()]
|
||||
).all()
|
||||
|
21
atst/jobs.py
21
atst/jobs.py
@ -1,4 +1,5 @@
|
||||
from flask import current_app as app
|
||||
import pendulum
|
||||
|
||||
from atst.database import db
|
||||
from atst.queue import celery
|
||||
@ -121,15 +122,23 @@ def create_environment_baseline(self, environment_id=None):
|
||||
|
||||
@celery.task(bind=True)
|
||||
def dispatch_create_environment(self):
|
||||
for environment_id in Environments.get_environments_pending_creation(
|
||||
pendulum.now()
|
||||
):
|
||||
create_environment.delay(environment_id=environment_id, atat_user_id="TODO")
|
||||
for environment in Environments.get_environments_pending_creation(pendulum.now()):
|
||||
create_environment.delay(
|
||||
environment_id=environment.id, atat_user_id=environment.creator_id
|
||||
)
|
||||
|
||||
|
||||
@celery.task(bind=True)
|
||||
def dispatch_create_atat_admin_user(self):
|
||||
for environment_id in Environments.get_environments_pending_atat_user_creation(
|
||||
for environment in Environments.get_environments_pending_atat_user_creation(
|
||||
pendulum.now()
|
||||
):
|
||||
create_atat_admin_user.delay(environment_id=environment_id, atat_user_id="TODO")
|
||||
create_atat_admin_user.delay(environment_id=environment.id)
|
||||
|
||||
|
||||
@celery.task(bind=True)
|
||||
def dispatch_create_environment_baseline(self):
|
||||
for environment in Environments.get_environments_pending_baseline_creation(
|
||||
pendulum.now()
|
||||
):
|
||||
create_environment_baseline.delay(environment_id=environment.id)
|
||||
|
@ -329,7 +329,7 @@ def test_task_orders_submit_task_order(client, user_session, task_order):
|
||||
({"_pdf": None, "number": "", "clins": []}, "step_1"),
|
||||
({"number": "", "clins": []}, "step_2"),
|
||||
({"number": "1234567890123", "clins": []}, "step_3"),
|
||||
({"number": "1234567890123", "create_clins": [1]}, "step_4"),
|
||||
({"number": "1234567890123", "create_clins": [{"number": 1}]}, "step_4"),
|
||||
],
|
||||
)
|
||||
def test_task_orders_edit_redirects_to_latest_incomplete_step(
|
||||
|
@ -541,7 +541,7 @@ def test_task_orders_new_get_routes(get_url_assert_status):
|
||||
task_order = TaskOrderFactory.create(
|
||||
creator=owner,
|
||||
portfolio=portfolio,
|
||||
create_clins=["1234567890123456789012345678901234567890123"],
|
||||
create_clins=[{"number": "1234567890123456789012345678901234567890123"}],
|
||||
)
|
||||
|
||||
for route in get_routes:
|
||||
|
@ -11,8 +11,16 @@ from atst.jobs import (
|
||||
do_create_environment,
|
||||
do_create_atat_admin_user,
|
||||
do_create_environment_baseline,
|
||||
dispatch_create_environment,
|
||||
dispatch_create_atat_admin_user,
|
||||
dispatch_create_environment_baseline,
|
||||
)
|
||||
from tests.factories import (
|
||||
EnvironmentFactory,
|
||||
EnvironmentRoleFactory,
|
||||
UserFactory,
|
||||
PortfolioFactory,
|
||||
)
|
||||
from tests.factories import EnvironmentFactory, EnvironmentRoleFactory, UserFactory
|
||||
|
||||
|
||||
def test_environment_job_failure(celery_app, celery_worker):
|
||||
@ -105,3 +113,86 @@ def test_create_environment_baseline(csp, session):
|
||||
updated_environment = session.query(Environment).get(environment_id)
|
||||
|
||||
assert updated_environment.baseline_info
|
||||
|
||||
|
||||
def test_dispatch_create_environment(session, monkeypatch):
|
||||
portfolio = PortfolioFactory.create(
|
||||
applications=[{"environments": [{}]}],
|
||||
task_orders=[
|
||||
{
|
||||
"create_clins": [
|
||||
{
|
||||
"start_date": pendulum.now().subtract(days=1),
|
||||
"end_date": pendulum.now().add(days=1),
|
||||
}
|
||||
]
|
||||
}
|
||||
],
|
||||
)
|
||||
mock = Mock()
|
||||
monkeypatch.setattr("atst.jobs.create_environment", mock)
|
||||
environment = portfolio.applications[0].environments[0]
|
||||
|
||||
dispatch_create_environment.run()
|
||||
|
||||
mock.delay.assert_called_once_with(
|
||||
environment_id=environment.id, atat_user_id=environment.creator_id
|
||||
)
|
||||
|
||||
|
||||
def test_dispatch_create_atat_admin_user(session, monkeypatch):
|
||||
portfolio = PortfolioFactory.create(
|
||||
applications=[
|
||||
{"environments": [{"cloud_id": uuid4().hex, "root_user_info": None}]}
|
||||
],
|
||||
task_orders=[
|
||||
{
|
||||
"create_clins": [
|
||||
{
|
||||
"start_date": pendulum.now().subtract(days=1),
|
||||
"end_date": pendulum.now().add(days=1),
|
||||
}
|
||||
]
|
||||
}
|
||||
],
|
||||
)
|
||||
mock = Mock()
|
||||
monkeypatch.setattr("atst.jobs.create_atat_admin_user", mock)
|
||||
environment = portfolio.applications[0].environments[0]
|
||||
|
||||
dispatch_create_atat_admin_user.run()
|
||||
|
||||
mock.delay.assert_called_once_with(environment_id=environment.id)
|
||||
|
||||
|
||||
def test_dispatch_create_environment_baseline(session, monkeypatch):
|
||||
portfolio = PortfolioFactory.create(
|
||||
applications=[
|
||||
{
|
||||
"environments": [
|
||||
{
|
||||
"cloud_id": uuid4().hex,
|
||||
"root_user_info": {},
|
||||
"baseline_info": None,
|
||||
}
|
||||
]
|
||||
}
|
||||
],
|
||||
task_orders=[
|
||||
{
|
||||
"create_clins": [
|
||||
{
|
||||
"start_date": pendulum.now().subtract(days=1),
|
||||
"end_date": pendulum.now().add(days=1),
|
||||
}
|
||||
]
|
||||
}
|
||||
],
|
||||
)
|
||||
mock = Mock()
|
||||
monkeypatch.setattr("atst.jobs.create_environment_baseline", mock)
|
||||
environment = portfolio.applications[0].environments[0]
|
||||
|
||||
dispatch_create_environment_baseline.run()
|
||||
|
||||
mock.delay.assert_called_once_with(environment_id=environment.id)
|
||||
|
Loading…
x
Reference in New Issue
Block a user