led.py, test.py autosign: improve LED simulation code

This commit is contained in:
The MMGen Project 2022-01-06 20:24:22 +00:00
commit 6e0519dc12
Signed by: mmgen
GPG key ID: 3F8B1861E32B7DA2
3 changed files with 24 additions and 27 deletions

View file

@ -53,7 +53,7 @@ class LEDControl:
def __init__(self,enabled,simulate=False,debug=False):
self.enabled = enabled
self.debug = debug
self.debug = debug or simulate
if not enabled:
self.set = self.stop = self.noop
@ -62,11 +62,9 @@ class LEDControl:
self.ev = threading.Event()
self.led_thread = None
if simulate:
type(self).create_dummy_control_files()
self.debug = True
for board_id,board in self.boards.items():
if board_id == 'dummy' and not simulate:
continue
try: os.stat(board.status)
except: pass
else: break
@ -82,20 +80,6 @@ class LEDControl:
Trigger file: {board.trigger}
""",indent=' ',strip_char='\t'))
if board_id == 'dummy' and not simulate:
if g.test_suite:
msg('Warning: no simulation requested but dummy LED control files detected')
self.debug = True
else:
die(1,fmt(f"""
No simulation requested but dummy LED control files detected:
{board.status}
{board.trigger}
You may wish to remove them and restart
""",indent=' ',strip_char='\t'))
def check_access(fn,desc,init_val=None):
try:
if not init_val:
@ -127,6 +111,12 @@ class LEDControl:
with open(db.trigger,'w') as fp:
fp.write(db.trigger_states[1]+'\n')
@classmethod
def delete_dummy_control_files(cls):
db = cls.boards['dummy']
os.unlink(db.status)
os.unlink(db.trigger)
def noop(self,*args,**kwargs): pass
def ev_sleep(self,secs):

View file

@ -411,7 +411,9 @@ signal.signal(signal.SIGTERM,handler)
signal.signal(signal.SIGINT,handler)
from .led import LEDControl
led = LEDControl(enabled=opt.led,simulate=g.test_suite and not os.getenv('MMGEN_TEST_SUITE_AUTOSIGN_LIVE'))
led = LEDControl(
enabled = opt.led,
simulate = os.getenv('MMGEN_TEST_SUITE_AUTOSIGN_LED_SIMULATE') )
led.set('off')
async def main():

View file

@ -32,6 +32,8 @@ from .ts_base import *
from .ts_shared import *
from .input import *
from mmgen.led import LEDControl
class TestSuiteAutosign(TestSuiteBase):
'autosigning with BTC, BCH, LTC, ETH and ETC'
networks = ('btc',)
@ -249,24 +251,25 @@ class TestSuiteAutosign(TestSuiteBase):
ydie(1,f'Directory {txdir} does not exist! Exiting')
def init_led():
from mmgen.led import LEDControl
if simulate:
LEDControl.create_dummy_control_files()
try:
cf = LEDControl(enabled=True,simulate=simulate)
except:
ydie(2,"'no LED support detected'")
except Exception as e:
msg(str(e))
ydie(2,'LEDControl initialization failed')
for fn in (cf.board.status,cf.board.trigger):
if fn:
run(['sudo','chmod','0666',fn],check=True)
os.environ['MMGEN_TEST_SUITE_AUTOSIGN_LIVE'] = '1'
# begin execution
if simulate and not opt.exact_output:
rmsg('This command must be run with --exact-output enabled!')
return False
if simulate or not live:
os.environ['MMGEN_TEST_SUITE_AUTOSIGN_LED_SIMULATE'] = '1'
LEDControl.create_dummy_control_files()
network_ids = [c+'_tn' for c in daemon_coins] + daemon_coins
start_test_daemons(*network_ids)
@ -288,6 +291,8 @@ class TestSuiteAutosign(TestSuiteBase):
foo = do_autosign(opts,mountpoint,mn_type='bip39',short=True)
ret = do_autosign(opts,mountpoint)
finally:
if simulate or not live:
LEDControl.delete_dummy_control_files()
stop_test_daemons(*[i for i in network_ids if i != 'btc'])
return ret