[cli-release-discovery][iter-1] fix(install): select stable CLI releases and verify platform assets
这个提交包含在:
+23
@@ -0,0 +1,23 @@
|
||||
# Installer regression tests
|
||||
|
||||
Offline tests; no installation or public API calls are made.
|
||||
|
||||
```sh
|
||||
bash -n install
|
||||
python3 -m unittest discover -s tests -p 'test_cli_release_discovery.py' -v
|
||||
pwsh -NoProfile -File tests/release-discovery.ps1
|
||||
pwsh -NoProfile -File tests/test.ps1
|
||||
```
|
||||
|
||||
On Windows PowerShell 5.1, replace `pwsh` with `powershell`. The Gitea workflow
|
||||
runs Bash on the existing Linux pool and PowerShell on the Windows pool.
|
||||
|
||||
Discovery follows Gitea release order and paginates past desktop archives,
|
||||
drafts and prereleases. Only exact `vMAJOR.MINOR.PATCH` CLI tags qualify.
|
||||
The first eligible release must list the detected platform archive; an
|
||||
incomplete newest CLI release is an error, not a silent downgrade.
|
||||
|
||||
The Bash installer uses POSIX awk (no new jq/Python/Node runtime dependency).
|
||||
Tests execute its actual discovery functions with offline fixture responses.
|
||||
PowerShell tests load only the discovery function via the language AST, so they
|
||||
do not execute the installer. Existing host-safety tests also remain enabled.
|
||||
@@ -0,0 +1,69 @@
|
||||
#Requires -Version 5.1
|
||||
# Offline tests: pwsh -NoProfile -File tests/release-discovery.ps1
|
||||
Set-StrictMode -Version Latest
|
||||
$ErrorActionPreference = 'Stop'
|
||||
$repoRoot = Split-Path -Parent $PSScriptRoot
|
||||
$content = Get-Content (Join-Path $repoRoot 'install.ps1') -Raw
|
||||
$tokens = $null; $errors = $null
|
||||
$ast = [System.Management.Automation.Language.Parser]::ParseInput($content, [ref]$tokens, [ref]$errors)
|
||||
if ($errors.Count) { throw ($errors | Out-String) }
|
||||
$definition = $ast.Find({ param($node) $node -is [System.Management.Automation.Language.FunctionDefinitionAst] -and $node.Name -eq 'Get-LatestVersion' }, $true)
|
||||
. ([scriptblock]::Create($definition.Extent.Text))
|
||||
$REPO = 'Octopus/octopus-release'
|
||||
$env:GITEA_API_URL = 'https://fixture.invalid/api/v1'
|
||||
$script:pages = @{}
|
||||
$script:requests = @()
|
||||
$script:failHttp = $false
|
||||
function Invoke-RestMethod {
|
||||
param($Uri, [switch]$UseBasicParsing, $TimeoutSec, $ErrorAction)
|
||||
$script:requests += $Uri
|
||||
if ($script:failHttp) { throw 'HTTP 503' }
|
||||
$page = [int]([regex]::Match($Uri, 'page=([0-9]+)$').Groups[1].Value)
|
||||
if ($script:pages.ContainsKey($page)) { return $script:pages[$page] }
|
||||
return @()
|
||||
}
|
||||
function New-Release([string]$Tag = 'v1.3.1', [string]$Asset = 'octopus-windows-x64.zip') {
|
||||
[pscustomobject]@{ tag_name=$Tag; draft=$false; prerelease=$false; assets=@([pscustomobject]@{name=$Asset}) }
|
||||
}
|
||||
$script:passed = 0
|
||||
function Assert-Equal($Actual, $Expected, [string]$Message) {
|
||||
if ($Actual -cne $Expected) { throw "$Message : expected [$Expected], got [$Actual]" }
|
||||
$script:passed++
|
||||
}
|
||||
$script:pages = @{1=@((New-Release 'desktop-v1.3.1'), (New-Release))}
|
||||
Assert-Equal (Get-LatestVersion 'octopus-windows-x64.zip') '1.3.1' 'desktop-first must select CLI'
|
||||
foreach ($tag in @('desktop-beta-v1.3.1', 'cli-v1.3.1', 'V1.3.1', 'v1.4.0-rc.1', 'v1.4.0+build', 'v1.4', "v1.4.0`n")) {
|
||||
$script:pages = @{1=@((New-Release $tag), (New-Release))}
|
||||
Assert-Equal (Get-LatestVersion 'octopus-windows-x64.zip') '1.3.1' "reject $tag"
|
||||
}
|
||||
foreach ($field in @('draft', 'prerelease')) {
|
||||
foreach ($flag in @($true, 'false', $null)) {
|
||||
$bad = New-Release 'v2.0.0'; $bad.$field = $flag
|
||||
$script:pages = @{1=@($bad, (New-Release))}
|
||||
Assert-Equal (Get-LatestVersion 'octopus-windows-x64.zip') '1.3.1' "reject non-false $field"
|
||||
}
|
||||
}
|
||||
foreach ($asset in @('octopus-windows-x64.zip', 'octopus-windows-x64-baseline.zip', 'octopus-windows-arm64.zip')) {
|
||||
$script:pages = @{1=@((New-Release 'desktop-v1.3.1'), (New-Release 'v1.3.1' $asset))}
|
||||
Assert-Equal (Get-LatestVersion $asset) '1.3.1' "asset $asset"
|
||||
}
|
||||
$script:pages = @{1=@((New-Release 'desktop-v1.3.1')); 2=@((New-Release))}
|
||||
$script:requests = @()
|
||||
Assert-Equal (Get-LatestVersion 'octopus-windows-x64.zip') '1.3.1' 'pagination'
|
||||
Assert-Equal $script:requests.Count 2 'must fetch next page'
|
||||
$script:pages = @{1=@((New-Release 'desktop-v1.3.1'))}
|
||||
Assert-Equal (Get-LatestVersion 'octopus-windows-x64.zip') '' 'empty end'
|
||||
$script:pages = @{1=@((New-Release 'v1.3.1' 'wrong.zip'), (New-Release 'v1.3.0'))}
|
||||
$caught = $false
|
||||
try { Get-LatestVersion 'octopus-windows-x64.zip' | Out-Null } catch { $caught = $_.Exception.Message -like '*missing platform asset*' }
|
||||
Assert-Equal $caught $true 'missing asset fails, no silent downgrade'
|
||||
$script:pages = @{1=@((New-Release 'v1.3.1' 'OCTOPUS-WINDOWS-X64.ZIP'))}
|
||||
$caught = $false
|
||||
try { Get-LatestVersion 'octopus-windows-x64.zip' | Out-Null } catch { $caught = $true }
|
||||
Assert-Equal $caught $true 'asset name is case-sensitive'
|
||||
$script:failHttp = $true
|
||||
$caught = $false
|
||||
try { Get-LatestVersion 'octopus-windows-x64.zip' | Out-Null } catch { $caught = $_.Exception.Message -eq 'HTTP 503' }
|
||||
Assert-Equal $caught $true 'HTTP error fails closed'
|
||||
if ($content -notmatch 'Get-LatestVersion -Filename \$filename') { throw 'detected platform asset must be passed into discovery' }
|
||||
Write-Host "PASS: $script:passed discovery assertions; full installer parses cleanly."
|
||||
+2
-2
@@ -56,10 +56,10 @@ try {
|
||||
Write-Output ("RESULT=$r")
|
||||
'@.Replace("__INSTALL__", $installScript)
|
||||
|
||||
$tmp = Join-Path $env:TEMP ("octopus_iex_test_" + $PID + ".ps1")
|
||||
$tmp = Join-Path ([System.IO.Path]::GetTempPath()) ("octopus_iex_test_" + $PID + ".ps1")
|
||||
Set-Content -Path $tmp -Value $inner -Encoding UTF8
|
||||
try {
|
||||
$childOut = (& powershell -NoProfile -File $tmp 2>&1 | Out-String)
|
||||
$childOut = (& (Get-Process -Id $PID).Path -NoProfile -File $tmp 2>&1 | Out-String)
|
||||
} finally {
|
||||
Remove-Item $tmp -ErrorAction SilentlyContinue
|
||||
}
|
||||
|
||||
@@ -0,0 +1,120 @@
|
||||
#!/usr/bin/env python3
|
||||
# Offline behavioral tests of the actual Bash installer discovery functions.
|
||||
# python3 -m unittest discover -s tests -p 'test_cli_release_discovery.py' -v
|
||||
import json
|
||||
import os
|
||||
from pathlib import Path
|
||||
import subprocess
|
||||
import tempfile
|
||||
import unittest
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
SOURCE = (ROOT / "install").read_text()
|
||||
FUNCTIONS = SOURCE[SOURCE.index("select_cli_release() {"):SOURCE.index("requested_version=${VERSION:-}")]
|
||||
ASSET = "octopus-linux-x64.tar.gz"
|
||||
|
||||
|
||||
def release(tag="v1.3.1"):
|
||||
return dict(tag_name=tag, draft=False, prerelease=False, assets=[dict(name=ASSET)])
|
||||
|
||||
|
||||
class DiscoveryTests(unittest.TestCase):
|
||||
def select(self, releases, asset=ASSET, raw=False):
|
||||
data = releases if raw else json.dumps(releases, ensure_ascii=True, indent=2)
|
||||
return subprocess.run(["bash", "-c", FUNCTIONS + '\nselect_cli_release "$1"', "test", asset],
|
||||
input=data, text=True, capture_output=True)
|
||||
|
||||
def test_desktop_archive_first(self):
|
||||
desktop = release("desktop-v1.3.1")
|
||||
desktop["body"] = 'Notes with braces { [ ], escaped "tag_name": "v99.0.0" and unicode 中文'
|
||||
result = self.select([desktop, release()])
|
||||
self.assertEqual((result.returncode, result.stdout.strip()), (0, "1.3.1"))
|
||||
|
||||
def test_raw_utf8_release_notes(self):
|
||||
item = release("desktop-v1.3.1")
|
||||
item["body"] = "中文 notes with emoji 🚀 and punctuation —"
|
||||
data = json.dumps([item, release()], ensure_ascii=False)
|
||||
result = self.select(data, raw=True)
|
||||
self.assertEqual((result.returncode, result.stdout.strip()), (0, "1.3.1"))
|
||||
|
||||
def test_only_exact_stable_cli_tags(self):
|
||||
bad = [release(tag) for tag in ["desktop-beta-v1.3.1", "cli-v1.3.1", "V1.3.1", "v1.4.0-rc.1", "v1.4.0+build", "v1.4", "v1.4.0\n"]]
|
||||
for field, value in [("draft", True), ("prerelease", True), ("draft", "false"), ("prerelease", None)]:
|
||||
item = release("v2.0.0"); item[field] = value; bad.append(item)
|
||||
result = self.select(bad + [release()])
|
||||
self.assertEqual((result.returncode, result.stdout.strip()), (0, "1.3.1"))
|
||||
|
||||
def test_missing_platform_asset_is_not_silent_downgrade(self):
|
||||
latest = release(); latest["assets"] = [dict(name="octopus-desktop-linux-x86_64.AppImage")]
|
||||
self.assertEqual(self.select([latest, release("v1.3.0")]).returncode, 4)
|
||||
|
||||
def test_each_supported_archive_name(self):
|
||||
for asset in ["octopus-darwin-arm64.zip", "octopus-darwin-x64-baseline.zip", "octopus-linux-x64-musl.tar.gz", "octopus-windows-arm64.zip", "octopus-windows-x64-baseline.zip"]:
|
||||
with self.subTest(asset=asset):
|
||||
item = release(); item["assets"] = [dict(name=asset)]
|
||||
result = self.select([release("desktop-v1.3.1"), item], asset)
|
||||
self.assertEqual(result.stdout.strip(), "1.3.1")
|
||||
self.assertEqual(result.returncode, 0)
|
||||
|
||||
def test_asset_match_is_exact(self):
|
||||
self.assertEqual(self.select([release()], ASSET.upper()).returncode, 4)
|
||||
|
||||
def test_no_candidate_and_empty_page_have_distinct_results(self):
|
||||
self.assertEqual(self.select([release("desktop-v1.3.1")]).returncode, 3)
|
||||
result = self.select([])
|
||||
self.assertEqual((result.returncode, result.stdout.strip()), (0, "END"))
|
||||
|
||||
def test_reject_malformed_or_non_array_json(self):
|
||||
for raw in ['{"message":"rate limited"}', '[', '[{}] garbage', '[{"tag_name":"v1.3.1",}]', 'null', '[1]', '[{"a":1,"a":2}]']:
|
||||
with self.subTest(raw=raw):
|
||||
self.assertEqual(self.select(raw, raw=True).returncode, 2)
|
||||
|
||||
def test_unicode_escaped_ascii_fields(self):
|
||||
data = json.dumps([release()]).replace('v1.3.1', r'v1\u002e3.1')
|
||||
self.assertEqual(self.select(data, raw=True).stdout.strip(), "1.3.1")
|
||||
|
||||
def fetch(self, pages, fail=False):
|
||||
with tempfile.TemporaryDirectory() as directory:
|
||||
for i, page in enumerate(pages, 1):
|
||||
Path(directory, f"{i}.json").write_text(json.dumps(page))
|
||||
script = FUNCTIONS + r'''
|
||||
curl() {
|
||||
local url="${@: -1}" page
|
||||
printf '%s\n' "$url" >> "$FIXTURES/requests"
|
||||
if [ "$CURL_FAIL" = 1 ]; then return 22; fi
|
||||
page="${url##*page=}"
|
||||
if [ -f "$FIXTURES/$page.json" ]; then
|
||||
while IFS= read -r line || [ -n "$line" ]; do printf '%s\n' "$line"; done < "$FIXTURES/$page.json"
|
||||
else printf '[]'; fi
|
||||
}
|
||||
get_latest_cli_version "$1"
|
||||
'''
|
||||
env = dict(os.environ, FIXTURES=directory, CURL_FAIL="1" if fail else "0", GITEA_API_URL="https://fixture.invalid/api/v1")
|
||||
result = subprocess.run(["bash", "-c", script, "test", ASSET], text=True, capture_output=True, env=env)
|
||||
requests = Path(directory, "requests").read_text().splitlines()
|
||||
return result, requests
|
||||
|
||||
def test_paginates_past_desktop_only_page(self):
|
||||
result, requests = self.fetch([[release("desktop-v1.3.1")], [release()]])
|
||||
self.assertEqual((result.returncode, result.stdout.strip()), (0, "1.3.1"))
|
||||
self.assertEqual(len(requests), 2)
|
||||
self.assertTrue(requests[1].endswith("limit=50&page=2"))
|
||||
|
||||
def test_empty_end_fails_without_downloading(self):
|
||||
result, requests = self.fetch([[release("desktop-v1.3.1")], []])
|
||||
self.assertNotEqual(result.returncode, 0)
|
||||
self.assertIn("No public stable CLI release", result.stderr)
|
||||
self.assertEqual(len(requests), 2)
|
||||
|
||||
def test_http_error_fails_closed(self):
|
||||
result, requests = self.fetch([[release()]], fail=True)
|
||||
self.assertNotEqual(result.returncode, 0)
|
||||
self.assertEqual(len(requests), 1)
|
||||
|
||||
def test_installer_passes_detected_asset_to_discovery(self):
|
||||
self.assertIn('specific_version=$(get_latest_cli_version "$filename")', SOURCE)
|
||||
self.assertNotIn('releases?limit=1"', SOURCE)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
在新工单中引用
屏蔽一个用户