libvirtpy 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (154) hide show
  1. libvirtpy/__init__.py +5 -0
  2. libvirtpy/client/__init__.py +54 -0
  3. libvirtpy/client/_bases.py +326 -0
  4. libvirtpy/client/_runner.py +57 -0
  5. libvirtpy/client/aio.py +5922 -0
  6. libvirtpy/client/client.py +5695 -0
  7. libvirtpy/client/operations/__init__.py +631 -0
  8. libvirtpy/client/operations/_common.py +495 -0
  9. libvirtpy/client/operations/checkpoint.py +196 -0
  10. libvirtpy/client/operations/connection.py +93 -0
  11. libvirtpy/client/operations/connection_extra.py +256 -0
  12. libvirtpy/client/operations/domain.py +306 -0
  13. libvirtpy/client/operations/domain_agent.py +115 -0
  14. libvirtpy/client/operations/domain_block.py +368 -0
  15. libvirtpy/client/operations/domain_info.py +400 -0
  16. libvirtpy/client/operations/domain_lifecycle.py +338 -0
  17. libvirtpy/client/operations/domain_migration.py +164 -0
  18. libvirtpy/client/operations/domain_network.py +113 -0
  19. libvirtpy/client/operations/domain_tuning.py +494 -0
  20. libvirtpy/client/operations/host.py +246 -0
  21. libvirtpy/client/operations/host_interface.py +100 -0
  22. libvirtpy/client/operations/interface_extra.py +45 -0
  23. libvirtpy/client/operations/network.py +135 -0
  24. libvirtpy/client/operations/network_info.py +155 -0
  25. libvirtpy/client/operations/network_port.py +142 -0
  26. libvirtpy/client/operations/node_device.py +76 -0
  27. libvirtpy/client/operations/node_device_info.py +189 -0
  28. libvirtpy/client/operations/nwfilter.py +67 -0
  29. libvirtpy/client/operations/nwfilter_binding.py +74 -0
  30. libvirtpy/client/operations/secret.py +93 -0
  31. libvirtpy/client/operations/secret_extra.py +24 -0
  32. libvirtpy/client/operations/snapshot.py +100 -0
  33. libvirtpy/client/operations/snapshot_tree.py +114 -0
  34. libvirtpy/client/operations/storage.py +267 -0
  35. libvirtpy/client/operations/storage_info.py +188 -0
  36. libvirtpy/client/operations/streams.py +201 -0
  37. libvirtpy/client/references.py +249 -0
  38. libvirtpy/client/subscribing.py +88 -0
  39. libvirtpy/client/testing/__init__.py +24 -0
  40. libvirtpy/client/testing/_common.py +167 -0
  41. libvirtpy/client/testing/_domains.py +294 -0
  42. libvirtpy/client/testing/_objects.py +205 -0
  43. libvirtpy/client/testing/_state.py +246 -0
  44. libvirtpy/client/testing/_storage.py +171 -0
  45. libvirtpy/client/testing/async_transport.py +30 -0
  46. libvirtpy/client/testing/fake.py +81 -0
  47. libvirtpy/client/transport.py +87 -0
  48. libvirtpy/client/types.py +59 -0
  49. libvirtpy/client/wire.py +436 -0
  50. libvirtpy/client/wire_aio.py +348 -0
  51. libvirtpy/errors.py +7 -0
  52. libvirtpy/py.typed +0 -0
  53. libvirtpy/rpc/__init__.py +1 -0
  54. libvirtpy/rpc/aio.py +387 -0
  55. libvirtpy/rpc/cpumap.py +48 -0
  56. libvirtpy/rpc/errors.py +186 -0
  57. libvirtpy/rpc/events.py +259 -0
  58. libvirtpy/rpc/options.py +66 -0
  59. libvirtpy/rpc/procedure.py +27 -0
  60. libvirtpy/rpc/protocol/__init__.py +0 -0
  61. libvirtpy/rpc/protocol/procedures.py +5163 -0
  62. libvirtpy/rpc/protocol/typed_hints.py +210 -0
  63. libvirtpy/rpc/protocol/types.py +9299 -0
  64. libvirtpy/rpc/remote.py +295 -0
  65. libvirtpy/rpc/remote_aio.py +106 -0
  66. libvirtpy/rpc/sasl.py +467 -0
  67. libvirtpy/rpc/session.py +424 -0
  68. libvirtpy/rpc/sockets.py +251 -0
  69. libvirtpy/rpc/stream.py +323 -0
  70. libvirtpy/rpc/subscription.py +451 -0
  71. libvirtpy/rpc/sync.py +460 -0
  72. libvirtpy/rpc/typed_params.py +97 -0
  73. libvirtpy/rpc/uri.py +85 -0
  74. libvirtpy/rpc/xdr.py +552 -0
  75. libvirtpy/xml/__init__.py +49 -0
  76. libvirtpy/xml/_base.py +145 -0
  77. libvirtpy/xml/_shared/__init__.py +1 -0
  78. libvirtpy/xml/_shared/metadata.py +67 -0
  79. libvirtpy/xml/_shared/network.py +108 -0
  80. libvirtpy/xml/_shared/storage.py +131 -0
  81. libvirtpy/xml/_shared/sysinfo.py +83 -0
  82. libvirtpy/xml/_types.py +197 -0
  83. libvirtpy/xml/_xml_element.py +134 -0
  84. libvirtpy/xml/backup/__init__.py +25 -0
  85. libvirtpy/xml/backup/backup.py +89 -0
  86. libvirtpy/xml/backup/checkpoint.py +48 -0
  87. libvirtpy/xml/capabilities/__init__.py +43 -0
  88. libvirtpy/xml/capabilities/domain.py +436 -0
  89. libvirtpy/xml/capabilities/host.py +413 -0
  90. libvirtpy/xml/capabilities/storagepool.py +59 -0
  91. libvirtpy/xml/domain/__init__.py +493 -0
  92. libvirtpy/xml/domain/blkiotune.py +81 -0
  93. libvirtpy/xml/domain/clock.py +79 -0
  94. libvirtpy/xml/domain/cpu.py +265 -0
  95. libvirtpy/xml/domain/devices/__init__.py +495 -0
  96. libvirtpy/xml/domain/devices/address.py +152 -0
  97. libvirtpy/xml/domain/devices/audio.py +204 -0
  98. libvirtpy/xml/domain/devices/chardev.py +197 -0
  99. libvirtpy/xml/domain/devices/common.py +75 -0
  100. libvirtpy/xml/domain/devices/controller.py +262 -0
  101. libvirtpy/xml/domain/devices/devices.py +79 -0
  102. libvirtpy/xml/domain/devices/disk.py +471 -0
  103. libvirtpy/xml/domain/devices/filesystem.py +206 -0
  104. libvirtpy/xml/domain/devices/graphics.py +245 -0
  105. libvirtpy/xml/domain/devices/hostdev.py +221 -0
  106. libvirtpy/xml/domain/devices/input.py +89 -0
  107. libvirtpy/xml/domain/devices/interface.py +459 -0
  108. libvirtpy/xml/domain/devices/iommu.py +54 -0
  109. libvirtpy/xml/domain/devices/memballoon.py +42 -0
  110. libvirtpy/xml/domain/devices/memory_device.py +101 -0
  111. libvirtpy/xml/domain/devices/misc.py +169 -0
  112. libvirtpy/xml/domain/devices/nvram.py +20 -0
  113. libvirtpy/xml/domain/devices/shmem.py +46 -0
  114. libvirtpy/xml/domain/devices/sound.py +42 -0
  115. libvirtpy/xml/domain/devices/tpm.py +103 -0
  116. libvirtpy/xml/domain/devices/usb.py +86 -0
  117. libvirtpy/xml/domain/devices/video.py +101 -0
  118. libvirtpy/xml/domain/domain.py +157 -0
  119. libvirtpy/xml/domain/features.py +312 -0
  120. libvirtpy/xml/domain/iothread.py +41 -0
  121. libvirtpy/xml/domain/launch_security.py +82 -0
  122. libvirtpy/xml/domain/memory.py +149 -0
  123. libvirtpy/xml/domain/misc.py +113 -0
  124. libvirtpy/xml/domain/namespaces.py +96 -0
  125. libvirtpy/xml/domain/numa.py +140 -0
  126. libvirtpy/xml/domain/os.py +267 -0
  127. libvirtpy/xml/domain/qemu.py +102 -0
  128. libvirtpy/xml/domain/seclabel.py +29 -0
  129. libvirtpy/xml/domain/storage_source.py +466 -0
  130. libvirtpy/xml/domain/sysinfo.py +34 -0
  131. libvirtpy/xml/interface/__init__.py +17 -0
  132. libvirtpy/xml/interface/interface.py +153 -0
  133. libvirtpy/xml/network/__init__.py +101 -0
  134. libvirtpy/xml/network/network.py +311 -0
  135. libvirtpy/xml/network/port.py +84 -0
  136. libvirtpy/xml/nodedev/__init__.py +13 -0
  137. libvirtpy/xml/nodedev/nodedev.py +490 -0
  138. libvirtpy/xml/nwfilter/__init__.py +81 -0
  139. libvirtpy/xml/nwfilter/binding.py +51 -0
  140. libvirtpy/xml/nwfilter/filter.py +92 -0
  141. libvirtpy/xml/nwfilter/rules.py +534 -0
  142. libvirtpy/xml/secret/__init__.py +11 -0
  143. libvirtpy/xml/secret/secret.py +36 -0
  144. libvirtpy/xml/snapshot/__init__.py +29 -0
  145. libvirtpy/xml/snapshot/snapshot.py +136 -0
  146. libvirtpy/xml/storage/__init__.py +75 -0
  147. libvirtpy/xml/storage/pool.py +219 -0
  148. libvirtpy/xml/storage/volume.py +102 -0
  149. libvirtpy/xml/sysinfo.py +40 -0
  150. libvirtpy-0.1.0.dist-info/METADATA +109 -0
  151. libvirtpy-0.1.0.dist-info/RECORD +154 -0
  152. libvirtpy-0.1.0.dist-info/WHEEL +4 -0
  153. libvirtpy-0.1.0.dist-info/licenses/LICENSE +501 -0
  154. libvirtpy-0.1.0.dist-info/licenses/NOTICE +23 -0
@@ -0,0 +1,167 @@
1
+ """What the parts of the fake daemon share: types, samples and helpers."""
2
+
3
+ from collections.abc import Callable, Mapping
4
+ from dataclasses import dataclass
5
+ from typing import Literal, TypeVar
6
+ from uuid import uuid4
7
+
8
+ from libvirtpy.client.transport import Handle, Request
9
+ from libvirtpy.client.types import DomainState
10
+ from libvirtpy.rpc.protocol.procedures import PROCEDURES
11
+ from libvirtpy.xml._base import LibvirtXmlModel
12
+ from libvirtpy.xml.storage import Pool, Volume
13
+
14
+
15
+ @dataclass
16
+ class _Entry[ModelT: LibvirtXmlModel]:
17
+ model: ModelT
18
+ active: bool = False
19
+ autostart: bool = False
20
+ transient: bool = False
21
+ state: DomainState = DomainState.SHUTOFF
22
+ secret_value: bytes | None = None
23
+
24
+
25
+ ModelT = TypeVar("ModelT", bound=LibvirtXmlModel)
26
+
27
+ Arguments = Mapping[str, object]
28
+
29
+ Action = Callable[[Arguments], object]
30
+
31
+ _START_PAUSED = 1
32
+
33
+ _CAPABILITIES = (
34
+ "<capabilities><host><cpu><arch>x86_64</arch><model>fake</model></cpu></host>"
35
+ "<guest><os_type>hvm</os_type><arch name='x86_64'><wordsize>64</wordsize>"
36
+ "<domain type='kvm'/></arch></guest></capabilities>"
37
+ )
38
+
39
+ _DOMAIN_CAPABILITIES = (
40
+ "<domainCapabilities><path>/usr/bin/qemu-system-x86_64</path><domain>kvm</domain>"
41
+ "<arch>x86_64</arch><vcpu max='512'/></domainCapabilities>"
42
+ )
43
+
44
+ _POOL_CAPABILITIES = (
45
+ "<storagepoolCapabilities><pool type='dir' supported='yes'/>"
46
+ "<pool type='iscsi' supported='no'/></storagepoolCapabilities>"
47
+ )
48
+
49
+ _SYSINFO = (
50
+ "<sysinfo type='smbios'><system><entry name='manufacturer'>Fake Inc.</entry>"
51
+ "</system></sysinfo>"
52
+ )
53
+
54
+ _PCI_DEVICE = (
55
+ "<device><name>pci_0000_00_1f_2</name><capability type='pci'><domain>0</domain>"
56
+ "<bus>0</bus><slot>31</slot><function>2</function><product id='0x2922'/>"
57
+ "<vendor id='0x8086'/></capability></device>"
58
+ )
59
+
60
+ _PROCEDURES_BY_NAME = {p.name: p for p in PROCEDURES.values()}
61
+
62
+ _SNAPSHOT_STATES: dict[DomainState, Literal["running", "paused", "shutoff"]] = {
63
+ DomainState.RUNNING: "running",
64
+ DomainState.PAUSED: "paused",
65
+ DomainState.SHUTOFF: "shutoff",
66
+ }
67
+
68
+
69
+ def _check_against_protocol(request: Request) -> None:
70
+ """Fail if the request does not match the real procedure's arguments.
71
+
72
+ This keeps the fake faithful to the wire: whatever works against it names
73
+ real procedures with their real argument names.
74
+
75
+ :param request: the request to check.
76
+ :raises TypeError: if the procedure is unknown or the arguments differ.
77
+ """
78
+ procedure = _PROCEDURES_BY_NAME.get(request.procedure)
79
+ if procedure is None:
80
+ raise TypeError(f"{request.procedure} is not a libvirt procedure")
81
+
82
+ expected = (
83
+ sorted(name for name, _ in procedure.args.xdr_fields) if procedure.args else []
84
+ )
85
+ if sorted(request.arguments) != expected:
86
+ raise TypeError(
87
+ f"{request.procedure} takes {expected}, got {sorted(request.arguments)}"
88
+ )
89
+
90
+
91
+ def _redefine[ModelT: LibvirtXmlModel](
92
+ entry: _Entry[ModelT], model: ModelT
93
+ ) -> _Entry[ModelT]:
94
+ entry.model = model
95
+ return entry
96
+
97
+
98
+ def _key_of(model: LibvirtXmlModel) -> str:
99
+ uuid_text = getattr(model, "uuid", None)
100
+ name = getattr(model, "name", None)
101
+ if isinstance(name, str):
102
+ return name
103
+ if isinstance(uuid_text, str):
104
+ return uuid_text
105
+ raise TypeError(f"{model!r} has neither a name nor a UUID")
106
+
107
+
108
+ def _handle_of(model: LibvirtXmlModel) -> Handle:
109
+ uuid_text = getattr(model, "uuid", None)
110
+ name = getattr(model, "name", None)
111
+ return Handle(
112
+ name=name if isinstance(name, str) else str(uuid_text),
113
+ uuid=uuid_text if isinstance(uuid_text, str) else None,
114
+ id=getattr(model, "id", None),
115
+ )
116
+
117
+
118
+ def _ensure_uuid(model: LibvirtXmlModel) -> None:
119
+ if "uuid" in type(model).model_fields and getattr(model, "uuid", None) is None:
120
+ model.__setattr__("uuid", str(uuid4()))
121
+
122
+
123
+ def _pool_name(pool: Pool) -> str:
124
+ if pool.name is None:
125
+ raise TypeError("the pool has no name")
126
+ return pool.name
127
+
128
+
129
+ def _volume_handle(volume: Volume, pool: str) -> Handle:
130
+ return Handle(name=volume.name, parent=pool)
131
+
132
+
133
+ def _xml_of(model: LibvirtXmlModel) -> str:
134
+ text = model.to_xml(encoding="unicode")
135
+ if not isinstance(text, str):
136
+ raise TypeError("to_xml(encoding='unicode') did not return text")
137
+ return text
138
+
139
+
140
+ def _device_identity(device: object) -> str | None:
141
+ target = getattr(device, "target", None)
142
+ mac = getattr(device, "mac", None)
143
+ for candidate in (getattr(target, "dev", None), getattr(mac, "address", None)):
144
+ if isinstance(candidate, str):
145
+ return candidate
146
+ return None
147
+
148
+
149
+ def _handle_argument(arguments: Arguments) -> Handle:
150
+ for value in arguments.values():
151
+ if isinstance(value, Handle):
152
+ return value
153
+ raise TypeError("the request has no object handle")
154
+
155
+
156
+ def _text(arguments: Arguments, name: str) -> str:
157
+ value = arguments[name]
158
+ if not isinstance(value, str):
159
+ raise TypeError(f"{name} must be text, got {value!r}")
160
+ return value
161
+
162
+
163
+ def _int(arguments: Arguments, name: str) -> int:
164
+ value = arguments[name]
165
+ if not isinstance(value, int):
166
+ raise TypeError(f"{name} must be a number, got {value!r}")
167
+ return value
@@ -0,0 +1,294 @@
1
+ """Domains and their snapshots."""
2
+
3
+ import time
4
+
5
+ from libvirtpy.client.testing._common import (
6
+ _SNAPSHOT_STATES,
7
+ _START_PAUSED,
8
+ Action,
9
+ Arguments,
10
+ _device_identity,
11
+ _Entry,
12
+ _handle_argument,
13
+ _handle_of,
14
+ _int,
15
+ _key_of,
16
+ _text,
17
+ _xml_of,
18
+ )
19
+ from libvirtpy.client.testing._state import FakeState
20
+ from libvirtpy.client.transport import Handle
21
+ from libvirtpy.client.types import DomainInfo, DomainState
22
+ from libvirtpy.rpc.errors import (
23
+ ErrorNumber,
24
+ ExistsError,
25
+ LibvirtError,
26
+ OperationInvalidError,
27
+ )
28
+ from libvirtpy.xml.domain import CurrentMemory, Domain, Vcpu
29
+ from libvirtpy.xml.domain.devices import Devices
30
+ from libvirtpy.xml.snapshot import Snapshot, SnapshotParent
31
+
32
+
33
+ class DomainActions(FakeState):
34
+ """Domains and their snapshots."""
35
+
36
+ def _domain_actions(self) -> dict[str, Action]:
37
+ return {
38
+ "connect_list_all_domains": self._list_domains,
39
+ "domain_lookup_by_id": self._lookup_domain_by_id,
40
+ "domain_lookup_by_name": lambda a: self._find_by_name(
41
+ self._domains, _text(a, "name"), self._no_domain
42
+ ),
43
+ "domain_lookup_by_uuid": lambda a: self._find_by_uuid(
44
+ self._domains, _text(a, "uuid"), self._no_domain
45
+ ),
46
+ "domain_get_xml_desc": lambda a: _xml_of(self._domain(a).model),
47
+ "domain_define_xml_flags": self._define_domain,
48
+ "domain_create_xml": self._create_domain,
49
+ "domain_create_with_flags": self._start_domain,
50
+ "domain_undefine_flags": self._undefine_domain,
51
+ "domain_shutdown_flags": self._shutdown_domain,
52
+ "domain_destroy_flags": self._destroy_domain,
53
+ "domain_reboot": self._require_running,
54
+ "domain_reset": self._require_running,
55
+ "domain_suspend": self._suspend_domain,
56
+ "domain_resume": self._resume_domain,
57
+ "domain_get_state": lambda a: int(self._domain(a).state),
58
+ "domain_get_info": self._domain_info,
59
+ "domain_set_vcpus_flags": self._set_vcpus,
60
+ "domain_set_memory_flags": self._set_memory,
61
+ "domain_get_autostart": lambda a: int(self._domain(a).autostart),
62
+ "domain_set_autostart": self._set_domain_autostart,
63
+ "domain_attach_device_flags": self._attach_device,
64
+ "domain_detach_device_flags": self._detach_device,
65
+ "domain_update_device_flags": self._update_device,
66
+ }
67
+
68
+ def _snapshot_actions(self) -> dict[str, Action]:
69
+ return {
70
+ "domain_list_all_snapshots": self._list_snapshots,
71
+ "domain_snapshot_lookup_by_name": self._lookup_snapshot,
72
+ "domain_snapshot_create_xml": self._create_snapshot,
73
+ "domain_snapshot_get_xml_desc": lambda a: _xml_of(self._snapshot(a)),
74
+ "domain_revert_to_snapshot": self._revert_snapshot,
75
+ "domain_snapshot_delete": self._delete_snapshot,
76
+ }
77
+
78
+ def _domain(self, arguments: Arguments) -> _Entry[Domain]:
79
+ return self._get(self._domains, arguments, self._no_domain)
80
+
81
+ def _list_domains(self, arguments: Arguments) -> list[Handle]:
82
+ return self._list(self._domains, arguments, inactive_is_one=False)
83
+
84
+ def _lookup_domain_by_id(self, arguments: Arguments) -> Handle:
85
+ wanted = _int(arguments, "id")
86
+ for entry in self._domains.values():
87
+ if entry.model.id == wanted:
88
+ return _handle_of(entry.model)
89
+ raise self._no_domain(str(wanted))
90
+
91
+ def _define_domain(self, arguments: Arguments) -> Handle:
92
+ return self._define(self._domains, Domain, _text(arguments, "xml"))
93
+
94
+ def _create_domain(self, arguments: Arguments) -> Handle:
95
+ handle = self._create_transient(
96
+ self._domains, Domain, _text(arguments, "xml_desc")
97
+ )
98
+ entry = self._domains[handle.name]
99
+ self._run(entry, paused=bool(_int(arguments, "flags") & _START_PAUSED))
100
+ return _handle_of(entry.model)
101
+
102
+ def _start_domain(self, arguments: Arguments) -> None:
103
+ entry = self._domain(arguments)
104
+ if entry.state != DomainState.SHUTOFF:
105
+ raise OperationInvalidError(
106
+ f"domain is already {entry.state.name.lower()}",
107
+ code=ErrorNumber.OPERATION_INVALID,
108
+ )
109
+ self._run(entry, paused=bool(_int(arguments, "flags") & _START_PAUSED))
110
+
111
+ def _run(self, entry: _Entry[Domain], *, paused: bool) -> None:
112
+ entry.active = True
113
+ entry.state = DomainState.PAUSED if paused else DomainState.RUNNING
114
+ entry.model.id = self._next_domain_id
115
+ self._next_domain_id += 1
116
+
117
+ def _stop_domain(self, entry: _Entry[Domain]) -> None:
118
+ entry.active = False
119
+ entry.state = DomainState.SHUTOFF
120
+ entry.model.id = None
121
+ if entry.transient:
122
+ del self._domains[_key_of(entry.model)]
123
+
124
+ def _undefine_domain(self, arguments: Arguments) -> None:
125
+ entry = self._domain(arguments)
126
+ if entry.active:
127
+ raise OperationInvalidError(
128
+ "cannot undefine a running domain", code=ErrorNumber.OPERATION_INVALID
129
+ )
130
+ name = entry.model.name
131
+ del self._domains[_key_of(entry.model)]
132
+ for key in [k for k in self._snapshots if k[0] == name]:
133
+ del self._snapshots[key]
134
+
135
+ def _shutdown_domain(self, arguments: Arguments) -> None:
136
+ self._stop_domain(self._require_running(arguments))
137
+
138
+ def _destroy_domain(self, arguments: Arguments) -> None:
139
+ entry = self._domain(arguments)
140
+ if not entry.active:
141
+ raise OperationInvalidError(
142
+ "domain is not running", code=ErrorNumber.OPERATION_INVALID
143
+ )
144
+ self._stop_domain(entry)
145
+
146
+ def _require_running(self, arguments: Arguments) -> _Entry[Domain]:
147
+ entry = self._domain(arguments)
148
+ if not entry.active:
149
+ raise OperationInvalidError(
150
+ "domain is not running", code=ErrorNumber.OPERATION_INVALID
151
+ )
152
+ return entry
153
+
154
+ def _suspend_domain(self, arguments: Arguments) -> None:
155
+ entry = self._require_running(arguments)
156
+ if entry.state != DomainState.RUNNING:
157
+ raise OperationInvalidError(
158
+ "domain is already paused", code=ErrorNumber.OPERATION_INVALID
159
+ )
160
+ entry.state = DomainState.PAUSED
161
+
162
+ def _resume_domain(self, arguments: Arguments) -> None:
163
+ entry = self._domain(arguments)
164
+ if entry.state != DomainState.PAUSED:
165
+ raise OperationInvalidError(
166
+ "domain is not paused", code=ErrorNumber.OPERATION_INVALID
167
+ )
168
+ entry.state = DomainState.RUNNING
169
+
170
+ def _domain_info(self, arguments: Arguments) -> DomainInfo:
171
+ entry = self._domain(arguments)
172
+ model = entry.model
173
+ max_kib = model.memory.to_bytes() // 1024 if model.memory else 0
174
+ kib = (
175
+ model.current_memory.to_bytes() // 1024 if model.current_memory else max_kib
176
+ )
177
+ return DomainInfo(
178
+ state=entry.state,
179
+ max_memory_kib=max_kib,
180
+ memory_kib=kib,
181
+ vcpus=model.vcpu.value if model.vcpu else 1,
182
+ cpu_time_ns=1_000_000 if entry.active else 0,
183
+ )
184
+
185
+ def _set_vcpus(self, arguments: Arguments) -> None:
186
+ entry = self._domain(arguments)
187
+ count = _int(arguments, "nvcpus")
188
+ if count < 1:
189
+ raise LibvirtError("invalid vCPU count", code=ErrorNumber.INVALID_ARG)
190
+ entry.model.vcpu = Vcpu(value=count)
191
+
192
+ def _set_memory(self, arguments: Arguments) -> None:
193
+ entry = self._domain(arguments)
194
+ entry.model.current_memory = CurrentMemory(
195
+ value=_int(arguments, "memory"), unit="KiB"
196
+ )
197
+
198
+ def _set_domain_autostart(self, arguments: Arguments) -> None:
199
+ self._domain(arguments).autostart = bool(_int(arguments, "autostart"))
200
+
201
+ def _attach_device(self, arguments: Arguments) -> None:
202
+ entry = self._domain(arguments)
203
+ parsed = Devices.from_xml(f"<devices>{_text(arguments, 'xml')}</devices>")
204
+ devices = entry.model.devices or Devices()
205
+ for name in Devices.model_fields:
206
+ value = getattr(parsed, name)
207
+ if isinstance(value, list):
208
+ setattr(devices, name, [*getattr(devices, name), *value])
209
+ elif value is not None:
210
+ setattr(devices, name, value)
211
+ entry.model.devices = devices
212
+
213
+ def _detach_device(self, arguments: Arguments) -> None:
214
+ entry = self._domain(arguments)
215
+ parsed = Devices.from_xml(f"<devices>{_text(arguments, 'xml')}</devices>")
216
+ devices = entry.model.devices or Devices()
217
+ removed = False
218
+ for name in Devices.model_fields:
219
+ wanted = getattr(parsed, name)
220
+ if not isinstance(wanted, list):
221
+ continue
222
+ current = getattr(devices, name)
223
+ kept = [
224
+ d
225
+ for d in current
226
+ if _device_identity(d) not in map(_device_identity, wanted)
227
+ ]
228
+ removed = removed or len(kept) != len(current)
229
+ setattr(devices, name, kept)
230
+ if not removed:
231
+ raise LibvirtError("device not found", code=ErrorNumber.INVALID_ARG)
232
+ entry.model.devices = devices
233
+
234
+ def _update_device(self, arguments: Arguments) -> None:
235
+ self._detach_device(arguments)
236
+ self._attach_device(arguments)
237
+
238
+ def _snapshot(self, arguments: Arguments) -> Snapshot:
239
+ handle = _handle_argument(arguments)
240
+ snapshot = self._snapshots.get((handle.parent or "", handle.name))
241
+ if snapshot is None:
242
+ raise self._no_snapshot(handle.name)
243
+ return snapshot
244
+
245
+ def _list_snapshots(self, arguments: Arguments) -> list[Handle]:
246
+ domain = self._domain(arguments).model.name
247
+ return [
248
+ Handle(name=name, parent=owner)
249
+ for (owner, name) in self._snapshots
250
+ if owner == domain
251
+ ]
252
+
253
+ def _lookup_snapshot(self, arguments: Arguments) -> Handle:
254
+ domain = self._domain({"dom": arguments["dom"]}).model.name
255
+ name = _text(arguments, "name")
256
+ if (domain, name) not in self._snapshots:
257
+ raise self._no_snapshot(name)
258
+ return Handle(name=name, parent=domain)
259
+
260
+ def _create_snapshot(self, arguments: Arguments) -> Handle:
261
+ entry = self._domain(arguments)
262
+ snapshot = Snapshot.from_xml(_text(arguments, "xml_desc"))
263
+ self._snapshot_count += 1
264
+ domain = entry.model.name
265
+ if snapshot.name is None:
266
+ snapshot.name = f"snapshot{self._snapshot_count}"
267
+ if (domain, snapshot.name) in self._snapshots:
268
+ raise ExistsError(
269
+ f"snapshot {snapshot.name} exists", code=ErrorNumber.DOM_EXIST
270
+ )
271
+ previous = [name for (owner, name) in self._snapshots if owner == domain]
272
+ snapshot.state = _SNAPSHOT_STATES[entry.state]
273
+ snapshot.creation_time = str(int(time.time()) + self._snapshot_count)
274
+ if previous:
275
+ snapshot.parent = SnapshotParent(name=previous[-1])
276
+ self._snapshots[(domain, snapshot.name)] = snapshot
277
+ return Handle(name=snapshot.name, parent=domain)
278
+
279
+ def _revert_snapshot(self, arguments: Arguments) -> None:
280
+ handle = _handle_argument(arguments)
281
+ snapshot = self._snapshot(arguments)
282
+ entry = self._domains[handle.parent or ""]
283
+ wanted = next(
284
+ s for s, text in _SNAPSHOT_STATES.items() if text == snapshot.state
285
+ )
286
+ if wanted == DomainState.SHUTOFF and entry.active:
287
+ self._stop_domain(entry)
288
+ elif wanted != DomainState.SHUTOFF and not entry.active:
289
+ self._run(entry, paused=wanted == DomainState.PAUSED)
290
+
291
+ def _delete_snapshot(self, arguments: Arguments) -> None:
292
+ handle = _handle_argument(arguments)
293
+ self._snapshot(arguments)
294
+ del self._snapshots[(handle.parent or "", handle.name)]
@@ -0,0 +1,205 @@
1
+ """Connection data, networks, secrets, filters, interfaces and host devices."""
2
+
3
+ from libvirtpy.client.testing._common import (
4
+ _CAPABILITIES,
5
+ _DOMAIN_CAPABILITIES,
6
+ _POOL_CAPABILITIES,
7
+ _SYSINFO,
8
+ Action,
9
+ Arguments,
10
+ _handle_argument,
11
+ _handle_of,
12
+ _text,
13
+ _xml_of,
14
+ )
15
+ from libvirtpy.client.testing._state import FakeState
16
+ from libvirtpy.client.transport import Handle
17
+ from libvirtpy.rpc.errors import (
18
+ ErrorNumber,
19
+ OperationInvalidError,
20
+ )
21
+ from libvirtpy.xml.interface import HostInterface
22
+ from libvirtpy.xml.network import Network
23
+ from libvirtpy.xml.nodedev import Device
24
+ from libvirtpy.xml.nwfilter import Filter
25
+ from libvirtpy.xml.secret import Secret
26
+
27
+
28
+ class ObjectActions(FakeState):
29
+ """Connection data, networks, secrets, filters, interfaces and host devices."""
30
+
31
+ def _connection_actions(self) -> dict[str, Action]:
32
+ return {
33
+ "connect_get_version": lambda _: 11006000,
34
+ "connect_get_hostname": lambda _: "fake-host",
35
+ "connect_get_capabilities": lambda _: _CAPABILITIES,
36
+ "connect_get_domain_capabilities": lambda _: _DOMAIN_CAPABILITIES,
37
+ "connect_get_storage_pool_capabilities": lambda _: _POOL_CAPABILITIES,
38
+ "connect_get_sysinfo": lambda _: _SYSINFO,
39
+ }
40
+
41
+ def _network_actions(self) -> dict[str, Action]:
42
+ return {
43
+ "connect_list_all_networks": lambda a: self._list(self._networks, a, True),
44
+ "network_lookup_by_name": lambda a: self._find_by_name(
45
+ self._networks, _text(a, "name"), self._no_network
46
+ ),
47
+ "network_lookup_by_uuid": lambda a: self._find_by_uuid(
48
+ self._networks, _text(a, "uuid"), self._no_network
49
+ ),
50
+ "network_get_xml_desc": lambda a: _xml_of(
51
+ self._get(self._networks, a, self._no_network).model
52
+ ),
53
+ "network_define_xml_flags": lambda a: self._define(
54
+ self._networks, Network, _text(a, "xml")
55
+ ),
56
+ "network_create_xml_flags": lambda a: self._create_transient(
57
+ self._networks, Network, _text(a, "xml")
58
+ ),
59
+ "network_undefine": lambda a: self._undefine(
60
+ self._networks, a, self._no_network
61
+ ),
62
+ "network_create": lambda a: self._start(
63
+ self._networks, a, self._no_network
64
+ ),
65
+ "network_destroy": lambda a: self._stop(
66
+ self._networks, a, self._no_network
67
+ ),
68
+ "network_get_autostart": lambda a: int(
69
+ self._get(self._networks, a, self._no_network).autostart
70
+ ),
71
+ "network_set_autostart": lambda a: self._set_autostart(
72
+ self._networks, a, self._no_network
73
+ ),
74
+ }
75
+
76
+ def _secret_actions(self) -> dict[str, Action]:
77
+ return {
78
+ "connect_list_all_secrets": lambda _: [
79
+ _handle_of(e.model) for e in self._secrets.values()
80
+ ],
81
+ "secret_lookup_by_uuid": lambda a: self._find_by_uuid(
82
+ self._secrets, _text(a, "uuid"), self._no_secret
83
+ ),
84
+ "secret_get_xml_desc": lambda a: _xml_of(
85
+ self._get(self._secrets, a, self._no_secret).model
86
+ ),
87
+ "secret_define_xml": lambda a: self._define(
88
+ self._secrets, Secret, _text(a, "xml")
89
+ ),
90
+ "secret_undefine": lambda a: self._undefine(
91
+ self._secrets, a, self._no_secret, check_active=False
92
+ ),
93
+ "secret_set_value": self._set_secret_value,
94
+ "secret_get_value": self._get_secret_value,
95
+ }
96
+
97
+ def _filter_actions(self) -> dict[str, Action]:
98
+ return {
99
+ "connect_list_all_nwfilters": lambda _: [
100
+ _handle_of(e.model) for e in self._filters.values()
101
+ ],
102
+ "nwfilter_lookup_by_name": lambda a: self._find_by_name(
103
+ self._filters, _text(a, "name"), self._no_filter
104
+ ),
105
+ "nwfilter_lookup_by_uuid": lambda a: self._find_by_uuid(
106
+ self._filters, _text(a, "uuid"), self._no_filter
107
+ ),
108
+ "nwfilter_get_xml_desc": lambda a: _xml_of(
109
+ self._get(self._filters, a, self._no_filter).model
110
+ ),
111
+ "nwfilter_define_xml_flags": lambda a: self._define(
112
+ self._filters, Filter, _text(a, "xml")
113
+ ),
114
+ "nwfilter_undefine": lambda a: self._undefine(
115
+ self._filters, a, self._no_filter, check_active=False
116
+ ),
117
+ }
118
+
119
+ def _interface_actions(self) -> dict[str, Action]:
120
+ return {
121
+ "connect_list_all_interfaces": lambda a: self._list(
122
+ self._interfaces, a, True
123
+ ),
124
+ "interface_lookup_by_name": lambda a: self._find_by_name(
125
+ self._interfaces, _text(a, "name"), self._no_interface
126
+ ),
127
+ "interface_lookup_by_mac_string": self._lookup_interface_by_mac,
128
+ "interface_get_xml_desc": lambda a: _xml_of(
129
+ self._get(self._interfaces, a, self._no_interface).model
130
+ ),
131
+ "interface_define_xml": lambda a: self._define(
132
+ self._interfaces, HostInterface, _text(a, "xml")
133
+ ),
134
+ "interface_undefine": lambda a: self._undefine(
135
+ self._interfaces, a, self._no_interface
136
+ ),
137
+ "interface_create": lambda a: self._start(
138
+ self._interfaces, a, self._no_interface
139
+ ),
140
+ "interface_destroy": lambda a: self._stop(
141
+ self._interfaces, a, self._no_interface
142
+ ),
143
+ }
144
+
145
+ def _device_actions(self) -> dict[str, Action]:
146
+ return {
147
+ "connect_list_all_node_devices": self._list_devices,
148
+ "node_device_lookup_by_name": self._lookup_device,
149
+ "node_device_get_xml_desc": lambda a: _xml_of(self._device(a)),
150
+ "node_device_create_xml": self._create_device,
151
+ "node_device_destroy": self._destroy_device,
152
+ }
153
+
154
+ def _set_secret_value(self, arguments: Arguments) -> None:
155
+ entry = self._get(self._secrets, arguments, self._no_secret)
156
+ value = arguments["value"]
157
+ if not isinstance(value, bytes):
158
+ raise TypeError("a secret value is bytes")
159
+ entry.secret_value = value
160
+
161
+ def _get_secret_value(self, arguments: Arguments) -> bytes:
162
+ entry = self._get(self._secrets, arguments, self._no_secret)
163
+ if entry.model.private:
164
+ raise OperationInvalidError(
165
+ "the secret is private", code=ErrorNumber.OPERATION_INVALID
166
+ )
167
+ if entry.secret_value is None:
168
+ raise OperationInvalidError(
169
+ "the secret has no value", code=ErrorNumber.OPERATION_INVALID
170
+ )
171
+ return entry.secret_value
172
+
173
+ def _lookup_interface_by_mac(self, arguments: Arguments) -> Handle:
174
+ wanted = _text(arguments, "mac").lower()
175
+ for entry in self._interfaces.values():
176
+ if entry.model.mac and entry.model.mac.address.lower() == wanted:
177
+ return _handle_of(entry.model)
178
+ raise self._no_interface(wanted)
179
+
180
+ def _device(self, arguments: Arguments) -> Device:
181
+ handle = _handle_argument(arguments)
182
+ device = self._devices.get(handle.name)
183
+ if device is None:
184
+ raise self._no_device(handle.name)
185
+ return device
186
+
187
+ def _list_devices(self, _arguments: Arguments) -> list[Handle]:
188
+ return [Handle(name=name) for name in self._devices]
189
+
190
+ def _lookup_device(self, arguments: Arguments) -> Handle:
191
+ name = _text(arguments, "name")
192
+ if name not in self._devices:
193
+ raise self._no_device(name)
194
+ return Handle(name=name)
195
+
196
+ def _create_device(self, arguments: Arguments) -> Handle:
197
+ device = Device.from_xml(_text(arguments, "xml_desc"))
198
+ self._devices[device.name] = device
199
+ return Handle(name=device.name)
200
+
201
+ def _destroy_device(self, arguments: Arguments) -> None:
202
+ name = _text(arguments, "name")
203
+ if name not in self._devices:
204
+ raise self._no_device(name)
205
+ del self._devices[name]