Files
ServerKit/backend/tests/test_deployments_unified.py
T
Juan DenisandClaude Opus 4.8 afd042896b refactor(unify): §3 — canonical DeploymentJob links + unified /deployments surface
Additive foundation + one read namespace for the three parallel deploy records
(versioned build Deployment, webhook GitDeployment, DeploymentJob executions).

Backend:
- DeploymentJob gains deployment_id / git_deployment_id / webhook_id FKs +
  commit_hash / image_tag / container_id, so it can be the canonical execution
  record pointing at the release ledgers it produces (surfaced in to_dict).
  Migration 039 (idempotent, additive).
- New /api/v1/deployments surface:
    GET /apps/:id/history?source=build|webhook|all  → federated, source-tagged
    GET /:id                                         → build Deployment detail
  and the DeploymentJob surface re-mounted at /api/v1/deployments/jobs (alias of
  /deployment-jobs). Per-system blueprints (/builds, /deploy, /git) unchanged.

Deliberately NOT done (plan §3 Phase 1/2 deep merge, with rationale): folding
GitDeployment into Deployment+DeploymentJob and async-ifying the currently
synchronous build/webhook deploys. Recon confirmed those paths are synchronous
with distinct failure/timeout semantics and GitDeployment carries rollback state
(snapshot_data, rolled_back_to_id); flipping them to the queue can't be verified
without a live build/runner, so it's deferred behind the additive links above.

Verified: 6 new tests + previews/config-snapshot regression pass (35).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-24 14:52:04 -04:00

80 lines
3.3 KiB
Python

"""§3 unification — DeploymentJob links + the unified /api/v1/deployments surface.
DeploymentJob is the canonical execution record (now able to point at the
Deployment/GitDeployment release rows). The /api/v1/deployments namespace gives
one federated read over versioned build deploys and webhook git deploys, and
re-mounts the deployment-jobs surface under /deployments/jobs.
"""
import uuid
def test_deployment_job_link_columns_round_trip(app):
from app import db
from app.models.deployment_job import DeploymentJob
with app.app_context():
job = DeploymentJob(
id=str(uuid.uuid4()), kind='deploy.install', status='succeeded',
deployment_id=None, commit_hash='abc123', image_tag='img:1', container_id='c1',
)
db.session.add(job)
db.session.commit()
d = DeploymentJob.query.get(job.id).to_dict()
assert d['commit_hash'] == 'abc123'
assert d['image_tag'] == 'img:1'
assert d['container_id'] == 'c1'
assert 'deployment_id' in d and 'git_deployment_id' in d and 'webhook_id' in d
def _seed_app_with_deploys(app):
from app import db
from app.models import User
from app.models.application import Application
from app.models.deployment import Deployment
from app.models.webhook import GitDeployment
with app.app_context():
admin = User.query.filter_by(username='testadmin').first()
a = Application(name='dep-app', app_type='docker', status='running',
root_path='/srv/dep', user_id=admin.id if admin else 1)
db.session.add(a)
db.session.commit()
db.session.add(Deployment(app_id=a.id, version=1, status='live'))
db.session.add(GitDeployment(app_id=a.id, version=1, status='success', branch='main'))
db.session.commit()
return a.id
def test_unified_history_federates_sources(app, client, auth_headers):
app_id = _seed_app_with_deploys(app)
res = client.get(f'/api/v1/deployments/apps/{app_id}/history', headers=auth_headers)
assert res.status_code == 200
deployments = res.get_json()['deployments']
sources = {d['source'] for d in deployments}
assert sources == {'build', 'webhook'}
def test_unified_history_source_filter(app, client, auth_headers):
app_id = _seed_app_with_deploys(app)
res = client.get(f'/api/v1/deployments/apps/{app_id}/history?source=webhook', headers=auth_headers)
assert res.status_code == 200
deployments = res.get_json()['deployments']
assert deployments and all(d['source'] == 'webhook' for d in deployments)
def test_deployment_detail(app, client, auth_headers):
from app import db
from app.models.deployment import Deployment
app_id = _seed_app_with_deploys(app)
with app.app_context():
dep_id = Deployment.query.filter_by(app_id=app_id).first().id
res = client.get(f'/api/v1/deployments/{dep_id}', headers=auth_headers)
assert res.status_code == 200
assert res.get_json()['deployment']['id'] == dep_id
def test_jobs_alias_mounted_under_deployments(app, client, auth_headers):
canonical = client.get('/api/v1/deployment-jobs', headers=auth_headers)
alias = client.get('/api/v1/deployments/jobs', headers=auth_headers)
assert canonical.status_code == 200
assert alias.status_code == 200
assert alias.get_json() == canonical.get_json()