tools: hyperv: refactor the default switch code

Rename the method to get_default_external_switch() and modify the PS
command to return only external switches. Introduce a class VMSwitch to
represent a switch. Currently, `name` is the only property and more can
be added later.

Signed-off-by: Anirudh Rayabharam <anrayabh@microsoft.com>
This commit is contained in:
Anirudh Rayabharam 2024-02-01 17:00:15 +05:30 коммит произвёл Chi Song
Родитель 903bd2d017
Коммит 518306e21f
1 изменённых файлов: 17 добавлений и 3 удалений

Просмотреть файл

@ -3,9 +3,11 @@
import re import re
import time import time
from dataclasses import dataclass, field
from typing import Dict, Optional from typing import Dict, Optional
from assertpy import assert_that from assertpy import assert_that
from dataclasses_json import config, dataclass_json
from lisa.executable import Tool from lisa.executable import Tool
from lisa.operating_system import Windows from lisa.operating_system import Windows
@ -13,6 +15,12 @@ from lisa.tools.powershell import PowerShell
from lisa.util import LisaException from lisa.util import LisaException
@dataclass_json
@dataclass
class VMSwitch:
name: str = field(metadata=config(field_name="Name"))
class HyperV(Tool): class HyperV(Tool):
# 192.168.5.12 # 192.168.5.12
IP_REGEX = r"\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}" IP_REGEX = r"\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}"
@ -156,12 +164,18 @@ class HyperV(Tool):
if not is_ready: if not is_ready:
raise LisaException(f"VM {name} did not start") raise LisaException(f"VM {name} did not start")
def get_first_switch(self) -> str: def get_default_external_switch(self) -> Optional[VMSwitch]:
return self.node.tools[PowerShell].run_cmdlet( switch_json = self.node.tools[PowerShell].run_cmdlet(
"Get-VMSwitch | Select -First 1 -ExpandProperty Name", 'Get-VMSwitch | Where-Object {$_.SwitchType -eq "External"} '
"| Select -First 1 | select Name | ConvertTo-Json",
force_run=True, force_run=True,
) )
if not switch_json:
return None
return VMSwitch.from_json(switch_json) # type: ignore
def exists_switch(self, name: str) -> bool: def exists_switch(self, name: str) -> bool:
output = self.node.tools[PowerShell].run_cmdlet( output = self.node.tools[PowerShell].run_cmdlet(
f"Get-VMSwitch -Name {name}", f"Get-VMSwitch -Name {name}",