Welcome to mirror list, hosted at ThFree Co, Russian Federation.

mock_service.c « service « zabbix_server « tests - github.com/zabbix/zabbix.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: d350e69a8a1310edec238c43d194335162a4f779 (plain)
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
/*
** Zabbix
** Copyright (C) 2001-2022 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 "zbxmocktest.h"
#include "zbxmockdata.h"
#include "zbxmockassert.h"
#include "zbxmockutil.h"
#include "zbxalgo.h"
#include "zbxself.h"

#include "mock_service.h"

zbx_uint64_t __wrap_DCget_nextid(const char *table_name, int num);
void	*__wrap_zbx_add_event(unsigned char source, unsigned char object, zbx_uint64_t objectid,
		const zbx_timespec_t *timespec, int value, const char *trigger_description,
		const char *trigger_expression, const char *trigger_recovery_expression, unsigned char trigger_priority,
		unsigned char trigger_type, const zbx_vector_ptr_t *trigger_tags,
		unsigned char trigger_correlation_mode, const char *trigger_correlation_tag,
		unsigned char trigger_value, const char *trigger_opdata, const char *event_name, const char *error);
int	__wrap_zbx_process_events(zbx_vector_ptr_t *trigger_diff, zbx_vector_uint64_t *triggerids_lock);
void	__wrap_zbx_clean_events(void);
int	__wrap_zbx_interface_availability_is_set(const void *ia);


/* stubs to satisfy hard link dependenceies */

int	get_process_info_by_thread(int local_server_num, unsigned char *local_process_type, int *local_process_num);

int	CONFIG_SERVICEMAN_SYNC_FREQUENCY = 0;

pid_t	*threads;
int	threads_num;

void	update_selfmon_counter(unsigned char state)
{
	ZBX_UNUSED(state);
}

int	get_process_info_by_thread(int local_server_num, unsigned char *local_process_type, int *local_process_num)
{
	ZBX_UNUSED(local_server_num);
	ZBX_UNUSED(local_process_type);
	ZBX_UNUSED(local_process_num);
	return 0;
}

int	MAIN_ZABBIX_ENTRY(int flags)
{
	ZBX_UNUSED(flags);
	return 0;
}

/* service tree mock */

typedef struct
{
	zbx_hashset_t	services;
}
zbx_mock_service_cache_t;

static zbx_mock_service_cache_t	cache;

static zbx_hash_t	service_hash_func(const void *d)
{
	const zbx_service_t	*s = (const zbx_service_t *)d;

	return ZBX_DEFAULT_STRING_HASH_FUNC(s->name);
}

static int	service_compare_func(const void *d1, const void *d2)
{
	const zbx_service_t	*s1 = (const zbx_service_t *)d1;
	const zbx_service_t	*s2 = (const zbx_service_t *)d2;

	return strcmp(s1->name, s2->name);
}

zbx_service_t	*mock_get_service(const char *name)
{
	zbx_service_t	service_local;

	service_local.name = (char *)name;
	return zbx_hashset_search(&cache.services, &service_local);
}

void	mock_init_service_cache(const char *path)
{
	zbx_mock_handle_t	hservices, hservice, hchildren, hparents, hname, hevents, hevent, halgo, hweight, hprop,
				hrules, hrule;
	int			service_num = 0;
	zbx_mock_error_t	err;
	zbx_service_t		*service, service_local, *child, *parent;
	const char		*value;
	zbx_hashset_iter_t	iter;

	zbx_hashset_create(&cache.services, 100, service_hash_func, service_compare_func);

	/* load service objects in cache */

	hservices = zbx_mock_get_parameter_handle(path);
	while (ZBX_MOCK_END_OF_VECTOR != (err = (zbx_mock_vector_element(hservices, &hservice))))
	{
		if (ZBX_MOCK_SUCCESS != err)
			fail_msg("cannot read service #%d", service_num);

		memset(&service_local, 0, sizeof(zbx_service_t));
		service_local.name = zbx_strdup(NULL, zbx_mock_get_object_member_string(hservice, "name"));
		service = (zbx_service_t *)zbx_hashset_insert(&cache.services, &service_local, sizeof(service_local));

		zbx_vector_ptr_create(&service->children);
		zbx_vector_ptr_create(&service->parents);
		zbx_vector_ptr_create(&service->service_problem_tags);
		zbx_vector_ptr_create(&service->service_problems);
		zbx_vector_ptr_create(&service->status_rules);
		zbx_vector_ptr_create(&service->tags);

		service->status = zbx_mock_get_object_member_int(hservice, "status");

		if (ZBX_MOCK_SUCCESS == zbx_mock_object_member(hservice, "algorithm", &halgo))
		{
			if (ZBX_MOCK_SUCCESS != zbx_mock_string(halgo, &value))
				fail_msg("cannot read service '%s' algorithm", service->name);

			if (0 == strcmp(value, "MIN"))
				service->algorithm = ZBX_SERVICE_STATUS_CALC_MOST_CRITICAL_ONE;
			else if (0 == strcmp(value, "MAX"))
				service->algorithm = ZBX_SERVICE_STATUS_CALC_MOST_CRITICAL_ALL;
			else if (0 == strcmp(value, "OK"))
				service->algorithm = ZBX_SERVICE_STATUS_CALC_SET_OK;
			else
				fail_msg("unknown service '%s' algorithm '%s'", service->name, value);
		}
		else
			service->algorithm = ZBX_SERVICE_STATUS_CALC_MOST_CRITICAL_ONE;

		if (ZBX_MOCK_SUCCESS == zbx_mock_object_member(hservice, "weight", &hweight))
		{
			if (ZBX_MOCK_SUCCESS != zbx_mock_int(hweight, &service->weight))
				fail_msg("cannot read service '%s' weight", service->name);
		}

		if (ZBX_MOCK_SUCCESS == zbx_mock_object_member(hservice, "propagation", &hprop))
		{
			value = zbx_mock_get_object_member_string(hprop, "action");

			if (0 == strcmp(value, "SET"))
			{
				service->propagation_rule = ZBX_SERVICE_STATUS_PROPAGATION_FIXED;
				service->propagation_value = zbx_mock_get_object_member_int(hprop, "value");
			}
			else if (0 == strcmp(value, "KEEP"))
			{
				service->propagation_rule = ZBX_SERVICE_STATUS_PROPAGATION_AS_IS;
			}
			else if (0 == strcmp(value, "INCREASE"))
			{
				service->propagation_rule = ZBX_SERVICE_STATUS_PROPAGATION_INCREASE;
				service->propagation_value = zbx_mock_get_object_member_int(hprop, "value");
			}
			else if (0 == strcmp(value, "DECREASE"))
			{
				service->propagation_rule = ZBX_SERVICE_STATUS_PROPAGATION_DECREASE;
				service->propagation_value = zbx_mock_get_object_member_int(hprop, "value");
			}
			else if (0 == strcmp(value, "IGNORE"))
				service->propagation_rule = ZBX_SERVICE_STATUS_PROPAGATION_IGNORE;
			else
				fail_msg("unknown service '%s' propagation action '%s'", service->name, value);
		}

		if (ZBX_MOCK_SUCCESS == zbx_mock_object_member(hservice, "rules", &hrules))
		{
			zbx_service_rule_t	*rule;

			while (ZBX_MOCK_END_OF_VECTOR != (err = (zbx_mock_vector_element(hrules, &hrule))))
			{
				if (ZBX_MOCK_SUCCESS != err)
					fail_msg("cannot read service '%s' status rules", service->name);

				rule = (zbx_service_rule_t *)zbx_malloc(NULL, sizeof(zbx_service_rule_t));
				memset(rule, 0, sizeof(zbx_service_rule_t));
				value = zbx_mock_get_object_member_string(hrule, "type");
				if (0 == strcmp(value, "N_GE"))
					rule->type = ZBX_SERVICE_STATUS_RULE_TYPE_N_GE;
				else if (0 == strcmp(value, "NP_GE"))
					rule->type = ZBX_SERVICE_STATUS_RULE_TYPE_NP_GE;
				else if (0 == strcmp(value, "N_LT"))
					rule->type = ZBX_SERVICE_STATUS_RULE_TYPE_N_L;
				else if (0 == strcmp(value, "NP_LT"))
					rule->type = ZBX_SERVICE_STATUS_RULE_TYPE_NP_L;
				else if (0 == strcmp(value, "W_GE"))
					rule->type = ZBX_SERVICE_STATUS_RULE_TYPE_W_GE;
				else if (0 == strcmp(value, "WP_GE"))
					rule->type = ZBX_SERVICE_STATUS_RULE_TYPE_WP_GE;
				else if (0 == strcmp(value, "W_LT"))
					rule->type = ZBX_SERVICE_STATUS_RULE_TYPE_W_L;
				else if (0 == strcmp(value, "WP_LT"))
					rule->type = ZBX_SERVICE_STATUS_RULE_TYPE_WP_L;
				else
					fail_msg("unsupported service '%s' rule type '%s'", service->name, value);
				rule->limit_status = zbx_mock_get_object_member_int(hrule, "limit");
				rule->limit_value = zbx_mock_get_object_member_int(hrule, "value");
				rule->new_status = zbx_mock_get_object_member_int(hrule, "status");
				zbx_vector_ptr_append(&service->status_rules, rule);
			}
		}

		if (ZBX_MOCK_SUCCESS == zbx_mock_object_member(hservice, "events", &hevents))
		{
			zbx_service_problem_t	*problem;

			while (ZBX_MOCK_END_OF_VECTOR != (err = (zbx_mock_vector_element(hevents, &hevent))))
			{
				if (ZBX_MOCK_SUCCESS != err)
					fail_msg("cannot read service '%s' events", service->name);

				problem = (zbx_service_problem_t *)zbx_malloc(NULL, sizeof(zbx_service_problem_t));
				memset(problem, 0, sizeof(zbx_service_problem_t));
				problem->eventid = zbx_mock_get_object_member_uint64(hevent, "id");
				problem->severity = zbx_mock_get_object_member_int(hevent, "severity");
				zbx_vector_ptr_append(&service->service_problems, problem);
			}
		}

		service_num++;
	}

	/* set service relations */

	hservices = zbx_mock_get_parameter_handle(path);
	while (ZBX_MOCK_END_OF_VECTOR != (err = (zbx_mock_vector_element(hservices, &hservice))))
	{
		if (ZBX_MOCK_SUCCESS != err)
			fail_msg("cannot read service");

		if (NULL == (service = mock_get_service(zbx_mock_get_object_member_string(hservice, "name"))))
			fail_msg("failed to cache service '%s'", zbx_mock_get_object_member_string(hservice, "name"));

		if (ZBX_MOCK_SUCCESS == zbx_mock_object_member(hservice, "children", &hchildren))
		{
			while (ZBX_MOCK_END_OF_VECTOR != (err = (zbx_mock_vector_element(hchildren, &hname))))
			{
				if (ZBX_MOCK_SUCCESS != err || ZBX_MOCK_SUCCESS != zbx_mock_string(hname, &value))
					fail_msg("cannot read service '%s' children", service->name);

				if (NULL == (child = mock_get_service(value)))
				{
					fail_msg("cannot set service '%s' child '%s': no such service", service->name,
							value);
				}

				zbx_vector_ptr_append(&service->children, child);
				zbx_vector_ptr_append(&child->parents, service);
			}
		}

		if (ZBX_MOCK_SUCCESS == zbx_mock_object_member(hservice, "parents", &hparents))
		{
			while (ZBX_MOCK_END_OF_VECTOR != (err = (zbx_mock_vector_element(hparents, &hname))))
			{
				if (ZBX_MOCK_SUCCESS != err || ZBX_MOCK_SUCCESS != zbx_mock_string(hname, &value))
					fail_msg("cannot read service '%s' parents", service->name);

				if (NULL == (parent = mock_get_service(value)))
				{
					fail_msg("cannot set service '%s' parent '%s': no such service", service->name,
							value);
				}

				zbx_vector_ptr_append(&service->parents, parent);
				zbx_vector_ptr_append(&parent->children, service);
			}
		}

		service_num++;
	}

	zbx_hashset_iter_reset(&cache.services, &iter);

	/* remove duplicate parent/children references */
	while (NULL != (service = (zbx_service_t *)zbx_hashset_iter_next(&iter)))
	{
		zbx_vector_ptr_sort(&service->parents, ZBX_DEFAULT_PTR_COMPARE_FUNC);
		zbx_vector_ptr_uniq(&service->parents, ZBX_DEFAULT_PTR_COMPARE_FUNC);

		zbx_vector_ptr_sort(&service->children, ZBX_DEFAULT_PTR_COMPARE_FUNC);
		zbx_vector_ptr_uniq(&service->children, ZBX_DEFAULT_PTR_COMPARE_FUNC);
	}
}

void	mock_destroy_service_cache(void)
{
	zbx_hashset_iter_t	iter;
	zbx_service_t		*service;

	zbx_hashset_iter_reset(&cache.services, &iter);

	while (NULL != (service = (zbx_service_t *)zbx_hashset_iter_next(&iter)))
	{
		zbx_vector_ptr_destroy(&service->children);
		zbx_vector_ptr_destroy(&service->parents);
		zbx_vector_ptr_destroy(&service->service_problem_tags);
		zbx_vector_ptr_destroy(&service->tags);
		zbx_vector_ptr_clear_ext(&service->service_problems, zbx_ptr_free);
		zbx_vector_ptr_destroy(&service->service_problems);
		zbx_vector_ptr_clear_ext(&service->status_rules, zbx_ptr_free);
		zbx_vector_ptr_destroy(&service->status_rules);

		zbx_free(service->name);
	}

	zbx_hashset_destroy(&cache.services);
}

/* function stubs to cut off library dependencies */

zbx_uint64_t	__wrap_DCget_nextid(const char *table_name, int num)
{
	ZBX_UNUSED(table_name);
	ZBX_UNUSED(num);

	return 0;
}

void	*__wrap_zbx_add_event(unsigned char source, unsigned char object, zbx_uint64_t objectid,
		const zbx_timespec_t *timespec, int value, const char *trigger_description,
		const char *trigger_expression, const char *trigger_recovery_expression, unsigned char trigger_priority,
		unsigned char trigger_type, const zbx_vector_ptr_t *trigger_tags,
		unsigned char trigger_correlation_mode, const char *trigger_correlation_tag,
		unsigned char trigger_value, const char *trigger_opdata, const char *event_name, const char *error)
{
	ZBX_UNUSED(source);
	ZBX_UNUSED(object);
	ZBX_UNUSED(objectid);
	ZBX_UNUSED(timespec);
	ZBX_UNUSED(value);
	ZBX_UNUSED(trigger_description);
	ZBX_UNUSED(trigger_expression);
	ZBX_UNUSED(trigger_recovery_expression);
	ZBX_UNUSED(trigger_priority);
	ZBX_UNUSED(trigger_type);
	ZBX_UNUSED(trigger_tags);
	ZBX_UNUSED(trigger_correlation_mode);
	ZBX_UNUSED(trigger_correlation_tag);
	ZBX_UNUSED(trigger_value);
	ZBX_UNUSED(trigger_opdata);
	ZBX_UNUSED(event_name);
	ZBX_UNUSED(error);

	return NULL;
}

int	__wrap_zbx_process_events(zbx_vector_ptr_t *trigger_diff, zbx_vector_uint64_t *triggerids_lock)
{
	ZBX_UNUSED(trigger_diff);
	ZBX_UNUSED(triggerids_lock);

	return 0;
}

void	__wrap_zbx_clean_events(void)
{
}

int	__wrap_zbx_interface_availability_is_set(const void *ia)
{
	ZBX_UNUSED(ia);

	return FAIL;
}