| 1 |
|
| 2 |
|
| 3 |
from __future__ import absolute_import |
| 4 |
|
| 5 |
import errno |
| 6 |
import sys |
| 7 |
from itertools import islice |
| 8 |
from threading import Timer, Thread, current_thread |
| 9 |
|
| 10 |
import pytest |
| 11 |
|
| 12 |
from pyxs.client import RVar, Router, Client |
| 13 |
from pyxs.connection import UnixSocketConnection, XenBusConnection |
| 14 |
from pyxs.exceptions import InvalidPath, InvalidPermission, \ |
| 15 |
UnexpectedPacket, PyXSError |
| 16 |
from pyxs._internal import NUL, Op, Event, Packet |
| 17 |
|
| 18 |
from . import virtualized |
| 19 |
|
| 20 |
|
| 21 |
def setup_function(f): |
| 22 |
try: |
| 23 |
with Client() as c: |
| 24 |
c.delete(b"/foo") |
| 25 |
except PyXSError: |
| 26 |
pass |
| 27 |
|
| 28 |
|
| 29 |
def test_init(): |
| 30 |
|
| 31 |
c = Client() |
| 32 |
assert c.tx_id == 0 |
| 33 |
assert isinstance(c.router.connection, UnixSocketConnection) |
| 34 |
assert not c.router.thread.is_alive() |
| 35 |
|
| 36 |
c = Client(unix_socket_path="/var/run/xenstored/socket") |
| 37 |
assert isinstance(c.router.connection, UnixSocketConnection) |
| 38 |
assert not c.router.thread.is_alive() |
| 39 |
|
| 40 |
|
| 41 |
c = Client(xen_bus_path="/dev/xen/xenbus") |
| 42 |
assert isinstance(c.router.connection, XenBusConnection) |
| 43 |
assert not c.router.thread.is_alive() |
| 44 |
|
| 45 |
|
| 46 |
@virtualized |
| 47 |
def test_context_manager(): |
| 48 |
|
| 49 |
c = Client() |
| 50 |
assert not c.router.thread.is_alive() |
| 51 |
|
| 52 |
with c: |
| 53 |
assert c.router.thread.is_alive() |
| 54 |
|
| 55 |
assert not c.router.thread.is_alive() |
| 56 |
|
| 57 |
|
| 58 |
@virtualized |
| 59 |
def test_execute_command_invalid_characters(): |
| 60 |
with Client() as c: |
| 61 |
c.execute_command(Op.WRITE, b"/foo/bar" + NUL, b"baz") |
| 62 |
|
| 63 |
with pytest.raises(ValueError): |
| 64 |
c.execute_command(Op.DEBUG, b"\x07foo" + NUL) |
| 65 |
|
| 66 |
|
| 67 |
@virtualized |
| 68 |
def test_execute_command_error(): |
| 69 |
with Client() as c: |
| 70 |
with pytest.raises(PyXSError): |
| 71 |
c.execute_command(Op.READ, b"/unexisting/path" + NUL) |
| 72 |
|
| 73 |
with pytest.raises(PyXSError): |
| 74 |
c.execute_command(-42, b"/unexisting/path" + NUL) |
| 75 |
|
| 76 |
|
| 77 |
def monkeypatch_router(client, response_packet): |
| 78 |
class FakeRouter: |
| 79 |
def send(self, packet): |
| 80 |
rvar = RVar() |
| 81 |
rvar.set(response_packet) |
| 82 |
return rvar |
| 83 |
|
| 84 |
def terminate(self): |
| 85 |
pass |
| 86 |
|
| 87 |
client.close() |
| 88 |
client.router = FakeRouter() |
| 89 |
|
| 90 |
|
| 91 |
@virtualized |
| 92 |
def test_execute_command_invalid_op(): |
| 93 |
with Client() as c: |
| 94 |
monkeypatch_router(c, Packet(Op.DEBUG, b"/local" + NUL, rq_id=0)) |
| 95 |
|
| 96 |
with pytest.raises(UnexpectedPacket): |
| 97 |
c.execute_command(Op.READ, b"/local" + NUL) |
| 98 |
|
| 99 |
|
| 100 |
@virtualized |
| 101 |
def test_execute_command_invalid_tx_id(): |
| 102 |
with Client() as c: |
| 103 |
monkeypatch_router(c, Packet(Op.READ, b"/local" + NUL, |
| 104 |
rq_id=0, tx_id=42)) |
| 105 |
|
| 106 |
with pytest.raises(UnexpectedPacket): |
| 107 |
c.execute_command(Op.READ, b"/local" + NUL) |
| 108 |
|
| 109 |
|
| 110 |
@virtualized |
| 111 |
def test_close_idempotent(): |
| 112 |
c = Client() |
| 113 |
c.connect() |
| 114 |
c.close() |
| 115 |
c.close() |
| 116 |
|
| 117 |
|
| 118 |
@pytest.mark.parametrize("op", [ |
| 119 |
"read", "mkdir", "delete", "list", "exists", "get_perms" |
| 120 |
]) |
| 121 |
def test_check_path(op): |
| 122 |
with pytest.raises(InvalidPath): |
| 123 |
getattr(Client(), op)(b"INVALID%PATH!") |
| 124 |
|
| 125 |
|
| 126 |
@pytest.yield_fixture(params=[UnixSocketConnection, XenBusConnection]) |
| 127 |
def client(request): |
| 128 |
with Client(router=Router(request.param())) as c: |
| 129 |
yield c |
| 130 |
|
| 131 |
|
| 132 |
@virtualized |
| 133 |
def test_read(client): |
| 134 |
|
| 135 |
try: |
| 136 |
client.read(b"/foo/bar") |
| 137 |
except PyXSError as e: |
| 138 |
assert e.args[0] == errno.ENOENT |
| 139 |
|
| 140 |
|
| 141 |
client.read(b"/foo/bar", b"baz") == b"baz" |
| 142 |
|
| 143 |
|
| 144 |
assert client.read(b"/local") == b"" |
| 145 |
assert client[b"/local"] == b"" |
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
@virtualized |
| 151 |
def test_write(client): |
| 152 |
client.write(b"/foo/bar", b"baz") |
| 153 |
assert client.read(b"/foo/bar") == b"baz" |
| 154 |
|
| 155 |
client[b"/foo/bar"] = b"boo" |
| 156 |
assert client[b"/foo/bar"] == b"boo" |
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
def test_write_invalid(): |
| 162 |
with pytest.raises(InvalidPath): |
| 163 |
Client().write(b"INVALID%PATH!", b"baz") |
| 164 |
|
| 165 |
|
| 166 |
@virtualized |
| 167 |
def test_mkdir(client): |
| 168 |
client.mkdir(b"/foo/bar") |
| 169 |
assert client.list(b"/foo") == [b"bar"] |
| 170 |
assert client.read(b"/foo/bar") == b"" |
| 171 |
|
| 172 |
|
| 173 |
@virtualized |
| 174 |
def test_delete(client): |
| 175 |
client.mkdir(b"/foo/bar") |
| 176 |
client.delete(b"/foo/bar") |
| 177 |
|
| 178 |
try: |
| 179 |
client.read(b"/foo/bar") |
| 180 |
except PyXSError as e: |
| 181 |
assert e.args[0] == errno.ENOENT |
| 182 |
|
| 183 |
assert client.read(b"/foo") == b"" |
| 184 |
|
| 185 |
|
| 186 |
@virtualized |
| 187 |
def test_list(client): |
| 188 |
client.mkdir(b"/foo/bar") |
| 189 |
|
| 190 |
|
| 191 |
assert client.list(b"/foo") == [b"bar"] |
| 192 |
assert client.list(b"/foo/bar") == [] |
| 193 |
|
| 194 |
|
| 195 |
try: |
| 196 |
client.list(b"/path/to/something") |
| 197 |
except PyXSError as e: |
| 198 |
assert e.args[0] == errno.ENOENT |
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
@virtualized |
| 204 |
def test_exists(client): |
| 205 |
|
| 206 |
client.mkdir(b"/foo/bar") |
| 207 |
assert client.exists(b"/foo/bar") |
| 208 |
|
| 209 |
|
| 210 |
client.delete(b"/foo/bar") |
| 211 |
assert not client.exists(b"/foo/bar") |
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
@virtualized |
| 217 |
def test_perms(client): |
| 218 |
client.delete(b"/foo") |
| 219 |
client.mkdir(b"/foo/bar") |
| 220 |
|
| 221 |
|
| 222 |
assert client.get_perms(b"/foo/bar") == [b"n0"] |
| 223 |
|
| 224 |
|
| 225 |
client.set_perms(b"/foo/bar", [b"b0"]) |
| 226 |
assert client.get_perms(b"/foo/bar") == [b"b0"] |
| 227 |
|
| 228 |
|
| 229 |
client.set_perms(b"/foo/bar", [b"b0", b"n0", b"r0"]) |
| 230 |
assert client.get_perms(b"/foo/bar") == [b"b0", b"n0", b"r0"] |
| 231 |
|
| 232 |
|
| 233 |
with pytest.raises(InvalidPermission): |
| 234 |
client.set_perms(b"/foo/bar", [b"x0"]) |
| 235 |
|
| 236 |
|
| 237 |
def test_set_perms_invalid(): |
| 238 |
with pytest.raises(InvalidPath): |
| 239 |
Client().set_perms(b"INVALID%PATH!", []) |
| 240 |
|
| 241 |
with pytest.raises(InvalidPermission): |
| 242 |
Client().set_perms(b"/foo/bar", [b"z"]) |
| 243 |
|
| 244 |
|
| 245 |
@virtualized |
| 246 |
def test_get_domain_path(client): |
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
assert client.get_domain_path(0) == b"/local/domain/0" |
| 251 |
assert client.get_domain_path(999) == b"/local/domain/999" |
| 252 |
|
| 253 |
|
| 254 |
@virtualized |
| 255 |
def test_is_domain_introduced(client): |
| 256 |
for domid in map(int, client.list(b"/local/domain")): |
| 257 |
assert client.is_domain_introduced(domid) |
| 258 |
|
| 259 |
assert not client.is_domain_introduced(999) |
| 260 |
|
| 261 |
|
| 262 |
@virtualized |
| 263 |
def test_transaction(client): |
| 264 |
assert client.tx_id == 0 |
| 265 |
client.transaction() |
| 266 |
assert client.tx_id != 0 |
| 267 |
|
| 268 |
|
| 269 |
@virtualized |
| 270 |
def test_nested_transaction(client): |
| 271 |
client.transaction() |
| 272 |
|
| 273 |
with pytest.raises(PyXSError): |
| 274 |
client.transaction() |
| 275 |
|
| 276 |
|
| 277 |
@virtualized |
| 278 |
def test_transaction_rollback(client): |
| 279 |
assert not client.exists(b"/foo/bar") |
| 280 |
client.transaction() |
| 281 |
client[b"/foo/bar"] = b"boo" |
| 282 |
client.rollback() |
| 283 |
assert client.tx_id == 0 |
| 284 |
assert not client.exists(b"/foo/bar") |
| 285 |
|
| 286 |
|
| 287 |
@virtualized |
| 288 |
def test_transaction_commit_ok(client): |
| 289 |
assert not client.exists(b"/foo/bar") |
| 290 |
client.transaction() |
| 291 |
client[b"/foo/bar"] = b"boo" |
| 292 |
assert client.commit() |
| 293 |
assert client.tx_id == 0 |
| 294 |
assert client[b"/foo/bar"] == b"boo" |
| 295 |
|
| 296 |
|
| 297 |
@virtualized |
| 298 |
def test_transaction_commit_retry(client): |
| 299 |
def writer(): |
| 300 |
with Client() as other: |
| 301 |
other[b"/foo/bar"] = b"unexpected write" |
| 302 |
|
| 303 |
assert not client.exists(b"/foo/bar") |
| 304 |
client.transaction() |
| 305 |
writer() |
| 306 |
client[b"/foo/bar"] = b"boo" |
| 307 |
assert not client.commit() |
| 308 |
assert client.tx_id == 0 |
| 309 |
|
| 310 |
|
| 311 |
@virtualized |
| 312 |
def test_transaction_exception(): |
| 313 |
try: |
| 314 |
with Client() as c: |
| 315 |
assert not c.exists(b"/foo/bar") |
| 316 |
c.transaction() |
| 317 |
c[b"/foo/bar"] = b"boo" |
| 318 |
raise ValueError |
| 319 |
except ValueError: |
| 320 |
pass |
| 321 |
|
| 322 |
with Client() as c: |
| 323 |
assert not c.exists(b"/foo/bar") |
| 324 |
|
| 325 |
|
| 326 |
@virtualized |
| 327 |
def test_uncommitted_transaction(): |
| 328 |
with pytest.raises(PyXSError): |
| 329 |
with Client() as c: |
| 330 |
c.transaction() |
| 331 |
|
| 332 |
|
| 333 |
def xfail_if_xenbus(client): |
| 334 |
if isinstance(client.router.connection, XenBusConnection): |
| 335 |
|
| 336 |
pytest.xfail("unsupported connection") |
| 337 |
|
| 338 |
|
| 339 |
@virtualized |
| 340 |
def test_monitor(client): |
| 341 |
xfail_if_xenbus(client) |
| 342 |
|
| 343 |
client.write(b"/foo/bar", b"baz") |
| 344 |
m = client.monitor() |
| 345 |
m.watch(b"/foo/bar", b"boo") |
| 346 |
|
| 347 |
waiter = m.wait() |
| 348 |
|
| 349 |
|
| 350 |
assert next(waiter) == (b"/foo/bar", b"boo") |
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
Thread(target=lambda: client.write(b"/foo/bar", b"baz")).start() |
| 355 |
assert next(waiter) == (b"/foo/bar", b"boo") |
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
Thread(target=lambda: client.write(b"/foo/bar/baz", b"???")).start() |
| 360 |
assert next(waiter) == (b"/foo/bar/baz", b"boo") |
| 361 |
|
| 362 |
|
| 363 |
@pytest.mark.parametrize("op", ["watch", "unwatch"]) |
| 364 |
def test_check_watch_path(op): |
| 365 |
with pytest.raises(InvalidPath): |
| 366 |
getattr(Client().monitor(), op)(b"INVALID%PATH", b"token") |
| 367 |
|
| 368 |
with pytest.raises(InvalidPath): |
| 369 |
getattr(Client().monitor(), op)(b"@arbitraryPath", b"token") |
| 370 |
|
| 371 |
|
| 372 |
@virtualized |
| 373 |
def test_monitor_leftover_events(client): |
| 374 |
xfail_if_xenbus(client) |
| 375 |
|
| 376 |
with client.monitor() as m: |
| 377 |
m.watch(b"/foo/bar", b"boo") |
| 378 |
|
| 379 |
def writer(): |
| 380 |
for i in range(128): |
| 381 |
client[b"/foo/bar"] = str(i).encode() |
| 382 |
|
| 383 |
t = Timer(.25, writer) |
| 384 |
t.start() |
| 385 |
m.unwatch(b"/foo/bar", b"boo") |
| 386 |
assert not m.events.empty() |
| 387 |
t.join() |
| 388 |
|
| 389 |
|
| 390 |
@virtualized |
| 391 |
def test_monitor_different_tokens(client): |
| 392 |
xfail_if_xenbus(client) |
| 393 |
|
| 394 |
with client.monitor() as m: |
| 395 |
m.watch(b"/foo/bar", b"boo") |
| 396 |
m.watch(b"/foo/bar", b"baz") |
| 397 |
|
| 398 |
def writer(): |
| 399 |
client[b"/foo/bar"] = str(i).encode() |
| 400 |
|
| 401 |
t = Timer(.25, lambda: client.write(b"/foo/bar", "???")) |
| 402 |
t.start() |
| 403 |
t.join() |
| 404 |
|
| 405 |
events = list(islice(m.wait(), 2)) |
| 406 |
assert len(events) == 2 |
| 407 |
assert set(token for wpath, token in events) == set([b"boo", b"baz"]) |
| 408 |
|
| 409 |
|
| 410 |
class Latch(object): |
| 411 |
def __init__(self, initial): |
| 412 |
self.value = initial |
| 413 |
|
| 414 |
def ready(self): |
| 415 |
self.value -= 1 |
| 416 |
while self.value: |
| 417 |
pass |
| 418 |
|
| 419 |
|
| 420 |
@virtualized |
| 421 |
def test_multiple_monitors(client): |
| 422 |
xfail_if_xenbus(client) |
| 423 |
|
| 424 |
n_events = 5 |
| 425 |
events = {} |
| 426 |
|
| 427 |
latch = Latch(2 + 1) |
| 428 |
|
| 429 |
def monitor_and_check(token): |
| 430 |
with client.monitor() as m: |
| 431 |
m.watch(b"/foo/bar", token) |
| 432 |
latch.ready() |
| 433 |
events[current_thread().ident] = list(islice(m.wait(), n_events)) |
| 434 |
|
| 435 |
t1 = Thread(target=monitor_and_check, args=(b"boo", )) |
| 436 |
t1.start() |
| 437 |
t2 = Thread(target=monitor_and_check, args=(b"baz", )) |
| 438 |
t2.start() |
| 439 |
|
| 440 |
latch.ready() |
| 441 |
for i in range(n_events): |
| 442 |
client[b"/foo/bar"] = str(i).encode() |
| 443 |
|
| 444 |
t1.join() |
| 445 |
t2.join() |
| 446 |
|
| 447 |
assert len(events) == 2 |
| 448 |
events1 = events[t1.ident] |
| 449 |
events2 = events[t2.ident] |
| 450 |
assert len(events1) == len(events2) == n_events |
| 451 |
assert set(events1) == set([Event(b"/foo/bar", b"boo")]) |
| 452 |
assert set(events2) == set([Event(b"/foo/bar", b"baz")]) |
| 453 |
|
| 454 |
|
| 455 |
@virtualized |
| 456 |
def test_header_decode_error(client): |
| 457 |
|
| 458 |
|
| 459 |
p = Packet(Op.WRITE, b"/foo", rq_id=0, tx_id=128) |
| 460 |
client.router.send(p) |