Skip to content

Commit 2bf6bf2

Browse files
Register Multiple Databases for FluentBecnhmarker Tests (#151)
* Register .benchmark1 and .benchmark2 databases for FluentBenchmarker * Create vapor_benchmark databases in GitHub Action workflow * Use prepared-migration-filtering FluentKit branch * Remove invalid 'creatdb' calls from Ubuntu workflow * Create becnhmark databases for Ubuntu GitHub workflows, take #1 * Add Postgres repository to APT before attempting to install the database in the Ubuntu GitHub workflows * Create benchmark databases for Ubuntu GitHub workflows, take #2 * Update APT repository before installing anything * Try not installing 'deb' * Attempt to wait for database to settle dynamically instead of a hardcoded second value * Pass host, username, and password to psql and createdb commands for Ubuntu workflows * Use PGPASSWORD environment variable to provide password to psql and createdb commands * Specify vapor_database as database name to wait for to settle * Fix FluentBenchmarker creation. Also make tests respect the LOG_LEVEL env var. * Full-matrix CI for PostgreSQL Maybe this one won't need six rounds of "oops this tiny thing is wrong..." * Replace .benchmark1 and .benchmark2 databases with .migrationExtra * Revert FluentKit dependency version Co-authored-by: Gwynne Raskind <[email protected]>
1 parent b276ac0 commit 2bf6bf2

File tree

2 files changed

+99
-50
lines changed

2 files changed

+99
-50
lines changed

.github/workflows/test.yml

+69-44
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,79 @@
1-
name: test
2-
on:
3-
- pull_request
1+
name: Test Matrix
2+
on: ['pull_request']
3+
defaults:
4+
run:
5+
shell: bash
46
jobs:
5-
fluent-postgres-driver_macos:
7+
PR-tests-linux:
68
strategy:
79
fail-fast: false
810
matrix:
9-
psql: ['postgresql@11', 'postgresql@12']
11+
dbimage: ['postgres:11', 'postgres:12']
12+
runner: [
13+
'swift:5.2-xenial', 'swift:5.2-bionic',
14+
'swiftlang/swift:nightly-5.2-xenial', 'swiftlang/swift:nightly-5.2-bionic',
15+
'swiftlang/swift:nightly-5.3-xenial', 'swiftlang/swift:nightly-5.3-bionic',
16+
'swiftlang/swift:nightly-master-xenial', 'swiftlang/swift:nightly-master-bionic',
17+
'swiftlang/swift:nightly-master-focal',
18+
'swiftlang/swift:nightly-master-centos8',
19+
'swiftlang/swift:nightly-master-amazonlinux2'
20+
]
1021
include:
11-
# The PostgreSQL formulas don't use a consistent data dir
12-
- psql: 'postgresql@11'
13-
dbroot: '/usr/local/var/postgresql@11'
14-
- psql: 'postgresql@12'
15-
dbroot: '/usr/local/var/postgres'
16-
runs-on: macos-latest
22+
- installcmd: 'apt-get -q update && apt-get -q install -y postgresql-client'
23+
- { 'runner': 'swiftlang/swift:nightly-master-centos8', 'installcmd': 'dnf install -y zlib-devel postgresql' }
24+
- { 'runner': 'swiftlang/swift:nightly-master-amazonlinux2', 'installcmd': 'yum install -y zlib-devel postgresql' }
25+
container: ${{ matrix.runner }}
26+
runs-on: ubuntu-latest
27+
services:
28+
postgres:
29+
image: ${{ matrix.dbimage }}
30+
env: { POSTGRES_USER: vapor_username, POSTGRES_PASSWORD: vapor_password, POSTGRES_DB: vapor_database }
31+
env: { 'PGPASSWORD': 'vapor_password' }
1732
steps:
18-
- run: sudo xcode-select -s /Applications/Xcode_11.4.app/Contents/Developer
19-
- run: brew uninstall --force libpq postgresql@11 postgresql@12 php
20-
- run: brew install ${{ matrix.psql }}
21-
- run: brew link --force ${{ matrix.psql }}
22-
- run: bash -c '[[ -d "${{ matrix.dbroot }}" ]] && mv "${{ matrix.dbroot }}" /usr/local/var/.outta-the-way || true'
23-
- run: mkdir -p /usr/local/var/log "${{ matrix.dbroot }}"
24-
- run: initdb --locale=C -E UTF-8 "${{ matrix.dbroot }}"
25-
- run: brew services start ${{ matrix.psql }}
26-
- run: sleep 4 # give the server a chance to settle
27-
- run: createuser -d -l -s vapor_username
28-
- run: createdb -O vapor_username vapor_database
29-
- uses: actions/checkout@v2
30-
- run: swift test --enable-test-discovery --sanitize=thread
31-
- run: brew services stop ${{ matrix.psql }}
32-
fluent-postgres-driver_ubuntu:
33+
- name: Install dependencies
34+
run: ${{ matrix.installcmd }}
35+
- name: Wait for Postgres server to be ready
36+
run: until echo | psql -hpostgres -Uvapor_username vapor_database; do sleep 1; done
37+
timeout-minutes: 5
38+
- name: Set up Postgres databases and privileges
39+
run: |
40+
for db in vapor_migration_extra; do createdb -hpostgres -Uvapor_username -Ovapor_username $db; done
41+
- name: Check out code
42+
uses: actions/checkout@v2
43+
- name: Run tests with Thread Sanitizer
44+
run: swift test --enable-test-discovery --sanitize=thread
45+
env: { 'POSTGRES_HOSTNAME': 'postgres' }
46+
PR-tests-macos:
3347
strategy:
3448
fail-fast: false
3549
matrix:
36-
psql: ['11', '11-alpine', '12', '12-alpine']
37-
base: ['xenial', 'bionic']
38-
container:
39-
image: vapor/swift:5.2-${{ matrix.base }}-ci
40-
services:
41-
psql:
42-
image: postgres:${{ matrix.psql }}
43-
ports:
44-
- 5432:5432
45-
env:
46-
POSTGRES_USER: vapor_username
47-
POSTGRES_DB: vapor_database
48-
POSTGRES_PASSWORD: vapor_password
49-
runs-on: ubuntu-latest
50+
include:
51+
- {'formula': 'postgresql@11', 'datadir': 'postgresql@11'}
52+
- {'formula': 'postgresql@12', 'datadir': 'postgres'}
53+
runs-on: macos-latest
54+
env: { 'PGPASSWORD': 'vapor_password' }
5055
steps:
51-
- uses: actions/checkout@v2
52-
- run: swift test --enable-test-discovery --sanitize=thread
53-
env:
54-
POSTGRES_HOSTNAME: psql
56+
- name: Select latest available Xcode
57+
uses: maxim-lobanov/[email protected]
58+
with: { 'xcode-version': 'latest' }
59+
- name: Blow away the default Postgres installation
60+
run: brew uninstall --force postgresql php && rm -rf /usr/local/{etc,var}/{postgres,pg}*
61+
- name: Install Postgres server from Homebrew
62+
run: brew install ${{ matrix.formula }} && brew link --force ${{ matrix.formula }}
63+
- name: Make sure Postgres has a database, Homebrew fails REALLY bad at this
64+
run: initdb --locale=C -E UTF-8 $(brew --prefix)/var/${{ matrix.datadir }}
65+
- name: Start Postgres server
66+
run: brew services start ${{ matrix.formula }}
67+
- name: Wait for Postgres server to be ready
68+
run: until echo | psql postgres; do sleep 1; done
69+
timeout-minutes: 5
70+
- name: Set up Postgres databases and privileges
71+
run: |
72+
createuser --createdb --login vapor_username
73+
for db in vapor_{database,migration_extra}; do
74+
createdb -Ovapor_username $db && psql $db <<<"ALTER SCHEMA public OWNER TO vapor_username;"
75+
done
76+
- name: Check out code
77+
uses: actions/checkout@v2
78+
- name: Run tests with Thread Sanitizer
79+
run: swift test --enable-test-discovery --sanitize=thread

Tests/FluentPostgresDriverTests/FluentPostgresDriverTests.swift

+30-6
Original file line numberDiff line numberDiff line change
@@ -173,21 +173,42 @@ final class FluentPostgresDriverTests: XCTestCase {
173173
override func setUpWithError() throws {
174174
try super.setUpWithError()
175175

176-
let configuration = PostgresConfiguration(
176+
let defaultConfig = PostgresConfiguration(
177177
hostname: hostname,
178178
username: "vapor_username",
179179
password: "vapor_password",
180180
database: "vapor_database"
181181
)
182+
183+
let migrationExtraConfig = PostgresConfiguration(
184+
hostname: hostname,
185+
username: "vapor_username",
186+
password: "vapor_password",
187+
database: "vapor_migration_extra"
188+
)
189+
182190
XCTAssert(isLoggingConfigured)
183191
self.eventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: 1)
184192
self.threadPool = NIOThreadPool(numberOfThreads: 1)
185193
self.dbs = Databases(threadPool: threadPool, on: self.eventLoopGroup)
186-
self.dbs.use(.postgres(configuration: configuration), as: .psql)
194+
195+
self.dbs.use(.postgres(configuration: defaultConfig), as: .psql)
196+
self.dbs.use(.postgres(configuration: migrationExtraConfig), as: .migrationExtra)
187197

188198
// reset the database
199+
let databaseExtra = try XCTUnwrap(
200+
self.benchmarker.databases.database(
201+
.migrationExtra,
202+
logger: Logger(label: "com.test.migration_extra"),
203+
on: self.eventLoopGroup.next()
204+
) as? PostgresDatabase
205+
)
206+
189207
_ = try self.postgres.query("drop schema public cascade").wait()
190208
_ = try self.postgres.query("create schema public").wait()
209+
210+
_ = try databaseExtra.query("drop schema public cascade").wait()
211+
_ = try databaseExtra.query("create schema public").wait()
191212
}
192213

193214
override func tearDownWithError() throws {
@@ -199,9 +220,8 @@ final class FluentPostgresDriverTests: XCTestCase {
199220
}
200221

201222
extension DatabaseID {
202-
static var iso8601: Self {
203-
.init(string: "iso8601")
204-
}
223+
static let iso8601 = DatabaseID(string: "iso8601")
224+
static let migrationExtra = DatabaseID(string: "migration_extra")
205225
}
206226

207227
var hostname: String {
@@ -247,10 +267,14 @@ struct EventMigration: Migration {
247267
}
248268
}
249269

270+
func env(_ name: String) -> String? {
271+
return ProcessInfo.processInfo.environment[name]
272+
}
273+
250274
let isLoggingConfigured: Bool = {
251275
LoggingSystem.bootstrap { label in
252276
var handler = StreamLogHandler.standardOutput(label: label)
253-
handler.logLevel = .debug
277+
handler.logLevel = env("LOG_LEVEL").flatMap { Logger.Level(rawValue: $0) } ?? .debug
254278
return handler
255279
}
256280
return true

0 commit comments

Comments
 (0)