Skip to content

Commit

Permalink
fix(ci): add beforeAll to force create needed system tables
Browse files Browse the repository at this point in the history
  • Loading branch information
duyet committed Nov 29, 2024
1 parent 8f7579d commit ba8d6c7
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 9 deletions.
6 changes: 2 additions & 4 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -282,9 +282,7 @@ jobs:
${{ runner.os }}-nextjs-${{ hashFiles('**/package-lock.json', '**/yarn.lock') }}-
- name: Install dependencies
run: |
yarn install
run: yarn install

- name: Jest
run: |
yarn jest -t 'query config'
run: yarn test-queries-config
2 changes: 2 additions & 0 deletions app/[host]/[query]/more/backups.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ export const backupsConfig: QueryConfig = {
description: `To restore a backup:
RESTORE TABLE data_lake.events AS data_lake.events_restore FROM Disk('s3_backup', 'data_lake.events_20231212')`,
docs: BACKUP_LOG,
// system.backup_log can be not exist if no backups were made
optional: true,
sql: `
SELECT *,
formatReadableSize(total_size) as readable_total_size,
Expand Down
1 change: 1 addition & 0 deletions app/[host]/[query]/more/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { type QueryConfig } from '@/types/query-config'
export const errorsConfig: QueryConfig = {
name: 'errors',
description: 'System error logs and history',
optional: true,
sql: `
SELECT *
FROM system.error_log
Expand Down
2 changes: 2 additions & 0 deletions app/[host]/[query]/more/zookeeper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ export const zookeeperConfig: QueryConfig = {
description:
'Exposes data from the Keeper cluster defined in the config. https://clickhouse.com/docs/en/operations/system-tables/zookeeper',
docs: ZOOKEEPER,
// system.zookeeper can be not exist if no zookeeper is configured
optional: true,
sql: `
SELECT
replaceOne(format('{}/{}', path, name), '//', '/') AS _path,
Expand Down
46 changes: 43 additions & 3 deletions app/[host]/[query]/query-config.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { beforeAll, expect, test } from '@jest/globals'

import { fetchData } from '@/lib/clickhouse'
import { expect, test } from '@jest/globals'
import { queries } from './clickhouse-queries'

describe('query config', () => {
Expand All @@ -11,23 +12,62 @@ describe('query config', () => {
return { name: config.name, config }
})

beforeAll(async () => {
try {
console.log('prepare data for system.error_log')
await fetchData({
query: 'SELECT * FROM not_found_table_will_fail',
})
await fetchData({
query: 'INSERT INTO not_found',
})
} catch (e) {
console.log('generated a record in system.error_log', e)
}

try {
console.log('prepare data for system.backup_log')
await fetchData({
query: "BACKUP DATABASE default TO File('/tmp/backup')",
})
console.log('generated a record in system.backup_log')
} catch (e) {
console.log('generated a record in system.backup_log', e)
console.log(`
Although the backup can be failed, it will generate a record in system.backup_log
DB::Exception: Path '/tmp/backup' is not allowed for backups,
see the 'backups.allowed_path' configuration parameter`)
}
})

test.each(namedConfig)(
'check if valid sql for $name config',
async ({ name, config }) => {
expect(config.sql).toBeDefined()
console.log(`Testing config ${name} query:`, config.sql)
console.log('with default params:', config.defaultParams)
console.log('with default params:', config.defaultParams || {})

try {
const { data, metadata } = await fetchData({
query: config.sql,
query_params: config.defaultParams,
query_params: config.defaultParams || {},
format: 'JSONEachRow',
})

console.log('Response:', data)
console.log('Metadata:', metadata)

expect(data).toBeDefined()
expect(metadata).toBeDefined()
} catch (e) {
if (config.optional) {
console.log(
'Query is marked optional, that mean can be failed due to missing table for example'
)
expect(e).toHaveProperty('type', 'UNKNOWN_TABLE')
return
}

console.error(e)
throw e
}
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@
"component": "cypress open --component",
"component:headless": "cypress run --component",
"fmt": "prettier --write \"**/*.{ts,tsx,md,mdx,json}\" --cache",
"test": "jest --coverage",
"jest": "jest --coverage"
"test": "jest --coverage --testPathIgnorePatterns=/query-config/",
"jest": "jest --coverage --testPathIgnorePatterns=/query-config/",
"test-queries-config": "jest --coverage --testPathPattern=query-config"
},
"dependencies": {
"@clickhouse/client": "^0.3.0",
Expand Down
7 changes: 7 additions & 0 deletions types/query-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,13 @@ export interface QueryConfig {
* The documents or url to be used when query is errors. e.g. log table missing due to cluster configuration.
*/
docs?: string
/**
* Whether the query is optional or not. If the query is optional, it can be raised as a error due to missing table or view.
* e.g. system.error_log (when there is no error), system.zookeeper (when zookeeper is not configured), system.backup_log (when there is no backup).
*
* Default: false
*/
optional?: boolean
}

export type QueryConfigNoName = PartialBy<QueryConfig, 'name'>

0 comments on commit ba8d6c7

Please sign in to comment.