| 13 | |
| 14 | @pytest.fixture(autouse=True) |
| 15 | def canned_github_server(directory): |
| 16 | global NETWORK |
| 17 | NETWORK = os.environ.get('TEST_NETWORK') |
| 18 | if NETWORK is None: |
| 19 | NETWORK = 'regtest' |
| 20 | FILE_PATH = Path(os.path.dirname(os.path.realpath(__file__))) |
| 21 | if os.environ.get('LIGHTNING_CLI') is None: |
| 22 | os.environ['LIGHTNING_CLI'] = str(FILE_PATH.parent / 'cli/lightning-cli') |
| 23 | print('LIGHTNING_CALL: ', os.environ.get('LIGHTNING_CLI')) |
| 24 | # Use socket to provision a random free port |
| 25 | sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 26 | sock.bind(('localhost', 0)) |
| 27 | free_port = str(sock.getsockname()[1]) |
| 28 | sock.close() |
| 29 | global my_env |
| 30 | my_env = os.environ.copy() |
| 31 | # This tells reckless to redirect to the canned server rather than github. |
| 32 | my_env['REDIR_GITHUB_API'] = f'http://127.0.0.1:{free_port}/api' |
| 33 | my_env['REDIR_GITHUB'] = directory |
| 34 | my_env['FLASK_RUN_PORT'] = free_port |
| 35 | my_env['FLASK_APP'] = str(FILE_PATH / 'rkls_github_canned_server') |
| 36 | server = subprocess.Popen(["python3", "-m", "flask", "run"], |
| 37 | env=my_env) |
| 38 | |
| 39 | # Generate test plugin repository to test reckless against. |
| 40 | repo_dir = os.path.join(directory, "lightningd") |
| 41 | os.mkdir(repo_dir, 0o777) |
| 42 | plugins_path = str(FILE_PATH / 'data/recklessrepo/lightningd') |
| 43 | |
| 44 | # Create requirements.txt file for the testpluginpass |
| 45 | # with pyln-client installed from the local source |
| 46 | requirements_file_path = os.path.join(plugins_path, 'testplugpass', 'requirements.txt') |
| 47 | with open(requirements_file_path, 'w') as f: |
| 48 | pyln_client_path = os.path.abspath(os.path.join(FILE_PATH, '..', 'contrib', 'pyln-client')) |
| 49 | f.write(f"pyln-client @ file://{pyln_client_path}\n") |
| 50 | |
| 51 | # This lets us temporarily set .gitconfig user info in order to commit |
| 52 | my_env['HOME'] = directory |
| 53 | with open(os.path.join(directory, '.gitconfig'), 'w') as conf: |
| 54 | conf.write(("[user]\n" |
| 55 | "\temail = reckless@example.com\n" |
| 56 | "\tname = reckless CI\n" |
| 57 | "\t[init]\n" |
| 58 | "\tdefaultBranch = master")) |
| 59 | |
| 60 | with open(os.path.join(directory, '.gitconfig'), 'r') as conf: |
| 61 | print(conf.readlines()) |
| 62 | |
| 63 | # Bare repository must be initialized prior to setting other git env vars |
| 64 | subprocess.check_output(['git', 'init', '--bare', 'plugins'], cwd=repo_dir, |
| 65 | env=my_env) |
| 66 | |
| 67 | my_env['GIT_DIR'] = os.path.join(repo_dir, 'plugins') |
| 68 | my_env['GIT_WORK_TREE'] = repo_dir |
| 69 | my_env['GIT_INDEX_FILE'] = os.path.join(repo_dir, 'scratch-index') |
| 70 | repo_initialization = (f'cp -r {plugins_path}/* .;' |
| 71 | 'git add --all;' |
| 72 | 'git commit -m "initial commit - autogenerated by test_reckless.py";') |