Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 25 additions & 12 deletions tools/toolsHCK.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -1232,7 +1232,7 @@ function queuetest {
Write-Output ""
Write-Output "Usage:"
Write-Output ""
Write-Output "queuetest <testid> <targetkey> <projectname> <testmachine> <poolname> [-sup <name>]"
Write-Output "queuetest <testid> <targetkey> <projectname> <testmachine> <poolname> [-sup <json>]"
Write-Output " [-IPv6 <address>] [-help] [-parameters <parameters>]"
Write-Output ""
Write-Output "Any parameter in [] is optional."
Expand All @@ -1254,8 +1254,10 @@ function queuetest {
Write-Output ""
Write-Output " parameters = Additional parameters in JSON format '{ ParameterName1: ParameterValue, ParameterName2: ParameterValue2 }'."
Write-Output ""
Write-Output " sup = The support machine's name as registered with the HCK\HLK controller."
Write-Output " NOTE: test machine should be in a READY state."
Write-Output " sup = A JSON object mapping HLK role names to arrays of machine names."
Write-Output " Roles not listed default to the primary test machine."
Write-Output " Example: '{""MC"":[""CL2""],""SC"":[""CL3""]}'"
Write-Output " NOTE: support machines should be in a READY state."
Write-Output ""
Write-Output "NOTE: Windows HCK\HLK Studio should be installed on the machine running the script!"
}
Expand Down Expand Up @@ -1305,20 +1307,31 @@ function queuetest {
}

if (-Not [String]::IsNullOrEmpty($sup)) {
if (-Not ($WntdSMachine = $WntdPool.GetMachines()| Where-Object { $_.Name -eq $sup })) { throw "The support machine was not found, aborting..." }
$supData = ConvertFrom-Json $sup
$MachineSet = $WntdTest.GetMachineRole()
$RoleMachines = [System.Collections.Generic.List[object]]::new()

$roleAliases = @{ 'MasterClient' = 'MC'; 'StressClient' = 'SC' }

foreach ($Role in $MachineSet.Roles) {
$RoleMachines.AddRange($Role.GetMachines())
$RoleMachines | foreach { $Role.RemoveMachine($_) }
$RoleMachines.Clear()
if ($Role.Name -eq "Client") {
$Role.AddMachine($WntdMachine)
$existing = [System.Collections.Generic.List[object]]::new()
$existing.AddRange($Role.GetMachines())
$existing | ForEach-Object { $Role.RemoveMachine($_) }

$machineNames = $supData.PSObject.Properties[$Role.Name]
if (-Not $machineNames -and $roleAliases.ContainsKey($Role.Name)) {
$machineNames = $supData.PSObject.Properties[$roleAliases[$Role.Name]]
}
if ($Role.Name -eq "Support") {
$Role.AddMachine($WntdSMachine)
if ($machineNames) {
foreach ($machineName in $machineNames.Value) {
if (-Not ($sm = $WntdPool.GetMachines() | Where-Object { $_.Name -eq $machineName.Trim() })) { throw "Machine '$machineName' for role '$($Role.Name)' was not found, aborting..." }
$Role.AddMachine($sm)
}
} else {
# Roles not listed in $supData (e.g. "Client"/"SUT") default to the primary test machine.
$Role.AddMachine($WntdMachine)
}
}
Comment on lines +1310 to 1333

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

There are two key areas of improvement in this block:

  1. Backward Compatibility: Currently, passing a plain machine name (e.g., 'CL2') to -sup will cause ConvertFrom-Json to throw an exception, breaking existing automation. We can check if $sup starts with { to parse it as JSON, and otherwise fall back to treating it as a single support machine name.
  2. Idiomatic PowerShell Property Access: Instead of using the verbose .PSObject.Properties[...] and .Value syntax, we can use standard PowerShell dynamic member access (e.g., $supData.$($Role.Name)). Wrapping the result in @(...) ensures it is always treated as an array, whether it's a single string or a list of strings.
        $supData = $null
        if ($sup -like '{*') {
            $supData = ConvertFrom-Json $sup
        } else {
            # Backward compatibility: treat $sup as a single support machine name
            $supData = [PSCustomObject]@{ 'Support' = @($sup) }
        }
        $MachineSet = $WntdTest.GetMachineRole()

        $roleAliases = @{ 'MasterClient' = 'MC'; 'StressClient' = 'SC' }

        foreach ($Role in $MachineSet.Roles) {
            $existing = [System.Collections.Generic.List[object]]::new()
            $existing.AddRange($Role.GetMachines())
            $existing | ForEach-Object { $Role.RemoveMachine($_) }

            $machineNames = $supData.$($Role.Name)
            if (-Not $machineNames -and $roleAliases.ContainsKey($Role.Name)) {
                $machineNames = $supData.$($roleAliases[$Role.Name])
            }
            if ($machineNames) {
                foreach ($machineName in @($machineNames)) {
                    if (-Not ($sm = $WntdPool.GetMachines() | Where-Object { $_.Name -eq $machineName.Trim() })) { throw "Machine '$machineName' for role '$($Role.Name)' was not found, aborting..." }
                    $Role.AddMachine($sm)
                }
            } else {
                # Roles not listed in $supData (e.g. "Client"/"SUT") default to the primary test machine.
                $Role.AddMachine($WntdMachine)
            }
        }

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Backward compatibility is not needed since AutoHCK was updated to always send JSON


$MachineSet.ApplyMachineDimensions()
$WntdTest.QueueTest($MachineSet) | Out-Null
} else {
Expand Down
Loading