Files
mtusi2026lab4/student-tests/rolling_update_check.py
T

61 lines
2.7 KiB
Python

"""Usage (стенд запущен, обе реплики в ротации):
python student-tests/rolling_update_check.py --output evidence/rolling-update.json
Под непрерывной нагрузкой (4 потока, повтор с тем же Idempotency-Key) выполняет scripts/rolling_update.py
и проверяет: ошибки клиента, RPS, что все реплики заменены, что подтверждённые сообщения не потеряны и не задвоены.
"""
import argparse
import json
from pathlib import Path
import subprocess
import sys
import time
from helpers import Load, api_containers, new_chat, new_user
ROOT = Path(__file__).resolve().parents[1]
def main():
parser = argparse.ArgumentParser()
parser.add_argument('--output')
parser.add_argument('--no-build', action='store_true')
args = parser.parse_args()
before = api_containers()
alice = new_user('rolling')
chat = new_chat(alice)
load = Load(alice['token'], chat['id']).start()
time.sleep(5)
started = time.monotonic()
command = [sys.executable, str(ROOT / 'scripts' / 'rolling_update.py')] + (['--no-build'] if args.no_build else [])
update = subprocess.run(command, capture_output=True, text=True, encoding='utf-8')
finished = time.monotonic()
time.sleep(5)
load.stop()
print(update.stdout.strip())
after = api_containers()
result = {
'replicas_before': [c[:12] for c in before], 'replicas_after': [c[:12] for c in after],
'update_seconds': round(finished - started, 1), 'update_exit_code': update.returncode,
'before_5s': load.window(started - 5, started),
'during_update': load.window(started, finished),
'after_5s': load.window(finished, finished + 5),
'data': load.verify(alice['token']),
'log': update.stdout.strip().splitlines(),
}
replaced = not set(before) & set(after) and len(after) == len(before)
ok = update.returncode == 0 and replaced and not result['data']['lost'] and not result['data']['duplicate_texts']
print('rolling update %.1fs: errors=%d, rps %.1f -> %.1f -> %.1f, lost=%d, duplicates=%d, all replaced=%s'
% (result['update_seconds'], result['during_update']['errors'], result['before_5s']['rps'],
result['during_update']['rps'], result['after_5s']['rps'], result['data']['lost'],
result['data']['duplicate_texts'], replaced))
if args.output:
with open(args.output, 'w', encoding='utf-8') as file:
json.dump(result, file, ensure_ascii=False, indent=2)
print('PASS' if ok else 'FAIL: ' + update.stderr.strip()[-500:])
raise SystemExit(0 if ok else 1)
if __name__ == '__main__':
main()