File: test-python-plugin.py

package info (click to toggle)
nbdkit 1.42.9-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 14,696 kB
  • sloc: ansic: 59,224; sh: 16,793; makefile: 6,463; python: 1,837; cpp: 1,116; ml: 504; perl: 502; tcl: 62
file content (212 lines) | stat: -rw-r--r-- 5,841 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
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# nbdkit test plugin
# Copyright Red Hat
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
#
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
#
# * Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# * Neither the name of Red Hat nor the names of its contributors may be
# used to endorse or promote products derived from this software without
# specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY RED HAT AND CONTRIBUTORS ''AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
# PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL RED HAT OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
# OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.

"""See tests/test-python.sh."""

import nbdkit
import pickle
import base64
import unittest

API_VERSION = 2

cfg = {}


# Not nice, but there doesn't seem to be a better way of putting this
class TestAPI(unittest.TestCase):

    def test_parse_size(self):
        self.assertEqual(nbdkit.parse_size('511'), 511)
        self.assertEqual(nbdkit.parse_size('7k'), 7*1024)
        self.assertEqual(nbdkit.parse_size('17M'), 17*1024*1024)

        with self.assertRaises(TypeError):
            nbdkit.parse_size(17)

        with self.assertRaises(ValueError):
            nbdkit.parse_size('foo')

    def test_parse_probability(self):
        test = "test_parse_probability"

        self.assertEqual(nbdkit.parse_probability(test, '1:10'), 0.1)
        self.assertEqual(nbdkit.parse_probability(test, '100%'), 1)
        self.assertEqual(nbdkit.parse_probability(test, '0'), 0)

        with self.assertRaises(TypeError):
            nbdkit.parse_probability(test, 17)

        with self.assertRaises(ValueError):
            nbdkit.parse_probability(test, 'bar')

    def test_parse_delay(self):
        test = "test_parse_delay"

        self.assertEqual(nbdkit.parse_delay(test, '10ms'), (0, 10000000))
        self.assertEqual(nbdkit.parse_delay(test, '1.5'), (1, 500000000))

        with self.assertRaises(TypeError):
            nbdkit.parse_delay(test, 17)

        with self.assertRaises(ValueError):
            nbdkit.parse_delay(test, 'bar')


TestAPI().test_parse_size()
TestAPI().test_parse_probability()
TestAPI().test_parse_delay()


def config(k, v):
    global cfg
    if k == "cfg":
        cfg = pickle.loads(base64.b64decode(v.encode()))


def config_complete():
    print("set_error = %r" % nbdkit.set_error)


def open(readonly):
    if cfg.get('create_disk', True):
        disk = bytearray(cfg.get('size', 0))
    else:
        disk = None
    return {
        'disk': disk
    }


def get_size(h):
    return cfg.get('size', 0)


def is_rotational(h):
    return cfg.get('is_rotational', False)


def can_multi_conn(h):
    return cfg.get('can_multi_conn', False)


def can_write(h):
    return cfg.get('can_write', True)


def can_flush(h):
    return cfg.get('can_flush', False)


def can_trim(h):
    return cfg.get('can_trim', False)


def can_zero(h):
    return cfg.get('can_zero', False)


def can_fast_zero(h):
    return cfg.get('can_fast_zero', False)


def can_fua(h):
    fua = cfg.get('can_fua', "none")
    if fua == "none":
        return nbdkit.FUA_NONE
    elif fua == "emulate":
        return nbdkit.FUA_EMULATE
    elif fua == "native":
        return nbdkit.FUA_NATIVE


def can_cache(h):
    cache = cfg.get('can_cache', "none")
    if cache == "none":
        return nbdkit.CACHE_NONE
    elif cache == "emulate":
        return nbdkit.CACHE_EMULATE
    elif cache == "native":
        return nbdkit.CACHE_NATIVE


def can_extents(h):
    return cfg.get('can_extents', False)


def pread(h, buf, offset, flags):
    assert flags == 0
    end = offset + len(buf)
    buf[:] = h['disk'][offset:end]


def pwrite(h, buf, offset, flags):
    expect_fua = cfg.get('pwrite_expect_fua', False)
    actual_fua = bool(flags & nbdkit.FLAG_FUA)
    assert expect_fua == actual_fua
    end = offset + len(buf)
    assert h['disk'] is not None
    h['disk'][offset:end] = buf


def flush(h, flags):
    assert flags == 0


def trim(h, count, offset, flags):
    expect_fua = cfg.get('trim_expect_fua', False)
    actual_fua = bool(flags & nbdkit.FLAG_FUA)
    assert expect_fua == actual_fua
    if h['disk'] is not None:
        h['disk'][offset:offset+count] = bytearray(count)


def zero(h, count, offset, flags):
    expect_fua = cfg.get('zero_expect_fua', False)
    actual_fua = bool(flags & nbdkit.FLAG_FUA)
    assert expect_fua == actual_fua
    expect_may_trim = cfg.get('zero_expect_may_trim', False)
    actual_may_trim = bool(flags & nbdkit.FLAG_MAY_TRIM)
    assert expect_may_trim == actual_may_trim
    expect_fast_zero = cfg.get('zero_expect_fast_zero', False)
    actual_fast_zero = bool(flags & nbdkit.FLAG_FAST_ZERO)
    assert expect_fast_zero == actual_fast_zero
    if h['disk'] is not None:
        h['disk'][offset:offset+count] = bytearray(count)


def cache(h, count, offset, flags):
    assert flags == 0
    # do nothing


def extents(h, count, offset, flags):
    return cfg.get('extents', [])