text: #!/usr/bin/env python3
  
  from testUtils import Utils
  from Cluster import Cluster
  from WalletMgr import WalletMgr
  from TestHelper import TestHelper
  
  import random
  
  ###############################################################
  # Test for different nodes restart scenarios.
  # Nodes can be producing or non-producing.
  # -p <producing nodes count>
  # -c <chain strategy[replay|resync|none]>
  # -s <topology>
  # -d <delay between nodes startup>
  # -v <verbose logging>
  # --kill-sig <kill signal [term|kill]>
  # --kill-count <nodaac instances to kill>
  # --dont-kill <Leave cluster running after test finishes>
  # --dump-error-details <Upon error print etc/aacio/node_*/config.ini and var/lib/node_*/stderr.log to stdout>
  # --keep-logs <Don't delete var/lib/node_* folders upon test completion>
  ###############################################################
  
  
  Print=Utils.Print
  errorExit=Utils.errorExit
  
  args=TestHelper.parse_args({"-p","-d","-s","-c","--kill-sig","--kill-count","--keep-logs","--p2p-plugin"
                              ,"--dump-error-details","-v","--leave-running","--clean-run"})
  pnodes=args.p
  topo=args.s
  delay=args.d
  chainSyncStrategyStr=args.c
  debug=args.v
  total_nodes = pnodes
  killCount=args.kill_count if args.kill_count > 0 else 1
  killSignal=args.kill_sig
  killAacInstances= not args.leave_running
  dumpErrorDetails=args.dump_error_details
  keepLogs=args.keep_logs
  killAll=args.clean_run
  p2pPlugin=args.p2p_plugin
  
  seed=1
  Utils.Debug=debug
  testSuccessful=False
  
  random.seed(seed) # Use a fixed seed for repeatability.
  cluster=Cluster(walletd=True)
  walletMgr=WalletMgr(True)
  
  try:
      TestHelper.printSystemInfo("BEGIN")
  
      cluster.setChainStrategy(chainSyncStrategyStr)
      cluster.setWalletMgr(walletMgr)
  
      cluster.killall(allInstances=killAll)
      cluster.cleanup()
      walletMgr.killall(allInstances=killAll)
      walletMgr.cleanup()
  
      Print ("producing nodes: %d, topology: %s, delay between nodes launch(seconds): %d, chain sync strategy: %s" % (
      pnodes, topo, delay, chainSyncStrategyStr))
  
      Print("Stand up cluster")
      if cluster.launch(pnodes, total_nodes, topo=topo, delay=delay, p2pPlugin=p2pPlugin) is False:
          errorExit("Failed to stand up aac cluster.")
  
      Print ("Wait for Cluster stabilization")
      # wait for cluster to start producing blocks
      if not cluster.waitOnClusterBlockNumSync(3):
          errorExit("Cluster never stabilized")
  
      Print("Stand up AAC wallet kaacd")
      walletMgr.killall(allInstances=killAll)
      walletMgr.cleanup()
      if walletMgr.launch() is False:
          errorExit("Failed to stand up kaacd.")
  
      accountsCount=total_nodes
      walletName="MyWallet"
      Print("Creating wallet %s if one doesn't already exist." % walletName)
      wallet=walletMgr.create(walletName, [cluster.aacioAccount,cluster.defproduceraAccount,cluster.defproducerbAccount])
  
      Print ("Populate wallet with %d accounts." % (accountsCount))
      if not cluster.populateWallet(accountsCount, wallet):
          errorExit("Wallet initialization failed.")
  
      defproduceraAccount=cluster.defproduceraAccount
      aacioAccount=cluster.aacioAccount
  
      Print("Importing keys for account %s into wallet %s." % (defproduceraAccount.name, wallet.name))
      if not walletMgr.importKey(defproduceraAccount, wallet):
          errorExit("Failed to import key for account %s" % (defproduceraAccount.name))
  
      Print("Create accounts.")
      if not cluster.createAccounts(aacioAccount):
          errorExit("Accounts creation failed.")
  
      Print("Wait on cluster sync.")
      if not cluster.waitOnClusterSync():
          errorExit("Cluster sync wait failed.")
  
      Print("Kill %d cluster node instances." % (killCount))
      if cluster.killSomeAacInstances(killCount, killSignal) is False:
          errorExit("Failed to kill Aac instances")
      Print("nodaac instances killed.")
  
      Print("Spread funds and validate")
      if not cluster.spreadFundsAndValidate(10):
          errorExit("Failed to spread and validate funds.")
  
      Print("Wait on cluster sync.")
      if not cluster.waitOnClusterSync():
          errorExit("Cluster sync wait failed.")
  
      Print ("Relaunch dead cluster nodes instances.")
      if cluster.relaunchAacInstances() is False:
          errorExit("Failed to relaunch Aac instances")
      Print("nodaac instances relaunched.")
  
      Print ("Resyncing cluster nodes.")
      if not cluster.waitOnClusterSync():
          errorExit("Cluster never synchronized")
      Print ("Cluster synched")
  
      Print("Spread funds and validate")
      if not cluster.spreadFundsAndValidate(10):
          errorExit("Failed to spread and validate funds.")
  
      Print("Wait on cluster sync.")
      if not cluster.waitOnClusterSync():
          errorExit("Cluster sync wait failed.")
  
      testSuccessful=True
  finally:
      TestHelper.shutdown(cluster, walletMgr, testSuccessful, killAacInstances, killAacInstances, keepLogs, killAll, dumpErrorDetails)
  
  exit(0)
  