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

prometheus_parse_row.c « zbxprometheus « libs « tests - github.com/zabbix/zabbix.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: cc5ad6d27e80f9c266630541231fc8006b97ec60 (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
/*
** Zabbix
** Copyright (C) 2001-2020 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 "common.h"
#include "zbxalgo.h"
#include "prometheus_test.h"

/******************************************************************************
 *                                                                            *
 * Function: zbx_mock_test_entry                                              *
 *                                                                            *
 ******************************************************************************/
void	zbx_mock_test_entry(void **state)
{
	const char		*data;
	char			*metric = NULL, *value = NULL;
	zbx_vector_ptr_pair_t	labels;
	int			ret, expected_ret, index, i;
	char			*error = NULL;
	zbx_mock_handle_t	hlabels, hlabel;
	zbx_mock_error_t	mock_ret;
	zbx_strloc_t		loc;
	char			buffer[ZBX_MAX_UINT64_LEN + 1];

	ZBX_UNUSED(state);

	zbx_vector_ptr_pair_create(&labels);

	data = zbx_mock_get_parameter_string("in.data");

	ret = zbx_prometheus_row_parse(data, &metric, &labels, &value, &loc, &error);
	expected_ret = zbx_mock_str_to_return_code(zbx_mock_get_parameter_string("out.return"));
	zbx_mock_assert_result_eq("prometheus row parsing", expected_ret, ret);

	if (SUCCEED == ret)
	{
		zbx_mock_assert_str_eq("", zbx_mock_get_parameter_string("out.metric"), metric);
		zbx_mock_assert_str_eq("", zbx_mock_get_parameter_string("out.value"), value);

		if (ZBX_MOCK_SUCCESS != zbx_mock_parameter("out.labels", &hlabels))
			hlabels = -1;

		if (-1 != hlabels && 0 == labels.values_num)
			fail_msg("expected to parse metric labels");

		if (-1 == hlabels && 0 != labels.values_num)
			fail_msg("did not expect to parse metric labels");

		if (-1 != hlabels)
		{
			index = 0;
			while (ZBX_MOCK_END_OF_VECTOR != (mock_ret = zbx_mock_vector_element(hlabels, &hlabel)) &&
					index < labels.values_num)
			{
				zbx_ptr_pair_t	*pair = &labels.values[index];

				zbx_snprintf(buffer, sizeof(buffer), "%d. ", index);

				zbx_mock_assert_str_eq(buffer, zbx_mock_get_object_member_string(hlabel, "name"),
						pair->first);
				zbx_mock_assert_str_eq(buffer, zbx_mock_get_object_member_string(hlabel, "value"),
						pair->second);
				index++;
			}

			if (ZBX_MOCK_END_OF_VECTOR != mock_ret)
				fail_msg("expected more than %d metric labels", index);

			if (index != labels.values_num)
				fail_msg("got more than the expected %d metric labels", index);
		}

		zbx_mock_assert_str_eq("next row", zbx_mock_get_parameter_string("out.next"), data + loc.r + 1);
	}
	else
		zbx_mock_assert_ptr_ne("error message", NULL, error);

	zbx_free(metric);
	zbx_free(value);

	for (i = 0; i < labels.values_num; i++)
	{
		zbx_free(labels.values[i].first);
		zbx_free(labels.values[i].second);
	}
	zbx_vector_ptr_pair_destroy(&labels);

	zbx_free(error);
}