-
Notifications
You must be signed in to change notification settings - Fork 8.3k
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
[scout] adding unit tests #204567
[scout] adding unit tests #204567
Changes from 4 commits
689acbb
d12a830
72cb4e8
7d58ec4
449be88
4b15eff
73a73e0
91f53ff
392b139
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 |
---|---|---|
@@ -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); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,15 +13,15 @@ import Path from 'path'; | |
import { cloneDeepWith, get, has, toPath } from 'lodash'; | ||
import { REPO_ROOT } from '@kbn/repo-info'; | ||
import { schema } from './schema'; | ||
import { ScoutServerConfig } from '../types'; | ||
import { formatCurrentDate, getProjectType } from './utils'; | ||
import { ScoutServerConfig, ScoutTestConfig } from '../types'; | ||
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.
|
||
import { formatCurrentDate, getProjectType } from './utils/utils'; | ||
|
||
const $values = Symbol('values'); | ||
|
||
export class Config { | ||
private [$values]: Record<string, any>; | ||
private [$values]: ScoutServerConfig; | ||
|
||
constructor(data: Record<string, any>) { | ||
constructor(data: ScoutServerConfig) { | ||
const { error, value } = schema.validate(data, { | ||
abortEarly: false, | ||
}); | ||
|
@@ -104,13 +104,14 @@ export class Config { | |
}); | ||
} | ||
|
||
public getTestServersConfig(): ScoutServerConfig { | ||
public getScoutTestConfig(): ScoutTestConfig { | ||
return { | ||
serverless: this.get('serverless'), | ||
projectType: this.get('serverless') | ||
? getProjectType(this.get('kbnTestServer.serverArgs')) | ||
: undefined, | ||
isCloud: false, | ||
license: this.get('esTestCluster.license'), | ||
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. might be useful for some tests |
||
cloudUsersFilePath: Path.resolve(REPO_ROOT, '.ftr', 'role_users.json'), | ||
hosts: { | ||
kibana: Url.format({ | ||
|
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'; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
/* | ||
* 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 { ToolingLog } from '@kbn/tooling-log'; | ||
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; | ||
let mockLog: jest.Mocked<ToolingLog>; | ||
|
||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
jest.resetModules(); | ||
mockLog = new ToolingLog() as jest.Mocked<ToolingLog>; | ||
}); | ||
|
||
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); | ||
}); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -75,7 +75,7 @@ export const schema = Joi.object() | |
|
||
esTestCluster: Joi.object() | ||
.keys({ | ||
license: Joi.valid('basic', 'trial', 'gold').default('basic'), | ||
license: Joi.valid('basic', 'trial', 'gold').default('trial'), | ||
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. default |
||
from: Joi.string().default('snapshot'), | ||
serverArgs: Joi.array().items(Joi.string()).default([]), | ||
esJavaOpts: Joi.string(), | ||
|
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.
Renamed to differentiate servers configuration and Scout test configuration, that used by Playwright fixtures/tests.