Separated ps module_util test targets, added WebRequest tests (#67914)
* Separated ps module_util test targets, added WebRequest tests * Simplify header test
This commit is contained in:
parent
c66ee7e994
commit
97d2d4512f
42 changed files with 615 additions and 121 deletions
|
@ -1,2 +1,3 @@
|
|||
windows
|
||||
shippable/windows/group1
|
||||
shippable/windows/smoketest
|
|
@ -0,0 +1,10 @@
|
|||
---
|
||||
- name: call module with AddType tests
|
||||
add_type_test:
|
||||
register: add_type_test
|
||||
|
||||
- name: assert call module with AddType tests
|
||||
assert:
|
||||
that:
|
||||
- not add_type_test is failed
|
||||
- add_type_test.res == 'success'
|
|
@ -0,0 +1,3 @@
|
|||
windows
|
||||
shippable/windows/group1
|
||||
shippable/windows/smoketest
|
|
@ -40,7 +40,7 @@ namespace Ansible.Command
|
|||
}
|
||||
'@
|
||||
|
||||
Function Run-Process($executable, $arguments) {
|
||||
Function Invoke-Process($executable, $arguments) {
|
||||
$proc = New-Object System.Diagnostics.Process
|
||||
$psi = $proc.StartInfo
|
||||
$psi.FileName = $executable
|
||||
|
@ -72,7 +72,7 @@ foreach ($expected in $tests) {
|
|||
$joined_string = Argv-ToString -arguments $expected
|
||||
# We can't used CommandLineToArgvW to test this out as it seems to mangle
|
||||
# \, might be something to do with unicode but not sure...
|
||||
$actual = Run-Process -executable $exe -arguments $joined_string
|
||||
$actual = Invoke-Process -executable $exe -arguments $joined_string
|
||||
|
||||
if ($expected.Count -ne $actual.Count) {
|
||||
$result.actual = $actual -join "`n"
|
|
@ -0,0 +1,3 @@
|
|||
---
|
||||
dependencies:
|
||||
- setup_win_printargv
|
|
@ -0,0 +1,9 @@
|
|||
---
|
||||
- name: call module with ArgvParser tests
|
||||
argv_parser_test:
|
||||
exe: '{{ win_printargv_path }}'
|
||||
register: argv_test
|
||||
|
||||
- assert:
|
||||
that:
|
||||
- argv_test.data == 'success'
|
|
@ -0,0 +1,3 @@
|
|||
windows
|
||||
shippable/windows/group1
|
||||
shippable/windows/smoketest
|
|
@ -52,12 +52,12 @@ $tests = @{
|
|||
|
||||
"Test backup file in check mode" = {
|
||||
$orig_file = Join-Path -Path $tmp_dir -ChildPath "file-check.txt"
|
||||
Set-Content -Path $orig_file -Value "abc"
|
||||
Set-Content -LiteralPath $orig_file -Value "abc"
|
||||
$actual = Backup-File -path $orig_file -WhatIf
|
||||
|
||||
(Test-Path -LiteralPath $actual) | Assert-Equals -Expected $false
|
||||
|
||||
$parent_dir = Split-Path -Path $actual
|
||||
$parent_dir = Split-Path -LiteralPath $actual
|
||||
$backup_file = Split-Path -Path $actual -Leaf
|
||||
$parent_dir | Assert-Equals -Expected $tmp_dir
|
||||
($backup_file -match "^file-check\.txt\.$pid\.\d{8}-\d{6}\.bak$") | Assert-Equals -Expected $true
|
||||
|
@ -66,16 +66,16 @@ $tests = @{
|
|||
"Test backup file" = {
|
||||
$content = "abc"
|
||||
$orig_file = Join-Path -Path $tmp_dir -ChildPath "file.txt"
|
||||
Set-Content -Path $orig_file -Value $content
|
||||
Set-Content -LiteralPath $orig_file -Value $content
|
||||
$actual = Backup-File -path $orig_file
|
||||
|
||||
(Test-Path -LiteralPath $actual) | Assert-Equals -Expected $true
|
||||
|
||||
$parent_dir = Split-Path -Path $actual
|
||||
$parent_dir = Split-Path -LiteralPath $actual
|
||||
$backup_file = Split-Path -Path $actual -Leaf
|
||||
$parent_dir | Assert-Equals -Expected $tmp_dir
|
||||
($backup_file -match "^file\.txt\.$pid\.\d{8}-\d{6}\.bak$") | Assert-Equals -Expected $true
|
||||
(Get-Content -Path $actual -Raw) | Assert-Equals -Expected "$content`r`n"
|
||||
(Get-Content -LiteralPath $actual -Raw) | Assert-Equals -Expected "$content`r`n"
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
---
|
||||
- name: call module with BackupFile tests
|
||||
backup_file_test:
|
||||
register: backup_file_test
|
||||
|
||||
- name: assert call module with BackupFile tests
|
||||
assert:
|
||||
that:
|
||||
- not backup_file_test is failed
|
||||
- backup_file_test.res == 'success'
|
|
@ -0,0 +1,3 @@
|
|||
windows
|
||||
shippable/windows/group1
|
||||
shippable/windows/smoketest
|
|
@ -0,0 +1,8 @@
|
|||
---
|
||||
- name: call module with camel conversion tests
|
||||
camel_conversion_test:
|
||||
register: camel_conversion
|
||||
|
||||
- assert:
|
||||
that:
|
||||
- camel_conversion.data == 'success'
|
|
@ -0,0 +1,3 @@
|
|||
windows
|
||||
shippable/windows/group1
|
||||
shippable/windows/smoketest
|
|
@ -98,7 +98,7 @@ Assert-Equals -actual $actual.stdout -expected "💩`n"
|
|||
Assert-Equals -actual $actual.stderr -expected ""
|
||||
|
||||
$test_name = "test default environment variable"
|
||||
Set-Item -Path env:TESTENV -Value "test"
|
||||
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) {
|
|
@ -0,0 +1,3 @@
|
|||
---
|
||||
dependencies:
|
||||
- setup_win_printargv
|
|
@ -0,0 +1,9 @@
|
|||
---
|
||||
- name: call module with CommandUtil tests
|
||||
command_util_test:
|
||||
exe: '{{ win_printargv_path }}'
|
||||
register: command_util
|
||||
|
||||
- assert:
|
||||
that:
|
||||
- command_util.data == 'success'
|
|
@ -0,0 +1,3 @@
|
|||
windows
|
||||
shippable/windows/group1
|
||||
shippable/windows/smoketest
|
|
@ -0,0 +1,8 @@
|
|||
---
|
||||
- name: call module with FileUtil tests
|
||||
file_util_test:
|
||||
register: file_util_test
|
||||
|
||||
- assert:
|
||||
that:
|
||||
- file_util_test.data == 'success'
|
|
@ -0,0 +1,3 @@
|
|||
windows
|
||||
shippable/windows/group1
|
||||
shippable/windows/smoketest
|
|
@ -2,7 +2,7 @@
|
|||
# They are being run as part of the Windows smoke tests. Please do not significantly
|
||||
# increase the size of these tests, as the smoke tests need to remain fast.
|
||||
# Any significant additions should be made to the (as yet nonexistent) PS module_utils unit tests.
|
||||
|
||||
---
|
||||
- name: find a nonexistent drive letter
|
||||
raw: foreach($c in [char[]]([char]'D'..[char]'Z')) { If (-not $(Get-PSDrive $c -ErrorAction SilentlyContinue)) { return $c } }
|
||||
register: bogus_driveletter
|
|
@ -0,0 +1,3 @@
|
|||
windows
|
||||
shippable/windows/group1
|
||||
shippable/windows/smoketest
|
|
@ -0,0 +1,8 @@
|
|||
---
|
||||
- name: call module with symbolic link tests
|
||||
symbolic_link_test:
|
||||
register: symbolic_link
|
||||
|
||||
- assert:
|
||||
that:
|
||||
- symbolic_link.data == 'success'
|
|
@ -0,0 +1,3 @@
|
|||
windows
|
||||
shippable/windows/group1
|
||||
shippable/windows/smoketest
|
|
@ -0,0 +1,8 @@
|
|||
---
|
||||
- name: call module with PrivilegeUtil tests
|
||||
privilege_util_test:
|
||||
register: privilege_util_test
|
||||
|
||||
- assert:
|
||||
that:
|
||||
- privilege_util_test.data == 'success'
|
|
@ -0,0 +1,3 @@
|
|||
windows
|
||||
shippable/windows/group1
|
||||
shippable/windows/smoketest
|
|
@ -0,0 +1,22 @@
|
|||
---
|
||||
- block:
|
||||
- name: create test user with well know SID as the name
|
||||
win_user:
|
||||
name: S-1-0-0
|
||||
password: AbcDef123!@#
|
||||
state: present
|
||||
|
||||
- name: call module with SID tests
|
||||
sid_utils_test:
|
||||
sid_account: S-1-0-0
|
||||
register: sid_test
|
||||
|
||||
always:
|
||||
- name: remove test SID user
|
||||
win_user:
|
||||
name: S-1-0-0
|
||||
state: absent
|
||||
|
||||
- assert:
|
||||
that:
|
||||
- sid_test.data == 'success'
|
|
@ -0,0 +1,4 @@
|
|||
windows
|
||||
shippable/windows/group1
|
||||
shippable/windows/smoketest
|
||||
needs/httptester
|
|
@ -0,0 +1,449 @@
|
|||
#!powershell
|
||||
|
||||
#AnsibleRequires -CSharpUtil Ansible.Basic
|
||||
#Requires -Module Ansible.ModuleUtils.WebRequest
|
||||
|
||||
$spec = @{
|
||||
options = @{
|
||||
httpbin_host = @{ type = 'str'; required = $true }
|
||||
}
|
||||
}
|
||||
|
||||
$module = [Ansible.Basic.AnsibleModule]::Create($args, $spec)
|
||||
|
||||
$httpbin_host = $module.Params.httpbin_host
|
||||
|
||||
Function Assert-Equals {
|
||||
param(
|
||||
[Parameter(Mandatory=$true, ValueFromPipeline=$true)][AllowNull()]$Actual,
|
||||
[Parameter(Mandatory=$true, Position=0)][AllowNull()]$Expected
|
||||
)
|
||||
|
||||
$matched = $false
|
||||
if ($Actual -is [System.Collections.ArrayList] -or $Actual -is [Array] -or $Actual -is [System.Collections.IList]) {
|
||||
$Actual.Count | Assert-Equals -Expected $Expected.Count
|
||||
for ($i = 0; $i -lt $Actual.Count; $i++) {
|
||||
$actualValue = $Actual[$i]
|
||||
$expectedValue = $Expected[$i]
|
||||
Assert-Equals -Actual $actualValue -Expected $expectedValue
|
||||
}
|
||||
$matched = $true
|
||||
} else {
|
||||
$matched = $Actual -ceq $Expected
|
||||
}
|
||||
|
||||
if (-not $matched) {
|
||||
if ($Actual -is [PSObject]) {
|
||||
$Actual = $Actual.ToString()
|
||||
}
|
||||
|
||||
$call_stack = (Get-PSCallStack)[1]
|
||||
$module.Result.test = $test
|
||||
$module.Result.actual = $Actual
|
||||
$module.Result.expected = $Expected
|
||||
$module.Result.line = $call_stack.ScriptLineNumber
|
||||
$module.Result.method = $call_stack.Position.Text
|
||||
|
||||
$module.FailJson("AssertionError: actual != expected")
|
||||
}
|
||||
}
|
||||
|
||||
Function Convert-StreamToString {
|
||||
[CmdletBinding()]
|
||||
param (
|
||||
[Parameter(Mandatory=$true)]
|
||||
[System.IO.Stream]
|
||||
$Stream
|
||||
)
|
||||
|
||||
$ms = New-Object -TypeName System.IO.MemoryStream
|
||||
try {
|
||||
$Stream.CopyTo($ms)
|
||||
[System.Text.Encoding]::UTF8.GetString($ms.ToArray())
|
||||
} finally {
|
||||
$ms.Dispose()
|
||||
}
|
||||
}
|
||||
|
||||
$tests = [Ordered]@{
|
||||
'GET request over http' = {
|
||||
$r = Get-AnsibleWebRequest -Uri "http://$httpbin_host/get"
|
||||
|
||||
$r.Method | Assert-Equals -Expected 'GET'
|
||||
$r.Timeout | Assert-Equals -Expected 30000
|
||||
$r.UseDefaultCredentials | Assert-Equals -Expected $false
|
||||
$r.Credentials | Assert-Equals -Expected $null
|
||||
$r.ClientCertificates.Count | Assert-Equals -Expected 0
|
||||
$r.Proxy.Credentials | Assert-Equals -Expected $null
|
||||
$r.UserAgent | Assert-Equals -Expected 'ansible-httpget'
|
||||
|
||||
$actual = Invoke-WithWebRequest -Module $module -Request $r -Script {
|
||||
Param ([System.Net.WebResponse]$Response, [System.IO.Stream]$Stream)
|
||||
|
||||
$Response.StatusCode | Assert-Equals -Expected 200
|
||||
Convert-StreamToString -Stream $Stream
|
||||
} | ConvertFrom-Json
|
||||
|
||||
$actual.headers.'User-Agent' | Assert-Equals -Expected 'ansible-httpget'
|
||||
$actual.headers.'Host' | Assert-Equals -Expected $httpbin_host
|
||||
|
||||
$module.Result.msg | Assert-Equals -Expected 'OK'
|
||||
$module.Result.status_code | Assert-Equals -Expected 200
|
||||
$module.Result.ContainsKey('elapsed') | Assert-Equals -Expected $true
|
||||
}
|
||||
|
||||
'GET request over https' = {
|
||||
# url is an alias for the -Uri parameter.
|
||||
$r = Get-AnsibleWebRequest -url "https://$httpbin_host/get"
|
||||
|
||||
$r.Method | Assert-Equals -Expected 'GET'
|
||||
$r.Timeout | Assert-Equals -Expected 30000
|
||||
$r.UseDefaultCredentials | Assert-Equals -Expected $false
|
||||
$r.Credentials | Assert-Equals -Expected $null
|
||||
$r.ClientCertificates.Count | Assert-Equals -Expected 0
|
||||
$r.Proxy.Credentials | Assert-Equals -Expected $null
|
||||
$r.UserAgent | Assert-Equals -Expected 'ansible-httpget'
|
||||
|
||||
$actual = Invoke-WithWebRequest -Module $module -Request $r -Script {
|
||||
Param ([System.Net.WebResponse]$Response, [System.IO.Stream]$Stream)
|
||||
|
||||
$Response.StatusCode | Assert-Equals -Expected 200
|
||||
Convert-StreamToString -Stream $Stream
|
||||
} | ConvertFrom-Json
|
||||
|
||||
$actual.headers.'User-Agent' | Assert-Equals -Expected 'ansible-httpget'
|
||||
$actual.headers.'Host' | Assert-Equals -Expected $httpbin_host
|
||||
}
|
||||
|
||||
'POST request' = {
|
||||
$getParams = @{
|
||||
Headers = @{
|
||||
'Content-Type' = 'application/json'
|
||||
}
|
||||
Method = 'POST'
|
||||
Uri = "https://$httpbin_host/post"
|
||||
}
|
||||
$r = Get-AnsibleWebRequest @getParams
|
||||
|
||||
$r.Method | Assert-Equals -Expected 'POST'
|
||||
$r.Timeout | Assert-Equals -Expected 30000
|
||||
$r.UseDefaultCredentials | Assert-Equals -Expected $false
|
||||
$r.Credentials | Assert-Equals -Expected $null
|
||||
$r.ClientCertificates.Count | Assert-Equals -Expected 0
|
||||
$r.Proxy.Credentials | Assert-Equals -Expected $null
|
||||
$r.ContentType | Assert-Equals -Expected 'application/json'
|
||||
$r.UserAgent | Assert-Equals -Expected 'ansible-httpget'
|
||||
|
||||
$body = New-Object -TypeName System.IO.MemoryStream -ArgumentList @(,
|
||||
([System.Text.Encoding]::UTF8.GetBytes('{"foo":"bar"}'))
|
||||
)
|
||||
$actual = Invoke-WithWebRequest -Module $module -Request $r -Body $body -Script {
|
||||
Param ([System.Net.WebResponse]$Response, [System.IO.Stream]$Stream)
|
||||
|
||||
$Response.StatusCode | Assert-Equals -Expected 200
|
||||
Convert-StreamToString -Stream $Stream
|
||||
} | ConvertFrom-Json
|
||||
|
||||
$actual.headers.'User-Agent' | Assert-Equals -Expected 'ansible-httpget'
|
||||
$actual.headers.'Host' | Assert-Equals -Expected $httpbin_host
|
||||
$actual.data | Assert-Equals -Expected '{"foo":"bar"}'
|
||||
}
|
||||
|
||||
'Safe redirection of GET' = {
|
||||
$r = Get-AnsibleWebRequest -Uri "http://$httpbin_host/redirect/2"
|
||||
|
||||
Invoke-WithWebRequest -Module $module -Request $r -Script {
|
||||
Param ([System.Net.WebResponse]$Response, [System.IO.Stream]$Stream)
|
||||
|
||||
$Response.ResponseUri | Assert-Equals -Expected "http://$httpbin_host/get"
|
||||
$Response.StatusCode | Assert-Equals -Expected 200
|
||||
}
|
||||
}
|
||||
|
||||
'Safe redirection of HEAD' = {
|
||||
$r = Get-AnsibleWebRequest -Uri "http://$httpbin_host/redirect/2" -Method HEAD
|
||||
|
||||
Invoke-WithWebRequest -Module $module -Request $r -Script {
|
||||
Param ([System.Net.WebResponse]$Response, [System.IO.Stream]$Stream)
|
||||
|
||||
$Response.ResponseUri | Assert-Equals -Expected "http://$httpbin_host/get"
|
||||
$Response.StatusCode | Assert-Equals -Expected 200
|
||||
}
|
||||
}
|
||||
|
||||
'Safe redirection of PUT' = {
|
||||
$params = @{
|
||||
Method = 'PUT'
|
||||
Uri = "http://$httpbin_host/redirect-to?url=https://$httpbin_host/put"
|
||||
}
|
||||
$r = Get-AnsibleWebRequest @params
|
||||
|
||||
Invoke-WithWebRequest -Module $module -Request $r -Script {
|
||||
Param ([System.Net.WebResponse]$Response, [System.IO.Stream]$Stream)
|
||||
|
||||
$Response.ResponseUri | Assert-Equals -Expected $r.RequestUri
|
||||
$Response.StatusCode | Assert-Equals -Expected 302
|
||||
}
|
||||
}
|
||||
|
||||
'None redirection of GET' = {
|
||||
$params = @{
|
||||
FollowRedirects = 'None'
|
||||
Uri = "http://$httpbin_host/redirect/2"
|
||||
}
|
||||
$r = Get-AnsibleWebRequest @params
|
||||
|
||||
Invoke-WithWebRequest -Module $module -Request $r -Script {
|
||||
Param ([System.Net.WebResponse]$Response, [System.IO.Stream]$Stream)
|
||||
|
||||
$Response.ResponseUri | Assert-Equals -Expected $r.RequestUri
|
||||
$Response.StatusCode | Assert-Equals -Expected 302
|
||||
}
|
||||
}
|
||||
|
||||
'None redirection of HEAD' = {
|
||||
$params = @{
|
||||
follow_redirects = 'None'
|
||||
method = 'HEAD'
|
||||
Uri = "http://$httpbin_host/redirect/2"
|
||||
}
|
||||
$r = Get-AnsibleWebRequest @params
|
||||
|
||||
Invoke-WithWebRequest -Module $module -Request $r -Script {
|
||||
Param ([System.Net.WebResponse]$Response, [System.IO.Stream]$Stream)
|
||||
|
||||
$Response.ResponseUri | Assert-Equals -Expected $r.RequestUri
|
||||
$Response.StatusCode | Assert-Equals -Expected 302
|
||||
}
|
||||
}
|
||||
|
||||
'None redirection of PUT' = {
|
||||
$params = @{
|
||||
FollowRedirects = 'None'
|
||||
Method = 'PUT'
|
||||
Uri = "http://$httpbin_host/redirect-to?url=https://$httpbin_host/put"
|
||||
}
|
||||
$r = Get-AnsibleWebRequest @params
|
||||
|
||||
Invoke-WithWebRequest -Module $module -Request $r -Script {
|
||||
Param ([System.Net.WebResponse]$Response, [System.IO.Stream]$Stream)
|
||||
|
||||
$Response.ResponseUri | Assert-Equals -Expected $r.RequestUri
|
||||
$Response.StatusCode | Assert-Equals -Expected 302
|
||||
}
|
||||
}
|
||||
|
||||
'All redirection of GET' = {
|
||||
$params = @{
|
||||
FollowRedirects = 'All'
|
||||
Uri = "http://$httpbin_host/redirect/2"
|
||||
}
|
||||
$r = Get-AnsibleWebRequest @params
|
||||
|
||||
Invoke-WithWebRequest -Module $module -Request $r -Script {
|
||||
Param ([System.Net.WebResponse]$Response, [System.IO.Stream]$Stream)
|
||||
|
||||
$Response.ResponseUri | Assert-Equals -Expected "http://$httpbin_host/get"
|
||||
$Response.StatusCode | Assert-Equals -Expected 200
|
||||
}
|
||||
}
|
||||
|
||||
'All redirection of HEAD' = {
|
||||
$params = @{
|
||||
follow_redirects = 'All'
|
||||
method = 'HEAD'
|
||||
Uri = "http://$httpbin_host/redirect/2"
|
||||
}
|
||||
$r = Get-AnsibleWebRequest @params
|
||||
|
||||
Invoke-WithWebRequest -Module $module -Request $r -Script {
|
||||
Param ([System.Net.WebResponse]$Response, [System.IO.Stream]$Stream)
|
||||
|
||||
$Response.ResponseUri | Assert-Equals -Expected "http://$httpbin_host/get"
|
||||
$Response.StatusCode | Assert-Equals -Expected 200
|
||||
}
|
||||
}
|
||||
|
||||
'All redirection of PUT' = {
|
||||
$params = @{
|
||||
FollowRedirects = 'All'
|
||||
Method = 'PUT'
|
||||
Uri = "http://$httpbin_host/redirect-to?url=https://$httpbin_host/put"
|
||||
}
|
||||
$r = Get-AnsibleWebRequest @params
|
||||
|
||||
Invoke-WithWebRequest -Module $module -Request $r -Script {
|
||||
Param ([System.Net.WebResponse]$Response, [System.IO.Stream]$Stream)
|
||||
|
||||
$Response.ResponseUri | Assert-Equals -Expected "https://$httpbin_host/put"
|
||||
$Response.StatusCode | Assert-Equals -Expected 200
|
||||
}
|
||||
}
|
||||
|
||||
'Exceeds maximum redirection - ignored' = {
|
||||
$params = @{
|
||||
MaximumRedirection = 4
|
||||
Uri = "https://$httpbin_host/redirect/5"
|
||||
}
|
||||
$r = Get-AnsibleWebRequest @params
|
||||
|
||||
Invoke-WithWebRequest -Module $module -Request $r -IgnoreBadResponse -Script {
|
||||
Param ([System.Net.WebResponse]$Response, [System.IO.Stream]$Stream)
|
||||
|
||||
$Response.ResponseUri | Assert-Equals -Expected "https://$httpbin_host/relative-redirect/1"
|
||||
$Response.StatusCode | Assert-Equals -Expected 302
|
||||
}
|
||||
}
|
||||
|
||||
'Exceeds maximum redirection - exception' = {
|
||||
$params = @{
|
||||
MaximumRedirection = 1
|
||||
Uri = "https://$httpbin_host/redirect/2"
|
||||
}
|
||||
$r = Get-AnsibleWebRequest @params
|
||||
|
||||
$failed = $false
|
||||
try {
|
||||
$null = Invoke-WithWebRequest -Module $module -Request $r -Script {}
|
||||
} catch {
|
||||
$_.Exception.GetType().Name | Assert-Equals -Expected 'WebException'
|
||||
$_.Exception.Message | Assert-Equals -Expected 'Too many automatic redirections were attempted.'
|
||||
$failed = $true
|
||||
}
|
||||
$failed | Assert-Equals -Expected $true
|
||||
}
|
||||
|
||||
'Basic auth as Credential' = {
|
||||
$params = @{
|
||||
Url = "http://$httpbin_host/basic-auth/username/password"
|
||||
UrlUsername = 'username'
|
||||
UrlPassword = 'password'
|
||||
}
|
||||
$r = Get-AnsibleWebRequest @params
|
||||
|
||||
Invoke-WithWebRequest -Module $module -Request $r -IgnoreBadResponse -Script {
|
||||
Param ([System.Net.WebResponse]$Response, [System.IO.Stream]$Stream)
|
||||
|
||||
$Response.StatusCode | Assert-Equals -Expected 200
|
||||
}
|
||||
}
|
||||
|
||||
'Basic auth as Header' = {
|
||||
$params = @{
|
||||
Url = "http://$httpbin_host/basic-auth/username/password"
|
||||
url_username = 'username'
|
||||
url_password = 'password'
|
||||
ForceBasicAuth = $true
|
||||
}
|
||||
$r = Get-AnsibleWebRequest @params
|
||||
|
||||
Invoke-WithWebRequest -Module $module -Request $r -IgnoreBadResponse -Script {
|
||||
Param ([System.Net.WebResponse]$Response, [System.IO.Stream]$Stream)
|
||||
|
||||
$Response.StatusCode | Assert-Equals -Expected 200
|
||||
}
|
||||
}
|
||||
|
||||
'Send request with headers' = {
|
||||
$params = @{
|
||||
Headers = @{
|
||||
'Content-Length' = 0
|
||||
testingheader = 'testing_header'
|
||||
TestHeader = 'test-header'
|
||||
'User-Agent' = 'test-agent'
|
||||
}
|
||||
Url = "https://$httpbin_host/get"
|
||||
}
|
||||
$r = Get-AnsibleWebRequest @params
|
||||
|
||||
$actual = Invoke-WithWebRequest -Module $module -Request $r -Script {
|
||||
Param ([System.Net.WebResponse]$Response, [System.IO.Stream]$Stream)
|
||||
|
||||
$Response.StatusCode | Assert-Equals -Expected 200
|
||||
Convert-StreamToString -Stream $Stream
|
||||
} | ConvertFrom-Json
|
||||
|
||||
$actual.headers.'Testheader' | Assert-Equals -Expected 'test-header'
|
||||
$actual.headers.'testingheader' | Assert-Equals -Expected 'testing_header'
|
||||
$actual.Headers.'User-Agent' | Assert-Equals -Expected 'test-agent'
|
||||
}
|
||||
|
||||
'Request with timeout' = {
|
||||
$params = @{
|
||||
Uri = "https://$httpbin_host/delay/5"
|
||||
Timeout = 1
|
||||
}
|
||||
$r = Get-AnsibleWebRequest @params
|
||||
|
||||
$failed = $false
|
||||
try {
|
||||
$null = Invoke-WithWebRequest -Module $module -Request $r -Script {}
|
||||
} catch {
|
||||
$failed = $true
|
||||
$_.Exception.GetType().Name | Assert-Equals -Expected WebException
|
||||
$_.Exception.Message | Assert-Equals -Expected 'The operation has timed out'
|
||||
}
|
||||
$failed | Assert-Equals -Expected $true
|
||||
}
|
||||
|
||||
'Request with file URI' = {
|
||||
$filePath = Join-Path $module.Tmpdir -ChildPath 'test.txt'
|
||||
Set-Content -LiteralPath $filePath -Value 'test'
|
||||
|
||||
$r = Get-AnsibleWebRequest -Uri $filePath
|
||||
|
||||
$actual = Invoke-WithWebRequest -Module $module -Request $r -Script {
|
||||
Param ([System.Net.WebResponse]$Response, [System.IO.Stream]$Stream)
|
||||
|
||||
$Response.ContentLength | Assert-Equals -Expected 6
|
||||
Convert-StreamToString -Stream $Stream
|
||||
}
|
||||
$actual | Assert-Equals -Expected "test`r`n"
|
||||
$module.Result.msg | Assert-Equals -Expected "OK"
|
||||
$module.Result.status_code | Assert-Equals -Expected 200
|
||||
}
|
||||
|
||||
'Web request based on module options' = {
|
||||
Set-Variable complex_args -Scope Global -Value @{
|
||||
url = "https://$httpbin_host/redirect/2"
|
||||
method = 'GET'
|
||||
follow_redirects = 'safe'
|
||||
headers = @{
|
||||
'User-Agent' = 'other-agent'
|
||||
}
|
||||
http_agent = 'actual-agent'
|
||||
maximum_redirection = 2
|
||||
timeout = 10
|
||||
validate_certs = $false
|
||||
}
|
||||
$spec = @{
|
||||
options = @{
|
||||
url = @{ type = 'str'; required = $true }
|
||||
test = @{ type = 'str'; choices = 'abc', 'def'}
|
||||
}
|
||||
mutually_exclusive = @(,@('url', 'test'))
|
||||
}
|
||||
$spec = Merge-WebRequestSpec -ModuleSpec $spec
|
||||
|
||||
$testModule = [Ansible.Basic.AnsibleModule]::Create(@(), $spec)
|
||||
$r = Get-AnsibleWebRequest -Url $testModule.Params.url -Module $testModule
|
||||
|
||||
$actual = Invoke-WithWebRequest -Module $testModule -Request $r -Script {
|
||||
Param ([System.Net.WebResponse]$Response, [System.IO.Stream]$Stream)
|
||||
|
||||
$Response.ResponseUri | Assert-Equals -Expected "https://$httpbin_host/get"
|
||||
Convert-StreamToString -Stream $Stream
|
||||
} | ConvertFrom-Json
|
||||
$actual.headers.'User-Agent' | Assert-Equals -Expected 'actual-agent'
|
||||
}
|
||||
}
|
||||
|
||||
# setup and teardown should favour native tools to create and delete the service and not the util we are testing.
|
||||
foreach ($testImpl in $tests.GetEnumerator()) {
|
||||
Set-Variable -Name complex_args -Scope Global -Value @{}
|
||||
$test = $testImpl.Key
|
||||
&$testImpl.Value
|
||||
}
|
||||
|
||||
$module.Result.data = "success"
|
||||
$module.ExitJson()
|
|
@ -0,0 +1,3 @@
|
|||
---
|
||||
dependencies:
|
||||
- prepare_http_tests
|
|
@ -0,0 +1,10 @@
|
|||
---
|
||||
- name: test Ansible.ModuleUtils.WebRequest
|
||||
web_request_test:
|
||||
httpbin_host: '{{ httpbin_host }}'
|
||||
register: web_request
|
||||
|
||||
- name: assert test Ansible.ModuleUtils.WebRequest succeeded
|
||||
assert:
|
||||
that:
|
||||
- web_request.data == 'success'
|
|
@ -0,0 +1,3 @@
|
|||
---
|
||||
dependencies:
|
||||
- setup_remote_tmp_dir
|
|
@ -0,0 +1,9 @@
|
|||
---
|
||||
- name: download the PrintArgv.exe binary to temp location
|
||||
win_get_url:
|
||||
url: https://ansible-ci-files.s3.amazonaws.com/test/integration/targets/setup_win_printargv/PrintArgv.exe
|
||||
dest: '{{ remote_tmp_dir }}\PrintArgv.exe'
|
||||
|
||||
- name: set fact containing PrintArgv binary path
|
||||
set_fact:
|
||||
win_printargv_path: '{{ remote_tmp_dir }}\PrintArgv.exe'
|
|
@ -60,93 +60,6 @@
|
|||
that:
|
||||
- 'recursive_requires.value == "Get-Test3: 2: Get-Test2, 1: Get-Test1, 3: Get-NewTest3"'
|
||||
|
||||
- name: call module with camel conversion tests
|
||||
camel_conversion_test:
|
||||
register: camel_conversion
|
||||
|
||||
- assert:
|
||||
that:
|
||||
- camel_conversion.data == 'success'
|
||||
|
||||
- block:
|
||||
- name: create test user with well know SID as the name
|
||||
win_user:
|
||||
name: S-1-0-0
|
||||
password: AbcDef123!@#
|
||||
state: present
|
||||
|
||||
- name: call module with SID tests
|
||||
sid_utils_test:
|
||||
sid_account: S-1-0-0
|
||||
register: sid_test
|
||||
|
||||
always:
|
||||
- name: remove test SID user
|
||||
win_user:
|
||||
name: S-1-0-0
|
||||
state: absent
|
||||
|
||||
- assert:
|
||||
that:
|
||||
- sid_test.data == 'success'
|
||||
|
||||
- name: create temp testing folder
|
||||
win_file:
|
||||
path: C:\ansible testing
|
||||
state: directory
|
||||
|
||||
- name: download binary the outputs argv to stdout
|
||||
win_get_url:
|
||||
url: https://ansible-ci-files.s3.amazonaws.com/test/integration/roles/test_win_module_utils/PrintArgv.exe
|
||||
dest: C:\ansible testing\PrintArgv.exe
|
||||
|
||||
- name: call module with CommandUtil tests
|
||||
command_util_test:
|
||||
exe: C:\ansible testing\PrintArgv.exe
|
||||
register: command_util
|
||||
|
||||
- assert:
|
||||
that:
|
||||
- command_util.data == 'success'
|
||||
|
||||
- name: call module with ArgvParser tests
|
||||
argv_parser_test:
|
||||
exe: C:\ansible testing\PrintArgv.exe
|
||||
register: argv_test
|
||||
|
||||
- assert:
|
||||
that:
|
||||
- argv_test.data == 'success'
|
||||
|
||||
- name: call module with symbolic link tests
|
||||
symbolic_link_test:
|
||||
register: symbolic_link
|
||||
|
||||
- assert:
|
||||
that:
|
||||
- symbolic_link.data == 'success'
|
||||
|
||||
- name: remove testing folder
|
||||
win_file:
|
||||
path: C:\ansible testing
|
||||
state: absent
|
||||
|
||||
- name: call module with FileUtil tests
|
||||
file_util_test:
|
||||
register: file_util_test
|
||||
|
||||
- assert:
|
||||
that:
|
||||
- file_util_test.data == 'success'
|
||||
|
||||
- name: call module with PrivilegeUtil tests
|
||||
privilege_util_test:
|
||||
register: privilege_util_test
|
||||
|
||||
- assert:
|
||||
that:
|
||||
- privilege_util_test.data == 'success'
|
||||
|
||||
- name: call module with C# reference
|
||||
csharp_util:
|
||||
register: csharp_res
|
||||
|
@ -156,23 +69,3 @@
|
|||
that:
|
||||
- not csharp_res is failed
|
||||
- csharp_res.res == '{"a":"a","b":1}'
|
||||
|
||||
- name: call module with AddType tests
|
||||
add_type_test:
|
||||
register: add_type_test
|
||||
|
||||
- name: assert call module with AddType tests
|
||||
assert:
|
||||
that:
|
||||
- not add_type_test is failed
|
||||
- add_type_test.res == 'success'
|
||||
|
||||
- name: call module with BackupFile tests
|
||||
backup_file_test:
|
||||
register: backup_file_test
|
||||
|
||||
- name: assert call module with BackupFile tests
|
||||
assert:
|
||||
that:
|
||||
- not backup_file_test is failed
|
||||
- backup_file_test.res == 'success'
|
||||
|
|
|
@ -8185,11 +8185,8 @@ test/integration/targets/win_dsc/files/xTestDsc/1.0.1/DSCResources/ANSIBLE_xTest
|
|||
test/integration/targets/win_dsc/files/xTestDsc/1.0.1/xTestDsc.psd1 pslint!skip
|
||||
test/integration/targets/win_exec_wrapper/library/test_fail.ps1 pslint:PSCustomUseLiteralPath
|
||||
test/integration/targets/win_iis_webbinding/library/test_get_webbindings.ps1 pslint:PSUseApprovedVerbs
|
||||
test/integration/targets/win_module_utils/library/argv_parser_test.ps1 pslint:PSUseApprovedVerbs
|
||||
test/integration/targets/win_module_utils/library/backup_file_test.ps1 pslint:PSCustomUseLiteralPath
|
||||
test/integration/targets/win_module_utils/library/command_util_test.ps1 pslint:PSCustomUseLiteralPath
|
||||
test/integration/targets/win_module_utils/library/legacy_only_new_way_win_line_ending.ps1 line-endings
|
||||
test/integration/targets/win_module_utils/library/legacy_only_old_way_win_line_ending.ps1 line-endings
|
||||
test/integration/targets/win_module_utils/library/legacy_only_new_way_win_line_ending.ps1 line-endings # Explicitly tests that we still work with Windows line endings
|
||||
test/integration/targets/win_module_utils/library/legacy_only_old_way_win_line_ending.ps1 line-endings # Explicitly tests that we still work with Windows line endings
|
||||
test/integration/targets/win_ping/library/win_ping_syntax_error.ps1 pslint!skip
|
||||
test/integration/targets/win_psmodule/files/module/template.psd1 pslint!skip
|
||||
test/integration/targets/win_psmodule/files/module/template.psm1 pslint!skip
|
||||
|
|
Loading…
Reference in a new issue