Skip to content

Commit f7f9a9f

Browse files
authored
Hyper-V: Auto select default switch (#3609)
* Update hyperv.py * Hyper-V: Auto select default switch * Update hyperv.py * Update hyperv.py * Update hyperv.py * Update hyperv.py * Update hyperv.py * Update hyperv.py
1 parent 07e67e4 commit f7f9a9f

File tree

1 file changed

+29
-24
lines changed

1 file changed

+29
-24
lines changed

lisa/tools/hyperv.py

Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33

44
import re
55
import time
6-
from dataclasses import dataclass, field
6+
from dataclasses import dataclass
77
from enum import Enum
88
from typing import Dict, Optional
99

1010
from assertpy import assert_that
11-
from dataclasses_json import config, dataclass_json
11+
from dataclasses_json import dataclass_json
1212

1313
from lisa.base_tools import Service
1414
from lisa.executable import Tool
@@ -19,20 +19,22 @@
1919
from lisa.util.process import Process
2020

2121

22-
@dataclass_json
23-
@dataclass
24-
class VMSwitch:
25-
name: str = field(metadata=config(field_name="Name"))
26-
27-
2822
class HypervSwitchType(Enum):
2923
INTERNAL = "Internal"
3024
EXTERNAL = "External"
3125

3226

27+
@dataclass_json
28+
@dataclass
29+
class VMSwitch:
30+
name: str = ""
31+
type: HypervSwitchType = HypervSwitchType.EXTERNAL
32+
33+
3334
class HyperV(Tool):
3435
# 192.168.5.12
3536
IP_REGEX = r"\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}"
37+
_default_switch: Optional[VMSwitch] = None
3638

3739
@property
3840
def command(self) -> str:
@@ -209,23 +211,26 @@ def enable_device_passthrough(self, name: str, mmio_mb: int = 5120) -> None:
209211
force_run=True,
210212
)
211213

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
227230

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
229234

230235
def exists_switch(self, name: str) -> bool:
231236
output = self.node.tools[PowerShell].run_cmdlet(

0 commit comments

Comments
 (0)