diff --git a/dist/datasource-zabbix/specs/datasource.spec.js b/dist/datasource-zabbix/specs/datasource.spec.js index abe5757..5dfe2b8 100644 --- a/dist/datasource-zabbix/specs/datasource.spec.js +++ b/dist/datasource-zabbix/specs/datasource.spec.js @@ -1,5 +1,5 @@ import _ from 'lodash'; -import Q from "q"; +import Q, { Promise } from "q"; import {Datasource} from "../module"; import {zabbixTemplateFormat} from "../datasource"; @@ -23,7 +23,10 @@ describe('ZabbixDatasource', () => { ctx.templateSrv = {}; ctx.alertSrv = {}; ctx.dashboardSrv = {}; - ctx.zabbixAlertingSrv = {}; + ctx.zabbixAlertingSrv = { + setPanelAlertState: jest.fn(), + removeZabbixThreshold: jest.fn(), + }; ctx.zabbix = () => {}; ctx.ds = new Datasource(ctx.instanceSettings, ctx.templateSrv, ctx.alertSrv, ctx.dashboardSrv, ctx.zabbixAlertingSrv, ctx.zabbix); @@ -93,6 +96,67 @@ describe('ZabbixDatasource', () => { }); + describe('When querying text data', () => { + beforeEach(() => { + ctx.ds.replaceTemplateVars = (str) => str; + ctx.ds.alertQuery = () => Q.when([]); + ctx.ds.zabbix.getHistory = jest.fn().mockReturnValue(Promise.resolve([ + {clock: "1500010200", itemid:"10100", ns:"900111000", value:"Linux first"}, + {clock: "1500010300", itemid:"10100", ns:"900111000", value:"Linux 2nd"}, + {clock: "1500010400", itemid:"10100", ns:"900111000", value:"Linux last"} + ])); + + ctx.ds.zabbix.getItemsFromTarget = jest.fn().mockReturnValue(Promise.resolve([ + { + hosts: [{hostid: "10001", name: "Zabbix server"}], + itemid: "10100", + name: "System information", + key_: "system.uname", + } + ])); + + ctx.options = { + range: {from: 'now-1h', to: 'now'}, + targets: [ + { + group: {filter: ""}, + host: {filter: "Zabbix server"}, + application: {filter: ""}, + item: {filter: "System information"}, + mode: 2, + resultFormat: "table" + } + ], + }; + }); + + it('should return data in table format', (done) => { + ctx.ds.query(ctx.options).then(result => { + expect(result.data.length).toBe(1); + + let tableData = result.data[0]; + expect(tableData.columns).toEqual([ + {text: 'Host'}, {text: 'Item'}, {text: 'Key'}, {text: 'Last value'} + ]); + expect(tableData.rows).toEqual([ + ['Zabbix server', 'System information', 'system.uname', 'Linux last'] + ]); + done(); + }); + }); + + it('should extract value if regex is specified', (done) => { + ctx.options.targets[0].textFilter = "Linux (.*)"; + ctx.ds.query(ctx.options).then(result => { + let tableData = result.data[0]; + expect(tableData.rows).toEqual([ + ['Zabbix server', 'System information', 'system.uname', 'last'] + ]); + done(); + }); + }); + }); + describe('When replacing template variables', () => { function testReplacingVariable(target, varValue, expectedResult, done) { diff --git a/src/datasource-zabbix/specs/datasource.spec.js b/src/datasource-zabbix/specs/datasource.spec.js index abe5757..5dfe2b8 100644 --- a/src/datasource-zabbix/specs/datasource.spec.js +++ b/src/datasource-zabbix/specs/datasource.spec.js @@ -1,5 +1,5 @@ import _ from 'lodash'; -import Q from "q"; +import Q, { Promise } from "q"; import {Datasource} from "../module"; import {zabbixTemplateFormat} from "../datasource"; @@ -23,7 +23,10 @@ describe('ZabbixDatasource', () => { ctx.templateSrv = {}; ctx.alertSrv = {}; ctx.dashboardSrv = {}; - ctx.zabbixAlertingSrv = {}; + ctx.zabbixAlertingSrv = { + setPanelAlertState: jest.fn(), + removeZabbixThreshold: jest.fn(), + }; ctx.zabbix = () => {}; ctx.ds = new Datasource(ctx.instanceSettings, ctx.templateSrv, ctx.alertSrv, ctx.dashboardSrv, ctx.zabbixAlertingSrv, ctx.zabbix); @@ -93,6 +96,67 @@ describe('ZabbixDatasource', () => { }); + describe('When querying text data', () => { + beforeEach(() => { + ctx.ds.replaceTemplateVars = (str) => str; + ctx.ds.alertQuery = () => Q.when([]); + ctx.ds.zabbix.getHistory = jest.fn().mockReturnValue(Promise.resolve([ + {clock: "1500010200", itemid:"10100", ns:"900111000", value:"Linux first"}, + {clock: "1500010300", itemid:"10100", ns:"900111000", value:"Linux 2nd"}, + {clock: "1500010400", itemid:"10100", ns:"900111000", value:"Linux last"} + ])); + + ctx.ds.zabbix.getItemsFromTarget = jest.fn().mockReturnValue(Promise.resolve([ + { + hosts: [{hostid: "10001", name: "Zabbix server"}], + itemid: "10100", + name: "System information", + key_: "system.uname", + } + ])); + + ctx.options = { + range: {from: 'now-1h', to: 'now'}, + targets: [ + { + group: {filter: ""}, + host: {filter: "Zabbix server"}, + application: {filter: ""}, + item: {filter: "System information"}, + mode: 2, + resultFormat: "table" + } + ], + }; + }); + + it('should return data in table format', (done) => { + ctx.ds.query(ctx.options).then(result => { + expect(result.data.length).toBe(1); + + let tableData = result.data[0]; + expect(tableData.columns).toEqual([ + {text: 'Host'}, {text: 'Item'}, {text: 'Key'}, {text: 'Last value'} + ]); + expect(tableData.rows).toEqual([ + ['Zabbix server', 'System information', 'system.uname', 'Linux last'] + ]); + done(); + }); + }); + + it('should extract value if regex is specified', (done) => { + ctx.options.targets[0].textFilter = "Linux (.*)"; + ctx.ds.query(ctx.options).then(result => { + let tableData = result.data[0]; + expect(tableData.rows).toEqual([ + ['Zabbix server', 'System information', 'system.uname', 'last'] + ]); + done(); + }); + }); + }); + describe('When replacing template variables', () => { function testReplacingVariable(target, varValue, expectedResult, done) { diff --git a/src/test-setup/jest-setup.js b/src/test-setup/jest-setup.js index d4202c4..cb031a4 100644 --- a/src/test-setup/jest-setup.js +++ b/src/test-setup/jest-setup.js @@ -38,7 +38,21 @@ jest.mock('app/core/utils/datemath', () => { }, {virtual: true}); jest.mock('app/core/table_model', () => { - return {}; + return class TableModel { + constructor() { + this.columns = []; + this.columnMap = {}; + this.rows = []; + this.type = 'table'; + } + + addColumn(col) { + if (!this.columnMap[col.text]) { + this.columns.push(col); + this.columnMap[col.text] = col; + } + } + }; }, {virtual: true}); jest.mock('jquery', () => 'module not found', {virtual: true});