|
| 1 | +/* |
| 2 | + * Copyright (c) 2024 Mustafa Abdullah Kus, Sparse Technology |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +#include <zephyr/ztest.h> |
| 8 | + |
| 9 | +#include <zephyr/net/prometheus/counter.h> |
| 10 | +#include <zephyr/net/prometheus/collector.h> |
| 11 | + |
| 12 | +struct prometheus_metric test_counter_metric = { |
| 13 | + .type = PROMETHEUS_COUNTER, |
| 14 | + .name = "test_counter", |
| 15 | + .description = "Test counter", |
| 16 | + .num_labels = 1, |
| 17 | + .labels = {{ |
| 18 | + .key = "test", |
| 19 | + .value = "counter", |
| 20 | + }}, |
| 21 | +}; |
| 22 | + |
| 23 | +PROMETHEUS_COUNTER_DEFINE(test_counter_m, &test_counter_metric); |
| 24 | + |
| 25 | +PROMETHEUS_COLLECTOR_DEFINE(test_custom_collector); |
| 26 | + |
| 27 | +/** |
| 28 | + * @brief Test prometheus_counter_inc |
| 29 | + * |
| 30 | + * @details The test shall increment the counter value by 1 and check if the |
| 31 | + * value is incremented correctly. |
| 32 | + * |
| 33 | + * @details The test shall register the counter to the collector and check if the |
| 34 | + * counter is found in the collector. |
| 35 | + */ |
| 36 | +ZTEST(test_collector, test_prometheus_collector_register) |
| 37 | +{ |
| 38 | + int ret; |
| 39 | + struct prometheus_counter *counter; |
| 40 | + |
| 41 | + prometheus_collector_register_metric(&test_custom_collector, test_counter_m.base); |
| 42 | + |
| 43 | + counter = (struct prometheus_counter *)prometheus_collector_get_metric( |
| 44 | + &test_custom_collector, "test_counter"); |
| 45 | + |
| 46 | + zassert_equal(counter, &test_counter_m, "Counter not found in collector"); |
| 47 | + |
| 48 | + zassert_equal(test_counter_m.value, 0, "Counter value is not 0"); |
| 49 | + |
| 50 | + ret = prometheus_counter_inc(counter); |
| 51 | + zassert_ok(ret, "Error incrementing counter"); |
| 52 | + |
| 53 | + zassert_equal(counter->value, 1, "Counter value is not 1"); |
| 54 | +} |
| 55 | + |
| 56 | +ZTEST_SUITE(test_collector, NULL, NULL, NULL, NULL, NULL); |
0 commit comments