"""Exercises the real service binary and CLI using a simulated Gradle Wrapper. This validates transport/lifecycle; it does not validate an Android toolchain. Run: python -m unittest discover -s client -p test_smoke.py -v """ import json import os from pathlib import Path import socket import subprocess import sys import tempfile import time import unittest import urllib.request ROOT = Path(__file__).resolve().parents[1] class SmokeTest(unittest.TestCase): def test_sync_build_download_and_second_revision(self): self.exercise(False) def test_https_sync_build_download_and_second_revision(self): self.exercise(True) def exercise(self, secure): binary = ROOT / 'dist' / ('remotecompile-windows-amd64.exe' if os.name == 'nt' else 'remotecompile') if not binary.exists(): self.skipTest(f'Build service binary first: {binary}') with tempfile.TemporaryDirectory(dir=ROOT) as temp: root = Path(temp) source = root / '中文 project' source.mkdir() (source / 'input.txt').write_bytes(b'first\n') (source / 'deleted.txt').write_text('remove') if os.name == 'nt': (source / 'gradlew.bat').write_text('@echo off\nmkdir app\\build\\outputs\\apk\\debug 2>nul\ncopy /y input.txt app\\build\\outputs\\apk\\debug\\app-debug.apk >nul\nexit /b 0\n') else: (source / 'gradlew').write_text('#!/bin/sh\nmkdir -p app/build/outputs/apk/debug\ncp input.txt app/build/outputs/apk/debug/app-debug.apk\n') with socket.socket() as sock: sock.bind(('127.0.0.1', 0)) port = sock.getsockname()[1] endpoint = f'{"https" if secure else "http"}://127.0.0.1:{port}' env = dict(os.environ, REMOTE_BUILD_TOKEN='test-only-token-abcdefghijklmnopqrstuvwxyz') workspaces = root / 'workspaces' workspace = workspaces / 'existing 工程' workspace.mkdir(parents=True) (source / '.gitignore').write_text('ignored/\n**/.cxx/\n**/build/\n.remotecompile/\n') (source / 'ignored').mkdir() (source / 'ignored/private.txt').write_text('must not upload') (source / 'app/.cxx/Debug').mkdir(parents=True) (source / 'app/.cxx/Debug/.ninja_deps').write_bytes(b'local native cache') subprocess.run(['git', '-C', str(source), 'init', '-q'], check=True, capture_output=True) subprocess.run(['git', '-C', str(source), '-c', 'core.autocrlf=false', 'add', '.'], check=True, capture_output=True) subprocess.run(['git', '-C', str(source), '-c', 'user.name=Test', '-c', 'user.email=test@example.invalid', 'commit', '-qm', 'base'], check=True, capture_output=True) subprocess.run(['git', 'clone', '--no-hardlinks', str(source), str(workspace)], check=True, capture_output=True) command = [str(binary), '-workspace-root', str(workspaces), '-listen', f'127.0.0.1:{port}', '-data', str(root / 'data')] context = None if secure: import ssl subprocess.run([str(binary), '-init-tls-host', '127.0.0.1', '-tls-dir', str(root/'tls')], check=True, capture_output=True) command += ['-tls-cert', str(root/'tls/server.crt'), '-tls-key', str(root/'tls/server.key')] context = ssl.create_default_context(cafile=str(root/'tls/server.crt')) subprocess.run(['git', '-C', str(workspace), 'config', 'core.autocrlf', 'true'], check=True, capture_output=True) (workspace / 'input.txt').write_bytes(b'first\r\n') process = subprocess.Popen(command, env=env, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) try: for _ in range(100): try: req = urllib.request.Request(endpoint + '/api/v1/health', headers={'Authorization': 'Bearer ' + env['REMOTE_BUILD_TOKEN']}) with urllib.request.urlopen(req, timeout=1, context=context): break except OSError: time.sleep(.05) else: self.fail('Service did not start') command = [sys.executable, str(ROOT / 'client' / 'remote_run.py'), str(source), '--url', endpoint, '--remote-folder', str(workspace)] req = urllib.request.Request(endpoint + '/api/v1/jdks', headers={'Authorization': 'Bearer ' + env['REMOTE_BUILD_TOKEN']}) with urllib.request.urlopen(req, context=context, timeout=20) as response: jdks = json.load(response)['jdks'] chosen_jdk = next((j for j in jdks if j['version'].startswith('21')), jdks[0] if jdks else None) if chosen_jdk: command += ['--java-home', chosen_jdk['home']] if secure: command += ['--server-cert', str(root/'tls/server.crt')] first = subprocess.run(command, env=env, capture_output=True, text=True, timeout=120) self.assertEqual(first.returncode, 0, first.stderr) self.assertEqual(next((source / '.remotecompile').glob('*.apk')).read_bytes(), b'first\r\n') self.assertEqual((workspace / 'input.txt').read_bytes(), b'first\r\n') self.assertRegex(first.stdout, r'skipped [1-9][0-9]* EOL-only files') (source / 'input.txt').write_bytes(b'second\n') (source / 'deleted.txt').unlink() second = subprocess.run(command, env=env, capture_output=True, text=True, timeout=120) self.assertEqual(second.returncode, 0, second.stderr) self.assertIn('Syncing 1 changed files', second.stdout) artifacts = [p.read_bytes() for p in (source / '.remotecompile').glob('*.apk')] self.assertCountEqual(artifacts, [b'first\r\n', b'second\n']) state = json.loads((source / '.remotecompile' / 'client.json').read_text()) self.assertFalse((workspace / 'deleted.txt').exists()) self.assertFalse((workspace / 'ignored/private.txt').exists()) self.assertFalse((workspace / 'app/.cxx/Debug/.ninja_deps').exists()) self.assertTrue((workspace / '.git').exists()) if chosen_jdk: req = urllib.request.Request(endpoint + '/api/v1/projects/' + state['project'] + '/builds', headers={'Authorization': 'Bearer ' + env['REMOTE_BUILD_TOKEN']}) with urllib.request.urlopen(req, context=context, timeout=20) as response: build = json.load(response)['builds'][0] self.assertEqual(build['javaHome'], chosen_jdk['home']) self.assertEqual(build['environment']['javaHome'], chosen_jdk['home']) finally: process.terminate() process.wait(timeout=15) if __name__ == '__main__': unittest.main()