[es/ensureTypesExist] ensure that random types are unique (#25601)

Fixes https://github.com/elastic/kibana/issues/22404

When `chance.word()` returns the same word twice in a set it would cause the failure seen in this issue, ensuring that unique words are always picked should prevent it.
This commit is contained in:
Spencer 2018-11-13 13:00:32 -08:00 committed by GitHub
parent 8ff8715fee
commit 30a7cefd39
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -7,10 +7,22 @@ import { ensureTypesExist } from '../ensure_types_exist';
const chance = new Chance();
const previousWords = new Set();
const uniqueWord = () => {
const word = chance.word();
if (previousWords.has(word)) {
return uniqueWord();
}
previousWords.add(word);
return word;
};
function createRandomTypes(n = chance.integer({ min: 10, max: 20 })) {
return chance.n(
() => ({
name: chance.word(),
name: uniqueWord(),
mapping: {
type: chance.pickone(['keyword', 'text', 'integer', 'boolean'])
}