Skip to content

Commit d964b0c

Browse files
committed
fixup! ci(cosmos): use separate common action to check Azure Cosmos Emulator readiness
1 parent 6fe6d60 commit d964b0c

5 files changed

Lines changed: 62 additions & 113 deletions

File tree

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#!/usr/bin/env bash
2+
set -euo pipefail
3+
4+
docker run -d --name cosmosdb \
5+
-p 8081:8081 \
6+
-p 8080:8080 \
7+
-p 1234:1234 \
8+
-e PROTOCOL=https \
9+
mcr.microsoft.com/cosmosdb/linux/azure-cosmos-emulator:vnext-preview
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
$emulatorPath = Join-Path $env:ProgramFiles 'Azure Cosmos DB Emulator\CosmosDB.Emulator.exe'
2+
if (Test-Path $emulatorPath) {
3+
"installed=true" >> $env:GITHUB_OUTPUT
4+
Write-Host 'Azure Cosmos DB Emulator is already installed.'
5+
exit 0
6+
}
7+
8+
choco install azure-cosmosdb-emulator -y --no-progress --install-arguments="'/l*v C:\azure-cosmosdb-emulator_msi_install.log'"
9+
if ($LASTEXITCODE -eq 0 -and (Test-Path $emulatorPath)) {
10+
"installed=true" >> $env:GITHUB_OUTPUT
11+
Write-Host 'Azure Cosmos DB Emulator installed successfully.'
12+
}
13+
else {
14+
"installed=false" >> $env:GITHUB_OUTPUT
15+
Write-Warning 'Azure Cosmos DB Emulator installation failed. Continuing because Windows job runs build-only target.'
16+
if (Test-Path 'C:\azure-cosmosdb-emulator_msi_install.log') {
17+
Write-Host 'MSI log tail:'
18+
Get-Content 'C:\azure-cosmosdb-emulator_msi_install.log' -Tail 120
19+
}
20+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
$emulatorPath = Join-Path $env:ProgramFiles 'Azure Cosmos DB Emulator\CosmosDB.Emulator.exe'
2+
Start-Process -FilePath $emulatorPath -ArgumentList '/NoUI /NoExplorer /AllowNetworkAccess /EnablePreview' -PassThru | Out-Null
3+
4+
$maxAttempts = 60
5+
for ($attempt = 1; $attempt -le $maxAttempts; $attempt++) {
6+
try {
7+
$response = Invoke-WebRequest -Uri 'https://127.0.0.1:8081/' -SkipCertificateCheck -Method Get -TimeoutSec 5
8+
if ($response.StatusCode -in 200, 401) {
9+
Write-Host 'Cosmos DB Emulator is ready on Windows.'
10+
exit 0
11+
}
12+
}
13+
catch {
14+
Write-Host "Cosmos DB Emulator is not ready yet (attempt $attempt/$maxAttempts)."
15+
}
16+
17+
Start-Sleep -Seconds 5
18+
}
19+
20+
throw 'Cosmos DB Emulator failed to become ready on Windows.'

.github/workflows/build.yml

Lines changed: 3 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -30,61 +30,17 @@ jobs:
3030
id: install_cosmos_windows
3131
if: runner.os == 'Windows'
3232
shell: pwsh
33-
run: |
34-
$emulatorPath = Join-Path $env:ProgramFiles 'Azure Cosmos DB Emulator\CosmosDB.Emulator.exe'
35-
if (Test-Path $emulatorPath) {
36-
"installed=true" >> $env:GITHUB_OUTPUT
37-
Write-Host 'Azure Cosmos DB Emulator is already installed.'
38-
exit 0
39-
}
40-
41-
choco install azure-cosmosdb-emulator -y --no-progress --install-arguments="'/l*v C:\azure-cosmosdb-emulator_msi_install.log'"
42-
if ($LASTEXITCODE -eq 0 -and (Test-Path $emulatorPath)) {
43-
"installed=true" >> $env:GITHUB_OUTPUT
44-
Write-Host 'Azure Cosmos DB Emulator installed successfully.'
45-
} else {
46-
"installed=false" >> $env:GITHUB_OUTPUT
47-
Write-Warning 'Azure Cosmos DB Emulator installation failed. Continuing because Windows job runs build-only target.'
48-
if (Test-Path 'C:\azure-cosmosdb-emulator_msi_install.log') {
49-
Write-Host 'MSI log tail:'
50-
Get-Content 'C:\azure-cosmosdb-emulator_msi_install.log' -Tail 120
51-
}
52-
}
33+
run: ./.github/scripts/windows/install-cosmos-emulator.ps1
5334
continue-on-error: true
5435

5536
- name: Start Azure Cosmos DB Emulator (Windows)
5637
if: runner.os == 'Windows' && steps.install_cosmos_windows.outputs.installed == 'true'
5738
shell: pwsh
58-
run: |
59-
$emulatorPath = Join-Path $env:ProgramFiles 'Azure Cosmos DB Emulator\CosmosDB.Emulator.exe'
60-
Start-Process -FilePath $emulatorPath -ArgumentList '/NoUI /NoExplorer /AllowNetworkAccess /EnablePreview' -PassThru | Out-Null
61-
62-
$maxAttempts = 60
63-
for ($attempt = 1; $attempt -le $maxAttempts; $attempt++) {
64-
try {
65-
$response = Invoke-WebRequest -Uri 'https://127.0.0.1:8081/' -SkipCertificateCheck -Method Get -TimeoutSec 5
66-
if ($response.StatusCode -in 200, 401) {
67-
Write-Host 'Cosmos DB Emulator is ready on Windows.'
68-
exit 0
69-
}
70-
} catch {
71-
Write-Host "Cosmos DB Emulator is not ready yet (attempt $attempt/$maxAttempts)."
72-
}
73-
74-
Start-Sleep -Seconds 5
75-
}
76-
77-
throw 'Cosmos DB Emulator failed to become ready on Windows.'
39+
run: ./.github/scripts/windows/start-cosmos-emulator.ps1
7840

7941
- name: Start Azure Cosmos DB Emulator container
8042
if: runner.os == 'Linux'
81-
run: |
82-
docker run -d --name cosmosdb \
83-
-p 8081:8081 \
84-
-p 8080:8080 \
85-
-p 1234:1234 \
86-
-e PROTOCOL=https \
87-
mcr.microsoft.com/cosmosdb/linux/azure-cosmos-emulator:vnext-preview
43+
run: bash ./.github/scripts/linux/start-cosmos-emulator.sh
8844

8945
- name: Setup Azure Cosmos DB Emulator
9046
if: runner.os == 'Linux'
@@ -99,7 +55,6 @@ jobs:
9955
CI: true
10056
CONFIGURATION: ${{ matrix.configuration }}
10157
ENABLE_COVERAGE: true
102-
COSMOS_ENDPOINT: https://localhost:8081
10358

10459
- name: Build via Windows (no integration tests)
10560
if: runner.os == 'Windows'

tests/Cosmos.Tests/IntegrationInfrastructure.fs

Lines changed: 10 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
namespace FSharp.Azure.Cosmos.Tests.Integration
22

33
open System
4+
open System.Net
45
open System.Net.Http
56
open System.Net.Security
6-
open System.Security.Cryptography.X509Certificates
77
open System.Threading
88
open System.Threading.Tasks
99

@@ -44,10 +44,8 @@ type TestBase () =
4444
member this.CancellationToken = this.TestContext.CancellationTokenSource.Token
4545

4646
type DatabaseTestApplicationFactory (testContext : TestContext) =
47-
let endpoint =
48-
match Environment.GetEnvironmentVariable "COSMOS_ENDPOINT" with
49-
| value when String.IsNullOrWhiteSpace value |> not -> value
50-
| _ -> "https://127.0.0.1:8081"
47+
[<Literal>]
48+
let endpoint = "https://127.0.0.1:8081"
5149

5250
[<Literal>]
5351
let primaryKey =
@@ -57,68 +55,19 @@ type DatabaseTestApplicationFactory (testContext : TestContext) =
5755

5856
let databaseId = buildDatabaseId ()
5957

60-
let endpointUri = Uri endpoint
61-
62-
let isTlsDiagnosticsEnabled =
63-
match Environment.GetEnvironmentVariable "COSMOS_TLS_DIAGNOSTICS" with
64-
| null -> false
65-
| value ->
66-
value.Equals ("1", StringComparison.OrdinalIgnoreCase)
67-
|| value.Equals ("true", StringComparison.OrdinalIgnoreCase)
68-
|| value.Equals ("yes", StringComparison.OrdinalIgnoreCase)
69-
7058
let isLocalEmulatorHost (uri : Uri) =
7159
uri.Host.Equals ("localhost", StringComparison.OrdinalIgnoreCase)
7260
|| uri.Host.Equals ("127.0.0.1", StringComparison.OrdinalIgnoreCase)
7361

74-
let formatChainStatus (chain : X509Chain) =
75-
match chain with
76-
| null -> "<no chain>"
77-
| chain when Array.isEmpty chain.ChainStatus -> "<empty chain status>"
78-
| chain ->
79-
chain.ChainStatus
80-
|> Array.map (fun status ->
81-
let statusInformation =
82-
match status.StatusInformation with
83-
| null -> ""
84-
| info -> info.Trim ()
85-
86-
$"{status.Status}:{statusInformation}"
87-
)
88-
|> String.concat "; "
89-
90-
let logTlsDiagnostics (requestUri : Uri) (certificate : X509Certificate2) (chain : X509Chain) (errors : SslPolicyErrors) =
91-
if isTlsDiagnosticsEnabled then
92-
let subject =
93-
match certificate with
94-
| null -> "<no certificate>"
95-
| cert -> cert.Subject
96-
97-
let issuer =
98-
match certificate with
99-
| null -> "<no issuer>"
100-
| cert -> cert.Issuer
101-
102-
Console.Error.WriteLine (
103-
$"[Cosmos TLS] URI={requestUri}; Errors={errors}; Subject={subject}; Issuer={issuer}; ChainStatus={formatChainStatus chain}"
104-
)
105-
10662
let createHttpClient () =
10763
let handler = new HttpClientHandler ()
10864

10965
handler.ServerCertificateCustomValidationCallback <-
110-
(fun request certificate chain errors ->
111-
let requestUri =
112-
match request.RequestUri with
113-
| null -> endpointUri
114-
| uri -> uri
115-
116-
logTlsDiagnostics requestUri certificate chain errors
117-
118-
if errors = SslPolicyErrors.None then
119-
true
120-
else
121-
isLocalEmulatorHost requestUri
66+
(fun request _ _ errors ->
67+
match request.RequestUri with
68+
| null -> errors = SslPolicyErrors.None
69+
| requestUri when errors = SslPolicyErrors.None -> true
70+
| requestUri -> isLocalEmulatorHost requestUri
12271
)
12372

12473
new HttpClient (handler, true)
@@ -136,12 +85,8 @@ type DatabaseTestApplicationFactory (testContext : TestContext) =
13685
member _.Database = database
13786

13887
member _.InitializeAsync (cancellationToken : CancellationToken) : Task = task {
139-
try
140-
let! createdDatabase = client.CreateDatabaseIfNotExistsAsync (databaseId, cancellationToken = cancellationToken)
141-
database <- ValueSome createdDatabase.Database
142-
with ex ->
143-
Console.Error.WriteLine ($"[Cosmos Initialize] Endpoint={endpoint}; DatabaseId={databaseId}; Error={ex.Message}")
144-
return raise ex
88+
let! createdDatabase = client.CreateDatabaseIfNotExistsAsync (databaseId, cancellationToken = cancellationToken)
89+
database <- ValueSome createdDatabase.Database
14590
}
14691

14792
member _.CleanupAsync (cancellationToken : CancellationToken) : Task = task {

0 commit comments

Comments
 (0)