Skip to content

Commit

Permalink
tests: Add tests for mysql and postgres socket path option
Browse files Browse the repository at this point in the history
  • Loading branch information
MaximeHeckel committed Jul 27, 2020
1 parent 7907b5d commit 7f7954e
Show file tree
Hide file tree
Showing 6 changed files with 129 additions and 13 deletions.
20 changes: 20 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,13 @@ jobs:
POSTGRES_PASSWORD: postgres
ports:
- 5432:5432
postgressocket:
image: postgres:12
volumes:
- /mnt:/var/run/postgresql
env:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
mysql:
image: mysql:5.7
env:
Expand All @@ -60,6 +67,14 @@ jobs:
ports:
- 33306:3306
options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=3
mysqlsocket:
image: mysql:5.7
volumes:
- /mnt:/var/run/mysqld
env:
MYSQL_ROOT_PASSWORD: mysql
MYSQL_DATABASE: test
options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=3
steps:
- name: Checkout repository
uses: actions/checkout@v2
Expand All @@ -74,12 +89,17 @@ jobs:
echo "::set-env name=CHROME_PATH::$(which google-chrome-stable)"
echo "::set-env name=POSTGRES_DB_URL::postgres://postgres:postgres@localhost/lighthouse_ci_test"
echo "::set-env name=MYSQL_DB_URL::mysql://root:[email protected]:33306/lighthouse_ci_test"
echo "::set-env name=MYSQL_SOCKET_PATH::/mnt/mysqld.sock"
echo "::set-env name=POSTGRES_SOCKET_PATH::/mnt/.s.PGSQL.5432"
export PGPASSWORD="postgres"
export MYSQL_PWD="mysql"
psql -h localhost -p 5432 -c 'create database lighthouse_ci_test;' -U postgres
mysql --host=127.0.0.1 --port=33306 -e 'create database lighthouse_ci_test;' -u root
psql --host /mnt -c 'create database lighthouse_ci_test;' -U postgres
mysql --socket=/mnt/mysqld.sock -e 'create database lighthouse_ci_test;' -u root
google-chrome-stable --version
- run: yarn
- run: yarn build
Expand Down
13 changes: 3 additions & 10 deletions docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -892,15 +892,6 @@ _sqlDialect=sqlite only_
The path to the sqlite database on the local filesystem relative to the current working directory.
##### `storage.sqlSocketPath`
_sqlDialect=mysql or sqlDialect=postgres only_
The database Unix socket file path for the MySQL or PostgreSQL database.
Note: If used, this option will require to specify the `database`, `username` and `password` sequelize options.
See `storage.sequelizeOptions` for more information.
##### `storage.sqlConnectionUrl`
_sqlDialect=mysql or sqlDialect=postgres only_
Expand Down Expand Up @@ -1213,7 +1204,9 @@ module.exports = {
"server": {
"storage": {
"sqlDialect": "mysql",
"sqlSocketPath": "/var/lib/mysql/mysql.sock",
"sqlDialectOptions": {
"socketPath": "/var/lib/mysql/mysql.sock"
},
"sequelizeOptions": {
"database": "reports",
"username": "admin",
Expand Down
3 changes: 1 addition & 2 deletions packages/server/src/api/storage/sql/sql.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ function createSequelize(options) {
});
}

if (options.sqlSocketPath) {
if (options.sqlDialectOptions && options.sqlDialectOptions.socketPath) {
if (
!options.sequelizeOptions ||
!options.sequelizeOptions.database ||
Expand All @@ -111,7 +111,6 @@ function createSequelize(options) {
{
...sequelizeOptions,
dialect: options.sqlDialect,
host: options.sqlSocketPath,
ssl: !!options.sqlConnectionSsl,
dialectOptions: options.sqlDialectOptions,
}
Expand Down
52 changes: 52 additions & 0 deletions packages/server/test/mysql-socket-path-server.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/**
* @license Copyright 2019 Google Inc. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
*/
'use strict';

/* eslint-env jest */

const runTests = require('./server-test-suite.js').runTests;
const runServer = require('../src/server.js').createServer;

describe('mysql server (socket path)', () => {
if (!process.env.MYSQL_SOCKET_PATH) {
it.skip('should work on mysql', () => {});
return;
}

const state = {
port: undefined,
closeServer: undefined,
};

beforeAll(async () => {
const {port, close, storageMethod} = await runServer({
logLevel: 'silent',
port: 0,
storage: {
storageMethod: 'sql',
sqlDialect: 'mysql',
sqlDialectOptions: {
socketPath: process.env.MYSQL_SOCKET_PATH,
},
sequelizeOptions: {
database: 'lighthouse_ci_test',
username: 'root',
password: 'mysql',
},
},
});

state.port = port;
state.closeServer = close;
state.storageMethod = storageMethod;
});

afterAll(() => {
state.closeServer();
});

runTests(state);
});
52 changes: 52 additions & 0 deletions packages/server/test/postgres-socket-path-server.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/**
* @license Copyright 2019 Google Inc. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
*/
'use strict';

/* eslint-env jest */

const runTests = require('./server-test-suite.js').runTests;
const runServer = require('../src/server.js').createServer;

describe('postgres server', () => {
if (!process.env.POSTGRES_DB_URL) {
it.skip('should work on postgres', () => {});
return;
}

const state = {
port: undefined,
closeServer: undefined,
};

beforeAll(async () => {
const {port, close, storageMethod} = await runServer({
logLevel: 'silent',
port: 0,
storage: {
storageMethod: 'sql',
sqlDialect: 'postgres',
sqlDialectOptions: {
socketPath: process.env.POSTGRES_SOCKET_PATH,
},
sequelizeOptions: {
database: 'lighthouse_ci_test',
username: 'postgres',
password: 'postgres',
},
},
});

state.port = port;
state.closeServer = close;
state.storageMethod = storageMethod;
});

afterAll(() => {
state.closeServer();
});

runTests(state);
});
2 changes: 1 addition & 1 deletion types/server.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ declare global {
storageMethod: 'sql' | 'spanner';
sqlDialect: 'sqlite' | 'mysql' | 'postgres';
sqlDialectOptions?: {
socketPath?: string;
ssl?: {
ca?: string;
key?: string;
Expand All @@ -170,7 +171,6 @@ declare global {
sqlDatabasePath?: string;
sqlConnectionSsl?: string;
sqlConnectionUrl?: string;
sqlSocketPath?: string;
sqlDangerouslyResetDatabase?: boolean;
sequelizeOptions?: import('sequelize').Options;
}
Expand Down

0 comments on commit 7f7954e

Please sign in to comment.