Skip to content

Commit

Permalink
Converting tests to typescript
Browse files Browse the repository at this point in the history
  • Loading branch information
simianhacker committed Oct 18, 2018
1 parent 47ea822 commit ab73a43
Show file tree
Hide file tree
Showing 7 changed files with 112 additions and 202 deletions.
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,6 @@
"@types/type-detect": "^4.0.1",
"@types/uuid": "^3.4.4",
"angular-mocks": "1.4.7",
"apollo-boost": "^0.1.16",
"babel-eslint": "^9.0.0",
"babel-jest": "^23.4.2",
"backport": "4.4.1",
Expand Down Expand Up @@ -354,4 +353,4 @@
"node": "8.11.4",
"yarn": "^1.10.1"
}
}
}
1 change: 1 addition & 0 deletions x-pack/test/api_integration/apis/infraops/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* you may not use this file except in compliance with the Elastic License.
*/


export default function ({ loadTestFile }) {
describe('InfraOps GraphQL Endpoints', () => {
loadTestFile(require.resolve('./metrics'));
Expand Down
70 changes: 0 additions & 70 deletions x-pack/test/api_integration/apis/infraops/metrics.js

This file was deleted.

77 changes: 77 additions & 0 deletions x-pack/test/api_integration/apis/infraops/metrics.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

import expect from 'expect.js';
import { first, last } from 'lodash';
import { MetricsQuery } from '../../../../plugins/infra/common/graphql/types';
import { metricsQuery } from '../../../../plugins/infra/public/containers/metrics/metrics.gql_query';
import { KbnTestProvider } from './types';

const metricTests: KbnTestProvider = ({ getService }) => {
const esArchiver = getService('esArchiver');
const client = getService('infraOpsGraphQLClient');

describe('metrics', () => {
before(() => esArchiver.load('infraops'));
after(() => esArchiver.unload('infraops'));

it('should basically work', () => {
return client
.query<MetricsQuery.Query>({
query: metricsQuery,
variables: {
sourceId: 'default',
metrics: ['hostCpuUsage'],
timerange: {
to: 1539806283952,
from: 1539805341208,
interval: '>=1m',
},
nodeId: 'demo-stack-nginx-01',
nodeType: 'host',
},
})
.then(resp => {
const { metrics } = resp.data.source;
expect(metrics.length).to.equal(1);
const metric = first(metrics);
expect(metric).to.have.property('id', 'hostCpuUsage');
expect(metric).to.have.property('series');
const series = first(metric.series);
expect(series).to.have.property('id', 'user');
expect(series).to.have.property('data');
const datapoint = last(series.data);
expect(datapoint).to.have.property('timestamp', 1539806220000);
expect(datapoint).to.have.property('value', 0.0065);
});
});

it('should support multiple metrics', () => {
return client
.query<MetricsQuery.Query>({
query: metricsQuery,
variables: {
sourceId: 'default',
metrics: ['hostCpuUsage', 'hostLoad'],
timerange: {
to: 1539806283952,
from: 1539805341208,
interval: '>=1m',
},
nodeId: 'demo-stack-nginx-01',
nodeType: 'host',
},
})
.then(resp => {
const { metrics } = resp.data.source;
expect(metrics.length).to.equal(2);
});
});
});
};

// tslint:disable-next-line no-default-export
export default metricTests;
21 changes: 21 additions & 0 deletions x-pack/test/api_integration/apis/infraops/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

import { InMemoryCache } from 'apollo-cache-inmemory';
import { ApolloClient } from 'apollo-client';

export interface EsArchiver {
load(name: string): void;
unload(name: string): void;
}

export interface KbnTestProviderOptions {
getService(name: string): any;
getService(name: 'esArchiver'): EsArchiver;
getService(name: 'infraOpsGraphQLClient'): ApolloClient<InMemoryCache>;
}

export type KbnTestProvider = (options: KbnTestProviderOptions) => void;
22 changes: 12 additions & 10 deletions x-pack/test/api_integration/services/infraops_graphql_client.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,26 @@
* you may not use this file except in compliance with the Elastic License.
*/

import ApolloClient from 'apollo-boost'; // eslint-disable-line
import { format as formatUrl } from 'url';
import fetch from 'node-fetch';
import { InMemoryCache } from 'apollo-cache-inmemory';
import { ApolloClient } from 'apollo-client';
import { HttpLink } from 'apollo-link-http';

export function InfraOpsGraphQLProvider({ getService }) {
const config = getService('config');
const kbnURL = formatUrl(config.get('servers.kibana'));

return new ApolloClient({
uri: `${kbnURL}/api/infra/graphql`,
fetch,
request: (operation) => {
operation.setContext({
headers: {
'kbn-xsrf': 'xxx'
}
});
}
cache: new InMemoryCache(),
link: new HttpLink({
credentials: 'same-origin',
fetch,
headers: {
'kbn-xsrf': 'xxx',
},
uri: `${kbnURL}/api/infra/graphql`,
}),
});

}
Expand Down
Loading

0 comments on commit ab73a43

Please sign in to comment.