From a022acd6a1c43040525cee2bfb6a054cd48729ec Mon Sep 17 00:00:00 2001 From: James Rosewell Date: Tue, 1 Dec 2026 07:50:28 +0000 Subject: [PATCH 1/2] Test the examples against the packages published on PyPI Nothing in CI ran the examples against the packages customers install from PyPI, so a release whose packages do not work together, or that leaves out something the examples need, was only found by a customer. The new workflow, Examples vs Published Packages, installs every package ci/packages.json marks for publishing from PyPI, each pinned to the version given when the workflow is run by hand or otherwise to its latest version, and takes only the three examples packages and their tests from this checkout. It checks that each published package was installed from PyPI at the pinned version, then runs the tests of the Device Detection, IP Intelligence and Pipeline examples on the free data files, with the resource key and licence keys the cloud tests read. A missing key, a missing data file or a skipped test fails the run, because a run that skips has checked nothing. The tests marked staging are left out, as only the Cloud Human Confidence workflow is given the staging cloud. It runs by hand, after each run of Publish or Nightly Pipeline that was not cancelled, and on a pull request that changes the workflow. Checked with ci/check-environment-variable-names.ps1 and the Pester tests in ci/key-names.Tests.ps1, ci/check-environment-variable-names.Tests.ps1 and ci/free-threaded-python.Tests.ps1, 246 passed and none failed. --- .github/workflows/examples-published.yml | 295 +++++++++++++++++++++++ 1 file changed, 295 insertions(+) create mode 100644 .github/workflows/examples-published.yml diff --git a/.github/workflows/examples-published.yml b/.github/workflows/examples-published.yml new file mode 100644 index 00000000..3dcf518f --- /dev/null +++ b/.github/workflows/examples-published.yml @@ -0,0 +1,295 @@ +name: Examples vs Published Packages + +# Runs the examples' own tests against the packages on PyPI. Every package +# ci/packages.json marks for publishing is installed from PyPI, and only the +# three examples packages and their tests come from this checkout. Each +# package is pinned to the version given when the workflow is run by hand, +# or otherwise to its latest version on PyPI. +# +# It runs by hand on a branch (gh workflow run "Examples vs Published +# Packages" --ref ), after each run of the two workflows that +# publish, and on a pull request that changes this file. An example that +# needs a change not yet released fails here until the release, which is why +# it does not run on other pull requests. +# +# The tests run on the free data files and with the keys the cloud tests +# need. A missing key or data file fails the run, and so does a test that +# skipped, because a run that skips has checked nothing. + +on: + workflow_dispatch: + inputs: + version: + description: 'Version of every published package, such as 4.6.2, or empty for the latest of each on PyPI' + type: string + default: '' + # A run of either may publish packages, and one that failed may still have + # published some, so only a cancelled run is passed over. + workflow_run: + workflows: [Publish, Nightly Pipeline] + types: [completed] + pull_request: + paths: + - '.github/workflows/examples-published.yml' + +permissions: + contents: read + +defaults: + run: + shell: pwsh + +jobs: + published: + name: examples vs published packages + if: >- + github.event_name != 'workflow_run' || + github.event.workflow_run.conclusion != 'cancelled' + runs-on: ubuntu-latest + timeout-minutes: 60 + steps: + # A pull request from a fork gets no secrets, so it is skipped with a + # warning. Anywhere else a missing key is an error. Only the Pipeline + # examples' tests read _51DEGREES_LICENSE_KEY, for a licence key to + # look for in the page the Human Confidence example serves. + - name: Check the keys are available + id: keys + env: + _51DEGREES_RESOURCE_KEY_BESPOKE: ${{ secrets._51DEGREES_RESOURCE_KEY_BESPOKE }} + _51DEGREES_LICENSE_KEY_BESPOKE: ${{ secrets._51DEGREES_LICENSE_KEY_BESPOKE }} + _51DEGREES_LICENSE_KEY: ${{ secrets._51DEGREES_LICENSE_KEY }} + _51DEGREES_FROM_FORK: ${{ github.event.pull_request.head.repo.fork == true }} + run: | + $names = '_51DEGREES_RESOURCE_KEY_BESPOKE', + '_51DEGREES_LICENSE_KEY_BESPOKE', '_51DEGREES_LICENSE_KEY' + $missing = @($names | + Where-Object { -not [Environment]::GetEnvironmentVariable($_) }) + if (-not $missing) { + 'run=true' >> $env:GITHUB_OUTPUT + exit 0 + } + $list = $missing -join ', ' + if ($env:_51DEGREES_FROM_FORK -eq 'true') { + Write-Host ("::warning title=No keys::A fork has no $list, so " + + "the examples are not tested against the published packages.") + 'run=false' >> $env:GITHUB_OUTPUT + exit 0 + } + Write-Host ("::error title=Missing organization secrets::$list " + + "not available to this workflow.") + exit 1 + + # After a publish, the commit that was published. The data repository + # inside cxx holds the evidence and the comparison files the IP + # Intelligence tests read, and a link for each data file. + - name: Checkout repository + if: steps.keys.outputs.run == 'true' + uses: actions/checkout@v4 + with: + ref: ${{ github.event.workflow_run.head_sha }} + persist-credentials: false + submodules: recursive + + - name: Set up Python + if: steps.keys.outputs.run == 'true' + uses: actions/setup-python@v5 + with: + python-version: '3.14' + + # The free data files, from the assets common-ci fetches and keeps in + # the Actions cache for the day, so nothing follows the links here. + - name: Fetch the free data files + if: steps.keys.outputs.run == 'true' + uses: 51Degrees/common-ci/actions/fetch-assets@main + with: + cache: 'true' + assets: | + 51Degrees-LiteV4.1.hash + 20000 Evidence Records.yml + 51Degrees-IPIV4AsnIpiV41.ipi + 51Degrees-LiteIpiV41.ipi + + # Under the names the data repository saves them as, where the + # examples look. A missing file fails the job here, because the tests + # would skip without it. + - name: Put the free data files where the examples look for them + if: steps.keys.outputs.run == 'true' + run: | + $dd = 'cxx/data/device-detection' + $ipi = 'cxx/data/ip-intelligence' + $files = [ordered]@{ + '51Degrees-LiteV4.1.hash' = "$dd/51Degrees-LiteV4.1.hash" + '20000 Evidence Records.yml' = "$dd/20000 Evidence Records.yml" + '51Degrees-IPIV4AsnIpiV41.ipi' = "$ipi/51Degrees-IPIV4AsnIpiV41.ipi" + '51Degrees-LiteIpiV41.ipi' = "$ipi/51Degrees-LiteV41.ipi" + } + foreach ($asset in $files.Keys) { + $source = Join-Path assets $asset + if (-not (Test-Path -LiteralPath $source -PathType Leaf)) { + Write-Host "::error title=Missing data file::$asset was not fetched." + exit 1 + } + Copy-Item -LiteralPath $source -Destination $files[$asset] + } + + # Each package is pinned, so pip installs that version or fails, and + # never an older one that happens to satisfy the others. + - name: Choose the published versions + if: steps.keys.outputs.run == 'true' + env: + _51DEGREES_PACKAGE_VERSION: ${{ inputs.version }} + run: | + $version = "$env:_51DEGREES_PACKAGE_VERSION".Trim() + if ($version -and $version -notmatch '^\d+\.\d+\.\d+$') { + Write-Host "::error::'$version' is not a version such as 4.6.2." + exit 1 + } + $names = Get-Content -Raw ci/packages.json | ConvertFrom-Json | + Where-Object Publish | ForEach-Object { $_.Name -replace '_', '-' } + $pins = foreach ($name in $names) { + $pinned = $version + if (-not $pinned) { + $pinned = (Invoke-RestMethod "https://pypi.org/pypi/$name/json").info.version + } + "$name==$pinned" + } + $pins | Set-Content '${{ runner.temp }}/published.txt' + @('| Package | Version |', '| --- | --- |') + + ($pins | ForEach-Object { "| $($_ -replace '==', ' | ') |" }) | + Add-Content $env:GITHUB_STEP_SUMMARY + $pins + + # The examples' dependencies on the published packages are held to the + # pinned versions, and everything else they need comes from PyPI. The + # two on-premise packages are source distributions, which pip compiles + # as it does on a customer's machine. + - name: Install the published packages and the examples + if: steps.keys.outputs.run == 'true' + run: | + $PSNativeCommandUseErrorActionPreference = $true + $published = '${{ runner.temp }}/published.txt' + python -m pip install --upgrade pip + python -m pip install -r $published + python -m pip install -c $published pytest ` + ./device-detection/fiftyone_devicedetection_examples ` + './ip-intelligence/fiftyone_ipintelligence_examples[mixed]' ` + './pipeline/fiftyone_pipeline_cloudrequestengine/examples[test]' + python -m pip check + + # A package installed from a folder or a URL records it in + # direct_url.json, and one from PyPI does not. + - name: Check each published package came from PyPI + if: steps.keys.outputs.run == 'true' + shell: python + run: | + import importlib + import importlib.metadata as metadata + import pathlib + import sys + import sysconfig + + site = {pathlib.Path(sysconfig.get_paths()[key]).resolve() + for key in ("purelib", "platlib")} + pins = pathlib.Path( + r"${{ runner.temp }}/published.txt").read_text().split() + problems = [] + for pin in pins: + name, version = pin.split("==") + distribution = metadata.distribution(name) + module = importlib.import_module(name.replace("-", "_")) + path = pathlib.Path(module.__file__).resolve() + print(f"{name} {distribution.version} {path}") + if distribution.version != version: + problems.append( + f"{name} is {distribution.version}, not {version}.") + if distribution.read_text("direct_url.json") is not None: + problems.append(f"{name} was not installed from PyPI.") + if not any(folder in path.parents for folder in site): + problems.append(f"{name} was imported from {path}.") + for problem in problems: + print(f"::error::{problem}") + sys.exit(1 if problems else 0) + + # Each package's tests run from its folder, as its tox.ini runs them, + # and the Device Detection ones without _51DEGREES_LICENSE_KEY, which + # that tox.ini empties. The tests marked staging call the staging + # cloud, which only the Cloud Human Confidence workflow is given, so + # they are left out. Every package is run even after one fails. + - name: Test the examples against the published packages + if: steps.keys.outputs.run == 'true' + env: + _51DEGREES_RESOURCE_KEY_BESPOKE: ${{ secrets._51DEGREES_RESOURCE_KEY_BESPOKE }} + _51DEGREES_LICENSE_KEY_BESPOKE: ${{ secrets._51DEGREES_LICENSE_KEY_BESPOKE }} + _51DEGREES_LICENSE_KEY: ${{ secrets._51DEGREES_LICENSE_KEY }} + # The cloud service the tests ask. The name is removed when the + # variable is not set, so the tests ask the public one. + _51DEGREES_CLOUD_ENDPOINT: ${{ vars._51DEGREES_CLOUD_ENDPOINT }} + run: | + if (-not $env:_51DEGREES_CLOUD_ENDPOINT) { + Remove-Item Env:_51DEGREES_CLOUD_ENDPOINT -ErrorAction SilentlyContinue + } + $licenseKey = $env:_51DEGREES_LICENSE_KEY + $env:_51DEGREES_DD_PATH = (Resolve-Path 'cxx/data/device-detection/51Degrees-LiteV4.1.hash').Path + $env:_51DEGREES_IPI_PATH = (Resolve-Path 'cxx/data/ip-intelligence/51Degrees-LiteV41.ipi').Path + $runs = @( + @{ Name = 'fiftyone_devicedetection_examples' + Path = 'device-detection/fiftyone_devicedetection_examples' + LicenseKey = $false; Arguments = @() } + @{ Name = 'fiftyone_ipintelligence_examples' + Path = 'ip-intelligence/fiftyone_ipintelligence_examples' + LicenseKey = $false; Arguments = @() } + @{ Name = 'fiftyone_pipeline_examples' + Path = 'pipeline/fiftyone_pipeline_cloudrequestengine/examples' + LicenseKey = $true; Arguments = @('-m', 'not staging') } + ) + $results = New-Item -ItemType Directory -Force -Path '${{ runner.temp }}/results' + $summary = @( + '| Examples | Tests | Passed | Failed | Errors | Skipped |' + '| --- | --- | --- | --- | --- | --- |') + $failed = $false + foreach ($run in $runs) { + if ($run.LicenseKey) { + $env:_51DEGREES_LICENSE_KEY = $licenseKey + } else { + Remove-Item Env:_51DEGREES_LICENSE_KEY -ErrorAction SilentlyContinue + } + $report = Join-Path $results "$($run.Name).xml" + Write-Host "::group::$($run.Name)" + Push-Location $run.Path + python -m pytest -p no:cacheprovider -rs "--junit-xml=$report" @($run.Arguments) + $code = $LASTEXITCODE + Pop-Location + Write-Host '::endgroup::' + if (-not (Test-Path $report)) { + Write-Host "::error title=$($run.Name)::pytest exited with code $code and wrote no results." + $summary += "| $($run.Name) | none | | | | |" + $failed = $true + continue + } + $suite = ([xml](Get-Content -Raw $report)).SelectSingleNode('//testsuite') + $tests = [int]$suite.tests + $failures = [int]$suite.failures + $errors = [int]$suite.errors + $skipped = [int]$suite.skipped + $passed = $tests - $failures - $errors - $skipped + $summary += "| $($run.Name) | $tests | $passed | $failures | $errors | $skipped |" + if ($code -ne 0) { + Write-Host "::error title=$($run.Name)::pytest exited with code $code." + $failed = $true + } + if ($tests -eq 0) { + Write-Host "::error title=$($run.Name)::No test ran." + $failed = $true + } + foreach ($case in $suite.SelectNodes('testcase[failure or error]')) { + Write-Host "::error title=$($run.Name)::$($case.classname).$($case.name) failed." + } + foreach ($case in $suite.SelectNodes('testcase[skipped]')) { + Write-Host ("::error title=$($run.Name)::$($case.classname)." + + "$($case.name) skipped, so it checked nothing. " + + "$($case.skipped.message)") + $failed = $true + } + } + $summary | Add-Content $env:GITHUB_STEP_SUMMARY + exit ($failed ? 1 : 0) From 8e261c6d07465be950ccd9cd7292cf9e20173d29 Mon Sep 17 00:00:00 2001 From: James Rosewell Date: Tue, 1 Dec 2026 08:31:56 +0000 Subject: [PATCH 2/2] Give the examples' tests the TAC data file and run every test module The first run against the packages on PyPI (https://github.com/51Degrees/51degrees-python/actions/runs/46601762739, attempt 3) showed two gaps in the job. The Device Detection tests of the data file update example serve a copy of TAC-HashV41.hash, and failed with "TAC-HashV41.hash is missing". The job now fetches it with common-ci's fetch-assets action and the data license from LICENSE_KEY_V4TAC_IPIV4ENTERPRISE, without keeping it in the Actions cache because it is licensed, and puts it in cxx/data/device-detection. A test module that could not be imported stopped pytest before any test of that package ran, so the IP Intelligence and Pipeline examples gave no totals. pytest now runs with --continue-on-collection-errors, which still fails the job on each module that cannot be imported. Checked with ci/check-environment-variable-names.ps1 and the Pester tests in ci/key-names.Tests.ps1, ci/check-environment-variable-names.Tests.ps1 and ci/free-threaded-python.Tests.ps1, 246 passed and none failed. --- .github/workflows/examples-published.yml | 33 ++++++++++++++++++------ 1 file changed, 25 insertions(+), 8 deletions(-) diff --git a/.github/workflows/examples-published.yml b/.github/workflows/examples-published.yml index 3dcf518f..476f00a3 100644 --- a/.github/workflows/examples-published.yml +++ b/.github/workflows/examples-published.yml @@ -12,9 +12,10 @@ name: Examples vs Published Packages # needs a change not yet released fails here until the release, which is why # it does not run on other pull requests. # -# The tests run on the free data files and with the keys the cloud tests -# need. A missing key or data file fails the run, and so does a test that -# skipped, because a run that skips has checked nothing. +# The tests run on the free data files, with the TAC data file the data file +# update example's tests need and the keys the cloud tests need. A missing +# key or data file fails the run, and so does a test that skipped, because a +# run that skips has checked nothing. on: workflow_dispatch: @@ -109,10 +110,23 @@ jobs: 51Degrees-IPIV4AsnIpiV41.ipi 51Degrees-LiteIpiV41.ipi + # The Device Detection tests of the data file update example serve a + # copy of the TAC data file. It comes from the Distributor with the + # data license, which common-ci's fetch reads as + # _51DEGREES_LICENSE_KEY and fails without. The file is licensed, so + # it is not kept in the Actions cache. + - name: Fetch the TAC data file + if: steps.keys.outputs.run == 'true' + uses: 51Degrees/common-ci/actions/fetch-assets@main + env: + _51DEGREES_LICENSE_KEY: ${{ secrets.LICENSE_KEY_V4TAC_IPIV4ENTERPRISE }} + with: + assets: TAC-HashV41.hash + # Under the names the data repository saves them as, where the # examples look. A missing file fails the job here, because the tests - # would skip without it. - - name: Put the free data files where the examples look for them + # would skip or fail without it. + - name: Put the data files where the examples look for them if: steps.keys.outputs.run == 'true' run: | $dd = 'cxx/data/device-detection' @@ -120,6 +134,7 @@ jobs: $files = [ordered]@{ '51Degrees-LiteV4.1.hash' = "$dd/51Degrees-LiteV4.1.hash" '20000 Evidence Records.yml' = "$dd/20000 Evidence Records.yml" + 'TAC-HashV41.hash' = "$dd/TAC-HashV41.hash" '51Degrees-IPIV4AsnIpiV41.ipi' = "$ipi/51Degrees-IPIV4AsnIpiV41.ipi" '51Degrees-LiteIpiV41.ipi' = "$ipi/51Degrees-LiteV41.ipi" } @@ -129,7 +144,7 @@ jobs: Write-Host "::error title=Missing data file::$asset was not fetched." exit 1 } - Copy-Item -LiteralPath $source -Destination $files[$asset] + Move-Item -LiteralPath $source -Destination $files[$asset] } # Each package is pinned, so pip installs that version or fails, and @@ -214,7 +229,8 @@ jobs: # and the Device Detection ones without _51DEGREES_LICENSE_KEY, which # that tox.ini empties. The tests marked staging call the staging # cloud, which only the Cloud Human Confidence workflow is given, so - # they are left out. Every package is run even after one fails. + # they are left out. Every package is run even after one fails, and + # every test module even after one cannot be imported. - name: Test the examples against the published packages if: steps.keys.outputs.run == 'true' env: @@ -256,7 +272,8 @@ jobs: $report = Join-Path $results "$($run.Name).xml" Write-Host "::group::$($run.Name)" Push-Location $run.Path - python -m pytest -p no:cacheprovider -rs "--junit-xml=$report" @($run.Arguments) + python -m pytest -p no:cacheprovider -rs --continue-on-collection-errors ` + "--junit-xml=$report" @($run.Arguments) $code = $LASTEXITCODE Pop-Location Write-Host '::endgroup::'