Skip to content

Commit 18f62e0

Browse files
committed
Add interpreter functional tests on the server.
1 parent 1e086ee commit 18f62e0

5 files changed

Lines changed: 167 additions & 2 deletions

File tree

test/interpreter_functional/plugins/kbn_tp_run_pipeline/kibana.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"kibanaUtils",
99
"expressions"
1010
],
11-
"server": false,
11+
"server": true,
1212
"ui": true,
1313
"requiredBundles": [
1414
"inspector"
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
* Licensed to Elasticsearch B.V. under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch B.V. licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
import { PluginInitializer } from '../../../../../src/core/server';
21+
import { TestPlugin, TestPluginSetup, TestPluginStart } from './plugin';
22+
23+
export const plugin: PluginInitializer<TestPluginSetup, TestPluginStart> = () => new TestPlugin();
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* Licensed to Elasticsearch B.V. under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch B.V. licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
import { schema } from '@kbn/config-schema';
21+
import { CoreSetup, Plugin, HttpResponsePayload } from '../../../../../src/core/server';
22+
import { PluginStart as DataPluginStart } from '../../../../../src/plugins/data/server';
23+
import { ExpressionsServerStart } from '../../../../../src/plugins/expressions/server';
24+
25+
export interface TestStartDeps {
26+
data: DataPluginStart;
27+
expressions: ExpressionsServerStart;
28+
}
29+
30+
export class TestPlugin implements Plugin<TestPluginSetup, TestPluginStart, {}, TestStartDeps> {
31+
public setup(core: CoreSetup<TestStartDeps>) {
32+
const router = core.http.createRouter();
33+
34+
router.post(
35+
{
36+
path: '/api/interpreter_functional/run_expression',
37+
validate: {
38+
body: schema.object({
39+
input: schema.maybe(schema.nullable(schema.object({}, { unknowns: 'allow' }))),
40+
expression: schema.string(),
41+
}),
42+
},
43+
},
44+
async (context, req, res) => {
45+
const [, { expressions }] = await core.getStartServices();
46+
const output = await expressions.run<unknown, HttpResponsePayload>(
47+
req.body.expression,
48+
req.body.input,
49+
{
50+
kibanaRequest: req,
51+
}
52+
);
53+
return res.ok({ body: output });
54+
}
55+
);
56+
}
57+
58+
public start() {}
59+
public stop() {}
60+
}
61+
62+
export type TestPluginSetup = ReturnType<TestPlugin['setup']>;
63+
export type TestPluginStart = ReturnType<TestPlugin['start']>;

test/interpreter_functional/plugins/kbn_tp_run_pipeline/tsconfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
"index.ts",
99
"public/**/*.ts",
1010
"public/**/*.tsx",
11+
"server/**/*.ts",
1112
"../../../../typings/**/*",
1213
],
1314
"exclude": [],

test/interpreter_functional/test_suites/run_pipeline/esaggs.ts

Lines changed: 79 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,27 @@ export default function ({
3333
getService,
3434
updateBaselines,
3535
}: FtrProviderContext & { updateBaselines: boolean }) {
36+
const supertest = getService('supertest');
3637
let expectExpression: ExpectExpression;
38+
39+
const expectClientToMatchServer = async (title: string, expression: string) => {
40+
const clientResult = await expectExpression(title, expression).getResponse();
41+
await supertest
42+
.post('/api/interpreter_functional/run_expression')
43+
.set('kbn-xsrf', 'anything')
44+
.send({ expression, input: undefined })
45+
.expect(200)
46+
.expect(({ body }) => {
47+
expect(body.rows).to.eql(clientResult.rows);
48+
});
49+
};
50+
3751
describe('esaggs pipeline expression tests', () => {
3852
before(() => {
3953
expectExpression = expectExpressionProvider({ getService, updateBaselines });
4054
});
4155

42-
describe('correctly renders tagcloud', () => {
56+
describe('correctly filters based on context', () => {
4357
it('filters on index pattern primary date field by default', async () => {
4458
const timeRange = {
4559
from: '2006-09-21T00:00:00Z',
@@ -88,5 +102,69 @@ export default function ({
88102
expect(getCell(result, 0, 0)).to.be(7452);
89103
});
90104
});
105+
106+
describe('correctly runs on the server', () => {
107+
it('runs the provided agg on the server', async () => {
108+
const expression = `
109+
esaggs index={indexPatternLoad id='logstash-*'}
110+
aggs={aggAvg id="1" enabled=true schema="metric" field="bytes"}
111+
`;
112+
await supertest
113+
.post('/api/interpreter_functional/run_expression')
114+
.set('kbn-xsrf', 'anything')
115+
.send({ expression, input: undefined })
116+
.expect(200)
117+
.expect(({ body }) => {
118+
expect(body.columns[0].meta.index).to.be('logstash-*');
119+
expect(body.columns[0].meta.source).to.be('esaggs');
120+
expect(body.columns[0].meta.sourceParams.type).to.be('avg');
121+
expect(getCell(body, 0, 0)).to.be(5727.3136246786635);
122+
});
123+
});
124+
125+
it('works with timeRange filters', async () => {
126+
const timeRange = {
127+
from: '2006-09-21T00:00:00Z',
128+
to: '2015-09-22T00:00:00Z',
129+
};
130+
// we need to manually pass timeRange in the input until
131+
// kibana_context is supported on the server
132+
const kibanaContext = {
133+
type: 'kibana_context',
134+
timeRange,
135+
};
136+
const expression = `
137+
esaggs index={indexPatternLoad id='logstash-*'}
138+
aggs={aggCount id="1" enabled=true schema="metric"}
139+
`;
140+
await supertest
141+
.post('/api/interpreter_functional/run_expression')
142+
.set('kbn-xsrf', 'anything')
143+
.send({ expression, input: kibanaContext })
144+
.expect(200)
145+
.expect(({ body }) => {
146+
expect(getCell(body, 0, 0)).to.be(9375);
147+
});
148+
});
149+
150+
it('returns same results on client & server', async () => {
151+
const a = `
152+
esaggs index={indexPatternLoad id='logstash-*'}
153+
aggs={aggCount id="1" enabled=true schema="metric"}
154+
aggs={aggTerms id="2" enabled=true schema="segment" field="response.raw"}
155+
`;
156+
await expectClientToMatchServer('multiple_aggs', a);
157+
158+
// const b = `
159+
// esaggs index={indexPatternLoad id='logstash-*'}
160+
// aggs={aggCount id="1" enabled=true schema="metric"}
161+
// `;
162+
163+
// const c = `
164+
// esaggs index={indexPatternLoad id='logstash-*'}
165+
// aggs={aggCount id="1" enabled=true schema="metric"}
166+
// `;
167+
});
168+
});
91169
});
92170
}

0 commit comments

Comments
 (0)