-fix the fix
[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-gnunet-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
75
76 cleanup ()
77
78 print ("Preparing: Starting service...")
79 t = r_arm (['-s'], want_stdo = False)
80 time.sleep (1)
81 t = r_arm (['-i', 'statistics'], want_stdo = False)
82 time.sleep (1)
83
84 print ("TEST: Bad argument checking...", end='')
85 r_st (['-x'], normal = False, nofail = True, want_stdo = False, want_stde = True)
86 print ("PASS")
87
88 print ("TEST: Set value...", end='')
89 r_st (['-n', 'test', '-s', 'subsystem', b'42'], nofail = True, want_stdo = False)
90 print ("PASS")
91
92 print ("TEST: Set another value...", end='')
93 r_st (['-n', 'other', '-s', 'osystem', b'43'], nofail = True, want_stdo = False)
94 print ("PASS")
95
96 print ("TEST: Viewing all stats...", end='')
97 rc, stdo, stde = r_st ([], nofail = True, want_stdo = True)
98 if len (stdo.splitlines ()) != 2:
99   fail ("FAIL: unexpected output:\n{}".format (stdo))
100 print ("PASS")
101
102 print ("TEST: Viewing stats by name...", end='')
103 rc, stdo, stde = r_st (['-n', 'other'], nofail = True, want_stdo = True)
104 if len ([x for x in stdo.splitlines () if re.search (b'43', x)]) != 1:
105   fail ("FAIL: unexpected output:\n{}".format (stdo))
106 print ("PASS")
107
108 print ("TEST: Viewing stats by subsystem...", end='')
109 rc, stdo, stde = r_st (['-s', 'subsystem'], nofail = True, want_stdo = True)
110 if len ([x for x in stdo.splitlines () if re.search (b'42', x)]) != 1:
111   fail ("FAIL: unexpected output:\n{}".format (stdo))
112 print ("PASS")
113
114 print ("TEST: Set persistent value...", end='')
115 rc, stdo, stde = r_st (['-n', 'lasting', '-s', 'subsystem', '40', '-p'], nofail = True, want_stdo = False)
116 rc, stdo, stde = r_st ([], nofail = True, want_stdo = True)
117 if len ([x for x in stdo.splitlines () if re.search (b'40', x)]) != 1:
118   fail ("FAIL: unexpected output:\n{}".format (stdo))
119 print ("PASS")
120
121 restart ()
122
123 print ("TEST: Checking persistence...", end='')
124 rc, stdo, stde = r_st ([], nofail = True, want_stdo = True)
125 if len ([x for x in stdo.splitlines () if re.search (b'40', x)]) != 1:
126   fail ("FAIL: unexpected output:\n{}".format (stdo))
127 print ("PASS")
128
129 print ("TEST: Removing persistence...", end='')
130 rc, stdo, stde = r_st (['-n', 'lasting', '-s', 'subsystem', '40'], nofail = True, want_stdo = False)
131 rc, stdo, stde = r_st ([], nofail = True, want_stdo = True)
132 if len ([x for x in stdo.splitlines () if re.search (b'!', x)]) != 0:
133   fail ("FAIL: unexpected output:\n{}".format (stdo))
134 print ("PASS")
135
136 restart ()
137
138 print ("TEST: Checking removed persistence...", end='')
139 rc, stdo, stde = r_st ([], nofail = True, want_stdo = True)
140 if len ([x for x in stdo.splitlines () if re.search (b'40', x)]) != 0:
141   fail ("FAIL: unexpected output:\n{}".format (stdo))
142 print ("PASS")
143
144 print ("Stopping service...")
145 t = r_arm (['-e'], want_stdo = False)
146 time.sleep (1)
147
148 cleanup ()