Skip to content

Commit

Permalink
[7.5] use a custom wrapper around chance with longer defaults… (#49867)
Browse files Browse the repository at this point in the history
* use a custom wrapper around chance with longer defaults to avoid conflicts

* fix a reference to chance

* fix another reference to chance service

# Conflicts:
#	x-pack/test/saml_api_integration/apis/security/saml_login.ts
  • Loading branch information
Spencer authored Oct 31, 2019
1 parent 37b4dd2 commit d093e9a
Show file tree
Hide file tree
Showing 16 changed files with 147 additions and 93 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
export default function ({ getService }) {
const esArchiver = getService('esArchiver');
const supertest = getService('supertest');
const chance = getService('chance');
const randomness = getService('randomness');

describe('params', () => {
before(() => esArchiver.load('index_patterns/basic_index'));
Expand Down Expand Up @@ -63,8 +63,8 @@ export default function ({ getService }) {
supertest
.get('/api/index_patterns/_fields_for_wildcard')
.query({
pattern: chance.word(),
[chance.word()]: chance.word(),
pattern: randomness.word(),
[randomness.word()]: randomness.word(),
})
.expect(400));
});
Expand Down
29 changes: 0 additions & 29 deletions test/api_integration/services/chance.js

This file was deleted.

5 changes: 1 addition & 4 deletions test/api_integration/services/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,11 @@ import { services as commonServices } from '../../common/services';
// @ts-ignore not TS yet
import { KibanaSupertestProvider, ElasticsearchSupertestProvider } from './supertest';

// @ts-ignore not TS yet
import { ChanceProvider } from './chance';

export const services = {
es: commonServices.es,
esArchiver: commonServices.esArchiver,
retry: commonServices.retry,
supertest: KibanaSupertestProvider,
esSupertest: ElasticsearchSupertestProvider,
chance: ChanceProvider,
randomness: commonServices.randomness,
};
2 changes: 2 additions & 0 deletions test/common/services/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@ import { LegacyEsProvider } from './legacy_es';
import { EsArchiverProvider } from './es_archiver';
import { KibanaServerProvider } from './kibana_server';
import { RetryProvider } from './retry';
import { RandomnessProvider } from './randomness';

export const services = {
es: LegacyEsProvider,
esArchiver: EsArchiverProvider,
kibanaServer: KibanaServerProvider,
retry: RetryProvider,
randomness: RandomnessProvider,
};
89 changes: 89 additions & 0 deletions test/common/services/randomness.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import Chance from 'chance';

import { FtrProviderContext } from '../ftr_provider_context';

interface CharOptions {
pool?: string;
alpha?: boolean;
numeric?: boolean;
symbols?: boolean;
casing?: 'lower' | 'upper';
}

interface StringOptions extends CharOptions {
length?: number;
}

interface NumberOptions {
min?: number;
max?: number;
}

export function RandomnessProvider({ getService }: FtrProviderContext) {
const log = getService('log');

const seed = Date.now();
log.debug('randomness seed: %j', seed);

const chance = new Chance(seed);

return new (class RandomnessService {
/**
* Generate a random natural number
*
* range: 0 to 9007199254740991
*
*/
naturalNumber(options?: NumberOptions) {
return chance.natural(options);
}

/**
* Generate a random integer
*/
integer(options?: NumberOptions) {
return chance.integer(options);
}

/**
* Generate a random number, defaults to at least 4 and no more than 8 syllables
*/
word(options: { syllables?: number } = {}) {
const { syllables = this.naturalNumber({ min: 4, max: 8 }) } = options;

return chance.word({
syllables,
});
}

/**
* Generate a random string, defaults to at least 8 and no more than 15 alpha-numeric characters
*/
string(options: StringOptions = {}) {
return chance.string({
length: this.naturalNumber({ min: 8, max: 15 }),
...(options.pool === 'undefined' ? { alpha: true, numeric: true, symbols: false } : {}),
...options,
});
}
})();
}
13 changes: 6 additions & 7 deletions x-pack/test/api_integration/apis/beats/assign_tags_to_beats.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export default function ({ getService }) {
const supertest = getService('supertest');
const esArchiver = getService('esArchiver');
const es = getService('es');
const chance = getService('chance');
const randomness = getService('randomness');

describe('assign_tags_to_beats', () => {
const archive = 'beats/list';
Expand Down Expand Up @@ -179,9 +179,8 @@ export default function ({ getService }) {
expect(beat.tags).to.eql(['production']);
});

// FLAKY: https://github.com/elastic/kibana/issues/34038
it.skip('should return errors for non-existent beats', async () => {
const nonExistentBeatId = chance.word();
it('should return errors for non-existent beats', async () => {
const nonExistentBeatId = randomness.word();

const { body: apiResponse } = await supertest
.post('/api/beats/agents_tags/assignments')
Expand All @@ -197,7 +196,7 @@ export default function ({ getService }) {
});

it('should return errors for non-existent tags', async () => {
const nonExistentTag = chance.word();
const nonExistentTag = randomness.word();

const { body: apiResponse } = await supertest
.post('/api/beats/agents_tags/assignments')
Expand All @@ -221,8 +220,8 @@ export default function ({ getService }) {
});

it('should return errors for non-existent beats and tags', async () => {
const nonExistentBeatId = chance.word();
const nonExistentTag = chance.word();
const nonExistentBeatId = randomness.word();
const nonExistentTag = randomness.word();

const { body: apiResponse } = await supertest
.post('/api/beats/agents_tags/assignments')
Expand Down
16 changes: 8 additions & 8 deletions x-pack/test/api_integration/apis/beats/enroll_beat.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { ES_INDEX_NAME } from './constants';

export default function ({ getService }) {
const supertest = getService('supertest');
const chance = getService('chance');
const randomness = getService('randomness');
const es = getService('es');

describe('enroll_beat', () => {
Expand All @@ -20,20 +20,20 @@ export default function ({ getService }) {
let beat;

beforeEach(async () => {
validEnrollmentToken = chance.word();
validEnrollmentToken = randomness.word();

beatId = chance.word();
beatId = randomness.word();
const version =
chance.integer({ min: 1, max: 10 }) +
randomness.integer({ min: 1, max: 10 }) +
'.' +
chance.integer({ min: 1, max: 10 }) +
randomness.integer({ min: 1, max: 10 }) +
'.' +
chance.integer({ min: 1, max: 10 });
randomness.integer({ min: 1, max: 10 });

beat = {
type: 'filebeat',
host_name: 'foo.bar.com',
name: chance.word(),
name: randomness.word(),
version,
};

Expand Down Expand Up @@ -94,7 +94,7 @@ export default function ({ getService }) {
const { body: apiResponse } = await supertest
.post(`/api/beats/agent/${beatId}`)
.set('kbn-xsrf', 'xxx')
.set('kbn-beats-enrollment-token', chance.word())
.set('kbn-beats-enrollment-token', randomness.word())
.send(beat)
.expect(400);

Expand Down
10 changes: 5 additions & 5 deletions x-pack/test/api_integration/apis/beats/remove_tags_from_beats.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export default function ({ getService }) {
const supertest = getService('supertest');
const esArchiver = getService('esArchiver');
const es = getService('es');
const chance = getService('chance');
const randomness = getService('randomness');

describe('remove_tags_from_beats', () => {
const archive = 'beats/list';
Expand Down Expand Up @@ -135,7 +135,7 @@ export default function ({ getService }) {
});

it('should return errors for non-existent beats', async () => {
const nonExistentBeatId = chance.word();
const nonExistentBeatId = randomness.word();

const { body: apiResponse } = await supertest
.post('/api/beats/agents_tags/removals')
Expand All @@ -151,7 +151,7 @@ export default function ({ getService }) {
});

it('should return errors for non-existent tags', async () => {
const nonExistentTag = chance.word();
const nonExistentTag = randomness.word();

const { body: apiResponse } = await supertest
.post('/api/beats/agents_tags/removals')
Expand All @@ -175,8 +175,8 @@ export default function ({ getService }) {
});

it('should return errors for non-existent beats and tags', async () => {
const nonExistentBeatId = chance.word();
const nonExistentTag = chance.word();
const nonExistentBeatId = randomness.word();
const nonExistentTag = randomness.word();

const { body: apiResponse } = await supertest
.post('/api/beats/agents_tags/removals')
Expand Down
1 change: 0 additions & 1 deletion x-pack/test/api_integration/apis/beats/set_config.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import { ES_INDEX_NAME } from './constants';

export default function ({ getService }) {
const supertest = getService('supertest');
// const chance = getService('chance');
const es = getService('es');
const esArchiver = getService('esArchiver');

Expand Down
20 changes: 10 additions & 10 deletions x-pack/test/api_integration/apis/beats/update_beat.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import moment from 'moment';

export default function ({ getService }) {
const supertest = getService('supertest');
const chance = getService('chance');
const randomness = getService('randomness');
const es = getService('es');
const esArchiver = getService('esArchiver');

Expand All @@ -27,18 +27,18 @@ export default function ({ getService }) {
'SSsX2Byyo1B1bGxV8C3G4QldhE5iH87EY_1r21-bwbI';

const version =
chance.integer({ min: 1, max: 10 }) +
randomness.integer({ min: 1, max: 10 }) +
'.' +
chance.integer({ min: 1, max: 10 }) +
randomness.integer({ min: 1, max: 10 }) +
'.' +
chance.integer({ min: 1, max: 10 });
randomness.integer({ min: 1, max: 10 });

beat = {
type: `${chance.word()}beat`,
host_name: `www.${chance.word()}.net`,
name: chance.word(),
type: `${randomness.word()}beat`,
host_name: `www.${randomness.word()}.net`,
name: randomness.word(),
version,
ephemeral_id: chance.word(),
ephemeral_id: randomness.word(),
};

await es.index({
Expand Down Expand Up @@ -90,7 +90,7 @@ export default function ({ getService }) {
const { body } = await supertest
.put(`/api/beats/agent/${beatId}`)
.set('kbn-xsrf', 'xxx')
.set('kbn-beats-access-token', chance.word())
.set('kbn-beats-access-token', randomness.word())
.send(beat)
.expect(401);

Expand All @@ -109,7 +109,7 @@ export default function ({ getService }) {
});

it('should return an error for a non-existent beat', async () => {
const beatId = chance.word();
const beatId = randomness.word();
const { body } = await supertest
.put(`/api/beats/agent/${beatId}`)
.set('kbn-xsrf', 'xxx')
Expand Down
7 changes: 2 additions & 5 deletions x-pack/test/api_integration/services/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,11 @@ import { SiemGraphQLClientProvider, SiemGraphQLClientFactoryProvider } from './s
import { InfraOpsSourceConfigurationProvider } from './infraops_source_configuration';

export const services = {
chance: kibanaApiIntegrationServices.chance,
...kibanaCommonServices,

esSupertest: kibanaApiIntegrationServices.esSupertest,
supertest: kibanaApiIntegrationServices.supertest,

esArchiver: kibanaCommonServices.esArchiver,
kibanaServer: kibanaCommonServices.kibanaServer,
retry: kibanaCommonServices.retry,

es: LegacyEsProvider,
esSupertestWithoutAuth: EsSupertestWithoutAuthProvider,
infraOpsGraphQLClient: InfraOpsGraphQLClientProvider,
Expand Down
2 changes: 1 addition & 1 deletion x-pack/test/functional/services/random.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export function RandomProvider({ getService }) {
log.debug('randomness seed: %j', seed);
const chance = new Chance(seed);

return new class Random {
return new class Randomness {
int(min = 3, max = 15) {
return chance.integer({ min, max });
}
Expand Down
Loading

0 comments on commit d093e9a

Please sign in to comment.