Merge branch 'master' of ssh://gnunet.org/gnunet
[oweals/gnunet.git] / src / dht / test_dht_tools.py.in
1 #!@PYTHON@
2 #
3 # This testcase simply checks that the DHT command-line tools work.
4 # It launches a single peer, stores a value "testdata" under "testkey",
5 # and then gives the system 50 ms to fetch it.
6 #
7 # This could fail if
8 # - command line tool interfaces fail
9 # - DHT plugins for storage are not installed / working
10 # - block plugins for verification (the test plugin) is not installed
11 #
12 # The code does NOT depend on DHT routing or any actual P2P functionality.
13 #
14 from __future__ import print_function
15 from __future__ import absolute_import
16 import os
17 import sys
18 import shutil
19 import re
20 import subprocess
21 import time
22 import tempfile
23
24 os.environ["PATH"] = "@bindir@" + ":" + os.environ["PATH"]
25
26 if os.name == "nt":
27     tmp = os.getenv("TEMP")
28 else:
29     tmp = "/tmp"
30
31 if os.name == 'nt':
32     get = './gnunet-dht-get.exe'
33     put = './gnunet-dht-put.exe'
34     arm = 'gnunet-arm.exe'
35 else:
36     get = './gnunet-dht-get'
37     put = './gnunet-dht-put'
38     arm = 'gnunet-arm'
39
40 cfgfile = 'test_dht_api_peer1.conf'
41 run_get = [get, '-c', cfgfile]
42 run_put = [put, '-c', cfgfile]
43 run_arm = [arm, '-c', cfgfile]
44 debug = os.getenv('DEBUG')
45 if debug:
46     run_arm += [debug.split(' ')]
47
48
49 def cleanup(exitcode):
50     sys.exit(exitcode)
51
52
53 def sub_run(args, want_stdo=True, want_stde=False, nofail=False):
54     if want_stdo:
55         stdo = subprocess.PIPE
56     else:
57         stdo = None
58     if want_stde:
59         stde = subprocess.PIPE
60     else:
61         stde = None
62     p = subprocess.Popen(args, stdout=stdo, stderr=stde)
63     stdo, stde = p.communicate()
64     if not nofail:
65         if p.returncode != 0:
66             sys.exit(p.returncode)
67     return (p.returncode, stdo, stde)
68
69
70 def fail(result):
71     print(result)
72     r_arm(['-e'], want_stdo=False)
73     cleanup(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, **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 def r_get(extra_args, **kw):
88     return r_something(run_get, extra_args, **kw)
89
90
91 def r_put(extra_args, **kw):
92     return r_something(run_put, extra_args, **kw)
93
94
95 def end_arm_failer(command, rc, stdo, stde, normal):
96     if normal:
97         if rc != 0:
98             fail("FAIL: error running {}\nCommand output was:\n{}\n{}".format(command, stdo, stde))
99     else:
100         if rc == 0:
101             fail("FAIL: expected error while running {}\nCommand output was:\n{}\n{}".format(command, stdo, stde))
102
103
104 def print_only_failer(command, rc, stdo, stde, normal):
105     if normal:
106         if rc != 0:
107             print("FAIL: error running {}\nCommand output was:\n{}\n{}".format(command, stdo, stde))
108             cleanup(1)
109     else:
110         if rc == 0:
111             print("FAIL: expected error while running {}\nCommand output was:\n{}\n{}".format(command, stdo, stde))
112             cleanup(1)
113
114
115 print("TEST: Starting ARM...", end='')
116 r_arm(['-s'], failer=end_arm_failer, want_stdo=False, want_stde=False)
117 print("PASS")
118 time.sleep(1)
119
120 print("TEST: Testing put...", end='')
121 r_put(['-k', 'testkey', '-d', 'testdata', '-t', '8'], failer=end_arm_failer)
122 print("PASS")
123 time.sleep(1)
124
125 print("TEST: Testing get...", end='')
126 rc, stdo, stde = r_get(['-k', 'testkey', '-T', '50 ms', '-t', '8'], want_stdo=True, failer=end_arm_failer)
127 stdo = stdo.replace('\r', '').splitlines()
128 expect = "Result 0, type 8:\ntestdata".splitlines()
129 if len(stdo) != 2 or len(expect) != 2 or stdo[0] != expect[0] or stdo[1] != expect[1]:
130     fail("output `{}' differs from expected `{}'".format(stdo, expect))
131 print("PASS")
132
133 r_arm(['-e', '-d'], failer=print_only_failer)