File: test-basic_ctrl.py

package info (click to toggle)
libfiu 1.2-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 768 kB
  • sloc: ansic: 2,633; python: 973; makefile: 599; sh: 309
file content (70 lines) | stat: -rwxr-xr-x 2,142 bytes parent folder | download | duplicates (4)
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
#!/usr/bin/env python3

import os
import subprocess
import time

def fiu_ctrl(p, args):
    subprocess.check_call("./wrap fiu-ctrl".split() + args + [str(p.pid)],
            universal_newlines = True)

def launch_sh():
    # We use cat as a subprocess as it is reasonably ubiquitous, simple and
    # straightforward (which helps debugging and troubleshooting), but at the
    # same time it is interactive and we can make it do the operations we
    # want.
    # We also set LC_ALL=C as we test the output for the word "error", which
    # does not necessarily appear in other languages.
    p = subprocess.Popen("./wrap fiu-run -x cat".split(),
            universal_newlines = True,
            stdin=subprocess.PIPE, stdout=subprocess.PIPE,
            stderr=subprocess.PIPE,
            env=dict(os.environ, LC_ALL="C"))

    # Give it a moment to initialize and create the control files.
    time.sleep(0.2)
    return p

def send_cmd(p, cmd):
    p.stdin.write(cmd)
    p.stdin.close()

    # Give the control thread a moment to process the command.
    time.sleep(0.2)
    return p.stdout.read(), p.stderr.read()


# Launch a subprocess and check that it shows up in fiu-ls.
p = launch_sh()
out = subprocess.check_output("./wrap fiu-ls".split(),
        universal_newlines = True)
assert ("%s: cat" % p.pid) in out, out

# Send it a command and check that it works.
# Nothing interesting here from libfiu's perspective, but it helps make sure
# the test environment is sane.
out, err = send_cmd(p, "test\n")
assert out == 'test\n', out
assert err == '', err

# Launch and then make I/O fail at runtime.
p = launch_sh()
fiu_ctrl(p, ["-c", "enable name=posix/io/*"])
out, err = send_cmd(p, "test\n")
assert out == '', out
assert 'error' in err, err

# Same, but with failinfo.
p = launch_sh()
fiu_ctrl(p, ["-c", "enable name=posix/io/*,failinfo=3"])
out, err = send_cmd(p, "test\n")
assert out == '', out
assert 'error' in err, err

# Same, but with probability.
p = launch_sh()
fiu_ctrl(p, ["-c", "enable_random name=posix/io/*,probability=0.999"])
out, err = send_cmd(p, "test\n")
assert out == '', out
assert 'error' in err, err