summaryrefslogtreecommitdiff
path: root/src/arm/test_gnunet_arm.py.in
blob: e657d4a3f0bdc2ccde53991ffff46d593c921dee (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#!@PYTHONEXE@

import os
import sys
import shutil
import re
import subprocess
import time

# FIXME: There's too much repetition, move generally used parts into reusable modules.
if os.name == "nt":
    tmp = os.getenv("TEMP")
else:
    tmp = "/tmp"

if os.name == 'nt':
    st = 'gnunet-statistics.exe'
    arm = './gnunet-arm.exe'
else:
    st = 'gnunet-statistics'
    arm = './gnunet-arm'

run_arm = [arm, '-c', 'test_arm_api_data.conf', '--no-stdout', '--no-stderr']
debug = os.getenv('DEBUG')
if debug:
    run_arm += [debug.split(' ')]


def cleanup():
    shutil.rmtree(os.path.join(tmp, "test-gnunetd-arm"), True)


def sub_run(args, want_stdo=True, want_stde=False, nofail=False):
    if want_stdo:
        stdo = subprocess.PIPE
    else:
        stdo = None
    if want_stde:
        stde = subprocess.PIPE
    else:
        stde = None
    p = subprocess.Popen(args, stdout=stdo, stderr=stde)
    stdo, stde = p.communicate()
    if not nofail:
        if p.returncode != 0:
            sys.exit(p.returncode)
    return (p.returncode, stdo, stde)


def fail(result):
    print(result)
    r_arm(['-e'], want_stdo=False)
    sys.exit(1)


def end_arm_failer(command, rc, stdo, stde, normal):
    if normal:
        if rc != 0:
            fail(
                "FAIL: error running {}\nCommand output was:\n{}\n{}".format(
                    command, stdo, stde
                )
            )
    else:
        if rc == 0:
            fail(
                "FAIL: expected error while running {}\nCommand output was:\n{}\n{}"
                .format(command, stdo, stde)
            )


def print_only_failer(command, rc, stdo, stde, normal):
    if normal:
        if rc != 0:
            print(
                "FAIL: error running {}\nCommand output was:\n{}\n{}".format(
                    command, stdo, stde
                )
            )
            sys.exit(1)
    else:
        if rc == 0:
            print(
                "FAIL: expected error while running {}\nCommand output was:\n{}\n{}"
                .format(command, stdo, stde)
            )
            sys.exit(1)


def r_something(to_run, extra_args, failer=None, normal=True, **kw):
    rc, stdo, stde = sub_run(
        to_run + extra_args, nofail=True, want_stde=True, **kw
    )
    if failer is not None:
        failer(to_run + extra_args, rc, stdo, stde, normal)
    return (rc, stdo, stde)


def r_arm(extra_args, **kw):
    return r_something(run_arm, extra_args, **kw)


cleanup()

print("TEST: Bad argument checking...", end='')
r_arm(['-x'], normal=False, failer=print_only_failer)
print("PASS")

print("TEST: Start ARM...", end='')
r_arm(['-s'], failer=print_only_failer)
time.sleep(1)
print("PASS")

print("TEST: Start another service...", end='')
r_arm(['-i', 'resolver'], failer=end_arm_failer)
time.sleep(1)
print("PASS")

print("TEST: Stop a service...", end='')
r_arm(['-k', 'resolver'], failer=end_arm_failer)
time.sleep(1)
print("PASS")

print("TEST: Stop ARM...", end='')
r_arm(['-e'], failer=print_only_failer)
time.sleep(1)
print("PASS")

cleanup()