Dash Core  0.12.2.1
P2P Digital Currency
coverage.py
Go to the documentation of this file.
1 """
2 This module contains utilities for doing coverage analysis on the RPC
3 interface.
4 
5 It provides a way to track which RPC commands are exercised during
6 testing.
7 
8 """
9 import os
10 
11 
12 REFERENCE_FILENAME = 'rpc_interface.txt'
13 
14 
16  """
17  An object that wraps AuthServiceProxy to record specific RPC calls.
18 
19  """
20  def __init__(self, auth_service_proxy_instance, coverage_logfile=None):
21  """
22  Kwargs:
23  auth_service_proxy_instance (AuthServiceProxy): the instance
24  being wrapped.
25  coverage_logfile (str): if specified, write each service_name
26  out to a file when called.
27 
28  """
29  self.auth_service_proxy_instance = auth_service_proxy_instance
30  self.coverage_logfile = coverage_logfile
31 
32  def __getattr__(self, *args, **kwargs):
33  return_val = self.auth_service_proxy_instance.__getattr__(
34  *args, **kwargs)
35 
36  return AuthServiceProxyWrapper(return_val, self.coverage_logfile)
37 
38  def __call__(self, *args, **kwargs):
39  """
40  Delegates to AuthServiceProxy, then writes the particular RPC method
41  called to a file.
42 
43  """
44  return_val = self.auth_service_proxy_instance.__call__(*args, **kwargs)
45  rpc_method = self.auth_service_proxy_instance._service_name
46 
47  if self.coverage_logfile:
48  with open(self.coverage_logfile, 'a+') as f:
49  f.write("%s\n" % rpc_method)
50 
51  return return_val
52 
53  @property
54  def url(self):
55  return self.auth_service_proxy_instance.url
56 
57 
58 def get_filename(dirname, n_node):
59  """
60  Get a filename unique to the test process ID and node.
61 
62  This file will contain a list of RPC commands covered.
63  """
64  pid = str(os.getpid())
65  return os.path.join(
66  dirname, "coverage.pid%s.node%s.txt" % (pid, str(n_node)))
67 
68 
69 def write_all_rpc_commands(dirname, node):
70  """
71  Write out a list of all RPC functions available in `bitcoin-cli` for
72  coverage comparison. This will only happen once per coverage
73  directory.
74 
75  Args:
76  dirname (str): temporary test dir
77  node (AuthServiceProxy): client
78 
79  Returns:
80  bool. if the RPC interface file was written.
81 
82  """
83  filename = os.path.join(dirname, REFERENCE_FILENAME)
84 
85  if os.path.isfile(filename):
86  return False
87 
88  help_output = node.help().split('\n')
89  commands = set()
90 
91  for line in help_output:
92  line = line.strip()
93 
94  # Ignore blanks and headers
95  if line and not line.startswith('='):
96  commands.add("%s\n" % line.split()[0])
97 
98  with open(filename, 'w') as f:
99  f.writelines(list(commands))
100 
101  return True
def write_all_rpc_commands(dirname, node)
Definition: coverage.py:69
def get_filename(dirname, n_node)
Definition: coverage.py:58
def __init__(self, auth_service_proxy_instance, coverage_logfile=None)
Definition: coverage.py:20