-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
# Backport This will backport the following commits from `main` to `8.x`: - [[scout] adding unit tests (#204567)](#204567) <!--- Backport version: 9.4.3 --> ### Questions ? Please refer to the [Backport tool documentation](https://github.com/sqren/backport) <!--BACKPORT [{"author":{"name":"Dzmitry Lemechko","email":"[email protected]"},"sourceCommit":{"committedDate":"2024-12-19T16:36:07Z","message":"[scout] adding unit tests (#204567)\n\n## Summary\r\n\r\nAdding tests and making adjustments/fixes based on the findings.\r\n\r\nNote: no integration tests were added to verify servers start as it is\r\nmostly equal to `@kbn-test` functionality that has jest integration\r\ntests. We can add it later, when Scout has specific logic.\r\n\r\nHow to run: `node scripts/jest --config\r\npackages/kbn-scout/jest.config.js`\r\n\r\nScope:\r\n\r\n```\r\n PASS packages/kbn-scout/src/config/config.test.ts\r\n PASS packages/kbn-scout/src/config/loader/read_config_file.test.ts\r\n PASS packages/kbn-scout/src/config/utils/get_config_file.test.ts\r\n PASS packages/kbn-scout/src/config/utils/load_servers_config.test.ts\r\n PASS packages/kbn-scout/src/config/utils/save_scout_test_config.test.ts\r\n PASS packages/kbn-scout/src/playwright/config/create_config.test.ts\r\n PASS packages/kbn-scout/src/playwright/runner/config_validator.test.ts\r\n PASS packages/kbn-scout/src/playwright/runner/flags.test.ts\r\n PASS packages/kbn-scout/src/playwright/utils/runner_utils.test.ts\r\n PASS packages/kbn-scout/src/servers/flags.test.ts\r\n```","sha":"2ba3247526331ec152f2d1e236c40635e86b3ce3","branchLabelMapping":{"^v9.0.0$":"main","^v8.18.0$":"8.x","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["release_note:skip","v9.0.0","backport:prev-minor","test:scout"],"title":"[scout] adding unit tests","number":204567,"url":"https://github.com/elastic/kibana/pull/204567","mergeCommit":{"message":"[scout] adding unit tests (#204567)\n\n## Summary\r\n\r\nAdding tests and making adjustments/fixes based on the findings.\r\n\r\nNote: no integration tests were added to verify servers start as it is\r\nmostly equal to `@kbn-test` functionality that has jest integration\r\ntests. We can add it later, when Scout has specific logic.\r\n\r\nHow to run: `node scripts/jest --config\r\npackages/kbn-scout/jest.config.js`\r\n\r\nScope:\r\n\r\n```\r\n PASS packages/kbn-scout/src/config/config.test.ts\r\n PASS packages/kbn-scout/src/config/loader/read_config_file.test.ts\r\n PASS packages/kbn-scout/src/config/utils/get_config_file.test.ts\r\n PASS packages/kbn-scout/src/config/utils/load_servers_config.test.ts\r\n PASS packages/kbn-scout/src/config/utils/save_scout_test_config.test.ts\r\n PASS packages/kbn-scout/src/playwright/config/create_config.test.ts\r\n PASS packages/kbn-scout/src/playwright/runner/config_validator.test.ts\r\n PASS packages/kbn-scout/src/playwright/runner/flags.test.ts\r\n PASS packages/kbn-scout/src/playwright/utils/runner_utils.test.ts\r\n PASS packages/kbn-scout/src/servers/flags.test.ts\r\n```","sha":"2ba3247526331ec152f2d1e236c40635e86b3ce3"}},"sourceBranch":"main","suggestedTargetBranches":[],"targetPullRequestStates":[{"branch":"main","label":"v9.0.0","branchLabelMappingKey":"^v9.0.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/204567","number":204567,"mergeCommit":{"message":"[scout] adding unit tests (#204567)\n\n## Summary\r\n\r\nAdding tests and making adjustments/fixes based on the findings.\r\n\r\nNote: no integration tests were added to verify servers start as it is\r\nmostly equal to `@kbn-test` functionality that has jest integration\r\ntests. We can add it later, when Scout has specific logic.\r\n\r\nHow to run: `node scripts/jest --config\r\npackages/kbn-scout/jest.config.js`\r\n\r\nScope:\r\n\r\n```\r\n PASS packages/kbn-scout/src/config/config.test.ts\r\n PASS packages/kbn-scout/src/config/loader/read_config_file.test.ts\r\n PASS packages/kbn-scout/src/config/utils/get_config_file.test.ts\r\n PASS packages/kbn-scout/src/config/utils/load_servers_config.test.ts\r\n PASS packages/kbn-scout/src/config/utils/save_scout_test_config.test.ts\r\n PASS packages/kbn-scout/src/playwright/config/create_config.test.ts\r\n PASS packages/kbn-scout/src/playwright/runner/config_validator.test.ts\r\n PASS packages/kbn-scout/src/playwright/runner/flags.test.ts\r\n PASS packages/kbn-scout/src/playwright/utils/runner_utils.test.ts\r\n PASS packages/kbn-scout/src/servers/flags.test.ts\r\n```","sha":"2ba3247526331ec152f2d1e236c40635e86b3ce3"}}]}] BACKPORT--> Co-authored-by: Dzmitry Lemechko <[email protected]>
- Loading branch information
1 parent
d3a325a
commit 2f17390
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.