File: test-fiu_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 (88 lines) | stat: -rw-r--r-- 2,234 bytes parent folder | download | duplicates (2)
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
"""
Tests for the fiu_ctrl.py module.

Note the command line utility is covered by the utils/ tests, not from here,
this is just for the Python module.
"""

import subprocess
import fiu_ctrl
import errno
import time

fiu_ctrl.PLIBPATH = "./libs/"


def run_cat(**kwargs):
    return fiu_ctrl.Subprocess(
        ["./small-cat"],
        universal_newlines=True,
        stdin=subprocess.PIPE,
        stdout=subprocess.PIPE,
        stderr=subprocess.PIPE,
        **kwargs
    )


# Run without any failure point being enabled.
cmd = run_cat()
p = cmd.start()
out, err = p.communicate("test\n")
assert out == "test\n", out
assert err == "", err

# Enable before starting.
cmd = run_cat(fiu_enable_posix=True)
cmd.enable("posix/io/rw/*", failinfo=errno.ENOSPC)
p = cmd.start()
out, err = p.communicate("test\n")
assert out == "", out
assert "space" in err, err

# Enable after starting.
cmd = run_cat(fiu_enable_posix=True)
p = cmd.start()
cmd.enable("posix/io/rw/*", failinfo=errno.ENOSPC)
out, err = p.communicate("test\n")
assert out == "", out
assert "space" in err, err

# Enable-disable.
cmd = run_cat(fiu_enable_posix=True)
p = cmd.start()
cmd.enable("posix/io/rw/*", failinfo=errno.ENOSPC)
cmd.disable("posix/io/rw/*")
out, err = p.communicate("test\n")
assert out == "test\n", (out, err)

# Bad command.
cmd = run_cat(fiu_enable_posix=True)
p = cmd.start()
exc = None
try:
    cmd.run_raw_cmd("badcommand", ["name=a"])
except Exception as e:
    exc = e
assert isinstance(exc, fiu_ctrl.CommandError), "got exception: %r" % exc
out, err = p.communicate("test\n")
assert out == "test\n", out
assert err == "libfiu: rc parsing error: Unknown command\n", err

# Enable random.
# This relies on cat doing a reasonably small number of read and writes, which
# our small-cat does.
result = {True: 0, False: 0}
for i in range(50):
    cmd = run_cat(fiu_enable_posix=True)
    p = cmd.start()
    cmd.enable_random("posix/io/rw/*", failinfo=errno.ENOSPC, probability=0.5)
    out, err = p.communicate("test\n")
    if "space" in err:
        result[False] += 1
    elif out == "test\n":
        result[True] += 1
    else:
        assert False, (out, err)

assert 10 < result[True] < 40, result
assert 10 < result[False] < 40, result