github.com/platonnetwork/platon-go@v0.7.6/cases/tests/rpc/test_rpc_admin.py (about)

     1  # -*- coding: utf-8 -*-
     2  '''
     3  @Description: rpc用例
     4  '''
     5  
     6  import allure
     7  import pytest
     8  from hexbytes import HexBytes
     9  
    10  startApi = "eth,web3,net,txpool,platon,admin,personal"
    11  
    12  
    13  @allure.title("Get current process datadir")
    14  @pytest.mark.P1
    15  def test_admin_datadir(global_running_env):
    16      node = global_running_env.get_rand_node()
    17      dataDir = node.remote_data_dir
    18      assert node.admin.datadir == dataDir
    19  
    20  
    21  @allure.title("Get program version")
    22  @pytest.mark.P1
    23  def test_admin_getProgramVersion(global_running_env):
    24      node = global_running_env.get_rand_node()
    25      msg = node.admin.getProgramVersion()
    26      ProgramVersionSign = msg["Sign"]
    27      ProgramVersion = msg["Version"]
    28      assert len(ProgramVersionSign) == 132
    29      assert ProgramVersion >= 1794
    30  
    31  
    32  @allure.title("Get schnorrNIZKProve")
    33  @pytest.mark.P1
    34  def test_admin_getSchnorrNIZKProve(global_running_env):
    35      node = global_running_env.get_rand_node()
    36      blsproof = node.admin.getSchnorrNIZKProve()
    37      assert len(blsproof) == 128
    38  
    39  
    40  @allure.title("get node info")
    41  @pytest.mark.P1
    42  def test_admin_nodeInfo(global_running_env):
    43      node = global_running_env.get_rand_node()
    44      genHash = HexBytes(node.eth.getBlock(0)["hash"]).hex()
    45      nodeInfo = node.admin.nodeInfo
    46  
    47      # config
    48      config = nodeInfo["protocols"]["platon"]["config"]
    49  
    50      # node id
    51      assert node.node_id == nodeInfo["id"]
    52      # listen port
    53      assert node.p2p_port == str(nodeInfo["ports"]["listener"])
    54      # discovery port
    55      assert node.p2p_port == str(nodeInfo["ports"]["discovery"])
    56  
    57      assert global_running_env.amount == config["cbft"]["amount"]
    58      assert global_running_env.period == config["cbft"]["period"]
    59      assert global_running_env.validatorMode == config["cbft"]["validatorMode"]
    60  
    61      assert global_running_env.chain_id == config["chainId"]
    62      assert genHash == nodeInfo["protocols"]["platon"]["genesis"]
    63  
    64  
    65  @allure.title("get node peers")
    66  @pytest.mark.P1
    67  def test_admin_peers(global_running_env):
    68      node = global_running_env.get_rand_node()
    69      lenPeers = len(node.admin.peers)
    70      assert lenPeers >= 0
    71  
    72  
    73  @allure.title("export chain")
    74  @pytest.mark.P1
    75  @pytest.fixture()
    76  def test_admin_exportChain(global_running_env):
    77      node = global_running_env.get_rand_node()
    78      filePath = node.admin.datadir + "chainData.txt"
    79      assert True == node.admin.exportChain(filePath)
    80      yield node
    81  
    82  
    83  @allure.title("import chain")
    84  @pytest.mark.P1
    85  def test_admin_importChain(test_admin_exportChain):
    86      filePath = test_admin_exportChain.admin.datadir + "chainData.txt"
    87      assert True == test_admin_exportChain.admin.importChain(filePath), "import chain failed!"
    88  
    89  
    90  @allure.title("remove peer")
    91  @pytest.mark.P1
    92  def test_admin_removePeer(global_running_env):
    93      node = global_running_env.get_rand_node()
    94      peers = node.admin.peers
    95      if len(peers) > 0:
    96          node_url = "enode://" + peers[0]["id"] + "@" + peers[0]["network"]["remoteAddress"]
    97          assert True == node.admin.removePeer(node_url)
    98  
    99  
   100  @allure.title("stop websocket rpc service")
   101  @pytest.fixture()
   102  def admin_stopWS(global_running_env):
   103      node = global_running_env.get_rand_node()
   104      try:
   105          ws = node.ws_web3
   106          assert True == ws.admin.stopWS()
   107      except Exception as e:
   108          print("websocket service not started===================>")
   109  
   110      yield node
   111  
   112  
   113  @allure.title("Start websocket rpc service")
   114  @pytest.mark.P1
   115  @pytest.fixture()
   116  def test_admin_startWS(admin_stopWS):
   117      node = admin_stopWS
   118      if None == node.wsport:
   119          node.wsport = 5789
   120      if None == node.wsurl:
   121          node.wsurl = "ws://" + str(node.host) + ":" + str(node.wsport)
   122      assert True == node.admin.startWS(node.host, int(node.wsport), "*", startApi)
   123  
   124      ws = node.ws_web3
   125      assert ws.eth.blockNumber >= 0
   126  
   127  
   128  @allure.title("stop http rpc service")
   129  @pytest.fixture()
   130  def admin_stopRPC(global_running_env):
   131      node = global_running_env.get_rand_node()
   132      try:
   133          ws = node.ws_web3
   134          assert True == ws.admin.stopRPC()
   135      except Exception as e:
   136          pass
   137  
   138      yield node
   139  
   140  
   141  '''
   142  @allure.title("start http rpc service")
   143  @pytest.mark.P0
   144  def test_admin_startRPC(admin_stopRPC):
   145      node = admin_stopRPC
   146      ws = node.ws_web3
   147      assert True == ws.admin.startRPC(admin_stopRPC.host, int(admin_stopRPC.rpc_port))
   148  '''
   149  
   150  if __name__ == '__main__':
   151      pytest.main(['-v', 'test_rpc_admin.py'])