Files
canina/infra/hermes-runtime/test_serve.py

66 lines
3.5 KiB
Python

import importlib.util, os, secrets, tempfile, threading, socket, time
from pathlib import Path
from fastapi.testclient import TestClient
os.environ['HERMES_HOME']=tempfile.mkdtemp(prefix='inventory-snapshot-')
os.environ['HERMES_SERVE_HEADLESS']='1'
spec=importlib.util.spec_from_file_location('runtime_serve','/contract/serve.py')
module=importlib.util.module_from_spec(spec);spec.loader.exec_module(module)
values={'hermes_username':'contract','hermes_password':secrets.token_urlsafe(32),'hermes_signing_key':secrets.token_urlsafe(32)}
module.secret=lambda name:values[name]
# A capture timestamp is supplied by a reviewed exporter, never inferred at boot.
import json
manifest=Path(os.environ['HERMES_HOME'])/'snapshot-manifest.json'
for invalid in [None, {}, {'schemaVersion':1,'source':'snapshot','capturedAt':'not-a-date'}, {'schemaVersion':1,'source':'snapshot','capturedAt':'2999-01-01T00:00:00Z'}]:
if invalid is None:
manifest.unlink(missing_ok=True)
else:
manifest.write_text(json.dumps(invalid))
try:
module.build_app()
except (RuntimeError, ValueError):
pass
else:
raise AssertionError('invalid or missing manifest accepted')
manifest.write_text(json.dumps({'schemaVersion':1,'source':'snapshot','capturedAt':'2020-01-01T00:00:00Z'}))
app=module.build_app()
import uvicorn
server=uvicorn.Server(uvicorn.Config(app,host='0.0.0.0',port=9119,lifespan='off',access_log=False,log_level='warning'))
thread=threading.Thread(target=server.run,daemon=True);thread.start()
for _ in range(200):
if server.started:break
time.sleep(.05)
assert server.started
import httpx
with httpx.Client(base_url='http://127.0.0.1:9119') as c:
assert c.get('/api/profiles').status_code==401
assert c.post('/auth/password-login',json={'provider':'basic','username':values['hermes_username'],'password':'wrong'}).status_code==401
r=c.post('/auth/password-login',json={'provider':'basic','username':values['hermes_username'],'password':values['hermes_password']})
assert r.status_code==200 and r.json()['ok'] is True
assert c.get('/api/profiles').status_code==200
health=c.get('/api/health').json()
assert health['auth_required'] is True
assert health['provenance']['source']=='snapshot'
assert health['provenance']['capturedAt']=='2020-01-01T00:00:00Z'
assert health['provenance']['freshness']=='stale'
assert health['provenance']['liveProcess']=='unknown'
profiles=c.get('/api/profiles').json()['profiles']
assert profiles and all(p['liveGatewayState']=='unknown' and p['gateway_running'] is False and p['provenance']['source']=='snapshot' for p in profiles)
status=c.get('/api/status',params={'profile':'default'}).json()
assert status['overall']=='unknown' and status['gateway_state'] is None
assert status['gateway_running'] is False and status['active_agents']==0
assert status['liveActiveAgents'] is None and status['liveGatewayState']=='unknown'
for path in ['/api/model/info','/api/analytics/usage','/api/sessions','/api/cron/jobs']:
response=c.get(path,params={'profile':'default'})
if response.status_code==200:
data=response.json()
if isinstance(data,list):
assert all(r['provenance']['source']=='snapshot' for r in data)
else:
assert data['provenance']['source']=='snapshot'
assert c.post('/api/cron/jobs/example/resume').status_code==404
assert c.get('/api/config').status_code==404
assert c.get('/api/ws').status_code==404
print('PASS real TCP pinned serve: unauth401 wrong-password401 login200 inventory200 health.auth_required=true mutations/config/ws404')
server.should_exit=True;thread.join(10)
assert not thread.is_alive()