Renato Botelho do Couto | ead1e53 | 2019-10-31 13:31:07 -0500 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Jakub Grajciar | b1be2a0 | 2018-09-19 13:36:16 +0200 | [diff] [blame] | 2 | |
Paul Vinciguerra | 00671cf | 2018-11-25 12:47:04 -0800 | [diff] [blame] | 3 | import inspect |
Jakub Grajciar | b1be2a0 | 2018-09-19 13:36:16 +0200 | [diff] [blame] | 4 | import os |
Paul Vinciguerra | 090096b | 2020-12-03 00:42:46 -0500 | [diff] [blame] | 5 | import reprlib |
Jakub Grajciar | b1be2a0 | 2018-09-19 13:36:16 +0200 | [diff] [blame] | 6 | import unittest |
Dave Wallace | 8800f73 | 2023-08-31 00:47:44 -0400 | [diff] [blame] | 7 | from framework import VppTestCase |
Jakub Grajciar | b1be2a0 | 2018-09-19 13:36:16 +0200 | [diff] [blame] | 8 | from multiprocessing import Process, Pipe |
Paul Vinciguerra | 00671cf | 2018-11-25 12:47:04 -0800 | [diff] [blame] | 9 | from pickle import dumps |
snaramre | d6df3ac | 2019-08-29 18:00:26 +0000 | [diff] [blame] | 10 | |
Paul Vinciguerra | 27860dd | 2020-12-03 00:46:03 -0500 | [diff] [blame] | 11 | from enum import IntEnum, IntFlag |
Jakub Grajciar | b1be2a0 | 2018-09-19 13:36:16 +0200 | [diff] [blame] | 12 | |
| 13 | |
Paul Vinciguerra | e061dad | 2020-12-04 14:57:51 -0500 | [diff] [blame] | 14 | class SerializableClassCopy: |
Jakub Grajciar | b1be2a0 | 2018-09-19 13:36:16 +0200 | [diff] [blame] | 15 | """ |
| 16 | Empty class used as a basis for a serializable copy of another class. |
| 17 | """ |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 18 | |
Jakub Grajciar | b1be2a0 | 2018-09-19 13:36:16 +0200 | [diff] [blame] | 19 | pass |
| 20 | |
Neale Ranns | 097fa66 | 2018-05-01 05:17:55 -0700 | [diff] [blame] | 21 | def __repr__(self): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 22 | return "<SerializableClassCopy dict=%s>" % self.__dict__ |
Neale Ranns | 097fa66 | 2018-05-01 05:17:55 -0700 | [diff] [blame] | 23 | |
Jakub Grajciar | b1be2a0 | 2018-09-19 13:36:16 +0200 | [diff] [blame] | 24 | |
Paul Vinciguerra | e061dad | 2020-12-04 14:57:51 -0500 | [diff] [blame] | 25 | class RemoteClassAttr: |
Jakub Grajciar | b1be2a0 | 2018-09-19 13:36:16 +0200 | [diff] [blame] | 26 | """ |
| 27 | Wrapper around attribute of a remotely executed class. |
| 28 | """ |
| 29 | |
| 30 | def __init__(self, remote, attr): |
| 31 | self._path = [attr] if attr else [] |
| 32 | self._remote = remote |
| 33 | |
| 34 | def path_to_str(self): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 35 | return ".".join(self._path) |
Jakub Grajciar | b1be2a0 | 2018-09-19 13:36:16 +0200 | [diff] [blame] | 36 | |
| 37 | def get_remote_value(self): |
| 38 | return self._remote._remote_exec(RemoteClass.GET, self.path_to_str()) |
| 39 | |
| 40 | def __repr__(self): |
| 41 | return self._remote._remote_exec(RemoteClass.REPR, self.path_to_str()) |
| 42 | |
| 43 | def __str__(self): |
| 44 | return self._remote._remote_exec(RemoteClass.STR, self.path_to_str()) |
| 45 | |
| 46 | def __getattr__(self, attr): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 47 | if attr[0] == "_": |
| 48 | if not (attr.startswith("__") and attr.endswith("__")): |
| 49 | raise AttributeError("tried to get private attribute: %s ", attr) |
Jakub Grajciar | b1be2a0 | 2018-09-19 13:36:16 +0200 | [diff] [blame] | 50 | self._path.append(attr) |
| 51 | return self |
| 52 | |
| 53 | def __setattr__(self, attr, val): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 54 | if attr[0] == "_": |
| 55 | if not (attr.startswith("__") and attr.endswith("__")): |
Paul Vinciguerra | d3a9be2 | 2019-03-10 07:03:22 -0700 | [diff] [blame] | 56 | super(RemoteClassAttr, self).__setattr__(attr, val) |
| 57 | return |
Jakub Grajciar | b1be2a0 | 2018-09-19 13:36:16 +0200 | [diff] [blame] | 58 | self._path.append(attr) |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 59 | self._remote._remote_exec(RemoteClass.SETATTR, self.path_to_str(), value=val) |
Jakub Grajciar | b1be2a0 | 2018-09-19 13:36:16 +0200 | [diff] [blame] | 60 | |
| 61 | def __call__(self, *args, **kwargs): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 62 | return self._remote._remote_exec( |
| 63 | RemoteClass.CALL, self.path_to_str(), *args, **kwargs |
| 64 | ) |
Jakub Grajciar | b1be2a0 | 2018-09-19 13:36:16 +0200 | [diff] [blame] | 65 | |
| 66 | |
| 67 | class RemoteClass(Process): |
| 68 | """ |
| 69 | This class can wrap around and adapt the interface of another class, |
| 70 | and then delegate its execution to a newly forked child process. |
Dave Wallace | d170681 | 2021-08-12 18:36:02 -0400 | [diff] [blame] | 71 | |
Jakub Grajciar | b1be2a0 | 2018-09-19 13:36:16 +0200 | [diff] [blame] | 72 | Usage: |
Dave Wallace | d170681 | 2021-08-12 18:36:02 -0400 | [diff] [blame] | 73 | |
| 74 | #. Create a remotely executed instance of MyClass. :: |
| 75 | |
| 76 | object = RemoteClass(MyClass, arg1='foo', arg2='bar') |
| 77 | object.start_remote() |
| 78 | |
| 79 | #. Access the object normally as if it was an instance of your |
| 80 | class. :: |
| 81 | |
| 82 | object.my_attribute = 20 |
| 83 | print object.my_attribute |
| 84 | print object.my_method(object.my_attribute) |
| 85 | object.my_attribute.nested_attribute = 'test' |
| 86 | |
| 87 | #. If you need the value of a remote attribute, use .get_remote_value |
| 88 | method. This method is automatically called when needed in the |
| 89 | context of a remotely executed class. E.g. :: |
| 90 | |
| 91 | if (object.my_attribute.get_remote_value() > 20): |
| 92 | object.my_attribute2 = object.my_attribute |
| 93 | |
| 94 | #. Destroy the instance. :: |
| 95 | |
| 96 | object.quit_remote() |
| 97 | object.terminate() |
Jakub Grajciar | b1be2a0 | 2018-09-19 13:36:16 +0200 | [diff] [blame] | 98 | """ |
| 99 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 100 | GET = 0 # Get attribute remotely |
| 101 | CALL = 1 # Call method remotely |
| 102 | SETATTR = 2 # Set attribute remotely |
| 103 | REPR = 3 # Get representation of a remote object |
| 104 | STR = 4 # Get string representation of a remote object |
| 105 | QUIT = 5 # Quit remote execution |
Jakub Grajciar | b1be2a0 | 2018-09-19 13:36:16 +0200 | [diff] [blame] | 106 | |
| 107 | PIPE_PARENT = 0 # Parent end of the pipe |
| 108 | PIPE_CHILD = 1 # Child end of the pipe |
| 109 | |
| 110 | DEFAULT_TIMEOUT = 2 # default timeout for an operation to execute |
| 111 | |
| 112 | def __init__(self, cls, *args, **kwargs): |
| 113 | super(RemoteClass, self).__init__() |
| 114 | self._cls = cls |
| 115 | self._args = args |
| 116 | self._kwargs = kwargs |
| 117 | self._timeout = RemoteClass.DEFAULT_TIMEOUT |
| 118 | self._pipe = Pipe() # pipe for input/output arguments |
| 119 | |
| 120 | def __repr__(self): |
Paul Vinciguerra | 090096b | 2020-12-03 00:42:46 -0500 | [diff] [blame] | 121 | return reprlib.repr(RemoteClassAttr(self, None)) |
Jakub Grajciar | b1be2a0 | 2018-09-19 13:36:16 +0200 | [diff] [blame] | 122 | |
| 123 | def __str__(self): |
| 124 | return str(RemoteClassAttr(self, None)) |
| 125 | |
| 126 | def __call__(self, *args, **kwargs): |
| 127 | return self.RemoteClassAttr(self, None)() |
| 128 | |
| 129 | def __getattr__(self, attr): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 130 | if attr[0] == "_" or not self.is_alive(): |
| 131 | if not (attr.startswith("__") and attr.endswith("__")): |
| 132 | if hasattr(super(RemoteClass, self), "__getattr__"): |
Paul Vinciguerra | d3a9be2 | 2019-03-10 07:03:22 -0700 | [diff] [blame] | 133 | return super(RemoteClass, self).__getattr__(attr) |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 134 | raise AttributeError("missing: %s", attr) |
Jakub Grajciar | b1be2a0 | 2018-09-19 13:36:16 +0200 | [diff] [blame] | 135 | return RemoteClassAttr(self, attr) |
| 136 | |
| 137 | def __setattr__(self, attr, val): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 138 | if attr[0] == "_" or not self.is_alive(): |
| 139 | if not (attr.startswith("__") and attr.endswith("__")): |
Paul Vinciguerra | d3a9be2 | 2019-03-10 07:03:22 -0700 | [diff] [blame] | 140 | super(RemoteClass, self).__setattr__(attr, val) |
| 141 | return |
Jakub Grajciar | b1be2a0 | 2018-09-19 13:36:16 +0200 | [diff] [blame] | 142 | setattr(RemoteClassAttr(self, None), attr, val) |
| 143 | |
Jakub Grajciar | 7db35de | 2019-06-25 10:22:11 +0200 | [diff] [blame] | 144 | def _remote_exec(self, op, path=None, *args, **kwargs): |
Jakub Grajciar | b1be2a0 | 2018-09-19 13:36:16 +0200 | [diff] [blame] | 145 | """ |
| 146 | Execute given operation on a given, possibly nested, member remotely. |
| 147 | """ |
| 148 | # automatically resolve remote objects in the arguments |
| 149 | mutable_args = list(args) |
| 150 | for i, val in enumerate(mutable_args): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 151 | if isinstance(val, RemoteClass) or isinstance(val, RemoteClassAttr): |
Jakub Grajciar | b1be2a0 | 2018-09-19 13:36:16 +0200 | [diff] [blame] | 152 | mutable_args[i] = val.get_remote_value() |
| 153 | args = tuple(mutable_args) |
Paul Vinciguerra | 090096b | 2020-12-03 00:42:46 -0500 | [diff] [blame] | 154 | for key, val in kwargs.items(): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 155 | if isinstance(val, RemoteClass) or isinstance(val, RemoteClassAttr): |
Jakub Grajciar | b1be2a0 | 2018-09-19 13:36:16 +0200 | [diff] [blame] | 156 | kwargs[key] = val.get_remote_value() |
| 157 | # send request |
| 158 | args = self._make_serializable(args) |
| 159 | kwargs = self._make_serializable(kwargs) |
| 160 | self._pipe[RemoteClass.PIPE_PARENT].send((op, path, args, kwargs)) |
Jakub Grajciar | b1be2a0 | 2018-09-19 13:36:16 +0200 | [diff] [blame] | 161 | timeout = self._timeout |
| 162 | # adjust timeout specifically for the .sleep method |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 163 | if path is not None and path.split(".")[-1] == "sleep": |
Jakub Grajciar | b1be2a0 | 2018-09-19 13:36:16 +0200 | [diff] [blame] | 164 | if args and isinstance(args[0], (long, int)): |
| 165 | timeout += args[0] |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 166 | elif "timeout" in kwargs: |
| 167 | timeout += kwargs["timeout"] |
Jakub Grajciar | b1be2a0 | 2018-09-19 13:36:16 +0200 | [diff] [blame] | 168 | if not self._pipe[RemoteClass.PIPE_PARENT].poll(timeout): |
| 169 | return None |
| 170 | try: |
| 171 | rv = self._pipe[RemoteClass.PIPE_PARENT].recv() |
| 172 | rv = self._deserialize(rv) |
| 173 | return rv |
| 174 | except EOFError: |
| 175 | return None |
| 176 | |
| 177 | def _get_local_object(self, path): |
| 178 | """ |
| 179 | Follow the path to obtain a reference on the addressed nested attribute |
| 180 | """ |
| 181 | obj = self._instance |
| 182 | for attr in path: |
| 183 | obj = getattr(obj, attr) |
| 184 | return obj |
| 185 | |
| 186 | def _get_local_value(self, path): |
| 187 | try: |
| 188 | return self._get_local_object(path) |
| 189 | except AttributeError: |
| 190 | return None |
| 191 | |
| 192 | def _call_local_method(self, path, *args, **kwargs): |
| 193 | try: |
| 194 | method = self._get_local_object(path) |
| 195 | return method(*args, **kwargs) |
| 196 | except AttributeError: |
| 197 | return None |
| 198 | |
| 199 | def _set_local_attr(self, path, value): |
| 200 | try: |
| 201 | obj = self._get_local_object(path[:-1]) |
| 202 | setattr(obj, path[-1], value) |
| 203 | except AttributeError: |
| 204 | pass |
| 205 | return None |
| 206 | |
| 207 | def _get_local_repr(self, path): |
| 208 | try: |
| 209 | obj = self._get_local_object(path) |
Paul Vinciguerra | 090096b | 2020-12-03 00:42:46 -0500 | [diff] [blame] | 210 | return reprlib.repr(obj) |
Jakub Grajciar | b1be2a0 | 2018-09-19 13:36:16 +0200 | [diff] [blame] | 211 | except AttributeError: |
| 212 | return None |
| 213 | |
| 214 | def _get_local_str(self, path): |
| 215 | try: |
| 216 | obj = self._get_local_object(path) |
| 217 | return str(obj) |
| 218 | except AttributeError: |
| 219 | return None |
| 220 | |
| 221 | def _serializable(self, obj): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 222 | """Test if the given object is serializable""" |
Jakub Grajciar | b1be2a0 | 2018-09-19 13:36:16 +0200 | [diff] [blame] | 223 | try: |
| 224 | dumps(obj) |
| 225 | return True |
| 226 | except: |
| 227 | return False |
| 228 | |
| 229 | def _make_obj_serializable(self, obj): |
| 230 | """ |
| 231 | Make a serializable copy of an object. |
| 232 | Members which are difficult/impossible to serialize are stripped. |
| 233 | """ |
| 234 | if self._serializable(obj): |
| 235 | return obj # already serializable |
Jakub Grajciar | 0b1f8a7 | 2019-02-21 12:01:31 +0100 | [diff] [blame] | 236 | |
Jakub Grajciar | b1be2a0 | 2018-09-19 13:36:16 +0200 | [diff] [blame] | 237 | copy = SerializableClassCopy() |
Jakub Grajciar | 0b1f8a7 | 2019-02-21 12:01:31 +0100 | [diff] [blame] | 238 | |
| 239 | """ |
| 240 | Dictionaries can hold complex values, so we split keys and values into |
| 241 | separate lists and serialize them individually. |
| 242 | """ |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 243 | if type(obj) is dict: |
Jakub Grajciar | 0b1f8a7 | 2019-02-21 12:01:31 +0100 | [diff] [blame] | 244 | copy.type = type(obj) |
| 245 | copy.k_list = list() |
| 246 | copy.v_list = list() |
| 247 | for k, v in obj.items(): |
| 248 | copy.k_list.append(self._make_serializable(k)) |
| 249 | copy.v_list.append(self._make_serializable(v)) |
| 250 | return copy |
| 251 | |
Jakub Grajciar | b1be2a0 | 2018-09-19 13:36:16 +0200 | [diff] [blame] | 252 | # copy at least serializable attributes and properties |
| 253 | for name, member in inspect.getmembers(obj): |
Neale Ranns | 097fa66 | 2018-05-01 05:17:55 -0700 | [diff] [blame] | 254 | # skip private members and non-writable dunder methods. |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 255 | if name[0] == "_": |
| 256 | if name in ["__weakref__"]: |
Neale Ranns | 097fa66 | 2018-05-01 05:17:55 -0700 | [diff] [blame] | 257 | continue |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 258 | if name in ["__dict__"]: |
Jakub Grajciar | 546f955 | 2019-08-21 10:51:21 +0200 | [diff] [blame] | 259 | continue |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 260 | if not (name.startswith("__") and name.endswith("__")): |
Paul Vinciguerra | d3a9be2 | 2019-03-10 07:03:22 -0700 | [diff] [blame] | 261 | continue |
Jakub Grajciar | b1be2a0 | 2018-09-19 13:36:16 +0200 | [diff] [blame] | 262 | if callable(member) and not isinstance(member, property): |
| 263 | continue |
| 264 | if not self._serializable(member): |
Jakub Grajciar | 546f955 | 2019-08-21 10:51:21 +0200 | [diff] [blame] | 265 | member = self._make_serializable(member) |
Jakub Grajciar | b1be2a0 | 2018-09-19 13:36:16 +0200 | [diff] [blame] | 266 | setattr(copy, name, member) |
| 267 | return copy |
| 268 | |
| 269 | def _make_serializable(self, obj): |
| 270 | """ |
| 271 | Make a serializable copy of an object or a list/tuple of objects. |
| 272 | Members which are difficult/impossible to serialize are stripped. |
| 273 | """ |
| 274 | if (type(obj) is list) or (type(obj) is tuple): |
| 275 | rv = [] |
| 276 | for item in obj: |
| 277 | rv.append(self._make_serializable(item)) |
| 278 | if type(obj) is tuple: |
| 279 | rv = tuple(rv) |
| 280 | return rv |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 281 | elif isinstance(obj, IntEnum) or isinstance(obj, IntFlag): |
Jakub Grajciar | 0b1f8a7 | 2019-02-21 12:01:31 +0100 | [diff] [blame] | 282 | return obj.value |
Jakub Grajciar | b1be2a0 | 2018-09-19 13:36:16 +0200 | [diff] [blame] | 283 | else: |
| 284 | return self._make_obj_serializable(obj) |
| 285 | |
| 286 | def _deserialize_obj(self, obj): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 287 | if hasattr(obj, "type"): |
Jakub Grajciar | 0b1f8a7 | 2019-02-21 12:01:31 +0100 | [diff] [blame] | 288 | if obj.type is dict: |
| 289 | _obj = dict() |
| 290 | for k, v in zip(obj.k_list, obj.v_list): |
| 291 | _obj[self._deserialize(k)] = self._deserialize(v) |
| 292 | return _obj |
Jakub Grajciar | b1be2a0 | 2018-09-19 13:36:16 +0200 | [diff] [blame] | 293 | return obj |
| 294 | |
| 295 | def _deserialize(self, obj): |
| 296 | if (type(obj) is list) or (type(obj) is tuple): |
| 297 | rv = [] |
| 298 | for item in obj: |
| 299 | rv.append(self._deserialize(item)) |
| 300 | if type(obj) is tuple: |
| 301 | rv = tuple(rv) |
| 302 | return rv |
| 303 | else: |
| 304 | return self._deserialize_obj(obj) |
| 305 | |
| 306 | def start_remote(self): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 307 | """Start remote execution""" |
Jakub Grajciar | b1be2a0 | 2018-09-19 13:36:16 +0200 | [diff] [blame] | 308 | self.start() |
| 309 | |
| 310 | def quit_remote(self): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 311 | """Quit remote execution""" |
Jakub Grajciar | 7db35de | 2019-06-25 10:22:11 +0200 | [diff] [blame] | 312 | self._remote_exec(RemoteClass.QUIT, None) |
Jakub Grajciar | b1be2a0 | 2018-09-19 13:36:16 +0200 | [diff] [blame] | 313 | |
| 314 | def get_remote_value(self): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 315 | """Get value of a remotely held object""" |
Jakub Grajciar | b1be2a0 | 2018-09-19 13:36:16 +0200 | [diff] [blame] | 316 | return RemoteClassAttr(self, None).get_remote_value() |
| 317 | |
| 318 | def set_request_timeout(self, timeout): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 319 | """Change request timeout""" |
Jakub Grajciar | b1be2a0 | 2018-09-19 13:36:16 +0200 | [diff] [blame] | 320 | self._timeout = timeout |
| 321 | |
| 322 | def run(self): |
| 323 | """ |
| 324 | Create instance of the wrapped class and execute operations |
| 325 | on it as requested by the parent process. |
| 326 | """ |
| 327 | self._instance = self._cls(*self._args, **self._kwargs) |
| 328 | while True: |
| 329 | try: |
| 330 | rv = None |
| 331 | # get request from the parent process |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 332 | (op, path, args, kwargs) = self._pipe[RemoteClass.PIPE_CHILD].recv() |
Jakub Grajciar | b1be2a0 | 2018-09-19 13:36:16 +0200 | [diff] [blame] | 333 | args = self._deserialize(args) |
| 334 | kwargs = self._deserialize(kwargs) |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 335 | path = path.split(".") if path else [] |
Jakub Grajciar | b1be2a0 | 2018-09-19 13:36:16 +0200 | [diff] [blame] | 336 | if op == RemoteClass.GET: |
| 337 | rv = self._get_local_value(path) |
| 338 | elif op == RemoteClass.CALL: |
| 339 | rv = self._call_local_method(path, *args, **kwargs) |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 340 | elif op == RemoteClass.SETATTR and "value" in kwargs: |
| 341 | self._set_local_attr(path, kwargs["value"]) |
Jakub Grajciar | b1be2a0 | 2018-09-19 13:36:16 +0200 | [diff] [blame] | 342 | elif op == RemoteClass.REPR: |
| 343 | rv = self._get_local_repr(path) |
| 344 | elif op == RemoteClass.STR: |
| 345 | rv = self._get_local_str(path) |
| 346 | elif op == RemoteClass.QUIT: |
| 347 | break |
| 348 | else: |
| 349 | continue |
| 350 | # send return value |
| 351 | if not self._serializable(rv): |
| 352 | rv = self._make_serializable(rv) |
| 353 | self._pipe[RemoteClass.PIPE_CHILD].send(rv) |
| 354 | except EOFError: |
| 355 | break |
| 356 | self._instance = None # destroy the instance |
| 357 | |
| 358 | |
| 359 | @unittest.skip("Remote Vpp Test Case Class") |
| 360 | class RemoteVppTestCase(VppTestCase): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 361 | """Re-use VppTestCase to create remote VPP segment |
Jakub Grajciar | b1be2a0 | 2018-09-19 13:36:16 +0200 | [diff] [blame] | 362 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 363 | In your test case:: |
Jakub Grajciar | b1be2a0 | 2018-09-19 13:36:16 +0200 | [diff] [blame] | 364 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 365 | @classmethod |
| 366 | def setUpClass(cls): |
| 367 | # fork new process before client connects to VPP |
| 368 | cls.remote_test = RemoteClass(RemoteVppTestCase) |
Jakub Grajciar | b1be2a0 | 2018-09-19 13:36:16 +0200 | [diff] [blame] | 369 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 370 | # start remote process |
| 371 | cls.remote_test.start_remote() |
Jakub Grajciar | b1be2a0 | 2018-09-19 13:36:16 +0200 | [diff] [blame] | 372 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 373 | # set up your test case |
| 374 | super(MyTestCase, cls).setUpClass() |
Jakub Grajciar | b1be2a0 | 2018-09-19 13:36:16 +0200 | [diff] [blame] | 375 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 376 | # set up remote test |
| 377 | cls.remote_test.setUpClass(cls.tempdir) |
Jakub Grajciar | b1be2a0 | 2018-09-19 13:36:16 +0200 | [diff] [blame] | 378 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 379 | @classmethod |
| 380 | def tearDownClass(cls): |
| 381 | # tear down remote test |
| 382 | cls.remote_test.tearDownClass() |
Jakub Grajciar | b1be2a0 | 2018-09-19 13:36:16 +0200 | [diff] [blame] | 383 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 384 | # stop remote process |
| 385 | cls.remote_test.quit_remote() |
Jakub Grajciar | b1be2a0 | 2018-09-19 13:36:16 +0200 | [diff] [blame] | 386 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 387 | # tear down your test case |
| 388 | super(MyTestCase, cls).tearDownClass() |
Jakub Grajciar | b1be2a0 | 2018-09-19 13:36:16 +0200 | [diff] [blame] | 389 | """ |
| 390 | |
| 391 | def __init__(self): |
| 392 | super(RemoteVppTestCase, self).__init__("emptyTest") |
| 393 | |
Paul Vinciguerra | f70cead | 2019-03-10 07:32:59 -0700 | [diff] [blame] | 394 | # Note: __del__ is a 'Finalizer" not a 'Destructor'. |
| 395 | # https://docs.python.org/3/reference/datamodel.html#object.__del__ |
Jakub Grajciar | b1be2a0 | 2018-09-19 13:36:16 +0200 | [diff] [blame] | 396 | def __del__(self): |
| 397 | if hasattr(self, "vpp"): |
Paul Vinciguerra | f70cead | 2019-03-10 07:32:59 -0700 | [diff] [blame] | 398 | self.vpp.poll() |
| 399 | if self.vpp.returncode is None: |
| 400 | self.vpp.terminate() |
| 401 | self.vpp.communicate() |
Jakub Grajciar | b1be2a0 | 2018-09-19 13:36:16 +0200 | [diff] [blame] | 402 | |
| 403 | @classmethod |
| 404 | def setUpClass(cls, tempdir): |
| 405 | # disable features unsupported in remote VPP |
| 406 | orig_env = dict(os.environ) |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 407 | if "STEP" in os.environ: |
| 408 | del os.environ["STEP"] |
| 409 | if "DEBUG" in os.environ: |
| 410 | del os.environ["DEBUG"] |
Jakub Grajciar | b1be2a0 | 2018-09-19 13:36:16 +0200 | [diff] [blame] | 411 | cls.tempdir_prefix = os.path.basename(tempdir) + "/" |
| 412 | super(RemoteVppTestCase, cls).setUpClass() |
| 413 | os.environ = orig_env |
| 414 | |
Paul Vinciguerra | 7f9b7f9 | 2019-03-12 19:23:27 -0700 | [diff] [blame] | 415 | @classmethod |
| 416 | def tearDownClass(cls): |
| 417 | super(RemoteVppTestCase, cls).tearDownClass() |
| 418 | |
Jakub Grajciar | b1be2a0 | 2018-09-19 13:36:16 +0200 | [diff] [blame] | 419 | @unittest.skip("Empty test") |
| 420 | def emptyTest(self): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 421 | """Do nothing""" |
Jakub Grajciar | b1be2a0 | 2018-09-19 13:36:16 +0200 | [diff] [blame] | 422 | pass |
| 423 | |
| 424 | def setTestFunctionInfo(self, name, doc): |
| 425 | """ |
| 426 | Store the name and documentation string of currently executed test |
| 427 | in the main VPP for logging purposes. |
| 428 | """ |
| 429 | self._testMethodName = name |
| 430 | self._testMethodDoc = doc |