summaryrefslogtreecommitdiffstats
path: root/test/integration/targets/module_utils_Ansible.ModuleUtils.CommandUtil/library/command_util_test.ps1
blob: ebffae7ff0e0bc929cdcf86a21894f6c20a88326 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#!powershell

#Requires -Module Ansible.ModuleUtils.Legacy
#Requires -Module Ansible.ModuleUtils.CommandUtil

$ErrorActionPreference = 'Stop'

$params = Parse-Args $args
$exe = Get-AnsibleParam -obj $params -name "exe" -type "path" -failifempty $true

$result = @{
    changed = $false
}

$exe_directory = Split-Path -Path $exe -Parent
$exe_filename = Split-Path -Path $exe -Leaf
$test_name = $null

Function Assert-Equal($actual, $expected) {
    if ($actual -cne $expected) {
        Fail-Json -obj $result -message "Test $test_name failed`nActual: '$actual' != Expected: '$expected'"
    }
}

$test_name = "full exe path"
$actual = Run-Command -command "`"$exe`" arg1 arg2 `"arg 3`""
Assert-Equal -actual $actual.rc -expected 0
Assert-Equal -actual $actual.stdout -expected "arg1`r`narg2`r`narg 3`r`n"
Assert-Equal -actual $actual.stderr -expected ""
Assert-Equal -actual $actual.executable -expected $exe

$test_name = "exe in special char dir"
$tmp_dir = Join-Path -Path $env:TEMP -ChildPath "ansible .ÅÑŚÌβŁÈ [$!@^&test(;)]"
try {
    New-Item -Path $tmp_dir -ItemType Directory > $null
    $exe_special = Join-Path $tmp_dir -ChildPath "PrintArgv.exe"
    Copy-Item -LiteralPath $exe -Destination $exe_special
    $actual = Run-Command -command "`"$exe_special`" arg1 arg2 `"arg 3`""
}
finally {
    Remove-Item -LiteralPath $tmp_dir -Force -Recurse
}
Assert-Equal -actual $actual.rc -expected 0
Assert-Equal -actual $actual.stdout -expected "arg1`r`narg2`r`narg 3`r`n"
Assert-Equal -actual $actual.stderr -expected ""
Assert-Equal -actual $actual.executable -expected $exe_special

$test_name = "invalid exe path"
try {
    $actual = Run-Command -command "C:\fakepath\$exe_filename arg1"
    Fail-Json -obj $result -message "Test $test_name failed`nCommand should have thrown an exception"
}
catch {
    $expected = "Exception calling `"SearchPath`" with `"1`" argument(s): `"Could not find file 'C:\fakepath\$exe_filename'.`""
    Assert-Equal -actual $_.Exception.Message -expected $expected
}

$test_name = "exe in current folder"
$actual = Run-Command -command "$exe_filename arg1" -working_directory $exe_directory
Assert-Equal -actual $actual.rc -expected 0
Assert-Equal -actual $actual.stdout -expected "arg1`r`n"
Assert-Equal -actual $actual.stderr -expected ""
Assert-Equal -actual $actual.executable -expected $exe

$test_name = "no working directory set"
$actual = Run-Command -command "cmd.exe /c cd"
Assert-Equal -actual $actual.rc -expected 0
Assert-Equal -actual $actual.stdout -expected "$($pwd.Path)`r`n"
Assert-Equal -actual $actual.stderr -expected ""
Assert-Equal -actual $actual.executable.ToUpper() -expected "$env:SystemRoot\System32\cmd.exe".ToUpper()

$test_name = "working directory override"
$actual = Run-Command -command "cmd.exe /c cd" -working_directory $env:SystemRoot
Assert-Equal -actual $actual.rc -expected 0
Assert-Equal -actual $actual.stdout -expected "$env:SystemRoot`r`n"
Assert-Equal -actual $actual.stderr -expected ""
Assert-Equal -actual $actual.executable.ToUpper() -expected "$env:SystemRoot\System32\cmd.exe".ToUpper()

$test_name = "working directory invalid path"
try {
    $actual = Run-Command -command "doesn't matter" -working_directory "invalid path here"
    Fail-Json -obj $result -message "Test $test_name failed`nCommand should have thrown an exception"
}
catch {
    Assert-Equal -actual $_.Exception.Message -expected "invalid working directory path 'invalid path here'"
}

$test_name = "invalid arguments"
$actual = Run-Command -command "ipconfig.exe /asdf"
Assert-Equal -actual $actual.rc -expected 1

$test_name = "test stdout and stderr streams"
$actual = Run-Command -command "cmd.exe /c echo stdout && echo stderr 1>&2"
Assert-Equal -actual $actual.rc -expected 0
Assert-Equal -actual $actual.stdout -expected "stdout `r`n"
Assert-Equal -actual $actual.stderr -expected "stderr `r`n"

$test_name = "Test UTF8 output from stdout stream"
$actual = Run-Command -command "powershell.exe -ExecutionPolicy ByPass -Command `"Write-Host '💩'`""
Assert-Equal -actual $actual.rc -expected 0
Assert-Equal -actual $actual.stdout -expected "💩`n"
Assert-Equal -actual $actual.stderr -expected ""

$test_name = "test default environment variable"
Set-Item -LiteralPath env:TESTENV -Value "test"
$actual = Run-Command -command "cmd.exe /c set"
$env_present = $actual.stdout -split "`r`n" | Where-Object { $_ -eq "TESTENV=test" }
if ($null -eq $env_present) {
    Fail-Json -obj $result -message "Test $test_name failed`nenvironment variable TESTENV not found in stdout`n$($actual.stdout)"
}

$test_name = "test custom environment variable1"
$actual = Run-Command -command "cmd.exe /c set" -environment @{ TESTENV2 = "testing" }
$env_not_present = $actual.stdout -split "`r`n" | Where-Object { $_ -eq "TESTENV=test" }
$env_present = $actual.stdout -split "`r`n" | Where-Object { $_ -eq "TESTENV2=testing" }
if ($null -ne $env_not_present) {
    Fail-Json -obj $result -message "Test $test_name failed`nenvironment variabel TESTENV found in stdout when it should be`n$($actual.stdout)"
}
if ($null -eq $env_present) {
    Fail-json -obj $result -message "Test $test_name failed`nenvironment variable TESTENV2 not found in stdout`n$($actual.stdout)"
}

$test_name = "input test"
$wrapper = @"
begin {
    `$string = ""
} process {
    `$current_input = [string]`$input
    `$string += `$current_input
} end {
    Write-Host `$string
}
"@
$encoded_wrapper = [System.Convert]::ToBase64String([System.Text.Encoding]::Unicode.GetBytes($wrapper))
$actual = Run-Command -command "powershell.exe -ExecutionPolicy ByPass -EncodedCommand $encoded_wrapper" -stdin "Ansible"
Assert-Equal -actual $actual.stdout -expected "Ansible`n"

$result.data = "success"
Exit-Json -obj $result