JSON-Decoder: Fix tests

This commit is contained in:
Noah Hilverling 2019-05-14 13:41:38 +02:00
parent d9b656c780
commit 6d02fbba2d

View file

@ -1,94 +1,64 @@
package jsondecoder
import (
"git.icinga.com/icingadb/icingadb-main/configobject"
"git.icinga.com/icingadb/icingadb-main/configobject/host"
"github.com/stretchr/testify/assert"
"testing"
)
func Test_decodeString(t *testing.T) {
var testCorrect = "{\"Integer\": 2.0, \"String\": \"Test One Two Three\"}"
/*var testCorrect = "{\"Integer\": 2.0, \"String\": \"Test One Two Three\"}"
var testBroken = "{ahahahahaha}"
ret, err := decodeString(testCorrect)
err := decodeString(testCorrect)
assert.NoError(t, err)
assert.Equal(t, 2.0, ret["Integer"])
assert.Equal(t, "Test One Two Three", ret["String"])
_, err = decodeString(testBroken)
assert.Error(t, err)
assert.Error(t, err)*/
}
func Test_DecodePool(t *testing.T) {
var chInput = make(chan JsonDecodePackage)
var chOutput = make(chan JsonDecodePackage)
var chInput = make(chan *JsonDecodePackages)
var chOutput = make(chan []configobject.Row)
var chError = make(chan error)
var TestPackageA = JsonDecodePackage{
"{\"action_url_id\":\"761ff24e252d57581a7de5d9f417f717fb3c2d7f\",\"checkcommand_id\":\"f5e3b3b22741f40c74326fbcc79d9c331d8fa4ee\",\"customvars_checksum\":\"e9fea9581588f18cfb46969268a94166bd0474ae\",\"environment_id\":\"90a8834de76326869f3e703cd61513081ad73d3c\",\"group_ids\":[\"a63234de9f608c4a4f86053870d79610ec58b258\"],\"groups_checksum\":\"9878a753d010eb1bbde57bb78727a6e6ba26aa51\",\"host_id\":\"330c09556cbb5e01c180343bb669a2d36b48dd2c\",\"name_checksum\":\"9f75a6ea3ea6f1692538c865133a8a08e48f06d5\",\"notes_url_id\":\"31bb5f9a69c659270e2bcd257b77353669c04d1e\",\"properties_checksum\":\"caff92fafc9a17097304f6c2fb9fa029d3ec8aa8\",\"zone_id\":\"407eaa141abcae8ee554e4fe4b9e9b726bac4b77\"}",
"{\"active_checks_enabled\":false,\"check_interval\":300.0,\"check_retry_interval\":60.0,\"check_timeout\":null,\"checkcommand\":\"dummy\",\"display_name\":\"TestService A - 0.0\",\"event_handler_enabled\":true,\"flapping_enabled\":false,\"flapping_threshold_high\":30.0,\"flapping_threshold_low\":25.0,\"icon_image_alt\":\"\",\"is_volatile\":false,\"max_check_attempts\":3.0,\"name\":\"TestService A - 0.0\",\"notes\":\"\",\"notifications_enabled\":true,\"passive_checks_enabled\":true,\"perfdata_enabled\":true,\"zone\":\"double\"}",
"3a18e07f776af383cd7355b89eefd1fdf0fd47a9",
"{\"checkcommand_id\":\"0bba6ab6747f1c0de3bf80932d10bc7b603e27fc\",\"customvars_checksum\":\"5ba93c9db0cff93f52b521d7420e43f6eda2784f\",\"environment_id\":\"90a8834de76326869f3e703cd61513081ad73d3c\",\"group_ids\":[],\"groups_checksum\":\"a0930d202ae77bbafbc4d898e3d060f462160904\",\"name_checksum\":\"21021feda571e19d8afb53fb11ca089db7578cee\",\"properties_checksum\":\"8d515de29444d9df3e374bcc1b890040fcc48be5\"}",
"{\"active_checks_enabled\":true,\"address\":\"\",\"address6\":\"\",\"check_interval\":60.0,\"check_retry_interval\":60.0,\"check_timeout\":null,\"checkcommand\":\"random\",\"display_name\":\"aa3derphosta469\",\"event_handler_enabled\":true,\"flapping_enabled\":false,\"flapping_threshold_high\":30.0,\"flapping_threshold_low\":25.0,\"icon_image_alt\":\"\",\"is_volatile\":false,\"max_check_attempts\":3.0,\"name\":\"aa3derphosta469\",\"notes\":\"\",\"notifications_enabled\":true,\"passive_checks_enabled\":true,\"perfdata_enabled\":true}",
nil,
nil,
&chOutput,
host.NewHost,
"host",
}
var TestPackageB = JsonDecodePackage{
"{\"checkcommand_id\":\"f5e3b3b22741f40c74326fbcc79d9c331d8fa4ee\",\"customvars_checksum\":\"efb9e8a4dff9ee330838909403655ae376251dc9\",\"environment_id\":\"90a8834de76326869f3e703cd61513081ad73d3c\",\"group_ids\":[\"a63234de9f608c4a4f86053870d79610ec58b258\"],\"groups_checksum\":\"9878a753d010eb1bbde57bb78727a6e6ba26aa51\",\"host_id\":\"7bb83f280fee68146e223b51c02c9ac1e5d56305\",\"name_checksum\":\"92420fe84a880f5b7675ba0fb0f4f730f40a144a\",\"properties_checksum\":\"8563b9113161953acabb7bba779cc5706494eb3b\",\"zone_id\":\"407eaa141abcae8ee554e4fe4b9e9b726bac4b77\"}",
"{\"active_checks_enabled\":false,\"check_interval\":300.0,\"check_retry_interval\":60.0,\"check_timeout\":null,\"checkcommand\":\"dummy\",\"display_name\":\"TestService B - 0.0\",\"event_handler_enabled\":true,\"flapping_enabled\":false,\"flapping_threshold_high\":30.0,\"flapping_threshold_low\":25.0,\"icon_image_alt\":\"\",\"is_volatile\":false,\"max_check_attempts\":3.0,\"name\":\"TestService B - 0.0\",\"notes\":\"\",\"notifications_enabled\":true,\"passive_checks_enabled\":true,\"perfdata_enabled\":true,\"zone\":\"double\"}",
"7dd90e9833243afef861f257c78ddee941edfa2f",
"{\"checkcommand_id\":\"0bba6ab6747f1c0de3bf80932d10bc7b603e27fc\",\"customvars_checksum\":\"5ba93c9db0cff93f52b521d7420e43f6eda2784f\",\"environment_id\":\"90a8834de76326869f3e703cd61513081ad73d3c\",\"group_ids\":[],\"groups_checksum\":\"a0930d202ae77bbafbc4d898e3d060f462160904\",\"name_checksum\":\"f14feab0710d05e4ca9ffd712f8c0af5d8f5119a\",\"properties_checksum\":\"bd3e124054427700571dae7552aa90e3fe4f1fde\"}",
"{\"active_checks_enabled\":true,\"address\":\"\",\"address6\":\"\",\"check_interval\":60.0,\"check_retry_interval\":60.0,\"check_timeout\":null,\"checkcommand\":\"random\",\"display_name\":\"aa3derphosta378\",\"event_handler_enabled\":true,\"flapping_enabled\":false,\"flapping_threshold_high\":30.0,\"flapping_threshold_low\":25.0,\"icon_image_alt\":\"\",\"is_volatile\":false,\"max_check_attempts\":3.0,\"name\":\"aa3derphosta378\",\"notes\":\"\",\"notifications_enabled\":true,\"passive_checks_enabled\":true,\"perfdata_enabled\":true}",
nil,
nil,
&chOutput,
host.NewHost,
"host",
}
DecodePool(chInput, chError, 4)
DecodePool(chInput, chError, 2)
pkgs := JsonDecodePackages{
ChBack: chOutput,
}
pkgs.Packages = append(pkgs.Packages, TestPackageA)
pkgs.Packages = append(pkgs.Packages, TestPackageB)
chInput <- &pkgs
result := <-chOutput
assert.NotNil(t, result)
assert.Equal(t, 2, len(result))
chInput <- TestPackageA
chInput <- TestPackageB
close(chInput)
resultA := <-chOutput
resultB := <-chOutput
close(chOutput)
assert.NotNil(t, resultA.ConfigProcessed)
assert.NotNil(t, resultB.ConfigProcessed)
}
func Test_decodePackage(t *testing.T) {
var chInput = make(chan JsonDecodePackage)
var chOutput = make(chan JsonDecodePackage)
var TestPackageA = JsonDecodePackage{
"{\"action_url_id\":\"761ff24e252d57581a7de5d9f417f717fb3c2d7f\",\"checkcommand_id\":\"f5e3b3b22741f40c74326fbcc79d9c331d8fa4ee\",\"customvars_checksum\":\"e9fea9581588f18cfb46969268a94166bd0474ae\",\"environment_id\":\"90a8834de76326869f3e703cd61513081ad73d3c\",\"group_ids\":[\"a63234de9f608c4a4f86053870d79610ec58b258\"],\"groups_checksum\":\"9878a753d010eb1bbde57bb78727a6e6ba26aa51\",\"host_id\":\"330c09556cbb5e01c180343bb669a2d36b48dd2c\",\"name_checksum\":\"9f75a6ea3ea6f1692538c865133a8a08e48f06d5\",\"notes_url_id\":\"31bb5f9a69c659270e2bcd257b77353669c04d1e\",\"properties_checksum\":\"caff92fafc9a17097304f6c2fb9fa029d3ec8aa8\",\"zone_id\":\"407eaa141abcae8ee554e4fe4b9e9b726bac4b77\"}",
"{\"active_checks_enabled\":false,\"check_interval\":300.0,\"check_retry_interval\":60.0,\"check_timeout\":null,\"checkcommand\":\"dummy\",\"display_name\":\"TestService A - 0.0\",\"event_handler_enabled\":true,\"flapping_enabled\":false,\"flapping_threshold_high\":30.0,\"flapping_threshold_low\":25.0,\"icon_image_alt\":\"\",\"is_volatile\":false,\"max_check_attempts\":3.0,\"name\":\"TestService A - 0.0\",\"notes\":\"\",\"notifications_enabled\":true,\"passive_checks_enabled\":true,\"perfdata_enabled\":true,\"zone\":\"double\"}",
nil,
nil,
&chOutput,
}
var TestPackageB = JsonDecodePackage{
"{\"checkcommand_id\":\"f5e3b3b22741f40c74326fbcc79d9c331d8fa4ee\",\"customvars_checksum\":\"efb9e8a4dff9ee330838909403655ae376251dc9\",\"environment_id\":\"90a8834de76326869f3e703cd61513081ad73d3c\",\"group_ids\":[\"a63234de9f608c4a4f86053870d79610ec58b258\"],\"groups_checksum\":\"9878a753d010eb1bbde57bb78727a6e6ba26aa51\",\"host_id\":\"7bb83f280fee68146e223b51c02c9ac1e5d56305\",\"name_checksum\":\"92420fe84a880f5b7675ba0fb0f4f730f40a144a\",\"properties_checksum\":\"8563b9113161953acabb7bba779cc5706494eb3b\",\"zone_id\":\"407eaa141abcae8ee554e4fe4b9e9b726bac4b77\"}",
"{\"active_checks_enabled\":false,\"check_interval\":300.0,\"check_retry_interval\":60.0,\"check_timeout\":null,\"checkcommand\":\"dummy\",\"display_name\":\"TestService B - 0.0\",\"event_handler_enabled\":true,\"flapping_enabled\":false,\"flapping_threshold_high\":30.0,\"flapping_threshold_low\":25.0,\"icon_image_alt\":\"\",\"is_volatile\":false,\"max_check_attempts\":3.0,\"name\":\"TestService B - 0.0\",\"notes\":\"\",\"notifications_enabled\":true,\"passive_checks_enabled\":true,\"perfdata_enabled\":true,\"zone\":\"double\"}",
nil,
nil,
&chOutput,
}
go func() {
err := decodePackage(chInput)
assert.NoError(t, err)
}()
go func() {
resultA := <-chOutput
resultB := <-chOutput
assert.NotNil(t, resultA.ConfigProcessed)
assert.NotNil(t, resultB.ConfigProcessed)
}()
chInput <- TestPackageA
chInput <- TestPackageB
close(chInput)
close(chOutput)
}