forked from mtusi2026/mtusi2026lab6
reyalysed grade 3
This commit is contained in:
@@ -71,3 +71,145 @@ class ComposeProject:
|
||||
)
|
||||
for row in map(json.loads, rows)
|
||||
]
|
||||
|
||||
|
||||
def cleanup_docker(*args, allow_failure=False):
|
||||
result = subprocess.run(['docker',*args],cwd=ROOT,text=True,capture_output=True,timeout=120)
|
||||
if result.returncode and not allow_failure:
|
||||
raise RuntimeError('Docker cleanup failed: ' + ' '.join(args[:2]))
|
||||
return result.stdout.strip()
|
||||
|
||||
|
||||
def inventory(projects):
|
||||
resources = {'containers':[], 'networks':[], 'volumes':[]}
|
||||
for project in projects:
|
||||
label = 'label=com.docker.compose.project=' + project
|
||||
resources['containers'].extend(cleanup_docker('ps','-aq','--filter',label).splitlines())
|
||||
resources['networks'].extend(cleanup_docker('network','ls','-q','--filter',label).splitlines())
|
||||
resources['volumes'].extend(cleanup_docker('volume','ls','-q','--filter',label).splitlines())
|
||||
return resources
|
||||
|
||||
|
||||
|
||||
def clean_stands(*, apply=False):
|
||||
config = json.loads(cleanup_docker('compose','--env-file','.env','config','--format','json'))
|
||||
projects = sorted({config['name'], os.getenv('MAIN_PROJECT', 'messenger'), os.getenv('TEST_PROJECT', 'messenger-test')})
|
||||
resources = inventory(projects)
|
||||
names = {service['image'] for service in config['services'].values() if 'build' in service}
|
||||
image_ids = set()
|
||||
for name in names:
|
||||
image_ids.update(cleanup_docker('image','ls','-q',name).splitlines())
|
||||
# У старых сборок API может уже не быть тега. Сверяем также команду и WORKDIR,
|
||||
# чтобы не затронуть сторонние образы с обычным OCI revision label.
|
||||
candidates = cleanup_docker('image','ls','-q','--filter','label=org.opencontainers.image.revision').splitlines()
|
||||
for candidate in candidates:
|
||||
image = json.loads(cleanup_docker('image','inspect',candidate))[0]
|
||||
settings = image['Config']
|
||||
if settings.get('Cmd') == ['python','-m','app'] and settings.get('WorkingDir') == '/app':
|
||||
image_ids.add(candidate)
|
||||
plan = {'projects':projects, 'resources':resources, 'application_image_ids':sorted(image_ids), 'local_settings_preserved':True}
|
||||
if not apply:
|
||||
return plan
|
||||
if resources['containers']:
|
||||
cleanup_docker('rm','-f','-v',*resources['containers'])
|
||||
if resources['networks']:
|
||||
cleanup_docker('network','rm',*resources['networks'])
|
||||
if resources['volumes']:
|
||||
cleanup_docker('volume','rm',*resources['volumes'])
|
||||
if image_ids:
|
||||
cleanup_docker('image','rm',*sorted(image_ids))
|
||||
remaining = inventory(projects)
|
||||
if any(remaining.values()):
|
||||
raise RuntimeError('Cleanup incomplete')
|
||||
plan['remaining'] = remaining
|
||||
plan['application_images_remaining'] = [name for name in sorted(names) if cleanup_docker('image','ls','-q',name)]
|
||||
if plan['application_images_remaining']:
|
||||
raise RuntimeError('Application images remain')
|
||||
plan['result'] = 'PASS'
|
||||
return plan
|
||||
|
||||
|
||||
def verify():
|
||||
"""Один последовательный прогон на одном стенде с обязательной очисткой."""
|
||||
from datetime import datetime, timezone
|
||||
import re
|
||||
import sys
|
||||
import time
|
||||
|
||||
project_name = os.getenv('TEST_PROJECT', 'messenger-test')
|
||||
os.environ.update(COMPOSE_PROJECT_NAME=project_name,
|
||||
COMPOSE_FILE='compose.yaml:compose.test.yaml',
|
||||
GRAFANA_HOST_PORT='23000', LB_HOST_PORT='28080',
|
||||
DOCKER_TESTS='1')
|
||||
report = {'date_utc': datetime.now(timezone.utc).isoformat(), 'checks': [],
|
||||
'profile': {'single_stack': True, 'load_duration_seconds': 10,
|
||||
'load_requests_per_second': 10, 'load_concurrency': 2}}
|
||||
destination = ROOT / 'evidence/verification.json'
|
||||
destination.parent.mkdir(exist_ok=True)
|
||||
|
||||
def run(name, command, timeout=600):
|
||||
print('RUN: ' + name, flush=True)
|
||||
started = time.monotonic()
|
||||
result = subprocess.run(command, cwd=ROOT, text=True, capture_output=True, timeout=timeout)
|
||||
executable = Path(command[0])
|
||||
display_command = [str(executable.relative_to(ROOT)) if executable.is_relative_to(ROOT)
|
||||
else command[0], *command[1:]]
|
||||
entry = {'name': name, 'command': display_command, 'exit_code': result.returncode,
|
||||
'duration_seconds': round(time.monotonic()-started, 3),
|
||||
'result': 'PASS' if result.returncode == 0 else 'FAIL'}
|
||||
if name == 'regression':
|
||||
summary = re.search(r'\d+ passed[^\n]*', result.stdout)
|
||||
if summary:
|
||||
entry['summary'] = summary.group(0)
|
||||
if result.returncode:
|
||||
# Полные диагностические логи локальны; в evidence нет путей/credentials.
|
||||
(ROOT / '.verification-error.log').write_text(result.stdout + result.stderr)
|
||||
entry['diagnostics'] = '.verification-error.log (local, not committed)'
|
||||
report['checks'].append(entry)
|
||||
print(entry['result'] + ': ' + name, flush=True)
|
||||
if result.returncode:
|
||||
raise RuntimeError(name + ' failed; inspect local diagnostic log')
|
||||
|
||||
try:
|
||||
run('contract', [sys.executable, 'scripts/check_contract.py'])
|
||||
run('openapi', [sys.executable, 'scripts/validate_openapi.py'])
|
||||
run('compose', ['docker', 'compose', '--env-file', '.env', 'config', '--quiet'])
|
||||
run('startup', ['make', '--no-print-directory', 'test-stand-up'], timeout=600)
|
||||
run('regression', ['make', '--no-print-directory', 'test-unit'])
|
||||
run('api', [sys.executable, 'tests/smoke.py', '--base-url', 'http://localhost:28080'])
|
||||
run('observability', [sys.executable, '-m', 'scripts.check_observability',
|
||||
'--base-url', 'http://localhost:28080',
|
||||
'--grafana-url', 'http://localhost:23000',
|
||||
'--incidents', '--restart-telemetry'])
|
||||
run('persistence', [sys.executable, 'tests/resilience.py', '--mode', 'persistence',
|
||||
'--action-script', 'scripts/restart.sh', '--base-url', 'http://localhost:28080'])
|
||||
run('failover', [sys.executable, 'tests/resilience.py', '--mode', 'failover',
|
||||
'--action-script', 'scripts/stop-one.sh', '--base-url', 'http://localhost:28080'])
|
||||
report['result'] = 'PASS'
|
||||
except BaseException:
|
||||
report['result'] = 'FAIL'
|
||||
raise
|
||||
finally:
|
||||
try:
|
||||
print('RUN: cleanup', flush=True)
|
||||
report['cleanup'] = clean_stands(apply=True)
|
||||
print('PASS: cleanup', flush=True)
|
||||
finally:
|
||||
report['finished_at_utc'] = datetime.now(timezone.utc).isoformat()
|
||||
destination.write_text(json.dumps(report, ensure_ascii=False, indent=2)+'\n')
|
||||
|
||||
|
||||
def main():
|
||||
import argparse
|
||||
parser = argparse.ArgumentParser(description='Проверка и очистка Compose-стендов')
|
||||
parser.add_argument('command', choices=['verify', 'clean'])
|
||||
parser.add_argument('--apply', action='store_true', help='Подтвердить удаление ресурсов')
|
||||
args = parser.parse_args()
|
||||
if args.command == 'verify':
|
||||
verify()
|
||||
else:
|
||||
print(json.dumps(clean_stands(apply=args.apply), indent=2))
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
|
||||
Reference in New Issue
Block a user