-API comments
[oweals/gnunet.git] / src / statistics / test_gnunet_statistics.py.in
1 #!@PYTHON@
2 from __future__ import print_function
3 import os
4 import sys
5 import shutil
6 import re
7 import subprocess
8 import time
9
10 if os.name == "nt":
11   tmp = os.getenv ("TEMP")
12 else:
13   tmp = "/tmp"
14
15 if os.name == 'nt':
16   st = 'gnunet-statistics.exe'
17   arm = 'gnunet-arm.exe'
18 else:
19   st = 'gnunet-statistics'
20   arm = 'gnunet-arm'
21
22 run_st = [st, '-c', 'test_statistics_api_data.conf']
23 run_arm = [arm, '-c', 'test_statistics_api_data.conf']
24 debug = os.getenv ('DEBUG')
25 if debug:
26   run_arm += [debug.split (' ')]
27
28 def cleanup ():
29   shutil.rmtree (os.path.join (tmp, "test-gnunetd-statistics"), True)
30
31 def sub_run (args, want_stdo = True, want_stde = False, nofail = False):
32   if want_stdo:
33     stdo = subprocess.PIPE
34   else:
35     stdo = None
36   if want_stde:
37     stde = subprocess.PIPE
38   else:
39     stde = None
40   p = subprocess.Popen (args, stdout = stdo, stderr = stde)
41   stdo, stde = p.communicate ()
42   if not nofail:
43     if p.returncode != 0:
44       sys.exit (p.returncode)
45   return (p.returncode, stdo, stde)
46
47 def fail (result):
48   print (result)
49   r_arm (['-e'], want_stdo = False)
50   sys.exit (1)
51
52 def r_arm (extra_args, **kw):
53   rc, stdo, stde = sub_run (run_arm + extra_args, **kw)
54   if rc != 0:
55     fail ("FAIL: error running {}".format (run_arm))
56   return (rc, stdo, stde)
57
58 def r_st (extra_args, normal = True, **kw):
59   rc, stdo, stde = sub_run (run_st + extra_args, **kw)
60   if normal:
61     if rc != 0:
62       fail ("FAIL: error running {}".format (run_st))
63   else:
64     if rc == 0:
65       fail ("FAIL: expected error while running {}".format (run_st))
66   return (rc, stdo, stde)
67
68 def restart ():
69   print ("Restarting service...")
70   t = r_arm (['-k', 'statistics'])
71   time.sleep (1)
72   t = r_arm (['-i', 'statistics'])
73   time.sleep (1)
74   print ("DONE")
75
76
77 cleanup ()
78
79 print ("Preparing: Starting service...")
80 t = r_arm (['-s'], want_stdo = False)
81 time.sleep (1)
82 t = r_arm (['-i', 'statistics'], want_stdo = False)
83 time.sleep (1)
84 print ("DONE")
85
86 print ("TEST: Bad argument checking...", end='')
87 r_st (['-x'], normal = False, nofail = True, want_stdo = False)
88 print ("PASS")
89
90 print ("TEST: Set value...", end='')
91 r_st (['-n', 'test', '-s', 'subsystem', '42'], nofail = True, want_stdo = False)
92 print ("PASS")
93
94 print ("TEST: Set another value...", end='')
95 r_st (['-n', 'other', '-s', 'osystem', '43'], nofail = True, want_stdo = False)
96 print ("PASS")
97
98 print ("TEST: Viewing all stats...", end='')
99 rc, stdo, stde = r_st ([], nofail = True, want_stdo = True)
100 if len (stdo.splitlines ()) != 2:
101   fail ("FAIL: unexpected output:\n{}".format (stdo))
102 print ("PASS")
103
104 print ("TEST: Viewing stats by name...", end='')
105 rc, stdo, stde = r_st (['-n', 'other'], nofail = True, want_stdo = True)
106 if len ([x for x in stdo.splitlines () if re.search ('43', x)]) != 1:
107   fail ("FAIL: unexpected output:\n{}".format (stdo))
108 print ("PASS")
109
110 print ("TEST: Viewing stats by subsystem...", end='')
111 rc, stdo, stde = r_st (['-s', 'subsystem'], nofail = True, want_stdo = True)
112 if len ([x for x in stdo.splitlines () if re.search ('42', x)]) != 1:
113   fail ("FAIL: unexpected output:\n{}".format (stdo))
114 print ("PASS")
115
116 print ("TEST: Set persistent value...", end='')
117 rc, stdo, stde = r_st (['-n', 'lasting', '-s', 'subsystem', '40', '-p'], nofail = True, want_stdo = False)
118 rc, stdo, stde = r_st ([], nofail = True, want_stdo = True)
119 if len ([x for x in stdo.splitlines () if re.search ('40', x)]) != 1:
120   fail ("FAIL: unexpected output:\n{}".format (stdo))
121 print ("PASS")
122
123 restart ()
124
125 print ("TEST: Checking persistence...", end='')
126 rc, stdo, stde = r_st ([], nofail = True, want_stdo = True)
127 if len ([x for x in stdo.splitlines () if re.search ('40', x)]) != 1:
128   fail ("FAIL: unexpected output:\n{}".format (stdo))
129 print ("PASS")
130
131 print ("TEST: Removing persistence...", end='')
132 rc, stdo, stde = r_st (['-n', 'lasting', '-s', 'subsystem', '40'], nofail = True, want_stdo = False)
133 rc, stdo, stde = r_st ([], nofail = True, want_stdo = True)
134 if len ([x for x in stdo.splitlines () if re.search ('!', x)]) != 0:
135   fail ("FAIL: unexpected output:\n{}".format (stdo))
136 print ("PASS")
137
138 restart ()
139
140 print ("TEST: Checking removed persistence...", end='')
141 rc, stdo, stde = r_st ([], nofail = True, want_stdo = True)
142 if len ([x for x in stdo.splitlines () if re.search ('40', x)]) != 0:
143   fail ("FAIL: unexpected output:\n{}".format (stdo))
144 print ("PASS")
145
146 print ("Stopping service...")
147 t = r_arm (['-e'], want_stdo = False)
148 time.sleep (1)
149 print ("DONE")
150
151 cleanup ()