Skip to content

Commit

Permalink
fix(forknet): Load env vars for every neard command that we run in ne…
Browse files Browse the repository at this point in the history
…ard_runner.py (#12740)
  • Loading branch information
VanBarbascu authored Jan 15, 2025
1 parent abe4a60 commit 1bde566
Showing 1 changed file with 29 additions and 10 deletions.
39 changes: 29 additions & 10 deletions pytest/tests/mocknet/helpers/neard_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ def neard_init(self, rpc_port, protocol_port, validator_id):
logging.warning(
f'ignoring validator ID "{validator_id}" for traffic generator node'
)
subprocess.check_call(cmd)
self.execute_neard_subcmd(cmd)

with open(self.tmp_near_home_path('config.json'), 'r') as f:
config = json.load(f)
Expand Down Expand Up @@ -403,7 +403,7 @@ def reset_starting_data_dir(self):
'run-migrations',
]
logging.info(f'running {" ".join(cmd)}')
subprocess.check_call(cmd)
self.execute_neard_subcmd(cmd)
cmd = [
self.data['binaries'][0]['system_path'],
'--home',
Expand All @@ -414,7 +414,7 @@ def reset_starting_data_dir(self):
self.target_near_home_path(),
]
logging.info(f'running {" ".join(cmd)}')
subprocess.check_call(cmd)
self.execute_neard_subcmd(cmd)

def move_init_files(self):
try:
Expand Down Expand Up @@ -794,15 +794,34 @@ def wanted_neard_path(self):
requests.exceptions.ReadTimeout, KeyError):
return self.data['current_neard_path']

def run_neard(self, cmd, out_file=None):
assert (self.neard is None)
assert (self.data['neard_process'] is None)
"""
Load the env variables from files. The order of override is:
1. Environment variables of the process
2. ~/.secrets
3. $NEARD_RUNNER_HOME/.env
"""

def get_env(self):
home_path = os.path.expanduser('~')
env = {
return {
**os.environ, # override loaded values with environment variables
**dotenv.dotenv_values(os.path.join(home_path, '.secrets')), # load sensitive variables
**dotenv.dotenv_values(self.home_path('.env')), # load neard variables
}

"""
Used to execute neard cli commands and wait for them to finish.
Do not use this for commands that are expected to run indefinitely.
"""

def execute_neard_subcmd(self, cmd):
env = self.get_env()
return subprocess.check_call(cmd, env=env)

def run_neard(self, cmd, out_file=None):
assert (self.neard is None)
assert (self.data['neard_process'] is None)
env = self.get_env()
logging.info(f'running {" ".join(cmd)}')
self.neard = subprocess.Popen(
cmd,
Expand Down Expand Up @@ -1136,7 +1155,7 @@ def check_set_validators(self):
'finalize',
]
logging.info(f'running {" ".join(cmd)}')
subprocess.check_call(cmd)
self.execute_neard_subcmd(cmd)
logging.info(
f'neard fork-network finalize succeeded. Node is ready')
self.make_initial_backup()
Expand Down Expand Up @@ -1236,7 +1255,7 @@ def make_backup(self):
'make-snapshot', '--destination', backup_dir
]
logging.info(f'running {" ".join(cmd)}')
exit_code = subprocess.check_call(cmd)
exit_code = self.execute_neard_subcmd(cmd)
logging.info(
f'copying data dir to {backup_dir} finished with code {exit_code}')
# Copy config, genesis and node_key to the backup folder to use them to restore the db.
Expand Down Expand Up @@ -1303,7 +1322,7 @@ def run_restore_from_backup_cmd(self, backup_path):
self.target_near_home_path()
]
logging.info(f'running {" ".join(cmd)}')
exit_code = subprocess.check_call(cmd)
exit_code = self.execute_neard_subcmd(cmd)
logging.info(
f'snapshot restoration of {backup_path} terminated with code {exit_code}'
)
Expand Down

0 comments on commit 1bde566

Please sign in to comment.