-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathtest.ps1
More file actions
194 lines (172 loc) · 7.6 KB
/
test.ps1
File metadata and controls
194 lines (172 loc) · 7.6 KB
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#!/usr/bin/env pwsh
# --------------------------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for license information.
# --------------------------------------------------------------------------------------------
#
# End-to-end tests for artifacts-keyring against a live Azure Artifacts feed.
# A user already authenticated to Azure DevOps (e.g. via az login or browser) should
# be able to run this against a private feed and all scenarios should succeed.
#
# Usage:
# .\test.ps1 -TestFeed <feed-simple-url> [-TestPackage <package-name>] [-WhlPath <path-to-whl>]
#
# Example:
# .\test.ps1 -TestFeed https://pkgs.dev.azure.com/<org>/_packaging/<feed>/pypi/simple/ -TestPackage numpy
param(
[Parameter(Mandatory = $true, Position = 0)]
[string]$TestFeed,
[Parameter(Mandatory = $false)]
[string]$TestPackage = "pip",
[Parameter(Mandatory = $false)]
[string]$WhlPath = ""
)
$PassCount = 0
$FailCount = 0
$TestResults = @()
function Clear-AllCaches {
Write-Host "`nClearing all credential and pip caches..." -ForegroundColor Yellow
# Session Token Cache
$sessionCache = Join-Path ([Environment]::GetFolderPath('LocalApplicationData')) "MicrosoftCredentialProvider" "SessionTokenCache.dat"
if (Test-Path $sessionCache) {
Remove-Item $sessionCache -Force -ErrorAction SilentlyContinue
Write-Host " Removed session token cache: $sessionCache" -ForegroundColor DarkYellow
}
# MSAL Token Cache
$msalCache = Join-Path ([Environment]::GetFolderPath('LocalApplicationData')) ".IdentityService"
if (Test-Path $msalCache) {
Remove-Item $msalCache -Recurse -Force -ErrorAction SilentlyContinue
Write-Host " Removed MSAL token cache: $msalCache" -ForegroundColor DarkYellow
}
# pip HTTP cache
& pip cache purge 2>&1 | Out-Null
Write-Host " Cleared pip cache" -ForegroundColor DarkYellow
}
function Invoke-Test {
param(
[string]$Name,
[string[]]$PipArgs
)
Write-Host "`n=== $Name ===" -ForegroundColor Cyan
# Run pip directly to the console with no pipeline so that
# credential provider stderr (device flow / browser prompts) is
# never buffered and appears immediately.
& pip @PipArgs
$exitCode = $LASTEXITCODE
if ($exitCode -eq 0) {
Write-Host "[PASS] $Name" -ForegroundColor Green
$script:PassCount++
$script:TestResults += [PSCustomObject]@{ Test = $Name; Result = "PASS" }
}
else {
Write-Host "[FAIL] $Name (exit code: $exitCode)" -ForegroundColor Red
$script:FailCount++
$script:TestResults += [PSCustomObject]@{ Test = $Name; Result = "FAIL" }
}
return $exitCode
}
# ---------------------------------------------------------------------------
# Install the wheel under test if provided, otherwise use whatever is installed
# ---------------------------------------------------------------------------
if (-not [string]::IsNullOrEmpty($WhlPath)) {
Write-Host "`nInstalling artifacts-keyring from: $WhlPath" -ForegroundColor Cyan
& pip install $WhlPath --force-reinstall
if ($LASTEXITCODE -ne 0) {
Write-Host "Failed to install artifacts-keyring wheel." -ForegroundColor Red
exit 1
}
}
# Confirm artifacts-keyring is registered as a keyring backend
Write-Host "`nRegistered keyring backends:" -ForegroundColor Cyan
& python -c "import keyring.backend; backends = keyring.backend.get_all_keyring(); [print(' -', type(b).__name__, b.priority) for b in sorted(backends, key=lambda b: b.priority, reverse=True)]"
# ---------------------------------------------------------------------------
# Test 1: pip install (interactive, fresh auth)
# ---------------------------------------------------------------------------
Clear-AllCaches
$result = Invoke-Test -Name "pip_install_interactive" -PipArgs @(
"install", $TestPackage,
"--index-url", $TestFeed,
"--force-reinstall",
"--no-cache-dir",
"-v"
)
# Tests 2 and 3 depend on a valid cached token from Test 1 — skip if it failed
if ($result -ne 0) {
Write-Host "`nSkipping Tests 2 and 3: require Test 1 to pass first." -ForegroundColor Yellow
}
else {
# ---------------------------------------------------------------------------
# Test 2: pip install (cached session token — should succeed silently)
# ---------------------------------------------------------------------------
$result = Invoke-Test -Name "pip_install_cached_token" -PipArgs @(
"install", $TestPackage,
"--index-url", $TestFeed,
"--force-reinstall",
"--no-cache-dir",
"-v"
)
# ---------------------------------------------------------------------------
# Test 3: pip install with non-interactive mode (requires valid cached token)
# ---------------------------------------------------------------------------
if ($result -ne 0) {
Write-Host "`nSkipping Test 3: requires Test 2 to pass first." -ForegroundColor Yellow
}
else {
$env:ARTIFACTS_KEYRING_NONINTERACTIVE_MODE = "true"
Invoke-Test -Name "pip_install_noninteractive" -PipArgs @(
"install", $TestPackage,
"--index-url", $TestFeed,
"--force-reinstall",
"--no-cache-dir",
"-v"
)
Remove-Item Env:\ARTIFACTS_KEYRING_NONINTERACTIVE_MODE -ErrorAction SilentlyContinue
}
}
# ---------------------------------------------------------------------------
# Test 4: pip install with credential provider logging enabled
# ---------------------------------------------------------------------------
Clear-AllCaches
$logPath = Join-Path $PSScriptRoot "credprovider_test4.log"
$env:ARTIFACTS_CREDENTIALPROVIDER_LOG_PATH = $logPath
Invoke-Test -Name "pip_install_with_credprovider_log" -PipArgs @(
"install", $TestPackage,
"--index-url", $TestFeed,
"--force-reinstall",
"--no-cache-dir",
"-v"
)
Remove-Item Env:\ARTIFACTS_CREDENTIALPROVIDER_LOG_PATH -ErrorAction SilentlyContinue
if (Test-Path $logPath) {
Write-Host " Credential provider log written to: $logPath" -ForegroundColor DarkCyan
}
else {
Write-Host " Warning: Credential provider log was not created at $logPath" -ForegroundColor Yellow
}
# ---------------------------------------------------------------------------
# Test 5: pip install after clearing only session token cache (forces cred provider re-auth via MSAL)
# ---------------------------------------------------------------------------
$sessionCache = Join-Path ([Environment]::GetFolderPath('LocalApplicationData')) "MicrosoftCredentialProvider" "SessionTokenCache.dat"
if (Test-Path $sessionCache) {
Remove-Item $sessionCache -Force -ErrorAction SilentlyContinue
Write-Host "`nCleared session token cache only (MSAL still warm)" -ForegroundColor Yellow
}
Invoke-Test -Name "pip_install_after_session_cache_clear" -PipArgs @(
"install", $TestPackage,
"--index-url", $TestFeed,
"--force-reinstall",
"--no-cache-dir",
"-v"
)
# ---------------------------------------------------------------------------
# Summary
# ---------------------------------------------------------------------------
Write-Host "`n========================================" -ForegroundColor Cyan
Write-Host " Test Results" -ForegroundColor Cyan
Write-Host "========================================" -ForegroundColor Cyan
$TestResults | Format-Table Test, Result -AutoSize
Write-Host "PASSED: $PassCount FAILED: $FailCount" -ForegroundColor $(if ($FailCount -eq 0) { "Green" } else { "Red" })
if ($FailCount -gt 0) {
exit 1
}
exit 0