RECLAIM/OIDC: code cleanup
[oweals/gnunet.git] / src / arm / test_gnunet_arm.py.in
1 #!@PYTHON@
2
3 import os
4 import sys
5 import shutil
6 import re
7 import subprocess
8 import time
9
10 # FIXME: There's too much repetition, move generally used parts into reusable modules.
11 if os.name == "nt":
12     tmp = os.getenv("TEMP")
13 else:
14     tmp = "/tmp"
15
16 if os.name == 'nt':
17     st = 'gnunet-statistics.exe'
18     arm = './gnunet-arm.exe'
19 else:
20     st = 'gnunet-statistics'
21     arm = './gnunet-arm'
22
23 run_arm = [arm, '-c', 'test_arm_api_data.conf', '--no-stdout', '--no-stderr']
24 debug = os.getenv('DEBUG')
25 if debug:
26     run_arm += [debug.split(' ')]
27
28
29 def cleanup():
30     shutil.rmtree(os.path.join(tmp, "test-gnunetd-arm"), True)
31
32
33 def sub_run(args, want_stdo=True, want_stde=False, nofail=False):
34     if want_stdo:
35         stdo = subprocess.PIPE
36     else:
37         stdo = None
38     if want_stde:
39         stde = subprocess.PIPE
40     else:
41         stde = None
42     p = subprocess.Popen(args, stdout=stdo, stderr=stde)
43     stdo, stde = p.communicate()
44     if not nofail:
45         if p.returncode != 0:
46             sys.exit(p.returncode)
47     return (p.returncode, stdo, stde)
48
49
50 def fail(result):
51     print(result)
52     r_arm(['-e'], want_stdo=False)
53     sys.exit(1)
54
55
56 def end_arm_failer(command, rc, stdo, stde, normal):
57     if normal:
58         if rc != 0:
59             fail("FAIL: error running {}\nCommand output was:\n{}\n{}".format(command, stdo, stde))
60     else:
61         if rc == 0:
62             fail("FAIL: expected error while running {}\nCommand output was:\n{}\n{}".format(command, stdo, stde))
63
64
65 def print_only_failer(command, rc, stdo, stde, normal):
66     if normal:
67         if rc != 0:
68             print("FAIL: error running {}\nCommand output was:\n{}\n{}".format(command, stdo, stde))
69             sys.exit(1)
70     else:
71         if rc == 0:
72             print("FAIL: expected error while running {}\nCommand output was:\n{}\n{}".format(command, stdo, stde))
73             sys.exit(1)
74
75
76 def r_something(to_run, extra_args, failer=None, normal=True, **kw):
77     rc, stdo, stde = sub_run(to_run + extra_args, nofail=True, want_stde=True, **kw)
78     if failer is not None:
79         failer(to_run + extra_args, rc, stdo, stde, normal)
80     return (rc, stdo, stde)
81
82
83 def r_arm(extra_args, **kw):
84     return r_something(run_arm, extra_args, **kw)
85
86
87 cleanup()
88
89 print("TEST: Bad argument checking...", end='')
90 r_arm(['-x'], normal=False, failer=print_only_failer)
91 print("PASS")
92
93 print("TEST: Start ARM...", end='')
94 r_arm(['-s'], failer=print_only_failer)
95 time.sleep(1)
96 print("PASS")
97
98 print("TEST: Start another service...", end='')
99 r_arm(['-i', 'resolver'], failer=end_arm_failer)
100 time.sleep(1)
101 print("PASS")
102
103 print("TEST: Stop a service...", end='')
104 r_arm(['-k', 'resolver'], failer=end_arm_failer)
105 time.sleep(1)
106 print("PASS")
107
108 print("TEST: Stop ARM...", end='')
109 r_arm(['-e'], failer=print_only_failer)
110 time.sleep(1)
111 print("PASS")
112
113 cleanup()