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
|
/*
** 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 "zbxmutexs.h"
#include "zbxalgo.h"
#include "dbcache.h"
#include "dbconfig.h"
#include "dbconfig_maintenance_test.h"
static int dc_compare_maintenance_tags(const void *d1, const void *d2)
{
const zbx_dc_maintenance_tag_t *tag1 = *(const zbx_dc_maintenance_tag_t **)d1;
const zbx_dc_maintenance_tag_t *tag2 = *(const zbx_dc_maintenance_tag_t **)d2;
return strcmp(tag1->tag, tag2->tag);
}
static int dc_compare_tags(const void *d1, const void *d2)
{
const zbx_tag_t *tag1 = *(const zbx_tag_t **)d1;
const zbx_tag_t *tag2 = *(const zbx_tag_t **)d2;
return strcmp(tag1->tag, tag2->tag);
}
static void get_maintenance_tags(zbx_mock_handle_t handle, zbx_vector_ptr_t *tags)
{
zbx_mock_error_t mock_err;
zbx_mock_handle_t htag;
zbx_dc_maintenance_tag_t *tag;
const char *key, *value, *op;
while (ZBX_MOCK_END_OF_VECTOR != (mock_err = (zbx_mock_vector_element(handle, &htag))))
{
key = zbx_mock_get_object_member_string(htag, "tag");
value = zbx_mock_get_object_member_string(htag, "value");
op = zbx_mock_get_object_member_string(htag, "operator");
tag = (zbx_dc_maintenance_tag_t *)zbx_malloc(NULL, sizeof(zbx_dc_maintenance_tag_t));
tag->tag = key;
tag->value = value;
if (0 == strcmp(op, "like"))
tag->op = ZBX_MAINTENANCE_TAG_OPERATOR_LIKE;
else if (0 == strcmp(op, "equal"))
tag->op = ZBX_MAINTENANCE_TAG_OPERATOR_EQUAL;
else
fail_msg("unknown maintenance tag operator '%s'", op);
zbx_vector_ptr_append(tags, tag);
}
zbx_vector_ptr_sort(tags, dc_compare_maintenance_tags);
}
static void get_tags(zbx_mock_handle_t handle, zbx_vector_ptr_t *tags)
{
zbx_mock_error_t mock_err;
zbx_mock_handle_t htag;
zbx_tag_t *tag;
const char *key, *value;
while (ZBX_MOCK_END_OF_VECTOR != (mock_err = (zbx_mock_vector_element(handle, &htag))))
{
key = zbx_mock_get_object_member_string(htag, "tag");
value = zbx_mock_get_object_member_string(htag, "value");
tag = (zbx_tag_t *)zbx_malloc(NULL, sizeof(zbx_tag_t));
tag->tag = (char *)key;
tag->value = (char *)value;
zbx_vector_ptr_append(tags, tag);
}
zbx_vector_ptr_sort(tags, dc_compare_tags);
}
static void get_maintenance(zbx_dc_maintenance_t *maintenance)
{
const char *tags_evaltype;
tags_evaltype = zbx_mock_get_parameter_string("in.maintenance.tags_evaltype");
if (0 == strcasecmp(tags_evaltype, "AND/OR"))
maintenance->tags_evaltype = ZBX_MAINTENANCE_TAG_EVAL_TYPE_AND_OR;
else if (0 == strcasecmp(tags_evaltype, "OR"))
maintenance->tags_evaltype = ZBX_MAINTENANCE_TAG_EVAL_TYPE_OR;
else
fail_msg("unknown tags_evaltype value '%s'", tags_evaltype);
get_maintenance_tags(zbx_mock_get_parameter_handle("in.maintenance.tags"), &maintenance->tags);
}
void zbx_mock_test_entry(void **state)
{
zbx_vector_ptr_t tags;
zbx_dc_maintenance_t maintenance;
int returned_ret, expected_ret;
ZBX_UNUSED(state);
zbx_vector_ptr_create(&tags);
zbx_vector_ptr_create(&maintenance.tags);
get_maintenance(&maintenance);
get_tags(zbx_mock_get_parameter_handle("in.tags"), &tags);
returned_ret = dc_maintenance_match_tags_test(&maintenance, &tags);
expected_ret = zbx_mock_str_to_return_code(zbx_mock_get_parameter_string("out.return"));
zbx_mock_assert_int_eq("dc_maintenance_match_tags return value", expected_ret, returned_ret);
zbx_vector_ptr_clear_ext(&maintenance.tags, zbx_ptr_free);
zbx_vector_ptr_destroy(&maintenance.tags);
zbx_vector_ptr_clear_ext(&tags, zbx_ptr_free);
zbx_vector_ptr_destroy(&tags);
}
|