File size: 5,188 Bytes
2b86c60
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
"""Smoke-test a running HyperFlow deployment end to end and save the results.

Checks health and readiness, that every documented /api/v2 route refuses anonymous callers,
security headers, and one isolated reviewer journey: replay discovery, proposal, approval and
execution with an idempotency key. Uses only simulation mode; no provider traffic.
"""
import argparse
import json
import sys
import time
from datetime import datetime, timezone
from pathlib import Path

import httpx

PUBLIC = {('POST', '/api/v2/auth/register'), ('POST', '/api/v2/auth/login'), ('POST', '/api/v2/auth/reviewer'),
          ('GET', '/api/v2/provider/swiggy/callback')}
HEADERS = ('Content-Security-Policy', 'X-Frame-Options', 'Referrer-Policy', 'Permissions-Policy', 'X-Content-Type-Options')


def run(base):
    checks = []

    def check(name, ok, detail=''):
        checks.append({'check': name, 'passed': bool(ok), 'detail': detail})
        print(('PASS ' if ok else 'FAIL ') + name + (f' ({detail})' if detail else ''))

    with httpx.Client(base_url=base, timeout=30, follow_redirects=False) as c:
        health = c.get('/health')
        check('health', health.status_code == 200, str(health.status_code))
        ready = c.get('/ready')
        check('ready: database and recent worker heartbeat', ready.status_code == 200 and ready.json() == {'database': True, 'worker_recent': True}, ready.text[:120])

        missing = [h for h in HEADERS if h not in health.headers]
        check('security headers', not missing, 'missing ' + ', '.join(missing) if missing else '')
        if base.startswith('https://'):
            check('HSTS over HTTPS', 'max-age=' in health.headers.get('Strict-Transport-Security', ''))

        spec = c.get('/openapi.json').json()
        open_routes = []
        for path, methods in spec['paths'].items():
            if not path.startswith('/api/v2/'):
                continue
            for method in methods:
                if (method.upper(), path) in PUBLIC:
                    continue
                concrete = path.replace('{', '').replace('}', '')
                status = c.request(method.upper(), concrete, json={} if method != 'get' else None).status_code
                if status not in (401, 410):
                    open_routes.append(f'{method.upper()} {path} -> {status}')
        check('every protected /api/v2 route refuses anonymous callers', not open_routes, '; '.join(open_routes[:5]))

        token = c.post('/api/v2/auth/reviewer').json()['access_token']
        auth = {'Authorization': 'Bearer ' + token}
        check('reviewer workspace', c.get('/api/v2/auth/me', headers=auth).json().get('role') == 'reviewer')

        def read(tool, arguments=None):
            r = c.post('/api/v2/provider/read', headers=auth, json={'provider': 'food', 'tool': tool, 'mode': 'replay', 'arguments': arguments or {}})
            r.raise_for_status()
            return r.json()['data']

        address = read('get_addresses')['addresses'][0]['id']
        restaurant = read('search_restaurants', {'query': 'meal'})['restaurants'][0]['id']
        item = read('get_restaurant_menu', {'restaurantId': restaurant})['items'][0]
        arguments = {'restaurantId': restaurant, 'addressId': address,
                     'items': [{'itemId': item.get('itemId', item.get('menu_item_id')), 'quantity': 1}]}
        proposal = c.post('/api/v2/proposals', headers=auth, json={'provider': 'food', 'tool': 'update_food_cart', 'mode': 'replay',
                                                                     'scenario': 'success', 'arguments': arguments}).json()
        key = {'Idempotency-Key': 'smoke-' + str(time.time_ns())}
        unapproved = c.post(f"/api/v2/actions/{proposal['id']}/execute", headers={**auth, **key}).status_code
        check('a proposal cannot execute before approval', unapproved == 404, str(unapproved))
        approved = c.post(f"/api/v2/actions/{proposal['id']}/approve", headers=auth, json={'version': proposal['version']}).json()
        first = c.post(f"/api/v2/actions/{approved['id']}/execute", headers={**auth, **key}).json()
        again = c.post(f"/api/v2/actions/{approved['id']}/execute", headers={**auth, **key}).json()
        check('approved action executes once and replays by idempotency key',
              first['data']['state'] == 'SUCCEEDED' and again['data'] == first['data'], first['data']['state'])
        c.post('/api/v2/auth/logout', headers=auth)
        check('logout revokes the session', c.get('/api/v2/auth/me', headers=auth).status_code == 401)
    return checks


def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('--base', required=True)
    parser.add_argument('--output', default='docs/flagship/deploy-smoke-results.json')
    args = parser.parse_args()
    checks = run(args.base.rstrip('/'))
    report = {'base': args.base, 'checked_at': datetime.now(timezone.utc).isoformat(),
              'passed': sum(c['passed'] for c in checks), 'total': len(checks), 'checks': checks}
    Path(args.output).write_text(json.dumps(report, indent=2) + '\n')
    sys.exit(0 if report['passed'] == report['total'] else 1)


if __name__ == '__main__':
    main()