diff --git a/test/integration/targets/win_module_utils_legacy/aliases b/test/integration/targets/module_utils_Ansible.ModuleUtils.AddType/aliases similarity index 86% rename from test/integration/targets/win_module_utils_legacy/aliases rename to test/integration/targets/module_utils_Ansible.ModuleUtils.AddType/aliases index 1eed2ecfaf4..cf714783f5c 100644 --- a/test/integration/targets/win_module_utils_legacy/aliases +++ b/test/integration/targets/module_utils_Ansible.ModuleUtils.AddType/aliases @@ -1,2 +1,3 @@ +windows shippable/windows/group1 shippable/windows/smoketest diff --git a/test/integration/targets/win_module_utils/library/add_type_test.ps1 b/test/integration/targets/module_utils_Ansible.ModuleUtils.AddType/library/add_type_test.ps1 similarity index 100% rename from test/integration/targets/win_module_utils/library/add_type_test.ps1 rename to test/integration/targets/module_utils_Ansible.ModuleUtils.AddType/library/add_type_test.ps1 diff --git a/test/integration/targets/module_utils_Ansible.ModuleUtils.AddType/tasks/main.yml b/test/integration/targets/module_utils_Ansible.ModuleUtils.AddType/tasks/main.yml new file mode 100644 index 00000000000..4c4810be436 --- /dev/null +++ b/test/integration/targets/module_utils_Ansible.ModuleUtils.AddType/tasks/main.yml @@ -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' diff --git a/test/integration/targets/module_utils_Ansible.ModuleUtils.ArgvParser/aliases b/test/integration/targets/module_utils_Ansible.ModuleUtils.ArgvParser/aliases new file mode 100644 index 00000000000..cf714783f5c --- /dev/null +++ b/test/integration/targets/module_utils_Ansible.ModuleUtils.ArgvParser/aliases @@ -0,0 +1,3 @@ +windows +shippable/windows/group1 +shippable/windows/smoketest diff --git a/test/integration/targets/win_module_utils/library/argv_parser_test.ps1 b/test/integration/targets/module_utils_Ansible.ModuleUtils.ArgvParser/library/argv_parser_test.ps1 similarity index 96% rename from test/integration/targets/win_module_utils/library/argv_parser_test.ps1 rename to test/integration/targets/module_utils_Ansible.ModuleUtils.ArgvParser/library/argv_parser_test.ps1 index b0405414f25..d7bd4bb3e7d 100644 --- a/test/integration/targets/win_module_utils/library/argv_parser_test.ps1 +++ b/test/integration/targets/module_utils_Ansible.ModuleUtils.ArgvParser/library/argv_parser_test.ps1 @@ -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" diff --git a/test/integration/targets/module_utils_Ansible.ModuleUtils.ArgvParser/meta/main.yml b/test/integration/targets/module_utils_Ansible.ModuleUtils.ArgvParser/meta/main.yml new file mode 100644 index 00000000000..fd0dc543127 --- /dev/null +++ b/test/integration/targets/module_utils_Ansible.ModuleUtils.ArgvParser/meta/main.yml @@ -0,0 +1,3 @@ +--- +dependencies: +- setup_win_printargv diff --git a/test/integration/targets/module_utils_Ansible.ModuleUtils.ArgvParser/tasks/main.yml b/test/integration/targets/module_utils_Ansible.ModuleUtils.ArgvParser/tasks/main.yml new file mode 100644 index 00000000000..b39155e040a --- /dev/null +++ b/test/integration/targets/module_utils_Ansible.ModuleUtils.ArgvParser/tasks/main.yml @@ -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' diff --git a/test/integration/targets/module_utils_Ansible.ModuleUtils.Backup/aliases b/test/integration/targets/module_utils_Ansible.ModuleUtils.Backup/aliases new file mode 100644 index 00000000000..cf714783f5c --- /dev/null +++ b/test/integration/targets/module_utils_Ansible.ModuleUtils.Backup/aliases @@ -0,0 +1,3 @@ +windows +shippable/windows/group1 +shippable/windows/smoketest diff --git a/test/integration/targets/win_module_utils/library/backup_file_test.ps1 b/test/integration/targets/module_utils_Ansible.ModuleUtils.Backup/library/backup_file_test.ps1 similarity index 89% rename from test/integration/targets/win_module_utils/library/backup_file_test.ps1 rename to test/integration/targets/module_utils_Ansible.ModuleUtils.Backup/library/backup_file_test.ps1 index 5f920ab2bd4..15527560a18 100644 --- a/test/integration/targets/win_module_utils/library/backup_file_test.ps1 +++ b/test/integration/targets/module_utils_Ansible.ModuleUtils.Backup/library/backup_file_test.ps1 @@ -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" } } diff --git a/test/integration/targets/module_utils_Ansible.ModuleUtils.Backup/tasks/main.yml b/test/integration/targets/module_utils_Ansible.ModuleUtils.Backup/tasks/main.yml new file mode 100644 index 00000000000..cb979ebc48b --- /dev/null +++ b/test/integration/targets/module_utils_Ansible.ModuleUtils.Backup/tasks/main.yml @@ -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' diff --git a/test/integration/targets/module_utils_Ansible.ModuleUtils.CamelConversion/aliases b/test/integration/targets/module_utils_Ansible.ModuleUtils.CamelConversion/aliases new file mode 100644 index 00000000000..cf714783f5c --- /dev/null +++ b/test/integration/targets/module_utils_Ansible.ModuleUtils.CamelConversion/aliases @@ -0,0 +1,3 @@ +windows +shippable/windows/group1 +shippable/windows/smoketest diff --git a/test/integration/targets/win_module_utils/library/camel_conversion_test.ps1 b/test/integration/targets/module_utils_Ansible.ModuleUtils.CamelConversion/library/camel_conversion_test.ps1 similarity index 100% rename from test/integration/targets/win_module_utils/library/camel_conversion_test.ps1 rename to test/integration/targets/module_utils_Ansible.ModuleUtils.CamelConversion/library/camel_conversion_test.ps1 diff --git a/test/integration/targets/module_utils_Ansible.ModuleUtils.CamelConversion/tasks/main.yml b/test/integration/targets/module_utils_Ansible.ModuleUtils.CamelConversion/tasks/main.yml new file mode 100644 index 00000000000..f28ea30dcf7 --- /dev/null +++ b/test/integration/targets/module_utils_Ansible.ModuleUtils.CamelConversion/tasks/main.yml @@ -0,0 +1,8 @@ +--- +- name: call module with camel conversion tests + camel_conversion_test: + register: camel_conversion + +- assert: + that: + - camel_conversion.data == 'success' diff --git a/test/integration/targets/module_utils_Ansible.ModuleUtils.CommandUtil/aliases b/test/integration/targets/module_utils_Ansible.ModuleUtils.CommandUtil/aliases new file mode 100644 index 00000000000..cf714783f5c --- /dev/null +++ b/test/integration/targets/module_utils_Ansible.ModuleUtils.CommandUtil/aliases @@ -0,0 +1,3 @@ +windows +shippable/windows/group1 +shippable/windows/smoketest diff --git a/test/integration/targets/win_module_utils/library/command_util_test.ps1 b/test/integration/targets/module_utils_Ansible.ModuleUtils.CommandUtil/library/command_util_test.ps1 similarity index 99% rename from test/integration/targets/win_module_utils/library/command_util_test.ps1 rename to test/integration/targets/module_utils_Ansible.ModuleUtils.CommandUtil/library/command_util_test.ps1 index a015d35c8c9..6e644fe242f 100644 --- a/test/integration/targets/win_module_utils/library/command_util_test.ps1 +++ b/test/integration/targets/module_utils_Ansible.ModuleUtils.CommandUtil/library/command_util_test.ps1 @@ -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) { diff --git a/test/integration/targets/module_utils_Ansible.ModuleUtils.CommandUtil/meta/main.yml b/test/integration/targets/module_utils_Ansible.ModuleUtils.CommandUtil/meta/main.yml new file mode 100644 index 00000000000..fd0dc543127 --- /dev/null +++ b/test/integration/targets/module_utils_Ansible.ModuleUtils.CommandUtil/meta/main.yml @@ -0,0 +1,3 @@ +--- +dependencies: +- setup_win_printargv diff --git a/test/integration/targets/module_utils_Ansible.ModuleUtils.CommandUtil/tasks/main.yml b/test/integration/targets/module_utils_Ansible.ModuleUtils.CommandUtil/tasks/main.yml new file mode 100644 index 00000000000..3001518b80e --- /dev/null +++ b/test/integration/targets/module_utils_Ansible.ModuleUtils.CommandUtil/tasks/main.yml @@ -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' diff --git a/test/integration/targets/module_utils_Ansible.ModuleUtils.FileUtil/aliases b/test/integration/targets/module_utils_Ansible.ModuleUtils.FileUtil/aliases new file mode 100644 index 00000000000..cf714783f5c --- /dev/null +++ b/test/integration/targets/module_utils_Ansible.ModuleUtils.FileUtil/aliases @@ -0,0 +1,3 @@ +windows +shippable/windows/group1 +shippable/windows/smoketest diff --git a/test/integration/targets/win_module_utils/library/file_util_test.ps1 b/test/integration/targets/module_utils_Ansible.ModuleUtils.FileUtil/library/file_util_test.ps1 similarity index 100% rename from test/integration/targets/win_module_utils/library/file_util_test.ps1 rename to test/integration/targets/module_utils_Ansible.ModuleUtils.FileUtil/library/file_util_test.ps1 diff --git a/test/integration/targets/module_utils_Ansible.ModuleUtils.FileUtil/tasks/main.yml b/test/integration/targets/module_utils_Ansible.ModuleUtils.FileUtil/tasks/main.yml new file mode 100644 index 00000000000..a636d32ec74 --- /dev/null +++ b/test/integration/targets/module_utils_Ansible.ModuleUtils.FileUtil/tasks/main.yml @@ -0,0 +1,8 @@ +--- +- name: call module with FileUtil tests + file_util_test: + register: file_util_test + +- assert: + that: + - file_util_test.data == 'success' diff --git a/test/integration/targets/module_utils_Ansible.ModuleUtils.Legacy/aliases b/test/integration/targets/module_utils_Ansible.ModuleUtils.Legacy/aliases new file mode 100644 index 00000000000..cf714783f5c --- /dev/null +++ b/test/integration/targets/module_utils_Ansible.ModuleUtils.Legacy/aliases @@ -0,0 +1,3 @@ +windows +shippable/windows/group1 +shippable/windows/smoketest diff --git a/test/integration/targets/win_module_utils_legacy/library/testlist.ps1 b/test/integration/targets/module_utils_Ansible.ModuleUtils.Legacy/library/testlist.ps1 similarity index 100% rename from test/integration/targets/win_module_utils_legacy/library/testlist.ps1 rename to test/integration/targets/module_utils_Ansible.ModuleUtils.Legacy/library/testlist.ps1 diff --git a/test/integration/targets/win_module_utils_legacy/library/testpath.ps1 b/test/integration/targets/module_utils_Ansible.ModuleUtils.Legacy/library/testpath.ps1 similarity index 100% rename from test/integration/targets/win_module_utils_legacy/library/testpath.ps1 rename to test/integration/targets/module_utils_Ansible.ModuleUtils.Legacy/library/testpath.ps1 diff --git a/test/integration/targets/win_module_utils_legacy/tasks/main.yml b/test/integration/targets/module_utils_Ansible.ModuleUtils.Legacy/tasks/main.yml similarity index 99% rename from test/integration/targets/win_module_utils_legacy/tasks/main.yml rename to test/integration/targets/module_utils_Ansible.ModuleUtils.Legacy/tasks/main.yml index 2d56393d736..0bd10558ce9 100644 --- a/test/integration/targets/win_module_utils_legacy/tasks/main.yml +++ b/test/integration/targets/module_utils_Ansible.ModuleUtils.Legacy/tasks/main.yml @@ -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 diff --git a/test/integration/targets/module_utils_Ansible.ModuleUtils.LinkUtil/aliases b/test/integration/targets/module_utils_Ansible.ModuleUtils.LinkUtil/aliases new file mode 100644 index 00000000000..cf714783f5c --- /dev/null +++ b/test/integration/targets/module_utils_Ansible.ModuleUtils.LinkUtil/aliases @@ -0,0 +1,3 @@ +windows +shippable/windows/group1 +shippable/windows/smoketest diff --git a/test/integration/targets/win_module_utils/library/symbolic_link_test.ps1 b/test/integration/targets/module_utils_Ansible.ModuleUtils.LinkUtil/library/symbolic_link_test.ps1 similarity index 100% rename from test/integration/targets/win_module_utils/library/symbolic_link_test.ps1 rename to test/integration/targets/module_utils_Ansible.ModuleUtils.LinkUtil/library/symbolic_link_test.ps1 diff --git a/test/integration/targets/module_utils_Ansible.ModuleUtils.LinkUtil/tasks/main.yml b/test/integration/targets/module_utils_Ansible.ModuleUtils.LinkUtil/tasks/main.yml new file mode 100644 index 00000000000..f121ad4aef5 --- /dev/null +++ b/test/integration/targets/module_utils_Ansible.ModuleUtils.LinkUtil/tasks/main.yml @@ -0,0 +1,8 @@ +--- +- name: call module with symbolic link tests + symbolic_link_test: + register: symbolic_link + +- assert: + that: + - symbolic_link.data == 'success' diff --git a/test/integration/targets/module_utils_Ansible.ModuleUtils.PrivilegeUtil/aliases b/test/integration/targets/module_utils_Ansible.ModuleUtils.PrivilegeUtil/aliases new file mode 100644 index 00000000000..cf714783f5c --- /dev/null +++ b/test/integration/targets/module_utils_Ansible.ModuleUtils.PrivilegeUtil/aliases @@ -0,0 +1,3 @@ +windows +shippable/windows/group1 +shippable/windows/smoketest diff --git a/test/integration/targets/win_module_utils/library/privilege_util_test.ps1 b/test/integration/targets/module_utils_Ansible.ModuleUtils.PrivilegeUtil/library/privilege_util_test.ps1 similarity index 100% rename from test/integration/targets/win_module_utils/library/privilege_util_test.ps1 rename to test/integration/targets/module_utils_Ansible.ModuleUtils.PrivilegeUtil/library/privilege_util_test.ps1 diff --git a/test/integration/targets/module_utils_Ansible.ModuleUtils.PrivilegeUtil/tasks/main.yml b/test/integration/targets/module_utils_Ansible.ModuleUtils.PrivilegeUtil/tasks/main.yml new file mode 100644 index 00000000000..5f54480eb2e --- /dev/null +++ b/test/integration/targets/module_utils_Ansible.ModuleUtils.PrivilegeUtil/tasks/main.yml @@ -0,0 +1,8 @@ +--- +- name: call module with PrivilegeUtil tests + privilege_util_test: + register: privilege_util_test + +- assert: + that: + - privilege_util_test.data == 'success' diff --git a/test/integration/targets/module_utils_Ansible.ModuleUtils.SID/aliases b/test/integration/targets/module_utils_Ansible.ModuleUtils.SID/aliases new file mode 100644 index 00000000000..cf714783f5c --- /dev/null +++ b/test/integration/targets/module_utils_Ansible.ModuleUtils.SID/aliases @@ -0,0 +1,3 @@ +windows +shippable/windows/group1 +shippable/windows/smoketest diff --git a/test/integration/targets/win_module_utils/library/sid_utils_test.ps1 b/test/integration/targets/module_utils_Ansible.ModuleUtils.SID/library/sid_utils_test.ps1 similarity index 100% rename from test/integration/targets/win_module_utils/library/sid_utils_test.ps1 rename to test/integration/targets/module_utils_Ansible.ModuleUtils.SID/library/sid_utils_test.ps1 diff --git a/test/integration/targets/module_utils_Ansible.ModuleUtils.SID/tasks/main.yml b/test/integration/targets/module_utils_Ansible.ModuleUtils.SID/tasks/main.yml new file mode 100644 index 00000000000..acbae50a760 --- /dev/null +++ b/test/integration/targets/module_utils_Ansible.ModuleUtils.SID/tasks/main.yml @@ -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' diff --git a/test/integration/targets/module_utils_Ansible.ModuleUtils.WebRequest/aliases b/test/integration/targets/module_utils_Ansible.ModuleUtils.WebRequest/aliases new file mode 100644 index 00000000000..b5ad7ca928f --- /dev/null +++ b/test/integration/targets/module_utils_Ansible.ModuleUtils.WebRequest/aliases @@ -0,0 +1,4 @@ +windows +shippable/windows/group1 +shippable/windows/smoketest +needs/httptester diff --git a/test/integration/targets/module_utils_Ansible.ModuleUtils.WebRequest/library/web_request_test.ps1 b/test/integration/targets/module_utils_Ansible.ModuleUtils.WebRequest/library/web_request_test.ps1 new file mode 100644 index 00000000000..a1d4db326c5 --- /dev/null +++ b/test/integration/targets/module_utils_Ansible.ModuleUtils.WebRequest/library/web_request_test.ps1 @@ -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() diff --git a/test/integration/targets/module_utils_Ansible.ModuleUtils.WebRequest/meta/main.yml b/test/integration/targets/module_utils_Ansible.ModuleUtils.WebRequest/meta/main.yml new file mode 100644 index 00000000000..829d0a789a7 --- /dev/null +++ b/test/integration/targets/module_utils_Ansible.ModuleUtils.WebRequest/meta/main.yml @@ -0,0 +1,3 @@ +--- +dependencies: +- prepare_http_tests diff --git a/test/integration/targets/module_utils_Ansible.ModuleUtils.WebRequest/tasks/main.yml b/test/integration/targets/module_utils_Ansible.ModuleUtils.WebRequest/tasks/main.yml new file mode 100644 index 00000000000..57d8138ada9 --- /dev/null +++ b/test/integration/targets/module_utils_Ansible.ModuleUtils.WebRequest/tasks/main.yml @@ -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' diff --git a/test/integration/targets/win_module_utils/files/PrintArgv.cs b/test/integration/targets/setup_win_printargv/files/PrintArgv.cs similarity index 100% rename from test/integration/targets/win_module_utils/files/PrintArgv.cs rename to test/integration/targets/setup_win_printargv/files/PrintArgv.cs diff --git a/test/integration/targets/setup_win_printargv/meta/main.yml b/test/integration/targets/setup_win_printargv/meta/main.yml new file mode 100644 index 00000000000..e3dd5fb100d --- /dev/null +++ b/test/integration/targets/setup_win_printargv/meta/main.yml @@ -0,0 +1,3 @@ +--- +dependencies: +- setup_remote_tmp_dir diff --git a/test/integration/targets/setup_win_printargv/tasks/main.yml b/test/integration/targets/setup_win_printargv/tasks/main.yml new file mode 100644 index 00000000000..5f6719248d0 --- /dev/null +++ b/test/integration/targets/setup_win_printargv/tasks/main.yml @@ -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' diff --git a/test/integration/targets/win_module_utils/tasks/main.yml b/test/integration/targets/win_module_utils/tasks/main.yml index 38873fdb412..87f2592c89e 100644 --- a/test/integration/targets/win_module_utils/tasks/main.yml +++ b/test/integration/targets/win_module_utils/tasks/main.yml @@ -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' diff --git a/test/sanity/ignore.txt b/test/sanity/ignore.txt index bcae2c3d0e5..64834a27375 100644 --- a/test/sanity/ignore.txt +++ b/test/sanity/ignore.txt @@ -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