1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
|
/*
** Zabbix
** Copyright (C) 2001-2021 Zabbix SIA
**
** This program 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 2 of the License, or
** (at your option) any later version.
**
** This program 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 this program; if not, write to the Free Software
** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
**/
#include "common.h"
#include "zbxmocktest.h"
#include "zbxmockdata.h"
#include "zbxmockassert.h"
#include "zbxmockutil.h"
#include "valuecache.h"
#include "zbxserver.h"
#include "mocks/valuecache/valuecache_mock.h"
#include "../../../src/libs/zbxserver/anomalystl.h"
int __wrap_substitute_simple_macros(zbx_uint64_t *actionid, const DB_EVENT *event, const DB_EVENT *r_event,
zbx_uint64_t *userid, const zbx_uint64_t *hostid, const DC_HOST *dc_host, const DC_ITEM *dc_item,
DB_ALERT *alert, const DB_ACKNOWLEDGE *ack, const zbx_service_alarm_t *service_alarm,
const DB_SERVICE *service, const char *tz, char **data, int macro_type, char *error,
int maxerrlen);
int __wrap_DCget_data_expected_from(zbx_uint64_t itemid, int *seconds);
int __wrap_substitute_simple_macros(zbx_uint64_t *actionid, const DB_EVENT *event, const DB_EVENT *r_event,
zbx_uint64_t *userid, const zbx_uint64_t *hostid, const DC_HOST *dc_host, const DC_ITEM *dc_item,
DB_ALERT *alert, const DB_ACKNOWLEDGE *ack, const zbx_service_alarm_t *service_alarm,
const DB_SERVICE *service, const char *tz, char **data, int macro_type, char *error,
int maxerrlen)
{
ZBX_UNUSED(actionid);
ZBX_UNUSED(event);
ZBX_UNUSED(r_event);
ZBX_UNUSED(userid);
ZBX_UNUSED(hostid);
ZBX_UNUSED(dc_host);
ZBX_UNUSED(dc_item);
ZBX_UNUSED(alert);
ZBX_UNUSED(ack);
ZBX_UNUSED(tz);
ZBX_UNUSED(data);
ZBX_UNUSED(macro_type);
ZBX_UNUSED(error);
ZBX_UNUSED(maxerrlen);
ZBX_UNUSED(service_alarm);
ZBX_UNUSED(service);
return SUCCEED;
}
int __wrap_DCget_data_expected_from(zbx_uint64_t itemid, int *seconds)
{
ZBX_UNUSED(itemid);
*seconds = zbx_vcmock_get_ts().sec - 600;
return SUCCEED;
}
static void zbx_vcmock_history_dump(unsigned char value_type, const zbx_vector_history_record_t *values)
{
int i;
char buffer[256];
for (i = 0; i < values->values_num; i++)
{
const zbx_history_record_t *rec = &values->values[i];
zbx_timespec_to_strtime(&rec->timestamp, buffer, sizeof(buffer));
printf(" - %s\n", buffer);
zbx_history_value2str(buffer, sizeof(buffer), &rec->value, value_type);
printf(" %s\n", buffer);
}
}
static void read_values_stl(zbx_mock_handle_t hdata, unsigned char value_type, zbx_vector_history_record_t *values)
{
zbx_mock_error_t err;
zbx_mock_handle_t hvalue;
zbx_history_record_t rec;
const char *data;
ZBX_UNUSED(value_type);
while (ZBX_MOCK_END_OF_VECTOR != (err = (zbx_mock_vector_element(hdata, &hvalue))))
{
rec.value.dbl = atof(zbx_mock_get_object_member_string(hvalue, "value"));
data = zbx_mock_get_object_member_string(hvalue, "ts");
if (ZBX_MOCK_SUCCESS != (err = zbx_strtime_to_timespec(data, &rec.timestamp)))
fail_msg("Invalid value timestamp \"%s\": %s", data, zbx_mock_error_string(err));
zbx_vector_history_record_append_ptr(values, &rec);
}
}
static void check_records_stl(const char *prefix, unsigned char value_type,
const zbx_vector_history_record_t *expected_values, const zbx_vector_history_record_t *returned_values)
{
int i;
const zbx_history_record_t *expected, *returned;
printf("Expected %s:\n", prefix);
zbx_vcmock_history_dump(value_type, expected_values);
printf("Returned %s:\n", prefix);
zbx_vcmock_history_dump(value_type, returned_values);
zbx_mock_assert_int_eq(prefix, expected_values->values_num, returned_values->values_num);
for (i = 0; i < expected_values->values_num; i++)
{
expected = &expected_values->values[i];
returned = &returned_values->values[i];
zbx_mock_assert_timespec_eq(prefix, &expected->timestamp, &returned->timestamp);
zbx_mock_assert_double_eq(prefix, expected->value.dbl, returned->value.dbl);
}
}
void zbx_mock_test_entry(void **state)
{
int err, expected_ret, returned_ret, nvalues = 0;
zbx_uint64_t s_window, season, seconds = 0;
char *error = NULL;
DC_ITEM item;
zbx_vcmock_ds_item_t *ds_item;
zbx_timespec_t ts;
zbx_mock_handle_t handle;
zbx_vector_history_record_t values_in, trend_values_received, trend_values_expected,
seasonal_values_received, seasonal_values_expected, remainder_values_received,
remainder_values_expected;
/*ZBX_DOUBLE_EPSILON = 0.000001; results into output that is different from python test case output */
ZBX_DOUBLE_EPSILON = 0.0001;
zbx_history_record_vector_create(&values_in);
zbx_history_record_vector_create(&trend_values_received);
zbx_history_record_vector_create(&trend_values_expected);
zbx_history_record_vector_create(&seasonal_values_received);
zbx_history_record_vector_create(&seasonal_values_expected);
zbx_history_record_vector_create(&remainder_values_received);
zbx_history_record_vector_create(&remainder_values_expected);
err = zbx_vc_init(&error);
zbx_mock_assert_result_eq("Value cache initialization failed", SUCCEED, err);
zbx_vc_enable();
zbx_vcmock_ds_init();
memset(&item, 0, sizeof(DC_ITEM));
ds_item = zbx_vcmock_ds_first_item();
item.itemid = ds_item->itemid;
item.value_type = ds_item->value_type;
season = zbx_mock_get_parameter_uint64("in.season");
s_window = zbx_mock_get_parameter_uint64("in.s_window");
seconds = zbx_mock_get_parameter_uint64("in.seconds");
handle = zbx_mock_get_parameter_handle("in");
zbx_vcmock_set_time(handle, "time");
ts = zbx_vcmock_get_ts();
if (FAIL == zbx_vc_get_values(item.itemid, item.value_type, &values_in, (int)seconds, nvalues, &ts))
{
error = zbx_strdup(error, "cannot get values from value cache");
printf("%s\n", error);
zbx_free(error);
goto out;
}
if (SUCCEED != (returned_ret = zbx_STL(&values_in, (int)season, ROBUST_DEF, (int)s_window, S_DEGREE_DEF,
T_WINDOW_DEF, T_DEGREE_DEF, L_WINDOW_DEF, L_DEGREE_DEF, S_JUMP_DEF, T_JUMP_DEF, L_JUMP_DEF,
INNER_DEF, OUTER_DEF, &trend_values_received, &seasonal_values_received,
&remainder_values_received, &error)))
{
printf("zbx_STL returned error: %s\n", error);
zbx_free(error);
}
expected_ret = zbx_mock_str_to_return_code(zbx_mock_get_parameter_string("out.return"));
zbx_mock_assert_result_eq("return value", expected_ret, returned_ret);
zbx_vc_flush_stats();
if (SUCCEED == expected_ret)
{
read_values_stl(zbx_mock_get_parameter_handle("out.trend"), item.value_type, &trend_values_expected);
read_values_stl(zbx_mock_get_parameter_handle("out.seasonal"), item.value_type,
&seasonal_values_expected);
read_values_stl(zbx_mock_get_parameter_handle("out.remainder"), item.value_type,
&remainder_values_expected);
check_records_stl("trend values", item.value_type, &trend_values_expected, &trend_values_received);
check_records_stl("seasonal values", item.value_type, &seasonal_values_expected,
&seasonal_values_received);
check_records_stl("remainder values", item.value_type, &remainder_values_expected,
&remainder_values_received);
}
out:
zbx_history_record_vector_destroy(&trend_values_received, ITEM_VALUE_TYPE_FLOAT);
zbx_history_record_vector_destroy(&trend_values_expected, ITEM_VALUE_TYPE_FLOAT);
zbx_history_record_vector_destroy(&seasonal_values_received, ITEM_VALUE_TYPE_FLOAT);
zbx_history_record_vector_destroy(&seasonal_values_expected, ITEM_VALUE_TYPE_FLOAT);
zbx_history_record_vector_destroy(&remainder_values_received, ITEM_VALUE_TYPE_FLOAT);
zbx_history_record_vector_destroy(&remainder_values_expected, ITEM_VALUE_TYPE_FLOAT);
zbx_history_record_vector_destroy(&values_in, item.value_type);
zbx_vcmock_ds_destroy();
ZBX_UNUSED(state);
}
|