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 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435
|
/*
* ProFTPD - mod_prometheus API testsuite
* Copyright (c) 2021 TJ Saunders <tj@castaglia.org>
*
* 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, Suite 500, Boston, MA 02110-1335, USA.
*
* As a special exemption, TJ Saunders and other respective copyright holders
* give permission to link this program with OpenSSL, and distribute the
* resulting executable, without including the source code for OpenSSL in the
* source distribution.
*/
/* Registry API tests. */
#include "tests.h"
#include "prometheus/registry.h"
#include "prometheus/metric/db.h"
static pool *p = NULL;
static const char *test_dir = "/tmp/prt-mod_prometheus-test-registry";
static void set_up(void) {
if (p == NULL) {
p = permanent_pool = make_sub_pool(NULL);
}
(void) tests_rmpath(p, test_dir);
(void) tests_mkpath(p, test_dir);
if (getenv("TEST_VERBOSE") != NULL) {
pr_trace_set_levels("prometheus.db", 1, 20);
pr_trace_set_levels("prometheus.registry", 1, 20);
}
mark_point();
prom_db_init(p);
}
static void tear_down(void) {
if (getenv("TEST_VERBOSE") != NULL) {
pr_trace_set_levels("prometheus.db", 0, 0);
pr_trace_set_levels("prometheus.registry", 0, 0);
}
prom_db_free();
(void) tests_rmpath(p, test_dir);
if (p != NULL) {
destroy_pool(p);
p = permanent_pool = NULL;
}
}
START_TEST (registry_free_test) {
int res;
mark_point();
res = prom_registry_free(NULL);
fail_unless(res < 0, "Failed to handle null registry");
fail_unless(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
strerror(errno), errno);
}
END_TEST
START_TEST (registry_init_test) {
int res;
const char *name;
struct prom_registry *registry;
mark_point();
registry = prom_registry_init(NULL, NULL);
fail_unless(registry == NULL, "Failed to handle null pool");
fail_unless(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
strerror(errno), errno);
mark_point();
registry = prom_registry_init(p, NULL);
fail_unless(registry == NULL, "Failed to handle null name");
fail_unless(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
strerror(errno), errno);
mark_point();
registry = prom_registry_init(p, "test");
fail_unless(registry != NULL, "Failed to create registry: %s",
strerror(errno));
mark_point();
name = prom_registry_get_name(NULL);
fail_unless(name == NULL, "Failed to handle null registry");
fail_unless(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
strerror(errno), errno);
mark_point();
name = prom_registry_get_name(registry);
fail_unless(name != NULL, "Failed to get name: %s", strerror(errno));
fail_unless(strcmp(name, "test") == 0, "Expected 'test', got '%s'",
name);
res = prom_registry_free(registry);
fail_unless(res == 0, "Failed to free registry: %s", strerror(errno));
}
END_TEST
START_TEST (registry_get_metric_test) {
struct prom_registry *registry;
const struct prom_metric *metric;
mark_point();
metric = prom_registry_get_metric(NULL, NULL);
fail_unless(metric == NULL, "Failed to handle null registry");
fail_unless(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
strerror(errno), errno);
mark_point();
registry = prom_registry_init(p, "test");
fail_unless(registry != NULL, "Failed to create registry: %s",
strerror(errno));
mark_point();
metric = prom_registry_get_metric(registry, NULL);
fail_unless(metric == NULL, "Failed to handle null metric name");
fail_unless(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
strerror(errno), errno);
prom_registry_free(registry);
}
END_TEST
START_TEST (registry_add_metric_test) {
int res;
struct prom_registry *registry;
char *metric_name;
struct prom_metric *metric;
struct prom_dbh *dbh;
mark_point();
res = prom_registry_add_metric(NULL, NULL);
fail_unless(res < 0, "Failed to handle null registry");
fail_unless(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
strerror(errno), errno);
mark_point();
registry = prom_registry_init(p, "test");
fail_unless(registry != NULL, "Failed to create registry: %s",
strerror(errno));
mark_point();
res = prom_registry_add_metric(registry, NULL);
fail_unless(res < 0, "Failed to handle null metric");
fail_unless(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
strerror(errno), errno);
/* For purposes of testing, we don't need a real dbh here. */
mark_point();
metric_name = "metric";
dbh = palloc(p, 8);
metric = prom_metric_create(p, metric_name, dbh);
fail_unless(metric != NULL, "Failed to create metric: %s", strerror(errno));
res = prom_registry_add_metric(registry, metric);
fail_unless(res == 0, "Failed to add metric: %s", strerror(errno));
mark_point();
metric = (struct prom_metric *) prom_registry_get_metric(registry,
metric_name);
fail_unless(metric != NULL, "Failed to get metric: %s", strerror(errno));
prom_registry_free(registry);
}
END_TEST
START_TEST (registry_sort_metrics_test) {
int res;
struct prom_registry *registry;
struct prom_metric *first_metric, *second_metric;
struct prom_dbh *dbh;
mark_point();
res = prom_registry_sort_metrics(NULL);
fail_unless(res < 0, "Failed to handle null registry");
fail_unless(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
strerror(errno), errno);
mark_point();
registry = prom_registry_init(p, "test");
fail_unless(registry != NULL, "Failed to create registry: %s",
strerror(errno));
/* For purposes of testing, we don't need a real dbh here. */
mark_point();
dbh = palloc(p, 8);
first_metric = prom_metric_create(p, "first", dbh);
fail_unless(first_metric != NULL, "Failed to create metric: %s",
strerror(errno));
res = prom_registry_add_metric(registry, first_metric);
fail_unless(res == 0, "Failed to add metric: %s", strerror(errno));
mark_point();
res = prom_registry_sort_metrics(registry);
fail_unless(res == 0, "Failed to sort metrics: %s", strerror(errno));
mark_point();
second_metric = prom_metric_create(p, "second", dbh);
fail_unless(second_metric != NULL, "Failed to create metric: %s",
strerror(errno));
res = prom_registry_add_metric(registry, second_metric);
fail_unless(res == 0, "Failed to add metric: %s", strerror(errno));
res = prom_registry_sort_metrics(registry);
fail_unless(res == 0, "Failed to sort metrics: %s", strerror(errno));
prom_registry_free(registry);
}
END_TEST
START_TEST (registry_set_dbh_test) {
int res;
struct prom_registry *registry;
struct prom_dbh *dbh;
mark_point();
res = prom_registry_set_dbh(NULL, NULL);
fail_unless(res < 0, "Failed to handle null registry");
fail_unless(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
strerror(errno), errno);
mark_point();
registry = prom_registry_init(p, "test");
fail_unless(registry != NULL, "Failed to create registry: %s",
strerror(errno));
mark_point();
res = prom_registry_set_dbh(registry, NULL);
fail_unless(res < 0, "Failed to handle null registry");
fail_unless(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
strerror(errno), errno);
/* For purposes of testing, we don't need a real dbh here. */
mark_point();
dbh = palloc(p, 8);
res = prom_registry_set_dbh(registry, dbh);
fail_unless(res == 0, "Failed to handle set dbh: %s", strerror(errno));
prom_registry_free(registry);
}
END_TEST
START_TEST (registry_get_text_test) {
const char *text;
struct prom_registry *registry;
mark_point();
text = prom_registry_get_text(NULL, NULL);
fail_unless(text == NULL, "Failed to handle null pool");
fail_unless(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
strerror(errno), errno);
mark_point();
text = prom_registry_get_text(p, NULL);
fail_unless(text == NULL, "Failed to handle null registry");
fail_unless(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
strerror(errno), errno);
mark_point();
registry = prom_registry_init(p, "test");
fail_unless(registry != NULL, "Failed to create registry: %s",
strerror(errno));
mark_point();
text = prom_registry_get_text(p, registry);
fail_unless(text == NULL, "Failed to handle absent metrics");
fail_unless(errno == ENOENT, "Expected ENOENT (%d), got %s (%d)", ENOENT,
strerror(errno), errno);
prom_registry_free(registry);
}
END_TEST
START_TEST (registry_get_text_with_metrics_test) {
int res;
const char *text;
struct prom_registry *registry;
struct prom_metric *metric;
struct prom_dbh *dbh;
(void) tests_rmpath(p, test_dir);
(void) tests_mkpath(p, test_dir);
mark_point();
registry = prom_registry_init(p, "test");
fail_unless(registry != NULL, "Failed to create registry: %s",
strerror(errno));
mark_point();
dbh = prom_metric_init(p, test_dir);
fail_unless(dbh != NULL, "Failed to init metrics: %s", strerror(errno));
mark_point();
metric = prom_metric_create(p, "metric", dbh);
fail_unless(metric != NULL, "Failed to create metric: %s", strerror(errno));
mark_point();
res = prom_registry_add_metric(registry, metric);
fail_unless(res == 0, "Failed to register metric: %s", strerror(errno));
mark_point();
res = prom_metric_add_counter(metric, "total", "testing");
fail_unless(res == 0, "Failed to add counter to metric: %s", strerror(errno));
mark_point();
res = prom_metric_incr(p, metric, 1, NULL);
fail_unless(res == 0, "Failed to increment metric: %s", strerror(errno));
mark_point();
text = prom_registry_get_text(p, registry);
fail_unless(text != NULL, "Failed to get registry text: %s", strerror(errno));
/* Use strstr(3) to assert bits of text. */
fail_unless(strstr(text, "# HELP test_metric_total") != NULL,
"Expected metric help, got '%s'", text);
fail_unless(strstr(text, "# TYPE test_metric_total counter") != NULL,
"Expected metric type, got '%s'", text);
fail_unless(strstr(text, "test_metric_total 1") != NULL,
"Expected metric sample, got '%s'", text);
prom_registry_free(registry);
prom_db_close(p, dbh);
(void) tests_rmpath(p, test_dir);
}
END_TEST
START_TEST (registry_get_text_with_metrics_readonly_test) {
int res;
const char *text;
struct prom_registry *registry;
struct prom_metric *metric;
struct prom_dbh *dbh;
(void) tests_rmpath(p, test_dir);
(void) tests_mkpath(p, test_dir);
mark_point();
registry = prom_registry_init(p, "test");
fail_unless(registry != NULL, "Failed to create registry: %s",
strerror(errno));
mark_point();
dbh = prom_metric_init(p, test_dir);
fail_unless(dbh != NULL, "Failed to init metrics: %s", strerror(errno));
mark_point();
metric = prom_metric_create(p, "metric", dbh);
fail_unless(metric != NULL, "Failed to create metric: %s", strerror(errno));
mark_point();
res = prom_registry_add_metric(registry, metric);
fail_unless(res == 0, "Failed to register metric: %s", strerror(errno));
mark_point();
res = prom_metric_add_counter(metric, "total", "testing");
fail_unless(res == 0, "Failed to add counter to metric: %s", strerror(errno));
mark_point();
res = prom_metric_incr(p, metric, 1, NULL);
fail_unless(res == 0, "Failed to increment metric: %s", strerror(errno));
mark_point();
/* Now, close that dbh. Open a readonly one, set it in the registry.
* This approximates what happens with the exporter process.
*/
(void) prom_db_close(p, dbh);
dbh = prom_metric_db_open(p, test_dir);
fail_unless(dbh != NULL, "Failed to open readonly dbh: %s", strerror(errno));
mark_point();
res = prom_registry_set_dbh(registry, dbh);
fail_unless(res == 0, "Failed to set registry dbh: %s", strerror(errno));
mark_point();
text = prom_registry_get_text(p, registry);
fail_unless(text != NULL, "Failed to get registry text: %s", strerror(errno));
/* Use strstr(3) to assert bits of text. */
fail_unless(strstr(text, "# HELP test_metric_total") != NULL,
"Expected metric help, got '%s'", text);
fail_unless(strstr(text, "# TYPE test_metric_total counter") != NULL,
"Expected metric type, got '%s'", text);
fail_unless(strstr(text, "test_metric_total 1") != NULL,
"Expected metric sample, got '%s'", text);
prom_registry_free(registry);
prom_db_close(p, dbh);
(void) tests_rmpath(p, test_dir);
}
END_TEST
Suite *tests_get_registry_suite(void) {
Suite *suite;
TCase *testcase;
suite = suite_create("registry");
testcase = tcase_create("base");
tcase_add_checked_fixture(testcase, set_up, tear_down);
tcase_add_test(testcase, registry_free_test);
tcase_add_test(testcase, registry_init_test);
tcase_add_test(testcase, registry_get_metric_test);
tcase_add_test(testcase, registry_add_metric_test);
tcase_add_test(testcase, registry_sort_metrics_test);
tcase_add_test(testcase, registry_set_dbh_test);
tcase_add_test(testcase, registry_get_text_test);
tcase_add_test(testcase, registry_get_text_with_metrics_test);
tcase_add_test(testcase, registry_get_text_with_metrics_readonly_test);
suite_add_tcase(suite, testcase);
return suite;
}
|