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 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610
|
import abc
import re
import attr
import pytest
from labgrid import Target, target_factory
from labgrid.binding import BindingError
from labgrid.resource import Resource
from labgrid.driver import Driver
from labgrid.strategy import Strategy
from labgrid.exceptions import NoSupplierFoundError, NoDriverFoundError, NoResourceFoundError, NoStrategyFoundError
# test basic construction
def test_instanziation():
t = Target("name")
assert (isinstance(t, Target))
def test_get_resource(target):
class A(Resource):
pass
a = A(target, "aresource")
assert isinstance(target.get_resource(A), A)
assert target.get_resource(A) is a
assert target.get_resource(A, name="aresource") is a
# make sure resources named "default" are prioritized
b = A(target, "default")
assert target.get_resource(A) is b
assert target.get_resource(A, name="aresource") is a
def test_get_resource_multiple_no_default(target):
class A(Resource):
pass
a = A(target, "aresource")
b = A(target, "default")
with pytest.raises(NoResourceFoundError) as excinfo:
target.get_resource(A, name="nosuchresource")
def test_get_resource_multiple_with_default(target):
class A(Resource):
pass
class B(Resource):
pass
a = A(target, "aresource")
adef = A(target, "default")
b = B(target, "bresource")
bdef = B(target, "default")
assert target.get_resource(A) is adef
assert target.get_resource(B) is bdef
assert target.get_resource(A, name="aresource") is a
assert target.get_resource(B, name="bresource") is b
def test_get_driver(target):
class A(Driver):
pass
a = A(target, "adriver")
assert isinstance(target.get_driver(A), A)
assert target.get_driver(A) is a
assert target.get_driver(A, name="adriver") is a
def test_getitem(target):
class AProtocol(abc.ABC):
pass
class A(Driver, AProtocol):
pass
class B(Driver):
pass
a = A(target, "adriver")
target.activate(a)
assert isinstance(target[A], A)
assert target[A] is a
assert target[AProtocol] is a
assert target[A, "adriver"] is a
assert target[AProtocol, "adriver"] is a
with pytest.raises(NoDriverFoundError) as excinfo:
target[A, "bdriver"]
assert "matching resources with other names" in excinfo.value.msg
with pytest.raises(NoDriverFoundError) as excinfo:
target[B, "adriver"]
assert re.match(f"no active .*? driver named '{a.name}' found in Target",
excinfo.value.msg)
a2 = A(target, None)
target.activate(a2)
with pytest.raises(NoDriverFoundError) as excinfo:
target[A]
assert "multiple active drivers" in excinfo.value.msg
def test_no_resource(target):
with pytest.raises(NoResourceFoundError):
target.get_resource(Target)
def test_no_driver(target):
with pytest.raises(NoDriverFoundError):
target.get_driver(Target)
# test alternative suppliers
class ResourceA(Resource):
pass
class ResourceB(Resource):
pass
class DriverWithA(Driver):
bindings = {"res": ResourceA}
class DriverWithASet(Driver):
bindings = {"res": {ResourceA}, }
class DriverWithAB(Driver):
bindings = {"res": {ResourceA, ResourceB}, }
def test_suppliers_a(target):
ra = ResourceA(target, "resource")
d = DriverWithA(target, "resource")
assert d.res is ra
def test_suppliers_aset(target):
ra = ResourceA(target, "resource")
d = DriverWithASet(target, "driver")
assert d.res is ra
def test_suppliers_ab_a(target):
ra = ResourceA(target, "resource")
d = DriverWithAB(target, "driver")
assert d.res is ra
def test_suppliers_ab_b(target):
rb = ResourceB(target, "resource")
d = DriverWithAB(target, "driver")
assert d.res is rb
def test_suppliers_ab_both(target):
ra = ResourceA(target, "resource_a")
rb = ResourceB(target, "resource_b")
with pytest.raises(NoSupplierFoundError):
d = DriverWithAB(target, "driver")
def test_suppliers_ab_missing(target):
with pytest.raises(NoSupplierFoundError):
d = DriverWithAB(target, "driver")
def test_suppliers_unexpected_binding(target):
ra = ResourceA(target, "resource")
target.set_binding_map({"res": "resource", "unexpected": "foo"})
with pytest.raises(BindingError) as excinfo:
DriverWithA(target, "driver")
assert "got unexpected bindings" in excinfo.value.msg
class DriverWithNamedA(Driver):
bindings = {
"res": Driver.NamedBinding(ResourceA),
}
def test_suppliers_named_a(target):
ra = ResourceA(target, "resource")
target.set_binding_map({"res": "resource"})
d = DriverWithNamedA(target, "driver")
assert d.res is ra
class DriverWithMultiA(Driver):
bindings = {
"res1": ResourceA,
"res2": ResourceA,
}
def test_suppliers_multi_a(target):
ra1 = ResourceA(target, "resource1")
with pytest.raises(BindingError) as excinfo:
DriverWithMultiA(target, "driver")
assert "duplicate bindings" in excinfo.value.msg
def test_suppliers_multi_a_explict(target):
ra1 = ResourceA(target, "resource1")
ra2 = ResourceA(target, "resource2")
target.set_binding_map({
"res1": "resource1",
"res2": "resource2",
})
d = DriverWithMultiA(target, "driver")
assert d.res1 is ra1
assert d.res2 is ra2
class DriverWithNamedMultiA(Driver):
bindings = {
"res1": Driver.NamedBinding(ResourceA),
"res2": Driver.NamedBinding(ResourceA),
}
def test_suppliers_multi_named_a(target):
ra1 = ResourceA(target, "resource1")
ra2 = ResourceA(target, "resource2")
target.set_binding_map({
"res1": "resource1",
"res2": "resource2",
})
d = DriverWithNamedMultiA(target, "driver")
assert d.res1 is ra1
assert d.res2 is ra2
# test optional bindings
class DriverWithOptionalA(Driver):
bindings = {"res": {ResourceA, None}, }
class DriverWithOptionalAB(Driver):
bindings = {"res": {ResourceA, ResourceB, None}, }
def test_suppliers_optional_a(target):
ra = ResourceA(target, "resource")
d = DriverWithOptionalA(target, "driver")
assert d.res is ra
def test_suppliers_optional_a_missing(target):
rb = ResourceB(target, "resource")
d = DriverWithOptionalA(target, "driver")
assert d.res is None
def test_suppliers_optional_ab_a(target):
ra = ResourceA(target, "resource")
d = DriverWithOptionalAB(target, "driver")
assert d.res is ra
class DriverWithOptionalNamedA(Driver):
bindings = {
"res": Driver.NamedBinding({ResourceA, None}),
}
def test_suppliers_optional_named_a(target):
ra = ResourceA(target, "resource")
target.set_binding_map({"res": "resource"})
d = DriverWithOptionalNamedA(target, "driver")
assert d.res is ra
def test_suppliers_optional_named_a_missing(target):
rb = ResourceB(target, "resource")
target.set_binding_map({"res": "resource"})
d = DriverWithOptionalNamedA(target, "driver")
assert d.res is None
class StrategyA(Strategy):
bindings = {
"drv": DriverWithA,
}
def test_get_strategy(target):
ra = ResourceA(target, "resource")
d = DriverWithA(target, "driver")
with pytest.raises(NoStrategyFoundError):
target.get_strategy()
s1 = StrategyA(target, "s1")
assert target.get_strategy() is s1
s2 = StrategyA(target, "s2")
with pytest.raises(NoStrategyFoundError):
target.get_strategy()
# test nested resource creation
@attr.s(eq=False)
class DiscoveryResource(Resource):
def __attrs_post_init__(self):
super().__attrs_post_init__()
ResourceA(self.target, "resource")
def test_nested(target):
rd = DiscoveryResource(target, "discovery")
d = DriverWithAB(target, "driver")
assert isinstance(d.res, ResourceA)
# Test retrieving drivers, resources and protocols by name
def test_get_by_string(target):
class AProtocol(abc.ABC):
pass
@target_factory.reg_driver
class A(Driver, AProtocol):
pass
@target_factory.reg_driver
class C(Resource):
pass
a = A(target, None)
target.activate(a)
assert target.get_driver('A') == a
assert target.get_active_driver('A') == a
c = C(target, None)
assert target.get_resource('C') == c
assert target['AProtocol'] == a
with pytest.raises(KeyError):
target.get_driver("nosuchdriver")
# Test priorities
def test_get_by_diff_priority(target):
class AProtocol(abc.ABC):
pass
@attr.s
class A(Driver, AProtocol):
priorities = {AProtocol: -10}
@attr.s
class C(Driver, AProtocol):
priorities = {AProtocol: 10}
pass
a = A(target, None)
c = C(target, None)
target.activate(a)
target.activate(c)
assert target.get_driver(AProtocol) == c
def test_get_by_same_priority(target):
class AProtocol(abc.ABC):
pass
@attr.s
class A(Driver, AProtocol):
priorities = {AProtocol: 10}
@attr.s
class C(Driver, AProtocol):
priorities = {AProtocol: 10}
pass
a = A(target, None)
c = C(target, None)
target.activate(a)
target.activate(c)
with pytest.raises(NoDriverFoundError) as e_info:
target.get_driver(AProtocol)
assert "multiple drivers matching" in str(e_info.value)
def test_get_by_default_priority(target):
class AProtocol(abc.ABC):
pass
@attr.s
class A(Driver, AProtocol):
pass
@attr.s
class C(Driver, AProtocol):
pass
a = A(target, None)
c = C(target, None)
target.activate(a)
target.activate(c)
with pytest.raises(NoDriverFoundError) as e_info:
target.get_driver(AProtocol)
assert "multiple drivers matching" in str(e_info.value)
def test_target_deactivate_by_string(target):
@target_factory.reg_driver
@attr.s
class ADea(Driver):
pass
a = ADea(target, None)
target.activate(a)
target.deactivate("ADea")
assert a == target.get_driver(ADea, activate=False)
def test_target_activate_by_string(target):
@target_factory.reg_driver
@attr.s
class AActiv(Driver):
pass
a = AActiv(target, None)
target.activate("AActiv")
assert a == target.get_active_driver(AActiv)
def test_allow_binding_by_different_protocols(target):
class ADiffProtocol(abc.ABC):
pass
class BDiffProtocol(abc.ABC):
pass
class DiffDriver(Driver, ADiffProtocol, BDiffProtocol):
pass
class DiffStrategy(Strategy):
bindings = {
"a": ADiffProtocol,
"b": BDiffProtocol
}
d = DiffDriver(target, None)
s = DiffStrategy(target, None)
assert s.a == d
assert s.b == d
def test_allow_optional_binding_by_different_protocols(target):
class AOpt1DiffProtocol(abc.ABC):
pass
class BOpt1DiffProtocol(abc.ABC):
pass
class Opt1DiffDriver(Driver, AOpt1DiffProtocol):
pass
class Opt1DiffStrategy(Strategy):
bindings = {
"a": AOpt1DiffProtocol,
"b": {BOpt1DiffProtocol, None}
}
d = Opt1DiffDriver(target, None)
s = Opt1DiffStrategy(target, None)
assert s.a == d
assert s.b == None
def test_allow_optional_available_binding_by_different_protocols(target):
class AOpt2DiffProtocol(abc.ABC):
pass
class BOpt2DiffProtocol(abc.ABC):
pass
class Opt2DiffDriver(Driver, AOpt2DiffProtocol, BOpt2DiffProtocol):
pass
class Opt2DiffStrategy(Strategy):
bindings = {
"a": {AOpt2DiffProtocol, None},
"b": {BOpt2DiffProtocol, None}
}
d = Opt2DiffDriver(target, None)
s = Opt2DiffStrategy(target, None)
assert s.a == d
assert s.b == d
def test_allow_optional_no_available_binding_by_different_protocols(target):
class AOpt3DiffProtocol(abc.ABC):
pass
class BOpt3DiffProtocol(abc.ABC):
pass
class Opt3DiffDriver(Driver):
pass
class Opt3DiffStrategy(Strategy):
bindings = {
"a": {AOpt3DiffProtocol, None},
"b": {BOpt3DiffProtocol, None}
}
d = Opt3DiffDriver(target, None)
s = Opt3DiffStrategy(target, None)
assert s.a == None
assert s.b == None
def test_allow_optional_no_double_same_protocol_by_different_protocols(target):
class AOpt4DiffProtocol(abc.ABC):
pass
class Opt4DiffDriver(Driver, AOpt4DiffProtocol):
pass
class Opt4DiffStrategy(Strategy):
bindings = {
"a": {AOpt4DiffProtocol, None},
"b": {AOpt4DiffProtocol, None},
"c": {AOpt4DiffProtocol, None},
"d": {AOpt4DiffProtocol, None},
}
d1 = Opt4DiffDriver(target, name="driver1")
d2 = Opt4DiffDriver(target, name="driver2")
target.set_binding_map({"c": "driver1", "d": "driver2"})
s = Opt4DiffStrategy(target, None)
assert s.a is None
assert s.b is None
assert s.c == d1
assert s.d == d2
def test_get_bound_resources(target):
class AResource(Resource):
pass
class ADriver(Driver):
bindings = {
"a": AResource,
}
class BDriver(Driver):
bindings = {
"a": ADriver,
}
aresource = AResource(target, "aresource")
adriver = ADriver(target, "adriver")
bdriver = BDriver(target, "bdriver")
assert bdriver.get_bound_resources() == {aresource}
assert adriver.get_bound_resources() == {aresource}
assert target.get_driver(ADriver, resource=aresource)
def test_get_bound_multiple_resources(target):
class AResource(Resource):
pass
class BResource(Resource):
pass
class ADriver(Driver):
bindings = {
"a": AResource,
}
class BDriver(Driver):
bindings = {
"b": BResource,
}
class CDriver(Driver):
bindings = {
"a": ADriver,
"b": BDriver,
}
aresource = AResource(target, "aresource")
bresource = BResource(target, "bresource")
adriver = ADriver(target, "adriver")
bdriver = BDriver(target, "bdriver")
cdriver = CDriver(target, "bdriver")
assert adriver.get_bound_resources() == {aresource}
assert bdriver.get_bound_resources() == {bresource}
assert cdriver.get_bound_resources() == {aresource, bresource}
assert target.get_driver(ADriver, resource=aresource)
assert target.get_driver(BDriver, resource=bresource)
assert target.get_driver(CDriver, resource=aresource)
assert target.get_active_driver(ADriver, resource=aresource)
assert target.get_active_driver(BDriver, resource=bresource)
assert target.get_active_driver(CDriver, resource=aresource)
|