forked from JeffHarkavy/SafeguardCmdletTesting
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmdlet-tests-filter-properties.ps1
114 lines (110 loc) · 4 KB
/
cmdlet-tests-filter-properties.ps1
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
try {
Get-Command "writeCallHeader" -ErrorAction Stop > $null
} catch {
write-host "Not meant to be run as a standalone script" -ForegroundColor Red
exit
}
$TestBlockName ="Running Filter Properties Tests"
$blockInfo = testBlockHeader $TestBlockName
function nestedPropertyCheck($call, $object, $prefix) {
$failures = [System.Collections.ArrayList]@()
$depth++
#JIC there are some nested recursive definitions somewhere
if ($depth -gt 10) {
$failures.Add("Max depth of 10 exceeded - call=$call, prefix=$prefix")
return $failures
}
try {
$props = $object | get-member -membertype noteproperty
$props | ForEach-Object {$idx = 0} {
if ($_.Definition -match "PSCustomObject") {
$n = $_.Name
$newprefix = $prefix -ne "" ? "$prefix.$n" : $n
$nestedFailures = nestedPropertyCheck $call $object."$n" "$newprefix"
if ($nestedFailures.length -gt 0) {
$failures.AddRange($nestedFailures)
}
} elseif ( $_.Definition -match "^Object\[\]") {
$n = $prefix -ne "" ? "$prefix.$($_.Name)" : $_.Name
if (-not $quiet) {
write-host -ForegroundColor Cyan "SKIPPED $n"
}
} else {
$n = $prefix -ne "" ? "$prefix.$($_.Name)" : $_.Name
try {
$script:loops++
if ($quiet) {
if ($script:loops % 5 -eq 0) {
write-host -NoNewLine $script:loops
} else {
write-host -NoNewLine "."
}
}
$filter = "$n ne null"
$count = invoke-safeguardmethod core GET $call -parameters @{filter=$filter; count=$true}
if (-not $quiet) {
goodResult "GET $call" "$n - count=$count"
}
} catch {
$failures.Add("FAILED : $n") > $null
}
}
$idx++
}
} catch {
$failures.Add("FAILED : get-member $call - $prefix : L:$($_.InvocationInfo.ScriptLineNumber) $($_.Exception.Message)") > $null
}
$depth--
return $failures
}
function checkAllProperties($topLevel) {
infoResult "START" "######## $topLevel ########"
try {
$obj = Invoke-expression "invoke-safeguardmethod core GET $topLevel -parameters @{page=0;limit=1}"
if ($null -eq $obj -or $obj -eq "" -or $obj.length -eq 0) {
infoResult "$topLevel" "GET returned no results"
return
}
$script:loops = 0
$results = nestedPropertyCheck $topLevel $obj[0] ""
if ($quiet -and $script:loops -gt 0) {
Write-Host
}
if ($results.length -gt 0) {
$results | ForEach-Object { badResult "$toplevel" $_ }
} else {
goodResult "$topLevel" "No failures"
}
} catch {
badResult "$topLevel" "Unexpected error checking $topLevel" $_
} finally {
infoResult "END " "######## $topLevel ########`n"
}
}
# ===== Covered DTOs =====
# see DTO ForEach-Object below
#
# Note - a "GET" of the DTO must return something, e.g., if you
# don't have an AccountGroup defined then checking filter properties
# for AccountGroups will not work. This is reported as an infoResult
# in the output and is not considered a failure.
#
try {
$depth = 0
$loops = 0
$quiet = $true
@("AccessPolicies", "AccessRequests", "AccountGroups", "ArchiveServers", `
"AssetAccounts", "AssetGroups", "AssetPartitions", "Assets", `
"EmailTemplates", "Events", "EventSubscribers", "Identities", `
"IdentityProviders", "IdentityProviderTypes", "Licenses", "Platforms", `
"PolicyAccounts", "PolicyAssets", "ReasonCodes", "Roles", `
"SshAlgorithms", "SshKeys", "TicketSystems", "TimeZones", `
"UserGroups", "Users") | ForEach-Object {
checkAllProperties $_
}
}
catch {
badResult "general" "Error working with Filter Properties" $_
} finally {
}
testBlockHeader $TestBlockName $blockInfo