Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,15 @@ the production cloud does not serve yet. common-ci passes on only
integration tests those tests skip. A staging test never falls back to
`_51DEGREES_CLOUD_ENDPOINT` or a default address.

Until the production cloud offers every property today's data files hold,
[`ci/run-integration-tests.ps1`](ci/run-integration-tests.ps1) gives every
cloud test in the shared integration tests the staging cloud, when
`CLOUD_STAGING_ENDPOINT` is set, as `_51DEGREES_CLOUD_ENDPOINT`, and puts that
name back afterwards. It is the only CI script that reads
`CLOUD_STAGING_ENDPOINT`. The tests keep `_51DEGREES_RESOURCE_KEY_BESPOKE`,
which the staging cloud knows, because `RESOURCE_KEY_BESPOKE_STAGING` lacks the
hardware product that the TAC and native model lookups need.

`_51DEGREES_RESOURCE_KEY` is the name a person running a cloud example sets.
The tests that call the cloud service read `_51DEGREES_RESOURCE_KEY_BESPOKE`,
and a test of an example gives the example that key under
Expand Down
108 changes: 106 additions & 2 deletions ci/key-names.Tests.ps1
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Tests that the CI scripts, the tox configurations and the cloud tests read
# each key under its _51DEGREES_ name, and fill no name from another.
# each key under its _51DEGREES_ name, and fill no name from another, apart
# from the staging cloud below.
#
# common-ci exports every _51DEGREES_ secret and organization variable under
# its own name before any of this repository's scripts run. The resource
Expand All @@ -20,6 +21,12 @@
# marked staging, which check what the production cloud does not serve yet.
# The Cloud 51Did workflow gives CLOUD_STAGING_ENDPOINT as
# _51DEGREES_CLOUD_ENDPOINT, as its tests check what the staging cloud does.
# Of the CI scripts, only ci/run-integration-tests.ps1 reads
# CLOUD_STAGING_ENDPOINT. When it is set the script gives it to the cloud
# tests as _51DEGREES_CLOUD_ENDPOINT, and puts that name back afterwards. The
# tests keep _51DEGREES_RESOURCE_KEY_BESPOKE, and no CI script reads
# RESOURCE_KEY_BESPOKE_STAGING, which lacks the hardware product the TAC and
# native model lookups need.

BeforeDiscovery {
$script:DiscoveryRoot = (Resolve-Path (Join-Path $PSScriptRoot '..')).Path
Expand All @@ -34,7 +41,8 @@ BeforeAll {
'_51DEGREES_RESOURCE_KEY', '_51DEGREES_RESOURCE_KEY_BESPOKE',
'_51DEGREES_LICENSE_KEY', 'TEMP', 'GITHUB_ENV',
'GITHUB_JOB', 'PYTEST_ADDOPTS', '_51DEGREES_IPI_PATH',
'_51DEGREES_DD_PATH')
'_51DEGREES_DD_PATH', '_51DEGREES_CLOUD_ENDPOINT',
'CLOUD_STAGING_ENDPOINT', 'RESOURCE_KEY_BESPOKE_STAGING')

function New-TempFolder {
$path = Join-Path ([IO.Path]::GetTempPath()) ([Guid]::NewGuid().ToString())
Expand Down Expand Up @@ -91,6 +99,26 @@ foreach ($asset in $Assets) { Set-Content -LiteralPath "assets/$asset" '' }
return Join-Path $repo $Product
}

# A copy of the root ci/run-integration-tests.ps1 in a repository of its
# own, with a dispatch.ps1 whose step writes the cloud names the product
# scripts would see to seen.json, then fails when $Fails is set.
function New-RootRepo([switch]$Fails) {
$repo = New-TempFolder
$ci = [IO.Directory]::CreateDirectory((Join-Path $repo 'ci')).FullName
Copy-Item (Join-Path $script:Root 'ci' 'run-integration-tests.ps1') $ci
$throw = $Fails ? "throw 'the step failed'" : ''
Set-Content (Join-Path $ci 'dispatch.ps1') @"
function Invoke-ProductStep([string]`$Step, [string]`$RepoName, `$Options) {
@{
Endpoint = `$env:_51DEGREES_CLOUD_ENDPOINT
Resource = `$env:_51DEGREES_RESOURCE_KEY_BESPOKE
} | ConvertTo-Json | Set-Content (Join-Path `$RepoName 'seen.json')
$throw
}
"@
return $repo
}

# The run script of the step called $Step in the workflow file $Workflow,
# without its indent.
function Get-StepScript([string]$Workflow, [string]$Step) {
Expand Down Expand Up @@ -259,6 +287,62 @@ Describe 'Keys under their _51DEGREES_ names' {
}
}

Describe 'The root run-integration-tests.ps1' {

BeforeEach {
$env:_51DEGREES_CLOUD_ENDPOINT = 'https://production.example.test/api/v4/'
$env:_51DEGREES_RESOURCE_KEY_BESPOKE = 'bespoke-resource-key'
}

AfterEach {
Remove-Item -Recurse -Force $script:RootRepo
}

It 'gives the cloud tests the staging cloud when CLOUD_STAGING_ENDPOINT is set, with the Bespoke key, and puts the endpoint back after' {
$script:RootRepo = New-RootRepo
$env:CLOUD_STAGING_ENDPOINT = 'https://staging.example.test/api/v4/'
$env:RESOURCE_KEY_BESPOKE_STAGING = 'staging-bespoke-key'
& (Join-Path $script:RootRepo 'ci' 'run-integration-tests.ps1') `
-RepoName $script:RootRepo | Out-Null

$seen = Get-Content -Raw (Join-Path $script:RootRepo 'seen.json') | ConvertFrom-Json
$seen.Endpoint | Should -Be 'https://staging.example.test/api/v4/'
$seen.Resource | Should -Be 'bespoke-resource-key'
$env:_51DEGREES_CLOUD_ENDPOINT | Should -Be 'https://production.example.test/api/v4/'
$env:_51DEGREES_RESOURCE_KEY_BESPOKE | Should -Be 'bespoke-resource-key'
}

It 'leaves the cloud names as they were, and says so, when CLOUD_STAGING_ENDPOINT is not set' {
$script:RootRepo = New-RootRepo
$env:RESOURCE_KEY_BESPOKE_STAGING = 'staging-bespoke-key'
$output = & (Join-Path $script:RootRepo 'ci' 'run-integration-tests.ps1') `
-RepoName $script:RootRepo *>&1 | Out-String

$seen = Get-Content -Raw (Join-Path $script:RootRepo 'seen.json') | ConvertFrom-Json
$seen.Endpoint | Should -Be 'https://production.example.test/api/v4/'
$seen.Resource | Should -Be 'bespoke-resource-key'
$output | Should -Match 'CLOUD_STAGING_ENDPOINT is not set'
}

It 'puts the endpoint back when the step fails' {
$script:RootRepo = New-RootRepo -Fails
$env:CLOUD_STAGING_ENDPOINT = 'https://staging.example.test/api/v4/'
{ & (Join-Path $script:RootRepo 'ci' 'run-integration-tests.ps1') `
-RepoName $script:RootRepo } | Should -Throw '*the step failed*'

$env:_51DEGREES_CLOUD_ENDPOINT | Should -Be 'https://production.example.test/api/v4/'
}

It 'removes the endpoint after, when it was not set before' {
$script:RootRepo = New-RootRepo
$env:_51DEGREES_CLOUD_ENDPOINT = $null
$env:CLOUD_STAGING_ENDPOINT = 'https://staging.example.test/api/v4/'
& (Join-Path $script:RootRepo 'ci' 'run-integration-tests.ps1') `
-RepoName $script:RootRepo | Out-Null

$env:_51DEGREES_CLOUD_ENDPOINT | Should -BeNullOrEmpty
}
}
Describe 'Each product''s fetch-assets.ps1' {

BeforeEach {
Expand Down Expand Up @@ -383,6 +467,26 @@ Describe 'Keys under their _51DEGREES_ names' {
$text | Should -Not -Match '\$Keys\s*(\.|\[)'
$text | Should -Not -Match 'fetch-assets\.ps1[^\r\n]*\s-(?!Assets\b)[A-Za-z]'
}

# So the cloud tests are the only step that calls the staging cloud,
# and none of them with the staging key, which lacks the hardware
# product.
It 'read CLOUD_STAGING_ENDPOINT in ci/run-integration-tests.ps1 alone, and RESOURCE_KEY_BESPOKE_STAGING in none' {
$scripts = @(Get-ChildItem $script:Root -Recurse -Filter *.ps1 |
Where-Object {
$_.FullName -match '[\\/]ci[\\/]' -and
$_.FullName -notmatch '[\\/](cxx|\.tox|\.venv|\.virtualenv)[\\/]' -and
$_.Name -notlike '*.Tests.ps1'
})
function Get-Readers([string]$Name) {
@($scripts |
Select-String -Pattern "(env:|EnvironmentVariable\(\s*['`"])$Name\b" |
ForEach-Object { [IO.Path]::GetRelativePath($script:Root, $_.Path) -replace '\\', '/' } |
Sort-Object -Unique)
}
Get-Readers 'CLOUD_STAGING_ENDPOINT' | Should -Be @('ci/run-integration-tests.ps1')
Get-Readers 'RESOURCE_KEY_BESPOKE_STAGING' | Should -BeNullOrEmpty
}
}

Describe 'Each tox.ini' {
Expand Down
22 changes: 21 additions & 1 deletion ci/run-integration-tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,24 @@ param (
$ErrorActionPreference = "Stop"
. (Join-Path $PSScriptRoot 'dispatch.ps1')

Invoke-ProductStep 'run-integration-tests' $RepoName $PSBoundParameters
# The cloud tests call the staging cloud when CI names it, because the
# production cloud does not yet offer every property today's data files hold.
# They keep _51DEGREES_RESOURCE_KEY_BESPOKE, which the staging cloud knows and
# which carries every product they ask for. Remove this once the production
# cloud has the release. The endpoint is put back afterwards, as common-ci
# runs the later steps in the same process.
$savedEndpoint = $env:_51DEGREES_CLOUD_ENDPOINT
if ($env:CLOUD_STAGING_ENDPOINT) {
Write-Output ("The cloud tests call the staging cloud " +
"CLOUD_STAGING_ENDPOINT names.")
$env:_51DEGREES_CLOUD_ENDPOINT = $env:CLOUD_STAGING_ENDPOINT
} else {
Write-Output ("CLOUD_STAGING_ENDPOINT is not set, so the cloud tests " +
"call the cloud _51DEGREES_CLOUD_ENDPOINT names.")
}

try {
Invoke-ProductStep 'run-integration-tests' $RepoName $PSBoundParameters
} finally {
$env:_51DEGREES_CLOUD_ENDPOINT = $savedEndpoint
}
2 changes: 1 addition & 1 deletion pipeline/ci/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ The following secrets are required:
* `_51DEGREES_PYPI_TOKEN` - [PyPI token](https://pypi.org/help/#apitoken) for publishing packages
* Example: `pypi-sUp3r-l0nG_r4nd0m-s7r1Ng`

The Human Confidence tests marked `staging` check what the production cloud does not serve yet, and the Cloud Human Confidence workflow (`.github/workflows/cloud-human-confidence.yml`) runs them against the staging cloud. Only CI uses these two names, which have no `_51DEGREES_` prefix. common-ci does not pass them on, so in the shared integration tests the staging tests skip.
The Human Confidence tests marked `staging` check what the production cloud does not serve yet, and the Cloud Human Confidence workflow (`.github/workflows/cloud-human-confidence.yml`) runs them against the staging cloud. Only CI uses these two names, which have no `_51DEGREES_` prefix. common-ci does not pass them on, so in the shared integration tests the staging tests skip. Until the production cloud offers every property today's data files hold, `ci/run-integration-tests.ps1` at the root of the repository gives every cloud test in the shared integration tests the staging cloud, when `CLOUD_STAGING_ENDPOINT` is set, as `_51DEGREES_CLOUD_ENDPOINT`. The tests keep `_51DEGREES_RESOURCE_KEY_BESPOKE`, which the staging cloud knows, because `RESOURCE_KEY_BESPOKE_STAGING` lacks the hardware product that the TAC and native model lookups need.
* `CLOUD_STAGING_ENDPOINT` - the staging cloud's API base, ending `/api/v4/`, an organization variable. The staging tests have no default address and skip, naming it, when it is not set.
* `RESOURCE_KEY_BESPOKE_STAGING` - a Bespoke resource key the staging cloud knows, an organization secret.
Loading