Browse Source

test.py autosign: fixes & cleanups

The MMGen Project 3 years ago
parent
commit
1aad280b68
1 changed files with 45 additions and 40 deletions
  1. 45 40
      test/test_py_d/ts_autosign.py

+ 45 - 40
test/test_py_d/ts_autosign.py

@@ -126,7 +126,7 @@ class TestSuiteAutosign(TestSuiteBase):
 
 
 			# make 2 bad tx files
 			# make 2 bad tx files
 			for n in (1,2):
 			for n in (1,2):
-				bad_tx = joinpath(mountpoint,'tx','bad{}.rawtx'.format(n))
+				bad_tx = joinpath(mountpoint,'tx',f'bad{n}.rawtx')
 				if include_bad_tx and not remove_signed_only:
 				if include_bad_tx and not remove_signed_only:
 					open(bad_tx,'w').write('bad tx data')
 					open(bad_tx,'w').write('bad tx data')
 				if not include_bad_tx:
 				if not include_bad_tx:
@@ -135,6 +135,10 @@ class TestSuiteAutosign(TestSuiteBase):
 
 
 		def do_autosign_live(opts,mountpoint,led_opts=[],gen_wallet=True):
 		def do_autosign_live(opts,mountpoint,led_opts=[],gen_wallet=True):
 
 
+			omsg(purple('Running autosign test with {}'.format(
+				f"'{' '.join(led_opts)}'" if led_opts else 'no LED'
+			)))
+
 			def do_mount():
 			def do_mount():
 				try: run(['mount',mountpoint],check=True)
 				try: run(['mount',mountpoint],check=True)
 				except: pass
 				except: pass
@@ -220,7 +224,7 @@ class TestSuiteAutosign(TestSuiteBase):
 				'mmgen-autosign',
 				'mmgen-autosign',
 				opts + ['--quiet','--stealth-led','wait'],
 				opts + ['--quiet','--stealth-led','wait'],
 				extra_desc='(sign - --quiet --stealth-led)' )
 				extra_desc='(sign - --quiet --stealth-led)' )
-			autosign_expect(t,txcount)
+			autosign_expect(t,3)
 
 
 			copy_files(mountpoint,include_bad_tx=False,fdata_in=(('btc',''),))
 			copy_files(mountpoint,include_bad_tx=False,fdata_in=(('btc',''),))
 			t = self.spawn('mmgen-autosign',opts+['--quiet','--led'],extra_desc='(sign - --quiet --led)')
 			t = self.spawn('mmgen-autosign',opts+['--quiet','--led'],extra_desc='(sign - --quiet --led)')
@@ -229,59 +233,60 @@ class TestSuiteAutosign(TestSuiteBase):
 
 
 			return t
 			return t
 
 
-		# begin execution
-
-		if simulate and not opt.exact_output:
-			rmsg('This command must be run with --exact-output enabled!')
-			return False
-		network_ids = [c+'_tn' for c in daemon_coins] + daemon_coins
-		start_test_daemons(*network_ids)
-
-		if live:
-			mountpoint = '/mnt/tx'
+		def check_mountpoint(mountpoint):
 			if not os.path.ismount(mountpoint):
 			if not os.path.ismount(mountpoint):
 				try:
 				try:
 					run(['mount',mountpoint],check=True)
 					run(['mount',mountpoint],check=True)
-					imsg("Mounted '{}'".format(mountpoint))
+					imsg(f'Mounted {mountpoint}')
 				except:
 				except:
-					ydie(1,"Could not mount '{}'!  Exiting".format(mountpoint))
+					ydie(1,f'Could not mount {mountpoint}!  Exiting')
 
 
 			txdir = joinpath(mountpoint,'tx')
 			txdir = joinpath(mountpoint,'tx')
 			if not os.path.isdir(txdir):
 			if not os.path.isdir(txdir):
-				ydie(1,"Directory '{}' does not exist!  Exiting".format(mountpoint))
-
-			opts = ['--coins='+','.join(coins)]
+				ydie(1,f'Directory {txdir} does not exist!  Exiting')
 
 
+		def init_led():
 			from mmgen.led import LEDControl
 			from mmgen.led import LEDControl
-
 			if simulate:
 			if simulate:
 				LEDControl.create_dummy_control_files()
 				LEDControl.create_dummy_control_files()
-
 			try:
 			try:
 				cf = LEDControl(enabled=True,simulate=simulate)
 				cf = LEDControl(enabled=True,simulate=simulate)
 			except:
 			except:
-				ret = "'no LED support detected'"
-			else:
-				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'
-				omsg(purple('Running autosign test with no LED'))
-				do_autosign_live(opts,mountpoint)
-				omsg(purple("Running autosign test with '--led'"))
-				do_autosign_live(opts,mountpoint,led_opts=['--led'],gen_wallet=False)
-				omsg(purple("Running autosign test with '--stealth-led'"))
+				ydie(2,"'no LED support detected'")
+
+			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
+		network_ids = [c+'_tn' for c in daemon_coins] + daemon_coins
+		start_test_daemons(*network_ids)
+
+		try:
+			if live:
+				mountpoint = '/mnt/tx'
+				opts = ['--coins='+','.join(coins)]
+				check_mountpoint(mountpoint)
+				init_led()
+				foo = do_autosign_live(opts,mountpoint)
+				foo = do_autosign_live(opts,mountpoint,led_opts=['--led'],gen_wallet=False)
 				ret = do_autosign_live(opts,mountpoint,led_opts=['--stealth-led'],gen_wallet=False)
 				ret = do_autosign_live(opts,mountpoint,led_opts=['--stealth-led'],gen_wallet=False)
-		else:
-			mountpoint = self.tmpdir
-			opts = ['--no-insert-check','--mountpoint='+mountpoint,'--coins='+','.join(coins)]
-			try: os.mkdir(joinpath(mountpoint,'tx'))
-			except: pass
-			foo = do_autosign(opts,mountpoint,mn_type='mmgen',short=True)
-			foo = do_autosign(opts,mountpoint,mn_type='bip39',short=True)
-			ret = do_autosign(opts,mountpoint)
-
-		stop_test_daemons(*network_ids)
+			else:
+				mountpoint = self.tmpdir
+				opts = ['--no-insert-check','--mountpoint='+mountpoint,'--coins='+','.join(coins)]
+				try: os.mkdir(joinpath(mountpoint,'tx'))
+				except: pass
+				foo = do_autosign(opts,mountpoint,mn_type='mmgen',short=True)
+				foo = do_autosign(opts,mountpoint,mn_type='bip39',short=True)
+				ret = do_autosign(opts,mountpoint)
+		finally:
+			stop_test_daemons(*network_ids)
+
 		return ret
 		return ret
 
 
 class TestSuiteAutosignBTC(TestSuiteAutosign):
 class TestSuiteAutosignBTC(TestSuiteAutosign):