Implement InfluxDB query test

This commit is contained in:
emanuele-f 2018-11-09 10:16:06 +01:00
parent ff34f47241
commit 12e2e500b1
7 changed files with 178 additions and 49 deletions

View file

@ -3,26 +3,9 @@
--
local influxdb = require("influxdb")
local test_utils = require("test_utils")
local influx2Series = influxdb._influx2Series
local function makeTimeStamp(series, tstart, tstep)
local v = {}
for idx, serie in ipairs(series) do
local data = {}
t = tstart
for i, pt in ipairs(serie.data) do
data[i] = {t, pt}
t = t + tstep
end
v[idx] = data
end
return v
end
-- Reproduces 12c8fc315654c1a0e7bf82f089ee47d45a98fc07 - Fix occasional series ponts differences in InfluxDB
local function test_sampling1(test)
local schema = {
@ -326,7 +309,7 @@ function test_datafill3(test)
local time_step = 60 -- 60x sampling
local tstart = 1534254780; tend = 1534256640
local data1_series, data1_count = influx2Series(schema, tstart, tend, tags, options, data1.series[1], time_step)
local with_tstamp = makeTimeStamp(data1_series, tstart, time_step)[1]
local with_tstamp = test_utils.makeTimeStamp(data1_series, tstart, time_step)[1]
local last_val = with_tstamp[#with_tstamp - 1]
local last_expected_val = data1.series[1].values[#data1.series[1].values]
@ -381,7 +364,7 @@ function test_no_derivative1(test)
local time_step = 60 -- no sampling
local tstart = 1534492620; tend = 1534493220
local data1_series, data1_count = influx2Series(schema, tstart+time_step, tend, tags, options, data1.series[1], time_step)
local with_tstamp = makeTimeStamp(data1_series, tstart+time_step, time_step)[1]
local with_tstamp = test_utils.makeTimeStamp(data1_series, tstart+time_step, time_step)[1]
local first_expected_val = data1.series[1].values[1]
for _, pt in pairs(with_tstamp) do
@ -439,7 +422,7 @@ function test_skip_initial1(test)
local time_step = 60 -- no sampling
local tstart = 1534502340; tend = 1534493220
local data1_series, data1_count = influx2Series(schema, tstart, tend, tags, options, data1.series[1], time_step)
local with_tstamp = makeTimeStamp(data1_series, tstart, tstart)[1]
local with_tstamp = test_utils.makeTimeStamp(data1_series, tstart, tstart)[1]
local first_expected_val = data1.series[1].values[2]
for _, pt in ipairs(with_tstamp) do

View file

@ -0,0 +1,84 @@
--
-- (C) 2018 - ntop.org
--
local ts_utils = require("ts_utils")
local test_utils = require("test_utils")
local influxdb = ts_utils.getQueryDriver()
-- use relative times to avoid retention policy issues
local now = os.time()
local f = nil
local schema = ts_utils.newSchema("test:test", {step=1})
schema:addTag("t")
schema:addMetric("v")
local function api_string(schema, tags, metrics, tstamp)
return string.format("%s,%s %s %s000000000\n", schema, table.tconcat(tags, "=", ","), table.tconcat(metrics, "=", ","), tostring(tstamp))
end
local function insert(metrics, tstamp)
f:write(api_string("test:test", {t="test"}, metrics, tstamp))
end
local function init_test(test, points)
local fname = "/tmp/test_influx_ts"
f = io.open(fname, "w")
if not influxdb:delete("test") then
return test:fail("influxdb:delete failed")
end
-- Insert values
for _, point in ipairs(points) do
insert(point[1], point[2])
end
-- Write data
f:close()
if influxdb:_exportTsFile(fname) == nil then
return test:fail("influxdb:_exportTsFile failed")
end
return true
end
function test_simple_derivative(test)
local test_data = {
{{v=1000}, now},
{{v=2000}, now+1},
}
if not init_test(test, test_data) then
return false
end
local options = ts_utils.getQueryOptions()
local res = influxdb:query(schema, now-20, now+20, {t="test"}, options)
if table.empty(res) then
return test:assertion_failed("not table.empty(res)")
end
local rv = test_utils.timestampAsKey(test_utils.makeTimeStamp(res.series, res.start, schema.options.step))[1]
local t2 = test_data[2][2]
if not(rv[t2] == 1000) then
return test:assertion_failed("rv[t2=".. t2 .."] == 1000")
end
return test:success()
end
function run(tester)
local rv = tester.run_test("influx_query:test_simple_derivative", test_simple_derivative)
return rv
end
return {
run = run
}

View file

@ -10,8 +10,11 @@ package.path = dirs.installdir .. "/scripts/lua/modules/timeseries/tests/?.lua;"
-- ##############################################
local utils_test = require("utils_test")
local influxdb_test = require("influxdb_test")
local tests = {
require("utils_test"),
require("influxdb2series"),
require("influxdb_queries"),
}
-- ##############################################
@ -33,6 +36,11 @@ function test:success()
return true
end
function test:fail(message)
print(self.name .. " FAILED: ".. message .."<br>")
return false
end
function test:assertion_failed(assertion)
print(self.name .. " ASSERTION FAILED: ".. assertion .."<br>")
return false
@ -53,5 +61,6 @@ local tester = {
sendHTTPContentTypeHeader('text/html')
utils_test.run(tester)
influxdb_test.run(tester)
for _, test in ipairs(tests) do
test.run(tester)
end

View file

@ -0,0 +1,39 @@
--
-- (C) 2018 - ntop.org
--
local test_utils = {}
function test_utils.makeTimeStamp(series, tstart, tstep)
local v = {}
for idx, serie in ipairs(series) do
local data = {}
t = tstart
for i, pt in ipairs(serie.data) do
data[i] = {t, pt}
t = t + tstep
end
v[idx] = data
end
return v
end
function test_utils.timestampAsKey(tstamped_series)
local rv = {}
for idx, serie in pairs(tstamped_series) do
rv[idx] = {}
for _, val in ipairs(serie) do
rv[idx][val[1]] = val[2]
end
end
return rv
end
return test_utils