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

um_cache_sync.c « zbxdbcache « libs « tests - github.com/zabbix/zabbix.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 54bdec14753513958e99ccb1b85921834d984e44 (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
/*
** 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 "zbxcommon.h"
#include "zbxdbcache/user_macro.h"
#include "zbxalgo.h"
#include "um_cache_mock.h"

typedef struct
{
	zbx_uint32_t		refs;
	zbx_um_mock_cache_t	mock_cache;
	zbx_um_cache_t		*cache;
}
zbx_mock_step_t;

ZBX_PTR_VECTOR_DECL(mock_step, zbx_mock_step_t *)
ZBX_PTR_VECTOR_IMPL(mock_step, zbx_mock_step_t *)

static void	mock_step_free(zbx_mock_step_t *step)
{
	zbx_uint32_t	i;

	um_mock_cache_clear(&step->mock_cache);

	for (i = 0; i < step->refs; i++)
		um_cache_release(step->cache);

	zbx_free(step);
}

static void	dbsync_check_empty(const char *entity, zbx_dbsync_t *sync)
{
	unsigned char	tag;
	zbx_uint64_t	rowid;
	char		**row;
	char		*errors[] = {"unknown", "added", "updated", "removed"};

	if (SUCCEED == zbx_dbsync_next(sync, &rowid, &row, &tag))
	{
		char	*msg = NULL;
		size_t	msg_alloc = 0, msg_offset = 0;
		int	i;

		for (i = 0; i < sync->columns_num; i++)
			zbx_snprintf_alloc(&msg, &msg_alloc, &msg_offset, "'%s',", row[i]);

		fail_msg("unexpected %s change detected: %s row: %s", entity, errors[tag], msg);
	}
}

static void	mock_step_validate(zbx_mock_step_t *step)
{
	zbx_um_mock_cache_t	mock_cache;
	zbx_dbsync_t		gmacros, hmacros, htmpls;

	um_mock_cache_init_from_config(&mock_cache, step->cache);

	zbx_dbsync_init(&gmacros, ZBX_DBSYNC_UPDATE);
	zbx_dbsync_init(&hmacros, ZBX_DBSYNC_UPDATE);
	zbx_dbsync_init(&htmpls, ZBX_DBSYNC_UPDATE);

	printf("MOCK:\n");
	um_mock_cache_dump(&step->mock_cache);
	printf("CONFIG:\n");
	um_mock_cache_dump(&mock_cache);

	um_mock_cache_diff(&step->mock_cache, &mock_cache, &gmacros, &hmacros, &htmpls);

	dbsync_check_empty("global macro", &gmacros);
	dbsync_check_empty("host macro", &hmacros);
	dbsync_check_empty("host template", &htmpls);

	mock_dbsync_clear(&gmacros);
	mock_dbsync_clear(&hmacros);
	mock_dbsync_clear(&htmpls);

	um_mock_cache_clear(&mock_cache);
}

void	zbx_mock_test_entry(void **state)
{
	zbx_um_mock_cache_t	mock_cache0, *mock_cache = &mock_cache0;
	zbx_um_cache_t		*umc;
	zbx_vector_mock_step_t	steps;
	zbx_mock_step_t		*step;
	zbx_mock_handle_t	hsteps, hstep;
	zbx_mock_error_t	err;
	int			i, j;

	ZBX_UNUSED(state);

	zbx_vector_mock_step_create(&steps);

	hsteps = zbx_mock_get_parameter_handle("in.steps");

	while (ZBX_MOCK_END_OF_VECTOR != (err = (zbx_mock_vector_element(hsteps, &hstep))))
	{
		step = (zbx_mock_step_t *)zbx_malloc(NULL, sizeof(zbx_mock_step_t));
		step->refs = (zbx_uint32_t)zbx_mock_get_object_member_int(hstep, "refs");
		step->cache = NULL;
		um_mock_cache_init(&step->mock_cache, zbx_mock_get_object_member_handle(hstep, "config"));
		zbx_vector_mock_step_append(&steps, step);
	}

	um_mock_config_init();

	um_mock_cache_init(&mock_cache0, -1);
	umc = um_cache_create();

	for (i = 0; i < steps.values_num; i++)
	{
		zbx_dbsync_t	gmacros, hmacros, htmpls;

		printf("=== STEP %d ===\n", i + 1);

		zbx_dbsync_init(&gmacros, ZBX_DBSYNC_UPDATE);
		zbx_dbsync_init(&hmacros, ZBX_DBSYNC_UPDATE);
		zbx_dbsync_init(&htmpls, ZBX_DBSYNC_UPDATE);

		um_mock_cache_diff(mock_cache, &steps.values[i]->mock_cache, &gmacros, &hmacros, &htmpls);
		umc = steps.values[i]->cache = um_cache_sync(umc, 0, &gmacros, &hmacros, &htmpls);
		umc->refcount += steps.values[i]->refs;

		mock_dbsync_clear(&gmacros);
		mock_dbsync_clear(&hmacros);
		mock_dbsync_clear(&htmpls);

		for (j = 0; j <= i; j++)
		{
			if (0 != steps.values[j]->refs)
			{
				mock_step_validate(steps.values[j]);
				um_cache_release(steps.values[j]->cache);
				steps.values[j]->refs--;
			}
		}

		mock_cache = &steps.values[i]->mock_cache;
	}

	do
	{
		i = 0;
		for (j = 0; j < steps.values_num; j++)
		{
			if (0 != steps.values[j]->refs)
			{
				mock_step_validate(steps.values[j]);
				um_cache_release(steps.values[j]->cache);
				steps.values[j]->refs--;
				i++;
			}
		}
	}
	while (i != 0);

	um_cache_release(umc);
	um_mock_cache_clear(&mock_cache0);

	um_mock_config_destroy();

	zbx_vector_mock_step_clear_ext(&steps, mock_step_free);
	zbx_vector_mock_step_destroy(&steps);
}