|
3 | 3 | import inspect
|
4 | 4 | import os
|
5 | 5 | import subprocess
|
| 6 | +import time |
| 7 | +from shutil import rmtree |
| 8 | +from subprocess import DEVNULL, Popen, check_call, check_output |
| 9 | +from tempfile import mkdtemp |
| 10 | + |
| 11 | +import pytest |
6 | 12 |
|
7 | 13 |
|
8 | 14 | __all__ = [
|
9 | 15 | 'BaseTest',
|
10 | 16 | ]
|
11 | 17 |
|
| 18 | +__this_file = os.path.abspath(__file__) |
| 19 | +__this_dir = os.path.dirname(__this_file) |
| 20 | + |
12 | 21 |
|
13 | 22 | class BaseTest:
|
14 | 23 |
|
@@ -97,3 +106,66 @@ def run_test(self, input_path: str, output_path: str, *, inplace: bool):
|
97 | 106 | f.write(actual)
|
98 | 107 | else:
|
99 | 108 | assert actual == expected
|
| 109 | + |
| 110 | + |
| 111 | +@pytest.fixture |
| 112 | +def solana_test_validator(): |
| 113 | + |
| 114 | + ledger_dir = mkdtemp(prefix='stv_') |
| 115 | + cmd = [ |
| 116 | + 'solana-test-validator', |
| 117 | + '--rpc-port', '8899', |
| 118 | + '--ledger', ledger_dir, |
| 119 | + ] |
| 120 | + kwargs = { |
| 121 | + 'stdin': DEVNULL, |
| 122 | + 'stdout': DEVNULL, |
| 123 | + 'stderr': DEVNULL, |
| 124 | + } |
| 125 | + with Popen(cmd, **kwargs) as p: |
| 126 | + time.sleep(3) |
| 127 | + yield |
| 128 | + p.terminate() |
| 129 | + rmtree(ledger_dir) |
| 130 | + |
| 131 | + |
| 132 | +@pytest.fixture |
| 133 | +def solana_keygen(): |
| 134 | + |
| 135 | + cmd = ['solana-keygen', 'new', '--no-passphrase'] |
| 136 | + output = check_output(cmd) |
| 137 | + output = output.decode('ascii') |
| 138 | + output = output.splitlines() |
| 139 | + output = [line for line in output if 'pubkey' in line][0] |
| 140 | + output = output.split('pubkey: ')[1] |
| 141 | + return output |
| 142 | + |
| 143 | + |
| 144 | +@pytest.fixture |
| 145 | +def solana_airdrop(solana_test_validator, solana_keygen): |
| 146 | + |
| 147 | + cmd = [ |
| 148 | + 'solana', 'airdrop', '100', solana_keygen, |
| 149 | + '--commitment', 'finalized', |
| 150 | + '--url', 'localhost', |
| 151 | + ] |
| 152 | + check_call(cmd) |
| 153 | + |
| 154 | + |
| 155 | +@pytest.fixture |
| 156 | +def solana_program_deploy(solana_test_validator, solana_airdrop): |
| 157 | + |
| 158 | + cmd = [ |
| 159 | + 'solana', 'program', 'deploy', |
| 160 | + os.path.abspath( |
| 161 | + os.path.join(__this_dir, '..', '..', 'target', 'oracle.so') |
| 162 | + ), |
| 163 | + '--commitment', 'finalized', |
| 164 | + '--url', 'localhost', |
| 165 | + ] |
| 166 | + output = check_output(cmd) |
| 167 | + output = output.decode('ascii') |
| 168 | + output = output.splitlines() |
| 169 | + output = [line for line in output if 'Program Id' in line][0] |
| 170 | + output = output.split('Program Id: ')[1] |
| 171 | + return output |
0 commit comments