core/source/test/test.mocha.ts

168 lines
4.4 KiB
TypeScript
Raw Permalink Normal View History

2024-07-02 15:02:35 +02:00
/*
Copyright 2016-2024 'Christian Fraß, Christian Neubauer, Martin Springwald GbR'
<info@greenscale.de>
»heimdall« is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
»heimdall« is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with »heimdall«. If not, see <http://www.gnu.org/licenses/>.
*/
2023-08-03 08:34:33 +02:00
const nm_assert = require("assert");
2023-08-31 16:00:36 +02:00
const nm_fs = require("fs");
2023-08-03 08:34:33 +02:00
declare var describe;
declare var it;
_heimdall.init();
describe(
"heimdall",
() => {
function datetimestring_to_timestamp(datetimestring) {
return Math.floor((new Date(datetimestring)).getTime() / 1000);
};
2023-08-04 09:08:01 +02:00
function resolve_condition(condition_raw) {
return {
"unknown": _heimdall.enum_condition.unknown,
"ok": _heimdall.enum_condition.ok,
"concerning": _heimdall.enum_condition.concerning,
"critical": _heimdall.enum_condition.critical,
}[condition_raw];
};
2023-08-31 16:00:36 +02:00
const data = JSON.parse(nm_fs.readFileSync("./test-data.json"));
2023-08-03 08:34:33 +02:00
describe(
"master.determine_due",
() => {
2023-08-04 09:08:01 +02:00
data.cases.forEach(
2023-08-03 08:34:33 +02:00
case_ => {
it(
case_.name,
() => {
2023-08-04 09:08:01 +02:00
// setup
const check : _heimdall.type_check = Object.assign(
data.parameters["check"],
{
2023-08-31 19:15:40 +02:00
"annoy": false,
2023-08-04 09:08:01 +02:00
}
);
const old_item_state : _heimdall.type_item_state = {
"timestamp": datetimestring_to_timestamp(data.parameters["old_item_state"]["timestamp"]),
"condition": resolve_condition(case_.input["condition"]),
2023-08-31 16:00:36 +02:00
"count": (case_.input["threshold_reached"] ? null : 0),
2023-08-04 09:08:01 +02:00
"last_notification_timestamp": datetimestring_to_timestamp(data.parameters["old_item_state"]["last_notification_timestamp"]),
};
const timestamp : int = (
datetimestring_to_timestamp(
data.parameters["old_item_state"]["timestamp"]
)
+
(
{
"none": 0,
"attentive": data.parameters["check"]["schedule"]["attentive_interval"],
"regular": data.parameters["check"]["schedule"]["regular_interval"],
"reminding": data.parameters["check"]["schedule"]["reminding_interval"]
}[case_.input["passed"]]
)
+
60
);
2023-08-03 08:34:33 +02:00
// execution
const result : boolean = _heimdall.master.determine_due(
2023-08-04 09:08:01 +02:00
check,
old_item_state,
2023-08-03 08:34:33 +02:00
{
2023-08-04 09:08:01 +02:00
"timestamp": timestamp,
2023-08-03 08:34:33 +02:00
}
);
// assertions
nm_assert.equal(result, case_.output);
}
);
}
);
}
);
2023-08-31 19:15:40 +02:00
/*
describe(
"master.determine_shall_send_notification",
() => {
it(
"test",
() => {
// setup
const check : _heimdall.type_check = {
"name": "iks.web.2",
"title": "web.2",
"kind": "http_request",
"parameters": {
"request": {
"method": "GET",
"target": "https://iks-sachsen.de"
},
"timeout": 5,
"follow_redirects": true,
"response": {
"status_code": 200
},
"critical": true
},
"custom": null,
"active": false,
"threshold": 3,
"annoy": false,
"schedule": {
"regular_interval": 60,
"attentive_interval": 10,
"reminding_interval": 86400
},
"notifications": [
{
"kind": "console",
"parameters": {
}
}
]
};
const old_item_state : _heimdall.type_item_state = {
"timestamp": 1693493942,
"condition": _heimdall.enum_condition.critical,
"count": 2,
"last_notification_timestamp": 1693493942,
};
const timestamp : int = 1693495622;
// execution
const result : boolean = _heimdall.master.determine_shall_send_notification(
check,
false,
3,
timestamp,
old_item_state,
{
"condition": _heimdall.enum_condition.critical,
"info": {},
}
);
// assertions
nm_assert.equal(result, false);
}
);
}
);
*/
2023-08-03 08:34:33 +02:00
}
);