-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[FAI-13685] Write config/catalog files #97
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,4 +12,4 @@ target/ | |
out/pkg | ||
sample_command.sh | ||
*airbyte-local | ||
test/exec/resources/ | ||
test/exec/resources |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,10 @@ import {sep} from 'node:path'; | |
import pino from 'pino'; | ||
import pretty from 'pino-pretty'; | ||
|
||
import {AirbyteCliContext, AirbyteConfig} from './types'; | ||
import {AirbyteCliContext, AirbyteConfig, FarosConfig} from './types'; | ||
|
||
// constants | ||
export const FILENAME_PREFIX = 'faros_airbyte_cli'; | ||
|
||
// Create a pino logger instance | ||
export const logger = pino(pretty({colorize: true})); | ||
|
@@ -106,3 +109,51 @@ export function cleanUp(context: AirbyteCliContext): void { | |
} | ||
logger.info('Clean up completed.'); | ||
} | ||
|
||
// Write Airbyte config and catalog to temporary dir and a json file | ||
export function writeConfig(tmpDir: string, config: FarosConfig): void { | ||
const airbyteConfig = { | ||
src: config.src ?? ({} as AirbyteConfig), | ||
dst: config.dst ?? ({} as AirbyteConfig), | ||
}; | ||
|
||
// write Airbyte config for user's reference | ||
// TODO: @FAI-14122 React secrets | ||
logger.debug(`Writing Airbyte config for user reference...`); | ||
writeFileSync(`${FILENAME_PREFIX}_config.json`, JSON.stringify(airbyteConfig, null, 2)); | ||
logger.debug(airbyteConfig, `Airbyte config: `); | ||
logger.debug(`Airbyte config written to: ${FILENAME_PREFIX}_config.json`); | ||
|
||
// add config `feed_cfg.debug` if debug is enabled | ||
const regex = /^farosai\/airbyte-faros-feeds-source.*/; | ||
if (config.debug && regex.exec(airbyteConfig.src.image ?? '')) { | ||
airbyteConfig.src.config = { | ||
...airbyteConfig.src.config, | ||
feed_cfg: {debug: true}, | ||
}; | ||
} | ||
|
||
// write config to temporary directory config files | ||
logger.debug(`Writing Airbyte config to files...`); | ||
const srcConfigFilePath = `${tmpDir}${sep}${FILENAME_PREFIX}_src_config.json`; | ||
const dstConfigFilePath = `${tmpDir}${sep}${FILENAME_PREFIX}_dst_config.json`; | ||
writeFileSync(srcConfigFilePath, JSON.stringify(airbyteConfig.src.config ?? {}, null, 2)); | ||
writeFileSync(dstConfigFilePath, JSON.stringify(airbyteConfig.dst.config ?? {}, null, 2)); | ||
logger.debug(`Airbyte config files written to: ${srcConfigFilePath}, ${dstConfigFilePath}`); | ||
|
||
// write catalog to temporary directory catalog files | ||
// TODO: @FAI-14134 Discover catalog | ||
logger.debug(`Writing Airbyte catalog to files...`); | ||
const srcCatalogFilePath = `${tmpDir}${sep}${FILENAME_PREFIX}_src_catalog.json`; | ||
const dstCatalogFilePath = `${tmpDir}${sep}${FILENAME_PREFIX}_dst_catalog.json`; | ||
if ( | ||
(!airbyteConfig.dst.catalog || Object.keys(airbyteConfig.dst.catalog).length === 0) && | ||
airbyteConfig.src.catalog && | ||
Object.keys(airbyteConfig.src.catalog).length > 0 | ||
) { | ||
airbyteConfig.dst.catalog = airbyteConfig.src.catalog; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is ok for now, but be prepared for logic generate a destination-compatible catalog based on the src catalog. It has slightly different format. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ohhhh i see. thanks for the heads up |
||
} | ||
writeFileSync(srcCatalogFilePath, JSON.stringify(airbyteConfig.src.catalog ?? {}, null, 2)); | ||
writeFileSync(dstCatalogFilePath, JSON.stringify(airbyteConfig.dst.catalog ?? {}, null, 2)); | ||
logger.debug(`Airbyte catalog files written to: ${srcCatalogFilePath}, ${dstCatalogFilePath}`); | ||
} |
This file was deleted.
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,16 @@ | ||
import {existsSync, mkdtempSync, readFileSync, rmSync} from 'node:fs'; | ||
import {tmpdir} from 'node:os'; | ||
|
||
import {checkDockerInstalled, cleanUp, createTmpDir, loadStateFile, parseConfigFile} from '../src/utils'; | ||
import {FarosConfig} from '../src/types'; | ||
import { | ||
checkDockerInstalled, | ||
cleanUp, | ||
createTmpDir, | ||
FILENAME_PREFIX, | ||
loadStateFile, | ||
parseConfigFile, | ||
writeConfig, | ||
} from '../src/utils'; | ||
|
||
describe('parseConfigFile', () => { | ||
it('should pass', () => { | ||
|
@@ -35,31 +45,127 @@ describe('createTmpDir', () => { | |
}); | ||
}); | ||
|
||
describe('loadStateFile', () => { | ||
describe('write files to temporary dir', () => { | ||
let tmpDirPath: string; | ||
beforeAll(() => { | ||
tmpDirPath = mkdtempSync('test-temp-dir'); | ||
tmpDirPath = mkdtempSync(`${tmpdir()}/test-temp-dir`); | ||
}); | ||
afterAll(() => { | ||
rmSync(tmpDirPath, {recursive: true, force: true}); | ||
}); | ||
|
||
it('should pass without existing state file', () => { | ||
expect(() => loadStateFile(tmpDirPath)).not.toThrow(); | ||
expect(existsSync(`${tmpDirPath}/state.json`)).toBe(true); | ||
expect(readFileSync(`${tmpDirPath}/state.json`, 'utf8')).toBe('{}'); | ||
}); | ||
describe('loadStateFile', () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. didn't change these tests. just wrap it in another describe |
||
it('should pass without existing state file', () => { | ||
expect(() => loadStateFile(tmpDirPath)).not.toThrow(); | ||
expect(existsSync(`${tmpDirPath}/state.json`)).toBe(true); | ||
expect(readFileSync(`${tmpDirPath}/state.json`, 'utf8')).toBe('{}'); | ||
}); | ||
|
||
it('should pass with existing state file', () => { | ||
const testStateFile = 'test/resources/test__state.json'; | ||
expect(() => loadStateFile(tmpDirPath, testStateFile)).not.toThrow(); | ||
expect(existsSync(`${tmpDirPath}/state.json`)).toBe(true); | ||
expect(readFileSync(`${tmpDirPath}/state.json`, 'utf8')).toMatchSnapshot(); | ||
}); | ||
|
||
it('should pass with existing state file', () => { | ||
const testStateFile = 'test/resources/test__state.json'; | ||
expect(() => loadStateFile(tmpDirPath, testStateFile)).not.toThrow(); | ||
expect(existsSync(`${tmpDirPath}/state.json`)).toBe(true); | ||
expect(readFileSync(`${tmpDirPath}/state.json`, 'utf8')).toMatchSnapshot(); | ||
it('should fail if state file is not loaded', () => { | ||
expect(() => loadStateFile(tmpDirPath, 'non-exist-state-file')).toThrow( | ||
`State file 'non-exist-state-file' not found. Please make sure the state file exists and have read access.`, | ||
); | ||
}); | ||
}); | ||
|
||
it('should fail if state file is not loaded', () => { | ||
expect(() => loadStateFile(tmpDirPath, 'non-exist-state-file')).toThrow( | ||
`State file 'non-exist-state-file' not found. Please make sure the state file exists and have read access.`, | ||
); | ||
describe('writeConfig', () => { | ||
const testConfig: FarosConfig = { | ||
src: { | ||
image: 'farosai/airbyte-test-source', | ||
config: { | ||
username: 'test', | ||
password: 'test', | ||
url: 'test', | ||
}, | ||
catalog: { | ||
tests: {disabled: true}, | ||
projects: {disabled: true}, | ||
}, | ||
}, | ||
dst: { | ||
image: 'farosai/airbyte-test-destination', | ||
config: { | ||
edition_config: { | ||
graph: 'default', | ||
edition: 'cloud', | ||
api_url: 'https://test.api.faros.ai', | ||
}, | ||
}, | ||
}, | ||
|
||
// default values | ||
srcCheckConnection: false, | ||
dstUseHostNetwork: false, | ||
srcPull: false, | ||
dstPull: false, | ||
fullRefresh: false, | ||
rawMessages: false, | ||
keepContainers: false, | ||
logLevel: 'info', | ||
debug: false, | ||
stateFile: undefined, | ||
connectionName: undefined, | ||
srcOutputFile: undefined, | ||
srcInputFile: undefined, | ||
}; | ||
|
||
afterEach(() => { | ||
rmSync(`${FILENAME_PREFIX}_config.json`, {force: true}); | ||
rmSync(`${tmpDirPath}/${FILENAME_PREFIX}_src_config.json`, {force: true}); | ||
rmSync(`${tmpDirPath}/${FILENAME_PREFIX}_dst_config.json`, {force: true}); | ||
rmSync(`${tmpDirPath}/${FILENAME_PREFIX}_src_catalog.json`, {force: true}); | ||
rmSync(`${tmpDirPath}/${FILENAME_PREFIX}_dst_catalog.json`, {force: true}); | ||
}); | ||
|
||
it('should write files', () => { | ||
expect(() => writeConfig(tmpDirPath, structuredClone(testConfig))).not.toThrow(); | ||
expect(existsSync(`${FILENAME_PREFIX}_config.json`)).toBe(true); | ||
expect(existsSync(`${tmpDirPath}/${FILENAME_PREFIX}_src_config.json`)).toBe(true); | ||
expect(existsSync(`${tmpDirPath}/${FILENAME_PREFIX}_dst_config.json`)).toBe(true); | ||
expect(existsSync(`${tmpDirPath}/${FILENAME_PREFIX}_src_catalog.json`)).toBe(true); | ||
expect(existsSync(`${tmpDirPath}/${FILENAME_PREFIX}_dst_catalog.json`)).toBe(true); | ||
|
||
expect(readFileSync(`${FILENAME_PREFIX}_config.json`, 'utf8')).toEqual( | ||
JSON.stringify({src: testConfig.src, dst: testConfig.dst}, null, 2), | ||
); | ||
expect(readFileSync(`${tmpDirPath}/${FILENAME_PREFIX}_src_config.json`, 'utf8')).toEqual( | ||
JSON.stringify(testConfig.src?.config, null, 2), | ||
); | ||
expect(readFileSync(`${tmpDirPath}/${FILENAME_PREFIX}_dst_config.json`, 'utf8')).toEqual( | ||
JSON.stringify(testConfig.dst?.config, null, 2), | ||
); | ||
expect(readFileSync(`${tmpDirPath}/${FILENAME_PREFIX}_src_catalog.json`, 'utf8')).toEqual( | ||
JSON.stringify(testConfig.src?.catalog, null, 2), | ||
); | ||
expect(readFileSync(`${tmpDirPath}/${FILENAME_PREFIX}_dst_catalog.json`, 'utf8')).toEqual( | ||
JSON.stringify(testConfig.src?.catalog, null, 2), | ||
); | ||
}); | ||
|
||
it('should alter config if debug is enabled', () => { | ||
const testConfigDebug = {...structuredClone(testConfig), debug: true}; | ||
(testConfigDebug.src as any).image = 'farosai/airbyte-faros-feeds-source:v1'; | ||
expect(() => writeConfig(tmpDirPath, structuredClone(testConfigDebug))).not.toThrow(); | ||
expect(existsSync(`${FILENAME_PREFIX}_config.json`)).toBe(true); | ||
expect(existsSync(`${tmpDirPath}/${FILENAME_PREFIX}_src_config.json`)).toBe(true); | ||
expect(existsSync(`${tmpDirPath}/${FILENAME_PREFIX}_dst_config.json`)).toBe(true); | ||
|
||
expect(readFileSync(`${FILENAME_PREFIX}_config.json`, 'utf8')).toEqual( | ||
JSON.stringify({src: testConfigDebug.src, dst: testConfigDebug.dst}, null, 2), | ||
); | ||
expect(readFileSync(`${tmpDirPath}/${FILENAME_PREFIX}_src_config.json`, 'utf8')).toEqual( | ||
JSON.stringify({...testConfigDebug.src?.config, feed_cfg: {debug: true}}, null, 2), | ||
); | ||
expect(readFileSync(`${tmpDirPath}/${FILENAME_PREFIX}_dst_config.json`, 'utf8')).toEqual( | ||
JSON.stringify(testConfigDebug.dst?.config, null, 2), | ||
); | ||
}); | ||
}); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
discover catalog will be covered in another ticket as we need to spin up docker