Skip to content

Commit

Permalink
Add interpreter functional tests on the server.
Browse files Browse the repository at this point in the history
  • Loading branch information
lukeelmers committed Dec 9, 2020
1 parent 1e086ee commit 18f62e0
Show file tree
Hide file tree
Showing 5 changed files with 167 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"kibanaUtils",
"expressions"
],
"server": false,
"server": true,
"ui": true,
"requiredBundles": [
"inspector"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* 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 { PluginInitializer } from '../../../../../src/core/server';
import { TestPlugin, TestPluginSetup, TestPluginStart } from './plugin';

export const plugin: PluginInitializer<TestPluginSetup, TestPluginStart> = () => new TestPlugin();
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* 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 { schema } from '@kbn/config-schema';
import { CoreSetup, Plugin, HttpResponsePayload } from '../../../../../src/core/server';
import { PluginStart as DataPluginStart } from '../../../../../src/plugins/data/server';
import { ExpressionsServerStart } from '../../../../../src/plugins/expressions/server';

export interface TestStartDeps {
data: DataPluginStart;
expressions: ExpressionsServerStart;
}

export class TestPlugin implements Plugin<TestPluginSetup, TestPluginStart, {}, TestStartDeps> {
public setup(core: CoreSetup<TestStartDeps>) {
const router = core.http.createRouter();

router.post(
{
path: '/api/interpreter_functional/run_expression',
validate: {
body: schema.object({
input: schema.maybe(schema.nullable(schema.object({}, { unknowns: 'allow' }))),
expression: schema.string(),
}),
},
},
async (context, req, res) => {
const [, { expressions }] = await core.getStartServices();
const output = await expressions.run<unknown, HttpResponsePayload>(
req.body.expression,
req.body.input,
{
kibanaRequest: req,
}
);
return res.ok({ body: output });
}
);
}

public start() {}
public stop() {}
}

export type TestPluginSetup = ReturnType<TestPlugin['setup']>;
export type TestPluginStart = ReturnType<TestPlugin['start']>;
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"index.ts",
"public/**/*.ts",
"public/**/*.tsx",
"server/**/*.ts",
"../../../../typings/**/*",
],
"exclude": [],
Expand Down
80 changes: 79 additions & 1 deletion test/interpreter_functional/test_suites/run_pipeline/esaggs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,27 @@ export default function ({
getService,
updateBaselines,
}: FtrProviderContext & { updateBaselines: boolean }) {
const supertest = getService('supertest');
let expectExpression: ExpectExpression;

const expectClientToMatchServer = async (title: string, expression: string) => {
const clientResult = await expectExpression(title, expression).getResponse();
await supertest
.post('/api/interpreter_functional/run_expression')
.set('kbn-xsrf', 'anything')
.send({ expression, input: undefined })
.expect(200)
.expect(({ body }) => {
expect(body.rows).to.eql(clientResult.rows);
});
};

describe('esaggs pipeline expression tests', () => {
before(() => {
expectExpression = expectExpressionProvider({ getService, updateBaselines });
});

describe('correctly renders tagcloud', () => {
describe('correctly filters based on context', () => {
it('filters on index pattern primary date field by default', async () => {
const timeRange = {
from: '2006-09-21T00:00:00Z',
Expand Down Expand Up @@ -88,5 +102,69 @@ export default function ({
expect(getCell(result, 0, 0)).to.be(7452);
});
});

describe('correctly runs on the server', () => {
it('runs the provided agg on the server', async () => {
const expression = `
esaggs index={indexPatternLoad id='logstash-*'}
aggs={aggAvg id="1" enabled=true schema="metric" field="bytes"}
`;
await supertest
.post('/api/interpreter_functional/run_expression')
.set('kbn-xsrf', 'anything')
.send({ expression, input: undefined })
.expect(200)
.expect(({ body }) => {
expect(body.columns[0].meta.index).to.be('logstash-*');
expect(body.columns[0].meta.source).to.be('esaggs');
expect(body.columns[0].meta.sourceParams.type).to.be('avg');
expect(getCell(body, 0, 0)).to.be(5727.3136246786635);
});
});

it('works with timeRange filters', async () => {
const timeRange = {
from: '2006-09-21T00:00:00Z',
to: '2015-09-22T00:00:00Z',
};
// we need to manually pass timeRange in the input until
// kibana_context is supported on the server
const kibanaContext = {
type: 'kibana_context',
timeRange,
};
const expression = `
esaggs index={indexPatternLoad id='logstash-*'}
aggs={aggCount id="1" enabled=true schema="metric"}
`;
await supertest
.post('/api/interpreter_functional/run_expression')
.set('kbn-xsrf', 'anything')
.send({ expression, input: kibanaContext })
.expect(200)
.expect(({ body }) => {
expect(getCell(body, 0, 0)).to.be(9375);
});
});

it('returns same results on client & server', async () => {
const a = `
esaggs index={indexPatternLoad id='logstash-*'}
aggs={aggCount id="1" enabled=true schema="metric"}
aggs={aggTerms id="2" enabled=true schema="segment" field="response.raw"}
`;
await expectClientToMatchServer('multiple_aggs', a);

// const b = `
// esaggs index={indexPatternLoad id='logstash-*'}
// aggs={aggCount id="1" enabled=true schema="metric"}
// `;

// const c = `
// esaggs index={indexPatternLoad id='logstash-*'}
// aggs={aggCount id="1" enabled=true schema="metric"}
// `;
});
});
});
}

0 comments on commit 18f62e0

Please sign in to comment.