From: Christos Sfakianakis <csfakian(a)redhat.com>
Define a custom __instancecheck__ method that does special handling
for RemoteDevice instances defined on the controller. For such instances,
route the check via the _dev_cls attribute of the instance. For all
other cases, the handling remains the same as before.
The result is the ability to differentiate between physical, soft and virtual
devices on the tester's side. A use case follows.
#==============================================================
#Controller
#==============================================================
from lnst.Devices import VlanDevice
from lnst.Devices.Device import Device as Dev
from lnst.Devices.VlanDevice import VlanDevice as VlanDev
from lnst.Devices.BridgeDevice import BridgeDevice as BridgeDev
from lnst.Devices.SoftDevice import SoftDevice as SoftDev
from lnst.Devices.VirtualDevice import VirtualDevice as VirtDev
...
host1.eth0 = DeviceReq(label="net1",
driver=RecipeParam("driver"))
host1.vlan0 = VlanDevice(realdev=host1.eth0, vlan_id=10)
x = host1.vlan0
# Class hierarchy: Device >> SoftDevice >> VlanDevice
# Device >> SoftDevice >> MasterDevice >>
BridgeDevice
# Before | After
#------------------
isinstance(x, Dev) # True | True
isinstance(x, SoftDev) # False | True
isinstance(x, VlanDev) # False | True
isinstance(x, BridgeDev) # False | False
isinstance(host1.eth0, VirtDev) # True | True
isinstance(x, VirtDev) # False | False
# |
#====================================================|=======
#Slave |
#====================================================|=======
isinstance(dev, Dev) # True | True
isinstance(dev, SoftDev) # True | True
isinstance(dev, VlanDev) # True | True
isinstance(dev, BridgeDev) # False | False
The result on the slave hasn't changed. The result on the controller
is now in agreement with that on the slave.
Signed-off-by: Christos Sfakianakis <csfakian(a)redhat.com>
---
lnst/Devices/Device.py | 9 ++++++++-
1 file changed, 8 insertions(+), 1 deletion(-)
diff --git a/lnst/Devices/Device.py b/lnst/Devices/Device.py
index 30deb25..39d1dd2 100644
--- a/lnst/Devices/Device.py
+++ b/lnst/Devices/Device.py
@@ -30,7 +30,14 @@ from pyroute2.netlink.rtnl import RTM_NEWLINK
from pyroute2.netlink.rtnl import RTM_NEWADDR
from pyroute2.netlink.rtnl import RTM_DELADDR
-class Device(object, metaclass=ABCMeta):
+class DeviceMeta(ABCMeta):
+ def __instancecheck__(self, other):
+ try:
+ return issubclass(other._dev_cls, self)
+ except AttributeError:
+ return super(DeviceMeta, self).__instancecheck__(other)
+
+class Device(object, metaclass=DeviceMeta):
"""The base Device class
Implemented using the pyroute2 package to access different attributes of
--
2.17.1
_______________________________________________
LNST-developers mailing list -- lnst-developers(a)lists.fedorahosted.org
To unsubscribe send an email to lnst-developers-leave(a)lists.fedorahosted.org
Fedora Code of Conduct:
https://getfedora.org/code-of-conduct.html
List Guidelines:
https://fedoraproject.org/wiki/Mailing_list_guidelines
List Archives:
https://lists.fedorahosted.org/archives/list/lnst-developers@lists.fedora...