Skip to content
Merged
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
92 changes: 41 additions & 51 deletions .github/workflows/cloud-human-confidence.yml
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
name: Cloud Human Confidence Tests

# Runs the Human Confidence web example's live test
# Runs the Human Confidence web example's live tests
# (examples/device-detection/web/human-confidence, HumanConfidenceLiveTest)
# once for each resource key below. Each pass sets the single
# _51DEGREES_RESOURCE_KEY the example reads, and tells the test with
# -DhumanConfidenceSource what that key's answer carries. The Bespoke key's
# products offer HumanConfidenceSource, so its answer carries Passive. The
# Basic key's products don't, so its answer carries no source.
# once, against the staging cloud, because the production cloud does not
# serve HumanConfidenceSource yet. Each test reads its own resource key under
# the key's own name. The Bespoke key's products offer HumanConfidenceSource,
# so its answer carries Passive and a level. The Basic key's products don't,
# so the cloud answers its source as null, with the reason.
#
# The resource keys are organization secrets, and _51DEGREES_CLOUD_ENDPOINT,
# the service the example calls, is an organization variable. A new key needs
# a line here as well as the organization secret.
# CLOUD_STAGING_ENDPOINT, the staging cloud's API base, is an organization
# variable that only CI uses. RESOURCE_KEY_BESPOKE_STAGING is a Bespoke key
# the staging cloud resolves, and _51DEGREES_RESOURCE_KEY_BASIC resolves on
# both clouds. Both keys are organization secrets. A new key needs a line
# here and in the test, as well as the organization secret.
#
# A pass fails when its key is empty or when the test is skipped. A cloud
# test that passes without calling the cloud reports success having proved
# The run fails when a name is empty or when a test is skipped. A cloud test
# that passes without calling the cloud reports success having proved
# nothing, and that is worse than a red job.

on:
Expand Down Expand Up @@ -42,52 +44,40 @@ jobs:
distribution: temurin
java-version: '17'
cache: maven
- name: Human Confidence test for each resource key
- name: Human Confidence tests
shell: pwsh
working-directory: examples/device-detection
env:
_51DEGREES_RESOURCE_KEY_BESPOKE: ${{ secrets._51DEGREES_RESOURCE_KEY_BESPOKE }}
RESOURCE_KEY_BESPOKE_STAGING: ${{ secrets.RESOURCE_KEY_BESPOKE_STAGING }}
_51DEGREES_RESOURCE_KEY_BASIC: ${{ secrets._51DEGREES_RESOURCE_KEY_BASIC }}
_51DEGREES_CLOUD_ENDPOINT: ${{ vars._51DEGREES_CLOUD_ENDPOINT }}
CLOUD_STAGING_ENDPOINT: ${{ vars.CLOUD_STAGING_ENDPOINT }}
run: |
# Each key, with the source its answer carries.
$passes = [ordered]@{
_51DEGREES_RESOURCE_KEY_BESPOKE = 'Passive'
_51DEGREES_RESOURCE_KEY_BASIC = 'none'
# Each test skips without its key or the endpoint, so a missing one
# fails here, naming every organization secret or variable missing.
$missing = @(
'RESOURCE_KEY_BESPOKE_STAGING'
'_51DEGREES_RESOURCE_KEY_BASIC'
'CLOUD_STAGING_ENDPOINT'
) | Where-Object { -not [Environment]::GetEnvironmentVariable($_) }
if ($missing) {
throw ("The organization secret or variable " +
"$($missing -join ', ') is empty or missing, so the live " +
'tests cannot run.')
}
$report = 'web/human-confidence/target/surefire-reports/' +
'TEST-fiftyone.devicedetection.examples.web.HumanConfidenceLiveTest.xml'
$failed = $false
foreach ($name in $passes.Keys) {
Write-Host "::group::Live test with $name"
$key = [Environment]::GetEnvironmentVariable($name)
if (-not $key) {
Write-Host ("::error::The organization secret $name is " +
'empty or missing, so the live test cannot run with it.')
$failed = $true
Write-Host '::endgroup::'
continue
}
$env:_51DEGREES_RESOURCE_KEY = $key
Remove-Item $report -ErrorAction SilentlyContinue
mvn -B --no-transfer-progress -pl web/human-confidence -am test `
'-Dtest=HumanConfidenceLiveTest' `
'-Dsurefire.failIfNoSpecifiedTests=false' `
"-DhumanConfidenceSource=$($passes[$name])"
if ($LASTEXITCODE -ne 0) {
$failed = $true
} elseif (-not (Test-Path $report)) {
Write-Host "::error::The live test with $name did not run."
$failed = $true
} else {
$suite = ([xml](Get-Content -Raw $report)).testsuite
if ([int]$suite.tests -eq 0 -or [int]$suite.skipped -gt 0) {
Write-Host ("::error::The live test with $name was " +
"skipped, so it proved nothing. " +
"$($suite.testcase.skipped.message)")
$failed = $true
}
}
Write-Host '::endgroup::'
Remove-Item $report -ErrorAction SilentlyContinue
mvn -B --no-transfer-progress -pl web/human-confidence -am test `
'-Dtest=HumanConfidenceLiveTest' `
'-Dsurefire.failIfNoSpecifiedTests=false'
if ($LASTEXITCODE -ne 0) {
throw 'The live tests failed.'
}
if (-not (Test-Path $report)) {
throw 'The live tests did not run.'
}
$suite = ([xml](Get-Content -Raw $report)).testsuite
if ([int]$suite.tests -eq 0 -or [int]$suite.skipped -gt 0) {
throw ('A live test was skipped, so it proved nothing. ' +
"$($suite.testcase.skipped.message)")
}
if ($failed) { exit 1 }
10 changes: 6 additions & 4 deletions AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -270,11 +270,13 @@ and for a list of properties that fails once or every time.

Every name 51Degrees defines for an environment variable or a Java system
property starts `_51DEGREES_`, each name holds one thing, and no name is
filled from another. A name the platform or a build tool defines keeps the
name its owner gave it.
filled from another. A name only CI uses, which no customer sets, has no
prefix and says what it holds. A name the platform or a build tool defines
keeps the name its owner gave it.
[`ci/check-environment-variable-names.ps1`](ci/check-environment-variable-names.ps1)
reads every tracked file and fails on a name read or set that is neither. It
holds the short list of platform and tool names. Run it, and its tests in
reads every tracked file and fails on a name read or set that is none of
these. It holds the short lists of CI names and of platform and tool
names. Run it, and its tests in
[`ci/check-environment-variable-names.Tests.ps1`](ci/check-environment-variable-names.Tests.ps1),
after a change that reads or sets a name.

Expand Down
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,10 @@ on-premise tests need the free data files, which the script in
Intelligence data file, see `ip-intelligence/README.md`.

Every environment variable name that the packages, tests, examples and CI
scripts define starts `_51DEGREES_`, which
scripts define starts `_51DEGREES_`, apart from the few that only CI uses,
which
[`ci/check-environment-variable-names.ps1`](ci/check-environment-variable-names.ps1)
checks.
lists and checks.
Tests find data files by searching from the root of this repository, so run
Maven from here rather than from inside a package folder.

Expand Down
23 changes: 18 additions & 5 deletions ci/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,10 @@ Required:
secret that only CI uses, so it has no `_51DEGREES_` prefix.

The workflows pass the secrets to common-ci with `secrets: inherit`, and
common-ci exports each secret and each `_51DEGREES_` organization variable
under its own name before the scripts here run. The tests read only these
names.
common-ci exports each `_51DEGREES_` secret and variable, and the CI secrets
on its own list, under its own name before the scripts here run. The tests
read these names, apart from the Human Confidence live tests below, which
read two names that common-ci does not export.

To run the on-premise tests:

Expand All @@ -144,12 +145,24 @@ organization secrets:
- `_51DEGREES_RESOURCE_KEY_BASIC`: resource key whose products don't offer
`HumanConfidenceSource`.
[cloud-human-confidence.yml](../.github/workflows/cloud-human-confidence.yml)
runs the Human Confidence example's live test with it, expecting no
source, and with `_51DEGREES_RESOURCE_KEY_BESPOKE`, expecting `Passive`.
runs the Human Confidence example's live test of a key outside Bespoke
with it, on the staging cloud, expecting the cloud to answer the source as
null and the example's answer to carry none.
- `_51DEGREES_CLOUD_ENDPOINT`: an organization variable naming the cloud
service the cloud tests and examples call, the API base including
`/api/v4/`. They call the public service when it is not set.

The production cloud does not serve `HumanConfidenceSource` yet, so the
Human Confidence example's live tests run against the staging cloud, with
two names that only CI uses and so have no `_51DEGREES_` prefix:

- `CLOUD_STAGING_ENDPOINT`: an organization variable naming the staging
cloud, the API base including `/api/v4/`.
- `RESOURCE_KEY_BESPOKE_STAGING`: an organization secret holding a Bespoke
resource key the staging cloud resolves.
[cloud-human-confidence.yml](../.github/workflows/cloud-human-confidence.yml)
runs the live test of the Bespoke key with it, expecting `Passive`.

A cloud test with no key is skipped, naming the variable it reads.

To publish to Maven Central (51Degrees only), these organization secrets,
Expand Down
9 changes: 9 additions & 0 deletions ci/check-environment-variable-names.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@ Describe 'Test-AllowedName' {
Test-AllowedName $Name | Should -BeTrue
}

It 'allows the CI name <Name>' -ForEach @(
@{ Name = 'CLOUD_STAGING_ENDPOINT' }
@{ Name = 'RESOURCE_KEY_BESPOKE_STAGING' }
) {
Test-AllowedName $Name | Should -BeTrue
}

It 'allows the platform name <Name>' -ForEach @(
@{ Name = 'PATH' }
@{ Name = 'JAVA_HOME' }
Expand All @@ -55,6 +62,8 @@ Describe 'Test-AllowedName' {

It 'refuses <Name>' -ForEach @(
@{ Name = 'SAMPLE_NAME' }
@{ Name = 'cloud_staging_endpoint' }
@{ Name = 'RESOURCE_KEY_BESPOKE' }
@{ Name = 'SampleName' }
@{ Name = 'sample.name' }
@{ Name = '_51DEGREES_' }
Expand Down
25 changes: 18 additions & 7 deletions ci/check-environment-variable-names.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
# on:
#
# - any name that starts with the digit, wherever it is written, and
# - any name read or set that neither starts _51DEGREES_ nor is one the
# platform or a build tool defines, which are listed below.
# - any name read or set that neither starts _51DEGREES_, nor is one of the
# names only CI uses, nor is one the platform or a build tool defines,
# which are listed below.
#
# A name counts as read or set when it is a string given to getenv or to
# System.getProperty, setProperty or clearProperty, a name on PowerShell's
Expand Down Expand Up @@ -61,13 +62,22 @@ $script:PlatformNames = @(
'CHROMEWEBDRIVER'
)

# The names only CI uses. No customer sets them, so they have no
# _51DEGREES_ prefix, and each name says what it holds.
$script:CiNames = @(
# The staging cloud and a Bespoke key it resolves, for the tests of what
# only the staging cloud serves.
'CLOUD_STAGING_ENDPOINT', 'RESOURCE_KEY_BESPOKE_STAGING'
)

# The system properties the JDK defines start with one of these.
$script:PlatformPropertyPattern = [regex]::new('^(?:java|os|user|sun|file|line)\.[A-Za-z0-9_.]+$')

# Whether a name read or set is one 51Degrees defines under its prefix, or
# one the platform or a build tool defines.
# Whether a name read or set is one 51Degrees defines under its prefix, one
# only CI uses, or one the platform or a build tool defines.
function Test-AllowedName([string]$Name) {
return $script:OwnNamePattern.IsMatch($Name) -or
$script:CiNames -ccontains $Name -or
$script:PlatformNames -ccontains $Name -or
$script:PlatformPropertyPattern.IsMatch($Name)
}
Expand Down Expand Up @@ -259,8 +269,9 @@ if ($digits.Count -gt 0 -or $names.Count -gt 0) {
throw ("$($digits.Count) names start with a digit, which a POSIX shell " +
'cannot set, so each must start with an underscore, and ' +
"$($names.Count) names are read or set that are not _51DEGREES_ " +
'names or names the platform or a build tool defines.')
'names, names only CI uses, or names the platform or a build tool ' +
'defines.')
}

Write-Output ('Every name read or set is a _51DEGREES_ name or one the ' +
'platform or a build tool defines.')
Write-Output ('Every name read or set is a _51DEGREES_ name, one only CI ' +
'uses, or one the platform or a build tool defines.')
Loading
Loading