|
3 | 3 |
|
4 | 4 | import re
|
5 | 5 | import time
|
6 |
| -from dataclasses import dataclass, field |
| 6 | +from dataclasses import dataclass |
7 | 7 | from enum import Enum
|
8 | 8 | from typing import Dict, Optional
|
9 | 9 |
|
10 | 10 | from assertpy import assert_that
|
11 |
| -from dataclasses_json import config, dataclass_json |
| 11 | +from dataclasses_json import dataclass_json |
12 | 12 |
|
13 | 13 | from lisa.base_tools import Service
|
14 | 14 | from lisa.executable import Tool
|
|
19 | 19 | from lisa.util.process import Process
|
20 | 20 |
|
21 | 21 |
|
22 |
| -@dataclass_json |
23 |
| -@dataclass |
24 |
| -class VMSwitch: |
25 |
| - name: str = field(metadata=config(field_name="Name")) |
26 |
| - |
27 |
| - |
28 | 22 | class HypervSwitchType(Enum):
|
29 | 23 | INTERNAL = "Internal"
|
30 | 24 | EXTERNAL = "External"
|
31 | 25 |
|
32 | 26 |
|
| 27 | +@dataclass_json |
| 28 | +@dataclass |
| 29 | +class VMSwitch: |
| 30 | + name: str = "" |
| 31 | + type: HypervSwitchType = HypervSwitchType.EXTERNAL |
| 32 | + |
| 33 | + |
33 | 34 | class HyperV(Tool):
|
34 | 35 | # 192.168.5.12
|
35 | 36 | IP_REGEX = r"\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}"
|
| 37 | + _default_switch: Optional[VMSwitch] = None |
36 | 38 |
|
37 | 39 | @property
|
38 | 40 | def command(self) -> str:
|
@@ -209,23 +211,26 @@ def enable_device_passthrough(self, name: str, mmio_mb: int = 5120) -> None:
|
209 | 211 | force_run=True,
|
210 | 212 | )
|
211 | 213 |
|
212 |
| - def get_default_switch( |
213 |
| - self, switch_type: HypervSwitchType = HypervSwitchType.EXTERNAL |
214 |
| - ) -> VMSwitch: |
215 |
| - if switch_type not in (HypervSwitchType.INTERNAL, HypervSwitchType.EXTERNAL): |
216 |
| - raise LisaException(f"Unknown switch type {switch_type}") |
217 |
| - |
218 |
| - # get default switch of type `switch_type` from hyperv |
219 |
| - switch_json = self.node.tools[PowerShell].run_cmdlet( |
220 |
| - f'Get-VMSwitch | Where-Object {{$_.SwitchType -eq "{switch_type}"}}' |
221 |
| - "| Select -First 1 | select Name | ConvertTo-Json", |
222 |
| - force_run=True, |
223 |
| - ) |
224 |
| - |
225 |
| - if not switch_json: |
226 |
| - raise LisaException(f"Could not find default switch of type {switch_type}") |
| 214 | + # get default switch from hyperv |
| 215 | + def get_default_switch(self) -> VMSwitch: |
| 216 | + if self._default_switch is None: |
| 217 | + # try to get external switch first |
| 218 | + for switch_type in (HypervSwitchType.EXTERNAL, HypervSwitchType.INTERNAL): |
| 219 | + switch_json = self.node.tools[PowerShell].run_cmdlet( |
| 220 | + f'Get-VMSwitch | Where-Object {{$_.SwitchType -eq "{switch_type.value}"}}' # noqa: E501 |
| 221 | + " | Select -First 1", |
| 222 | + force_run=True, |
| 223 | + output_json=True, |
| 224 | + ) |
| 225 | + if switch_json: |
| 226 | + self._default_switch = VMSwitch() |
| 227 | + self._default_switch.name = switch_json["Name"] |
| 228 | + self._default_switch.type = switch_type |
| 229 | + break |
227 | 230 |
|
228 |
| - return VMSwitch.from_json(switch_json) # type: ignore |
| 231 | + if self._default_switch is None: |
| 232 | + raise LisaException("Could not find any Internal or External switch") |
| 233 | + return self._default_switch |
229 | 234 |
|
230 | 235 | def exists_switch(self, name: str) -> bool:
|
231 | 236 | output = self.node.tools[PowerShell].run_cmdlet(
|
|
0 commit comments