forked from elastic/kibana
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[scout] adding unit tests (elastic#204567)
## Summary Adding tests and making adjustments/fixes based on the findings. Note: no integration tests were added to verify servers start as it is mostly equal to `@kbn-test` functionality that has jest integration tests. We can add it later, when Scout has specific logic. How to run: `node scripts/jest --config packages/kbn-scout/jest.config.js` Scope: ``` PASS packages/kbn-scout/src/config/config.test.ts PASS packages/kbn-scout/src/config/loader/read_config_file.test.ts PASS packages/kbn-scout/src/config/utils/get_config_file.test.ts PASS packages/kbn-scout/src/config/utils/load_servers_config.test.ts PASS packages/kbn-scout/src/config/utils/save_scout_test_config.test.ts PASS packages/kbn-scout/src/playwright/config/create_config.test.ts PASS packages/kbn-scout/src/playwright/runner/config_validator.test.ts PASS packages/kbn-scout/src/playwright/runner/flags.test.ts PASS packages/kbn-scout/src/playwright/utils/runner_utils.test.ts PASS packages/kbn-scout/src/servers/flags.test.ts ```
- Loading branch information
1 parent
87d15ba
commit 2ba3247
Showing
43 changed files
with
1,234 additions
and
227 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the "Elastic License | ||
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side | ||
* Public License v 1"; you may not use this file except in compliance with, at | ||
* your election, the "Elastic License 2.0", the "GNU Affero General Public | ||
* License v3.0 only", or the "Server Side Public License, v 1". | ||
*/ | ||
|
||
import { Config } from './config'; | ||
|
||
describe('Config.getScoutTestConfig', () => { | ||
it(`should return a properly structured 'ScoutTestConfig' object for 'stateful'`, async () => { | ||
const config = new Config({ | ||
servers: { | ||
elasticsearch: { | ||
protocol: 'http', | ||
hostname: 'localhost', | ||
port: 9220, | ||
username: 'kibana_system', | ||
password: 'changeme', | ||
}, | ||
kibana: { | ||
protocol: 'http', | ||
hostname: 'localhost', | ||
port: 5620, | ||
username: 'elastic', | ||
password: 'changeme', | ||
}, | ||
}, | ||
dockerServers: {}, | ||
esTestCluster: { | ||
from: 'snapshot', | ||
files: [], | ||
serverArgs: [], | ||
ssl: false, | ||
}, | ||
kbnTestServer: { | ||
buildArgs: [], | ||
env: {}, | ||
sourceArgs: [], | ||
serverArgs: [], | ||
}, | ||
}); | ||
|
||
const scoutConfig = config.getScoutTestConfig(); | ||
|
||
const expectedConfig = { | ||
serverless: false, | ||
projectType: undefined, | ||
isCloud: false, | ||
license: 'trial', | ||
cloudUsersFilePath: expect.stringContaining('.ftr/role_users.json'), | ||
hosts: { | ||
kibana: 'http://localhost:5620', | ||
elasticsearch: 'http://localhost:9220', | ||
}, | ||
auth: { | ||
username: 'elastic', | ||
password: 'changeme', | ||
}, | ||
metadata: { | ||
generatedOn: expect.any(String), | ||
config: expect.any(Object), | ||
}, | ||
}; | ||
|
||
expect(scoutConfig).toEqual(expectedConfig); | ||
}); | ||
|
||
it(`should return a properly structured 'ScoutTestConfig' object for 'serverless=es'`, async () => { | ||
const config = new Config({ | ||
serverless: true, | ||
servers: { | ||
elasticsearch: { | ||
protocol: 'https', | ||
hostname: 'localhost', | ||
port: 9220, | ||
username: 'elastic_serverless', | ||
password: 'changeme', | ||
}, | ||
kibana: { | ||
protocol: 'http', | ||
hostname: 'localhost', | ||
port: 5620, | ||
username: 'elastic_serverless', | ||
password: 'changeme', | ||
}, | ||
}, | ||
dockerServers: {}, | ||
esTestCluster: { | ||
from: 'serverless', | ||
files: [], | ||
serverArgs: [], | ||
ssl: true, | ||
}, | ||
kbnTestServer: { | ||
buildArgs: [], | ||
env: {}, | ||
sourceArgs: [], | ||
serverArgs: ['--serverless=es'], | ||
}, | ||
}); | ||
|
||
const scoutConfig = config.getScoutTestConfig(); | ||
const expectedConfig = { | ||
serverless: true, | ||
projectType: 'es', | ||
isCloud: false, | ||
license: 'trial', | ||
cloudUsersFilePath: expect.stringContaining('.ftr/role_users.json'), | ||
hosts: { | ||
kibana: 'http://localhost:5620', | ||
elasticsearch: 'https://localhost:9220', | ||
}, | ||
auth: { | ||
username: 'elastic_serverless', | ||
password: 'changeme', | ||
}, | ||
metadata: { | ||
generatedOn: expect.any(String), | ||
config: expect.any(Object), | ||
}, | ||
}; | ||
|
||
expect(scoutConfig).toEqual(expectedConfig); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the "Elastic License | ||
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side | ||
* Public License v 1"; you may not use this file except in compliance with, at | ||
* your election, the "Elastic License 2.0", the "GNU Affero General Public | ||
* License v3.0 only", or the "Server Side Public License, v 1". | ||
*/ | ||
|
||
export { readConfigFile } from './read_config_file'; |
83 changes: 83 additions & 0 deletions
83
packages/kbn-scout/src/config/loader/read_config_file.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the "Elastic License | ||
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side | ||
* Public License v 1"; you may not use this file except in compliance with, at | ||
* your election, the "Elastic License 2.0", the "GNU Affero General Public | ||
* License v3.0 only", or the "Server Side Public License, v 1". | ||
*/ | ||
|
||
import path from 'path'; | ||
import { Config } from '../config'; | ||
import { readConfigFile } from './read_config_file'; | ||
|
||
jest.mock('path', () => ({ | ||
resolve: jest.fn(), | ||
})); | ||
|
||
jest.mock('../config', () => ({ | ||
Config: jest.fn(), | ||
})); | ||
|
||
describe('readConfigFile', () => { | ||
const configPath = '/mock/config/path'; | ||
const resolvedPath = '/resolved/config/path'; | ||
const mockPathResolve = path.resolve as jest.Mock; | ||
const mockConfigConstructor = Config as jest.Mock; | ||
|
||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
jest.resetModules(); | ||
}); | ||
|
||
it(`should load and return a valid 'Config' instance when the config file exports 'servers'`, async () => { | ||
const mockConfigModule = { servers: { host: 'localhost', port: 5601 } }; | ||
|
||
mockPathResolve.mockReturnValueOnce(resolvedPath); | ||
|
||
jest.isolateModules(async () => { | ||
jest.mock(resolvedPath, () => mockConfigModule, { virtual: true }); | ||
mockConfigConstructor.mockImplementation((servers) => ({ servers })); | ||
|
||
const result = await readConfigFile(configPath); | ||
|
||
expect(path.resolve).toHaveBeenCalledWith(configPath); | ||
expect(result).toEqual({ servers: mockConfigModule.servers }); | ||
}); | ||
}); | ||
|
||
it(`should throw an error if the config file does not export 'servers'`, async () => { | ||
const mockConfigModule = { otherProperty: 'value' }; | ||
|
||
mockPathResolve.mockReturnValueOnce(resolvedPath); | ||
|
||
jest.isolateModules(async () => { | ||
jest.mock(resolvedPath, () => mockConfigModule, { virtual: true }); | ||
|
||
await expect(readConfigFile(configPath)).rejects.toThrow( | ||
`No 'servers' found in the config file at path: ${resolvedPath}` | ||
); | ||
expect(path.resolve).toHaveBeenCalledWith(configPath); | ||
}); | ||
}); | ||
|
||
it('should throw an error if the config file cannot be loaded', async () => { | ||
mockPathResolve.mockReturnValueOnce(resolvedPath); | ||
|
||
jest.isolateModules(async () => { | ||
const message = 'Module not found'; | ||
jest.mock( | ||
resolvedPath, | ||
() => { | ||
throw new Error(message); | ||
}, | ||
{ virtual: true } | ||
); | ||
|
||
await expect(readConfigFile(configPath)).rejects.toThrow( | ||
`Failed to load config from ${configPath}: ${message}` | ||
); | ||
expect(path.resolve).toHaveBeenCalledWith(configPath); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.