From d2f31a716f44ce2da7836d2d359a5de8aa8dbb4a Mon Sep 17 00:00:00 2001 From: Marcus Efraimsson Date: Wed, 4 Jul 2018 12:16:39 +0200 Subject: [PATCH 01/12] remove unnecessary conversions --- pkg/services/alerting/extractor_test.go | 8 ++++---- .../client/search_request_test.go | 18 +++++++++--------- pkg/tsdb/mssql/mssql_test.go | 8 ++++---- pkg/tsdb/mysql/mysql_test.go | 10 +++++----- pkg/tsdb/postgres/postgres_test.go | 8 ++++---- 5 files changed, 26 insertions(+), 26 deletions(-) diff --git a/pkg/services/alerting/extractor_test.go b/pkg/services/alerting/extractor_test.go index 861e9b9cbfc..c7212e48174 100644 --- a/pkg/services/alerting/extractor_test.go +++ b/pkg/services/alerting/extractor_test.go @@ -50,7 +50,7 @@ func TestAlertRuleExtraction(t *testing.T) { So(err, ShouldBeNil) Convey("Extractor should not modify the original json", func() { - dashJson, err := simplejson.NewJson([]byte(json)) + dashJson, err := simplejson.NewJson(json) So(err, ShouldBeNil) dash := m.NewDashboardFromJson(dashJson) @@ -79,7 +79,7 @@ func TestAlertRuleExtraction(t *testing.T) { Convey("Parsing and validating dashboard containing graphite alerts", func() { - dashJson, err := simplejson.NewJson([]byte(json)) + dashJson, err := simplejson.NewJson(json) So(err, ShouldBeNil) dash := m.NewDashboardFromJson(dashJson) @@ -143,7 +143,7 @@ func TestAlertRuleExtraction(t *testing.T) { panelWithoutId, err := ioutil.ReadFile("./test-data/panels-missing-id.json") So(err, ShouldBeNil) - dashJson, err := simplejson.NewJson([]byte(panelWithoutId)) + dashJson, err := simplejson.NewJson(panelWithoutId) So(err, ShouldBeNil) dash := m.NewDashboardFromJson(dashJson) extractor := NewDashAlertExtractor(dash, 1) @@ -159,7 +159,7 @@ func TestAlertRuleExtraction(t *testing.T) { panelWithIdZero, err := ioutil.ReadFile("./test-data/panel-with-id-0.json") So(err, ShouldBeNil) - dashJson, err := simplejson.NewJson([]byte(panelWithIdZero)) + dashJson, err := simplejson.NewJson(panelWithIdZero) So(err, ShouldBeNil) dash := m.NewDashboardFromJson(dashJson) extractor := NewDashAlertExtractor(dash, 1) diff --git a/pkg/tsdb/elasticsearch/client/search_request_test.go b/pkg/tsdb/elasticsearch/client/search_request_test.go index b026578d64f..862b8058cba 100644 --- a/pkg/tsdb/elasticsearch/client/search_request_test.go +++ b/pkg/tsdb/elasticsearch/client/search_request_test.go @@ -32,7 +32,7 @@ func TestSearchRequest(t *testing.T) { Convey("When marshal to JSON should generate correct json", func() { body, err := json.Marshal(sr) So(err, ShouldBeNil) - json, err := simplejson.NewJson([]byte(body)) + json, err := simplejson.NewJson(body) So(err, ShouldBeNil) So(json.Get("size").MustInt(500), ShouldEqual, 0) So(json.Get("sort").Interface(), ShouldBeNil) @@ -81,7 +81,7 @@ func TestSearchRequest(t *testing.T) { Convey("When marshal to JSON should generate correct json", func() { body, err := json.Marshal(sr) So(err, ShouldBeNil) - json, err := simplejson.NewJson([]byte(body)) + json, err := simplejson.NewJson(body) So(err, ShouldBeNil) So(json.Get("size").MustInt(0), ShouldEqual, 200) @@ -124,7 +124,7 @@ func TestSearchRequest(t *testing.T) { Convey("When marshal to JSON should generate correct json", func() { body, err := json.Marshal(sr) So(err, ShouldBeNil) - json, err := simplejson.NewJson([]byte(body)) + json, err := simplejson.NewJson(body) So(err, ShouldBeNil) scriptFields, err := json.Get("script_fields").Map() @@ -163,7 +163,7 @@ func TestSearchRequest(t *testing.T) { Convey("When marshal to JSON should generate correct json", func() { body, err := json.Marshal(sr) So(err, ShouldBeNil) - json, err := simplejson.NewJson([]byte(body)) + json, err := simplejson.NewJson(body) So(err, ShouldBeNil) So(json.Get("aggs").MustMap(), ShouldHaveLength, 2) @@ -200,7 +200,7 @@ func TestSearchRequest(t *testing.T) { Convey("When marshal to JSON should generate correct json", func() { body, err := json.Marshal(sr) So(err, ShouldBeNil) - json, err := simplejson.NewJson([]byte(body)) + json, err := simplejson.NewJson(body) So(err, ShouldBeNil) So(json.Get("aggs").MustMap(), ShouldHaveLength, 1) @@ -251,7 +251,7 @@ func TestSearchRequest(t *testing.T) { Convey("When marshal to JSON should generate correct json", func() { body, err := json.Marshal(sr) So(err, ShouldBeNil) - json, err := simplejson.NewJson([]byte(body)) + json, err := simplejson.NewJson(body) So(err, ShouldBeNil) topAggOne := json.GetPath("aggs", "1") @@ -300,7 +300,7 @@ func TestSearchRequest(t *testing.T) { Convey("When marshal to JSON should generate correct json", func() { body, err := json.Marshal(sr) So(err, ShouldBeNil) - json, err := simplejson.NewJson([]byte(body)) + json, err := simplejson.NewJson(body) So(err, ShouldBeNil) topAgg := json.GetPath("aggs", "1") @@ -364,7 +364,7 @@ func TestSearchRequest(t *testing.T) { Convey("When marshal to JSON should generate correct json", func() { body, err := json.Marshal(sr) So(err, ShouldBeNil) - json, err := simplejson.NewJson([]byte(body)) + json, err := simplejson.NewJson(body) So(err, ShouldBeNil) termsAgg := json.GetPath("aggs", "1") @@ -419,7 +419,7 @@ func TestSearchRequest(t *testing.T) { Convey("When marshal to JSON should generate correct json", func() { body, err := json.Marshal(sr) So(err, ShouldBeNil) - json, err := simplejson.NewJson([]byte(body)) + json, err := simplejson.NewJson(body) So(err, ShouldBeNil) scriptFields, err := json.Get("script_fields").Map() diff --git a/pkg/tsdb/mssql/mssql_test.go b/pkg/tsdb/mssql/mssql_test.go index 2ecd3cd9e96..db04d6d1f02 100644 --- a/pkg/tsdb/mssql/mssql_test.go +++ b/pkg/tsdb/mssql/mssql_test.go @@ -531,7 +531,7 @@ func TestMSSQL(t *testing.T) { So(queryResult.Error, ShouldBeNil) So(len(queryResult.Series), ShouldEqual, 1) - So(queryResult.Series[0].Points[0][1].Float64, ShouldEqual, float64(float64(float32(tInitial.Unix())))*1e3) + So(queryResult.Series[0].Points[0][1].Float64, ShouldEqual, float64(float32(tInitial.Unix()))*1e3) }) Convey("When doing a metric query using epoch (float32 nullable) as time column and value column (float32 nullable) should return metric with time in milliseconds", func() { @@ -553,7 +553,7 @@ func TestMSSQL(t *testing.T) { So(queryResult.Error, ShouldBeNil) So(len(queryResult.Series), ShouldEqual, 1) - So(queryResult.Series[0].Points[0][1].Float64, ShouldEqual, float64(float64(float32(tInitial.Unix())))*1e3) + So(queryResult.Series[0].Points[0][1].Float64, ShouldEqual, float64(float32(tInitial.Unix()))*1e3) }) Convey("When doing a metric query grouping by time and select metric column should return correct series", func() { @@ -930,7 +930,7 @@ func TestMSSQL(t *testing.T) { columns := queryResult.Tables[0].Rows[0] //Should be in milliseconds - So(columns[0].(int64), ShouldEqual, int64(dt.Unix()*1000)) + So(columns[0].(int64), ShouldEqual, dt.Unix()*1000) }) Convey("When doing an annotation query with a time column in epoch second format (int) should return ms", func() { @@ -960,7 +960,7 @@ func TestMSSQL(t *testing.T) { columns := queryResult.Tables[0].Rows[0] //Should be in milliseconds - So(columns[0].(int64), ShouldEqual, int64(dt.Unix()*1000)) + So(columns[0].(int64), ShouldEqual, dt.Unix()*1000) }) Convey("When doing an annotation query with a time column in epoch millisecond format should return ms", func() { diff --git a/pkg/tsdb/mysql/mysql_test.go b/pkg/tsdb/mysql/mysql_test.go index 22e98ac63ca..850a37617e2 100644 --- a/pkg/tsdb/mysql/mysql_test.go +++ b/pkg/tsdb/mysql/mysql_test.go @@ -132,8 +132,8 @@ func TestMySQL(t *testing.T) { So(column[7].(float64), ShouldEqual, 1.11) So(column[8].(float64), ShouldEqual, 2.22) So(*column[9].(*float32), ShouldEqual, 3.33) - So(column[10].(time.Time), ShouldHappenWithin, time.Duration(10*time.Second), time.Now()) - So(column[11].(time.Time), ShouldHappenWithin, time.Duration(10*time.Second), time.Now()) + So(column[10].(time.Time), ShouldHappenWithin, 10*time.Second, time.Now()) + So(column[11].(time.Time), ShouldHappenWithin, 10*time.Second, time.Now()) So(column[12].(string), ShouldEqual, "11:11:11") So(column[13].(int64), ShouldEqual, 2018) So(*column[14].(*[]byte), ShouldHaveSameTypeAs, []byte{1}) @@ -578,7 +578,7 @@ func TestMySQL(t *testing.T) { So(queryResult.Error, ShouldBeNil) So(len(queryResult.Series), ShouldEqual, 1) - So(queryResult.Series[0].Points[0][1].Float64, ShouldEqual, float64(float64(float32(tInitial.Unix())))*1e3) + So(queryResult.Series[0].Points[0][1].Float64, ShouldEqual, float64(float32(tInitial.Unix()))*1e3) }) Convey("When doing a metric query using epoch (float32 nullable) as time column and value column (float32 nullable) should return metric with time in milliseconds", func() { @@ -600,7 +600,7 @@ func TestMySQL(t *testing.T) { So(queryResult.Error, ShouldBeNil) So(len(queryResult.Series), ShouldEqual, 1) - So(queryResult.Series[0].Points[0][1].Float64, ShouldEqual, float64(float64(float32(tInitial.Unix())))*1e3) + So(queryResult.Series[0].Points[0][1].Float64, ShouldEqual, float64(float32(tInitial.Unix()))*1e3) }) Convey("When doing a metric query grouping by time and select metric column should return correct series", func() { @@ -817,7 +817,7 @@ func TestMySQL(t *testing.T) { columns := queryResult.Tables[0].Rows[0] //Should be in milliseconds - So(columns[0].(int64), ShouldEqual, int64(dt.Unix()*1000)) + So(columns[0].(int64), ShouldEqual, dt.Unix()*1000) }) Convey("When doing an annotation query with a time column in epoch millisecond format should return ms", func() { diff --git a/pkg/tsdb/postgres/postgres_test.go b/pkg/tsdb/postgres/postgres_test.go index 18ceecb1080..a3a6d6546df 100644 --- a/pkg/tsdb/postgres/postgres_test.go +++ b/pkg/tsdb/postgres/postgres_test.go @@ -512,7 +512,7 @@ func TestPostgres(t *testing.T) { So(queryResult.Error, ShouldBeNil) So(len(queryResult.Series), ShouldEqual, 1) - So(queryResult.Series[0].Points[0][1].Float64, ShouldEqual, float64(float64(float32(tInitial.Unix())))*1e3) + So(queryResult.Series[0].Points[0][1].Float64, ShouldEqual, float64(float32(tInitial.Unix()))*1e3) }) Convey("When doing a metric query using epoch (float32 nullable) as time column and value column (float32 nullable) should return metric with time in milliseconds", func() { @@ -534,7 +534,7 @@ func TestPostgres(t *testing.T) { So(queryResult.Error, ShouldBeNil) So(len(queryResult.Series), ShouldEqual, 1) - So(queryResult.Series[0].Points[0][1].Float64, ShouldEqual, float64(float64(float32(tInitial.Unix())))*1e3) + So(queryResult.Series[0].Points[0][1].Float64, ShouldEqual, float64(float32(tInitial.Unix()))*1e3) }) Convey("When doing a metric query grouping by time and select metric column should return correct series", func() { @@ -721,7 +721,7 @@ func TestPostgres(t *testing.T) { columns := queryResult.Tables[0].Rows[0] //Should be in milliseconds - So(columns[0].(int64), ShouldEqual, int64(dt.Unix()*1000)) + So(columns[0].(int64), ShouldEqual, dt.Unix()*1000) }) Convey("When doing an annotation query with a time column in epoch second format (int) should return ms", func() { @@ -751,7 +751,7 @@ func TestPostgres(t *testing.T) { columns := queryResult.Tables[0].Rows[0] //Should be in milliseconds - So(columns[0].(int64), ShouldEqual, int64(dt.Unix()*1000)) + So(columns[0].(int64), ShouldEqual, dt.Unix()*1000) }) Convey("When doing an annotation query with a time column in epoch millisecond format should return ms", func() { From 01fc6c5d958f98fe952fb7b40f88ced8cf5a28e1 Mon Sep 17 00:00:00 2001 From: Augustin Husson Date: Wed, 4 Jul 2018 12:23:10 +0200 Subject: [PATCH 02/12] fix json indentation --- docs/sources/http_api/admin.md | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/docs/sources/http_api/admin.md b/docs/sources/http_api/admin.md index 0194c69caac..16b9115e8b9 100644 --- a/docs/sources/http_api/admin.md +++ b/docs/sources/http_api/admin.md @@ -36,11 +36,10 @@ HTTP/1.1 200 Content-Type: application/json { -"DEFAULT": -{ - "app_mode":"production"}, - "analytics": - { + "DEFAULT": { + "app_mode":"production" + }, + "analytics": { "google_analytics_ua_id":"", "reporting_enabled":"false" }, @@ -340,4 +339,4 @@ HTTP/1.1 200 Content-Type: application/json {state: "new state", message: "alerts pause/un paused", "alertsAffected": 100} -``` \ No newline at end of file +``` From 991a4b16637ea8fed191b08649d9f044987ab768 Mon Sep 17 00:00:00 2001 From: Augustin Husson Date: Wed, 4 Jul 2018 12:26:30 +0200 Subject: [PATCH 03/12] update stats admin doc --- docs/sources/http_api/admin.md | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/docs/sources/http_api/admin.md b/docs/sources/http_api/admin.md index 16b9115e8b9..2e03611b125 100644 --- a/docs/sources/http_api/admin.md +++ b/docs/sources/http_api/admin.md @@ -194,15 +194,16 @@ HTTP/1.1 200 Content-Type: application/json { - "user_count":2, - "org_count":1, - "dashboard_count":4, - "db_snapshot_count":2, - "db_tag_count":6, - "data_source_count":1, - "playlist_count":1, - "starred_db_count":2, - "grafana_admin_count":2 + "users":2, + "orgs":1, + "dashboards":4, + "snapshots":2, + "tags":6, + "datasources":1, + "playlists":1, + "stars":2, + "alerts":2, + "activeUsers":1 } ``` From 86a574cc5031f78156879d1cac9315a71bb123a0 Mon Sep 17 00:00:00 2001 From: Augustin Husson Date: Wed, 4 Jul 2018 12:30:23 +0200 Subject: [PATCH 04/12] refix the settings indentation --- docs/sources/http_api/admin.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/sources/http_api/admin.md b/docs/sources/http_api/admin.md index 2e03611b125..2d4be21bb78 100644 --- a/docs/sources/http_api/admin.md +++ b/docs/sources/http_api/admin.md @@ -37,7 +37,7 @@ Content-Type: application/json { "DEFAULT": { - "app_mode":"production" + "app_mode":"production" }, "analytics": { "google_analytics_ua_id":"", From a8970a4de95eed9cdb28267fee884207b4649703 Mon Sep 17 00:00:00 2001 From: Marcus Efraimsson Date: Wed, 4 Jul 2018 13:09:42 +0200 Subject: [PATCH 05/12] run enterprise build only on master for now --- .circleci/config.yml | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index 9b2f436adee..f351040fe2f 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -8,6 +8,9 @@ aliases: - &filter-not-release tags: ignore: /^v[0-9]+(\.[0-9]+){2}(-.+|[^-.]*)$/ + - &filter-only-master + branches: + only: master version: 2 @@ -242,7 +245,7 @@ workflows: - build-all: filters: *filter-not-release - build-enterprise: - filters: *filter-not-release + filters: *filter-only-master - codespell: filters: *filter-not-release - gometalinter: @@ -277,9 +280,7 @@ workflows: - mysql-integration-test - postgres-integration-test - build-enterprise - filters: - branches: - only: master + filters: *filter-only-master release: jobs: From bd417bedb238650b10a7bd62e9abc2e74c0d1a90 Mon Sep 17 00:00:00 2001 From: Tobias Skarhed Date: Tue, 3 Jul 2018 10:12:07 +0200 Subject: [PATCH 06/12] Start elastic ds test conversion --- ...datasource_specs.ts => datasource.jest.ts} | 116 ++++++++++-------- 1 file changed, 65 insertions(+), 51 deletions(-) rename public/app/plugins/datasource/elasticsearch/specs/{datasource_specs.ts => datasource.jest.ts} (75%) diff --git a/public/app/plugins/datasource/elasticsearch/specs/datasource_specs.ts b/public/app/plugins/datasource/elasticsearch/specs/datasource.jest.ts similarity index 75% rename from public/app/plugins/datasource/elasticsearch/specs/datasource_specs.ts rename to public/app/plugins/datasource/elasticsearch/specs/datasource.jest.ts index 558bccf3d0f..9a3d57f1d4f 100644 --- a/public/app/plugins/datasource/elasticsearch/specs/datasource_specs.ts +++ b/public/app/plugins/datasource/elasticsearch/specs/datasource.jest.ts @@ -2,31 +2,45 @@ import _ from 'lodash'; import { describe, beforeEach, it, expect, angularMocks } from 'test/lib/common'; import moment from 'moment'; import angular from 'angular'; -import helpers from 'test/specs/helpers'; import { ElasticDatasource } from '../datasource'; +import { TimeSrv } from 'app/features/dashboard/time_srv'; +import $q from 'q'; describe('ElasticDatasource', function() { - var ctx = new helpers.ServiceTestContext(); + //var ctx = new helpers.ServiceTestContext(); + let backendSrv = { + datasourceRequest: jest.fn() + }; - beforeEach(angularMocks.module('grafana.core')); - beforeEach(angularMocks.module('grafana.services')); - beforeEach(ctx.providePhase(['templateSrv', 'backendSrv', 'timeSrv'])); + let $rootScope = { + $on: jest.fn(), + appEvent: jest.fn(), + }; - beforeEach( - angularMocks.inject(function($q, $rootScope, $httpBackend, $injector) { - ctx.$q = $q; - ctx.$httpBackend = $httpBackend; - ctx.$rootScope = $rootScope; - ctx.$injector = $injector; - $httpBackend.when('GET', /\.html$/).respond(''); - }) - ); + let timeSrv = new TimeSrv($rootScope,jest.fn(),{},{},{}); + + let ctx = { + $rootScope, + backendSrv, + $q + }; + // beforeEach(angularMocks.module('grafana.core')); + // beforeEach(angularMocks.module('grafana.services')); + // beforeEach(ctx.providePhase(['templateSrv', 'backendSrv', 'timeSrv'])); + + // beforeEach( + // angularMocks.inject(function($q, $rootScope, $httpBackend, $injector) { + // ctx.$q = $q; + // ctx.$httpBackend = $httpBackend; + // ctx.$rootScope = $rootScope; + // ctx.$injector = $injector; + // $httpBackend.when('GET', /\.html$/).respond(''); + // }) + // ); function createDatasource(instanceSettings) { instanceSettings.jsonData = instanceSettings.jsonData || {}; - ctx.ds = ctx.$injector.instantiate(ElasticDatasource, { - instanceSettings: instanceSettings, - }); + ctx.ds = new ElasticDatasource(instanceSettings, {}, backendSrv, { replace: jest.fn() }, timeSrv); } describe('When testing datasource with index pattern', function() { @@ -40,13 +54,13 @@ describe('ElasticDatasource', function() { it('should translate index pattern to current day', function() { var requestOptions; - ctx.backendSrv.datasourceRequest = function(options) { + ctx.backendSrv.datasourceRequest = jest.fn((options) => { requestOptions = options; - return ctx.$q.when({ data: {} }); - }; + return Promise.resolve({ data: {} }); + }); ctx.ds.testDatasource(); - ctx.$rootScope.$apply(); + // ctx.$rootScope.$apply(); var today = moment.utc().format('YYYY.MM.DD'); expect(requestOptions.url).to.be('http://es.com/asd-' + today + '/_mapping'); @@ -63,10 +77,10 @@ describe('ElasticDatasource', function() { jsonData: { interval: 'Daily', esVersion: '2' }, }); - ctx.backendSrv.datasourceRequest = function(options) { + ctx.backendSrv.datasourceRequest = jest.fn((options) => { requestOptions = options; - return ctx.$q.when({ data: { responses: [] } }); - }; + return Promise.resolve({ data: { responses: [] } }); + }); ctx.ds.query({ range: { @@ -82,19 +96,19 @@ describe('ElasticDatasource', function() { ], }); - ctx.$rootScope.$apply(); + // ctx.$rootScope.$apply(); parts = requestOptions.data.split('\n'); header = angular.fromJson(parts[0]); }); it('should translate index pattern to current day', function() { - expect(header.index).to.eql(['asd-2015.05.30', 'asd-2015.05.31', 'asd-2015.06.01']); + expect(header.index).toEqual(['asd-2015.05.30', 'asd-2015.05.31', 'asd-2015.06.01']); }); it('should json escape lucene query', function() { var body = angular.fromJson(parts[1]); - expect(body.query.bool.filter[1].query_string.query).to.be('escape\\:test'); + expect(body.query.bool.filter[1].query_string.query).toBe('escape\\:test'); }); }); @@ -108,10 +122,10 @@ describe('ElasticDatasource', function() { jsonData: { esVersion: '2' }, }); - ctx.backendSrv.datasourceRequest = function(options) { + ctx.backendSrv.datasourceRequest = jest.fn((options) => { requestOptions = options; - return ctx.$q.when({ data: { responses: [] } }); - }; + return Promise.resolve({ data: { responses: [] } }); + }); ctx.ds.query({ range: { @@ -127,18 +141,18 @@ describe('ElasticDatasource', function() { ], }); - ctx.$rootScope.$apply(); + // ctx.$rootScope.$apply(); parts = requestOptions.data.split('\n'); header = angular.fromJson(parts[0]); }); it('should set search type to query_then_fetch', function() { - expect(header.search_type).to.eql('query_then_fetch'); + expect(header.search_type).toEqual('query_then_fetch'); }); it('should set size', function() { var body = angular.fromJson(parts[1]); - expect(body.size).to.be(500); + expect(body.size).toBe(500); }); }); @@ -146,8 +160,8 @@ describe('ElasticDatasource', function() { beforeEach(function() { createDatasource({ url: 'http://es.com', index: 'metricbeat' }); - ctx.backendSrv.datasourceRequest = function(options) { - return ctx.$q.when({ + ctx.backendSrv.datasourceRequest = jest.fn((options) => { + return Promise.resolve({ data: { metricbeat: { mappings: { @@ -190,7 +204,7 @@ describe('ElasticDatasource', function() { }, }, }); - }; + }) }); it('should return nested fields', function() { @@ -201,7 +215,7 @@ describe('ElasticDatasource', function() { }) .then(fieldObjects => { var fields = _.map(fieldObjects, 'text'); - expect(fields).to.eql([ + expect(fields).toEqual([ '@timestamp', 'beat.name.raw', 'beat.name', @@ -212,7 +226,7 @@ describe('ElasticDatasource', function() { 'system.process.name', ]); }); - ctx.$rootScope.$apply(); + // ctx.$rootScope.$apply(); }); it('should return fields related to query type', function() { @@ -224,7 +238,7 @@ describe('ElasticDatasource', function() { }) .then(fieldObjects => { var fields = _.map(fieldObjects, 'text'); - expect(fields).to.eql(['system.cpu.system', 'system.cpu.user', 'system.process.cpu.total']); + expect(fields).toEqual(['system.cpu.system', 'system.cpu.user', 'system.process.cpu.total']); }); ctx.ds @@ -235,10 +249,10 @@ describe('ElasticDatasource', function() { }) .then(fieldObjects => { var fields = _.map(fieldObjects, 'text'); - expect(fields).to.eql(['@timestamp']); + expect(fields).toEqual(['@timestamp']); }); - ctx.$rootScope.$apply(); + // ctx.$rootScope.$apply(); }); }); @@ -271,18 +285,18 @@ describe('ElasticDatasource', function() { ], }); - ctx.$rootScope.$apply(); + // ctx.$rootScope.$apply(); parts = requestOptions.data.split('\n'); header = angular.fromJson(parts[0]); }); it('should not set search type to count', function() { - expect(header.search_type).to.not.eql('count'); + expect(header.search_type).not.toEqual('count'); }); it('should set size to 0', function() { var body = angular.fromJson(parts[1]); - expect(body.size).to.be(0); + expect(body.size).toBe(0); }); }); @@ -324,7 +338,7 @@ describe('ElasticDatasource', function() { results = res; }); - ctx.$rootScope.$apply(); + // ctx.$rootScope.$apply(); parts = requestOptions.data.split('\n'); header = angular.fromJson(parts[0]); @@ -332,24 +346,24 @@ describe('ElasticDatasource', function() { }); it('should get results', function() { - expect(results.length).to.eql(2); + expect(results.length).toEqual(2); }); it('should use key or key_as_string', function() { - expect(results[0].text).to.eql('test'); - expect(results[1].text).to.eql('test2_as_string'); + expect(results[0].text).toEqual('test'); + expect(results[1].text).toEqual('test2_as_string'); }); it('should not set search type to count', function() { - expect(header.search_type).to.not.eql('count'); + expect(header.search_type).not.toEqual('count'); }); it('should set size to 0', function() { - expect(body.size).to.be(0); + expect(body.size).toBe(0); }); it('should not set terms aggregation size to 0', function() { - expect(body['aggs']['1']['terms'].size).to.not.be(0); + expect(body['aggs']['1']['terms'].size).not.toBe(0); }); }); }); From 12e76ad1972fe04fb97e870bab50524a3dcf014b Mon Sep 17 00:00:00 2001 From: Tobias Skarhed Date: Tue, 3 Jul 2018 10:47:50 +0200 Subject: [PATCH 07/12] Remove logs and comments --- .../elasticsearch/specs/datasource.jest.ts | 80 +++++++------------ 1 file changed, 30 insertions(+), 50 deletions(-) diff --git a/public/app/plugins/datasource/elasticsearch/specs/datasource.jest.ts b/public/app/plugins/datasource/elasticsearch/specs/datasource.jest.ts index 9a3d57f1d4f..def60ecdcb3 100644 --- a/public/app/plugins/datasource/elasticsearch/specs/datasource.jest.ts +++ b/public/app/plugins/datasource/elasticsearch/specs/datasource.jest.ts @@ -1,15 +1,14 @@ import _ from 'lodash'; -import { describe, beforeEach, it, expect, angularMocks } from 'test/lib/common'; import moment from 'moment'; import angular from 'angular'; import { ElasticDatasource } from '../datasource'; import { TimeSrv } from 'app/features/dashboard/time_srv'; +import { TemplateSrv } from 'app/features/templating/template_srv'; import $q from 'q'; describe('ElasticDatasource', function() { - //var ctx = new helpers.ServiceTestContext(); let backendSrv = { - datasourceRequest: jest.fn() + datasourceRequest: jest.fn(), }; let $rootScope = { @@ -17,30 +16,21 @@ describe('ElasticDatasource', function() { appEvent: jest.fn(), }; - let timeSrv = new TimeSrv($rootScope,jest.fn(),{},{},{}); + let templateSrv = new TemplateSrv(); + + templateSrv.variables = []; + + let timeSrv = new TimeSrv($rootScope, jest.fn(), {}, {}, {}); let ctx = { $rootScope, backendSrv, - $q + $q, }; - // beforeEach(angularMocks.module('grafana.core')); - // beforeEach(angularMocks.module('grafana.services')); - // beforeEach(ctx.providePhase(['templateSrv', 'backendSrv', 'timeSrv'])); - - // beforeEach( - // angularMocks.inject(function($q, $rootScope, $httpBackend, $injector) { - // ctx.$q = $q; - // ctx.$httpBackend = $httpBackend; - // ctx.$rootScope = $rootScope; - // ctx.$injector = $injector; - // $httpBackend.when('GET', /\.html$/).respond(''); - // }) - // ); function createDatasource(instanceSettings) { instanceSettings.jsonData = instanceSettings.jsonData || {}; - ctx.ds = new ElasticDatasource(instanceSettings, {}, backendSrv, { replace: jest.fn() }, timeSrv); + ctx.ds = new ElasticDatasource(instanceSettings, {}, backendSrv, templateSrv, timeSrv); } describe('When testing datasource with index pattern', function() { @@ -54,35 +44,34 @@ describe('ElasticDatasource', function() { it('should translate index pattern to current day', function() { var requestOptions; - ctx.backendSrv.datasourceRequest = jest.fn((options) => { + ctx.backendSrv.datasourceRequest = jest.fn(options => { requestOptions = options; return Promise.resolve({ data: {} }); }); ctx.ds.testDatasource(); - // ctx.$rootScope.$apply(); var today = moment.utc().format('YYYY.MM.DD'); - expect(requestOptions.url).to.be('http://es.com/asd-' + today + '/_mapping'); + expect(requestOptions.url).toBe('http://es.com/asd-' + today + '/_mapping'); }); }); describe('When issuing metric query with interval pattern', function() { var requestOptions, parts, header; - beforeEach(function() { + beforeEach(async () => { createDatasource({ url: 'http://es.com', index: '[asd-]YYYY.MM.DD', jsonData: { interval: 'Daily', esVersion: '2' }, }); - ctx.backendSrv.datasourceRequest = jest.fn((options) => { + ctx.backendSrv.datasourceRequest = jest.fn(options => { requestOptions = options; return Promise.resolve({ data: { responses: [] } }); }); - ctx.ds.query({ + await ctx.ds.query({ range: { from: moment.utc([2015, 4, 30, 10]), to: moment.utc([2015, 5, 1, 10]), @@ -96,8 +85,6 @@ describe('ElasticDatasource', function() { ], }); - // ctx.$rootScope.$apply(); - parts = requestOptions.data.split('\n'); header = angular.fromJson(parts[0]); }); @@ -122,7 +109,7 @@ describe('ElasticDatasource', function() { jsonData: { esVersion: '2' }, }); - ctx.backendSrv.datasourceRequest = jest.fn((options) => { + ctx.backendSrv.datasourceRequest = jest.fn(options => { requestOptions = options; return Promise.resolve({ data: { responses: [] } }); }); @@ -141,7 +128,6 @@ describe('ElasticDatasource', function() { ], }); - // ctx.$rootScope.$apply(); parts = requestOptions.data.split('\n'); header = angular.fromJson(parts[0]); }); @@ -157,10 +143,10 @@ describe('ElasticDatasource', function() { }); describe('When getting fields', function() { - beforeEach(function() { + beforeEach(() => { createDatasource({ url: 'http://es.com', index: 'metricbeat' }); - ctx.backendSrv.datasourceRequest = jest.fn((options) => { + ctx.backendSrv.datasourceRequest = jest.fn(options => { return Promise.resolve({ data: { metricbeat: { @@ -204,7 +190,7 @@ describe('ElasticDatasource', function() { }, }, }); - }) + }); }); it('should return nested fields', function() { @@ -226,7 +212,6 @@ describe('ElasticDatasource', function() { 'system.process.name', ]); }); - // ctx.$rootScope.$apply(); }); it('should return fields related to query type', function() { @@ -251,8 +236,6 @@ describe('ElasticDatasource', function() { var fields = _.map(fieldObjects, 'text'); expect(fields).toEqual(['@timestamp']); }); - - // ctx.$rootScope.$apply(); }); }); @@ -266,10 +249,10 @@ describe('ElasticDatasource', function() { jsonData: { esVersion: '5' }, }); - ctx.backendSrv.datasourceRequest = function(options) { + ctx.backendSrv.datasourceRequest = jest.fn(options => { requestOptions = options; - return ctx.$q.when({ data: { responses: [] } }); - }; + return Promise.resolve({ data: { responses: [] } }); + }); ctx.ds.query({ range: { @@ -285,7 +268,6 @@ describe('ElasticDatasource', function() { ], }); - // ctx.$rootScope.$apply(); parts = requestOptions.data.split('\n'); header = angular.fromJson(parts[0]); }); @@ -303,16 +285,16 @@ describe('ElasticDatasource', function() { describe('When issuing metricFind query on es5.x', function() { var requestOptions, parts, header, body, results; - beforeEach(function() { + beforeEach(() => { createDatasource({ url: 'http://es.com', index: 'test', jsonData: { esVersion: '5' }, }); - ctx.backendSrv.datasourceRequest = function(options) { + ctx.backendSrv.datasourceRequest = jest.fn(options => { requestOptions = options; - return ctx.$q.when({ + return Promise.resolve({ data: { responses: [ { @@ -332,37 +314,35 @@ describe('ElasticDatasource', function() { ], }, }); - }; + }); ctx.ds.metricFindQuery('{"find": "terms", "field": "test"}').then(res => { results = res; }); - // ctx.$rootScope.$apply(); - parts = requestOptions.data.split('\n'); header = angular.fromJson(parts[0]); body = angular.fromJson(parts[1]); }); - it('should get results', function() { + it('should get results', () => { expect(results.length).toEqual(2); }); - it('should use key or key_as_string', function() { + it('should use key or key_as_string', () => { expect(results[0].text).toEqual('test'); expect(results[1].text).toEqual('test2_as_string'); }); - it('should not set search type to count', function() { + it('should not set search type to count', () => { expect(header.search_type).not.toEqual('count'); }); - it('should set size to 0', function() { + it('should set size to 0', () => { expect(body.size).toBe(0); }); - it('should not set terms aggregation size to 0', function() { + it('should not set terms aggregation size to 0', () => { expect(body['aggs']['1']['terms'].size).not.toBe(0); }); }); From 69c85e0d7efc4bb2096b9855c75274240087947a Mon Sep 17 00:00:00 2001 From: Tobias Skarhed Date: Tue, 3 Jul 2018 10:55:48 +0200 Subject: [PATCH 08/12] Remove async --- .../plugins/datasource/elasticsearch/specs/datasource.jest.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/public/app/plugins/datasource/elasticsearch/specs/datasource.jest.ts b/public/app/plugins/datasource/elasticsearch/specs/datasource.jest.ts index def60ecdcb3..32b5a4dbbd5 100644 --- a/public/app/plugins/datasource/elasticsearch/specs/datasource.jest.ts +++ b/public/app/plugins/datasource/elasticsearch/specs/datasource.jest.ts @@ -59,7 +59,7 @@ describe('ElasticDatasource', function() { describe('When issuing metric query with interval pattern', function() { var requestOptions, parts, header; - beforeEach(async () => { + beforeEach(() => { createDatasource({ url: 'http://es.com', index: '[asd-]YYYY.MM.DD', @@ -71,7 +71,7 @@ describe('ElasticDatasource', function() { return Promise.resolve({ data: { responses: [] } }); }); - await ctx.ds.query({ + ctx.ds.query({ range: { from: moment.utc([2015, 4, 30, 10]), to: moment.utc([2015, 5, 1, 10]), From 80b2f5c7567cdef4c6cb288001ceb44f164d48be Mon Sep 17 00:00:00 2001 From: Tobias Skarhed Date: Tue, 3 Jul 2018 11:55:23 +0200 Subject: [PATCH 09/12] Add Jest stubs --- .../elasticsearch/specs/datasource.jest.ts | 10 ++-- public/test/specs/helpers.ts | 51 ++++++++++++++++--- 2 files changed, 49 insertions(+), 12 deletions(-) diff --git a/public/app/plugins/datasource/elasticsearch/specs/datasource.jest.ts b/public/app/plugins/datasource/elasticsearch/specs/datasource.jest.ts index 32b5a4dbbd5..bfd55544d7b 100644 --- a/public/app/plugins/datasource/elasticsearch/specs/datasource.jest.ts +++ b/public/app/plugins/datasource/elasticsearch/specs/datasource.jest.ts @@ -2,8 +2,8 @@ import _ from 'lodash'; import moment from 'moment'; import angular from 'angular'; import { ElasticDatasource } from '../datasource'; -import { TimeSrv } from 'app/features/dashboard/time_srv'; -import { TemplateSrv } from 'app/features/templating/template_srv'; +import { jestTimeSrvStub } from 'test/specs/helpers'; +import { jestTemplateSrvStub } from 'test/specs/helpers'; import $q from 'q'; describe('ElasticDatasource', function() { @@ -16,11 +16,9 @@ describe('ElasticDatasource', function() { appEvent: jest.fn(), }; - let templateSrv = new TemplateSrv(); + let templateSrv = new jestTemplateSrvStub(); - templateSrv.variables = []; - - let timeSrv = new TimeSrv($rootScope, jest.fn(), {}, {}, {}); + let timeSrv = new jestTimeSrvStub(); let ctx = { $rootScope, diff --git a/public/test/specs/helpers.ts b/public/test/specs/helpers.ts index dd8bd39846e..d98e79ff06b 100644 --- a/public/test/specs/helpers.ts +++ b/public/test/specs/helpers.ts @@ -195,13 +195,52 @@ export function TemplateSrvStub() { }; } +export function jestTemplateSrvStub() { + this.variables = []; + this.templateSettings = { interpolate: /\[\[([\s\S]+?)\]\]/g }; + this.data = {}; + this.replace = jest.fn(text => _.template(text, this.templateSettings)(this.data)); + this.init = jest.fn(); + this.getAdhocFilters = jest.fn(() => []); + this.fillVariableValuesForUrl = jest.fn(); + this.updateTemplateData = jest.fn(); + this.variableExists = jest.fn(() => false); + this.variableInitialized = jest.fn(); + this.highlightVariablesAsHtml = jest.fn(str => str); + this.setGrafanaVariable = jest.fn((name, value) => { + this.data[name] = value; + }); +} + +export function jestTimeSrvStub() { + this.init = jest.fn(); + this.time = { from: 'now-1h', to: 'now' }; + this.timeRange = jest.fn(parse => { + if (parse === false) { + return this.time; + } + return { + from: dateMath.parse(this.time.from, false), + to: dateMath.parse(this.time.to, true), + }; + }); + + this.replace = jest.fn(target => target); + + this.setTime = jest.fn(time => { + this.time = time; + }); +} + var allDeps = { - ContextSrvStub: ContextSrvStub, - TemplateSrvStub: TemplateSrvStub, - TimeSrvStub: TimeSrvStub, - ControllerTestContext: ControllerTestContext, - ServiceTestContext: ServiceTestContext, - DashboardViewStateStub: DashboardViewStateStub, + ContextSrvStub, + TemplateSrvStub, + TimeSrvStub, + ControllerTestContext, + ServiceTestContext, + DashboardViewStateStub, + jestTimeSrvStub, + jestTemplateSrvStub, }; // for legacy From 8f1bcd91178375c007688e022677611cd5d4c9f7 Mon Sep 17 00:00:00 2001 From: Tobias Skarhed Date: Tue, 3 Jul 2018 13:33:30 +0200 Subject: [PATCH 10/12] Remove q and stub --- .../datasource/elasticsearch/specs/datasource.jest.ts | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/public/app/plugins/datasource/elasticsearch/specs/datasource.jest.ts b/public/app/plugins/datasource/elasticsearch/specs/datasource.jest.ts index bfd55544d7b..b6d19d003da 100644 --- a/public/app/plugins/datasource/elasticsearch/specs/datasource.jest.ts +++ b/public/app/plugins/datasource/elasticsearch/specs/datasource.jest.ts @@ -2,9 +2,7 @@ import _ from 'lodash'; import moment from 'moment'; import angular from 'angular'; import { ElasticDatasource } from '../datasource'; -import { jestTimeSrvStub } from 'test/specs/helpers'; -import { jestTemplateSrvStub } from 'test/specs/helpers'; -import $q from 'q'; +import { jestTimeSrvStub, TemplateSrvStub } from 'test/specs/helpers'; describe('ElasticDatasource', function() { let backendSrv = { @@ -16,14 +14,13 @@ describe('ElasticDatasource', function() { appEvent: jest.fn(), }; - let templateSrv = new jestTemplateSrvStub(); + let templateSrv = new TemplateSrvStub(); let timeSrv = new jestTimeSrvStub(); let ctx = { $rootScope, backendSrv, - $q, }; function createDatasource(instanceSettings) { From 12d158f391d0256802ff5be4bb0fb1f9335f4b66 Mon Sep 17 00:00:00 2001 From: Tobias Skarhed Date: Wed, 4 Jul 2018 10:43:36 +0200 Subject: [PATCH 11/12] Add mocks in test file --- .../elasticsearch/specs/datasource.jest.ts | 21 ++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/public/app/plugins/datasource/elasticsearch/specs/datasource.jest.ts b/public/app/plugins/datasource/elasticsearch/specs/datasource.jest.ts index b6d19d003da..e9f7a61a572 100644 --- a/public/app/plugins/datasource/elasticsearch/specs/datasource.jest.ts +++ b/public/app/plugins/datasource/elasticsearch/specs/datasource.jest.ts @@ -2,7 +2,8 @@ import _ from 'lodash'; import moment from 'moment'; import angular from 'angular'; import { ElasticDatasource } from '../datasource'; -import { jestTimeSrvStub, TemplateSrvStub } from 'test/specs/helpers'; + +import * as dateMath from 'app/core/utils/datemath'; describe('ElasticDatasource', function() { let backendSrv = { @@ -14,9 +15,23 @@ describe('ElasticDatasource', function() { appEvent: jest.fn(), }; - let templateSrv = new TemplateSrvStub(); + let templateSrv = { + replace: jest.fn(text => text), + getAdhocFilters: jest.fn(() => []), + }; - let timeSrv = new jestTimeSrvStub(); + let timeSrv = { + time: { from: 'now-1h', to: 'now' }, + timeRange: jest.fn(parse => { + return { + from: dateMath.parse(this.time.from, false), + to: dateMath.parse(this.time.to, true), + }; + }), + setTime: jest.fn(time => { + this.time = time; + }), + }; let ctx = { $rootScope, From d3c213973c7d6eeb0eb2affbf2178bad2713c641 Mon Sep 17 00:00:00 2001 From: Tobias Skarhed Date: Wed, 4 Jul 2018 11:23:12 +0200 Subject: [PATCH 12/12] Basic cleanup --- .../elasticsearch/specs/datasource.jest.ts | 2 +- public/test/specs/helpers.ts | 39 ------------------- 2 files changed, 1 insertion(+), 40 deletions(-) diff --git a/public/app/plugins/datasource/elasticsearch/specs/datasource.jest.ts b/public/app/plugins/datasource/elasticsearch/specs/datasource.jest.ts index e9f7a61a572..36e7a63a005 100644 --- a/public/app/plugins/datasource/elasticsearch/specs/datasource.jest.ts +++ b/public/app/plugins/datasource/elasticsearch/specs/datasource.jest.ts @@ -22,7 +22,7 @@ describe('ElasticDatasource', function() { let timeSrv = { time: { from: 'now-1h', to: 'now' }, - timeRange: jest.fn(parse => { + timeRange: jest.fn(() => { return { from: dateMath.parse(this.time.from, false), to: dateMath.parse(this.time.to, true), diff --git a/public/test/specs/helpers.ts b/public/test/specs/helpers.ts index d98e79ff06b..677419f3f75 100644 --- a/public/test/specs/helpers.ts +++ b/public/test/specs/helpers.ts @@ -195,43 +195,6 @@ export function TemplateSrvStub() { }; } -export function jestTemplateSrvStub() { - this.variables = []; - this.templateSettings = { interpolate: /\[\[([\s\S]+?)\]\]/g }; - this.data = {}; - this.replace = jest.fn(text => _.template(text, this.templateSettings)(this.data)); - this.init = jest.fn(); - this.getAdhocFilters = jest.fn(() => []); - this.fillVariableValuesForUrl = jest.fn(); - this.updateTemplateData = jest.fn(); - this.variableExists = jest.fn(() => false); - this.variableInitialized = jest.fn(); - this.highlightVariablesAsHtml = jest.fn(str => str); - this.setGrafanaVariable = jest.fn((name, value) => { - this.data[name] = value; - }); -} - -export function jestTimeSrvStub() { - this.init = jest.fn(); - this.time = { from: 'now-1h', to: 'now' }; - this.timeRange = jest.fn(parse => { - if (parse === false) { - return this.time; - } - return { - from: dateMath.parse(this.time.from, false), - to: dateMath.parse(this.time.to, true), - }; - }); - - this.replace = jest.fn(target => target); - - this.setTime = jest.fn(time => { - this.time = time; - }); -} - var allDeps = { ContextSrvStub, TemplateSrvStub, @@ -239,8 +202,6 @@ var allDeps = { ControllerTestContext, ServiceTestContext, DashboardViewStateStub, - jestTimeSrvStub, - jestTemplateSrvStub, }; // for legacy