Skip to content

Commit d52893a

Browse files
mustafaabdullahkkrish2718
authored andcommitted
[nrf fromtree] test: net: lib: add prometheus library tests
Tests for prometheus library support. It contains integration and unit test each metric types. Signed-off-by: Mustafa Abdullah Kus <[email protected]> (cherry picked from commit abea54e)
1 parent 71cc9e4 commit d52893a

File tree

24 files changed

+506
-0
lines changed

24 files changed

+506
-0
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# SPDX-License-Identifier: Apache-2.0
2+
3+
cmake_minimum_required(VERSION 3.20.0)
4+
5+
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
6+
7+
project(test_prometheus_collector)
8+
9+
target_sources(app PRIVATE src/main.c)
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
CONFIG_LOG=y
2+
CONFIG_NET_LOG=y
3+
CONFIG_ZTEST=y
4+
CONFIG_ZTEST_STACK_SIZE=1024
5+
CONFIG_PROMETHEUS=y
6+
CONFIG_POSIX_API=y
7+
CONFIG_NETWORKING=y
8+
CONFIG_NET_SOCKETS=y
9+
CONFIG_HTTP_SERVER=y
10+
CONFIG_NET_TEST=y
11+
CONFIG_ENTROPY_GENERATOR=y
12+
CONFIG_TEST_RANDOM_GENERATOR=y
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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);
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
tests:
2+
# section.subsection
3+
prometheus.collector:
4+
depends_on: netif
5+
integration_platforms:
6+
- native_sim
7+
- qemu_x86
8+
platform_exclude:
9+
- native_posix
10+
- native_posix/native/64
11+
tags: prometheus
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# SPDX-License-Identifier: Apache-2.0
2+
3+
cmake_minimum_required(VERSION 3.20.0)
4+
5+
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
6+
7+
project(test_prometheus_counter)
8+
9+
target_sources(app PRIVATE src/main.c)
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
CONFIG_LOG=y
2+
CONFIG_ZTEST=y
3+
CONFIG_TEST_RANDOM_GENERATOR=y
4+
CONFIG_ZTEST_STACK_SIZE=1024
5+
CONFIG_PROMETHEUS=y
6+
CONFIG_POSIX_API=y
7+
CONFIG_NETWORKING=y
8+
CONFIG_NET_SOCKETS=y
9+
CONFIG_NET_LOG=y
10+
CONFIG_HTTP_SERVER=y
11+
CONFIG_NET_TEST=y
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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+
11+
struct prometheus_metric test_counter_metric = {
12+
.type = PROMETHEUS_COUNTER,
13+
.name = "test_counter",
14+
.description = "Test counter",
15+
.num_labels = 1,
16+
.labels = {{
17+
.key = "test",
18+
.value = "counter",
19+
}},
20+
};
21+
22+
PROMETHEUS_COUNTER_DEFINE(test_counter_m, &test_counter_metric);
23+
24+
/**
25+
* @brief Test prometheus_counter_inc
26+
* @details The test shall increment the counter value by 1 and check if the
27+
* value is incremented correctly.
28+
*/
29+
ZTEST(test_counter, test_prometheus_counter_inc)
30+
{
31+
int ret;
32+
33+
zassert_equal(test_counter_m.value, 0, "Counter value is not 0");
34+
35+
ret = prometheus_counter_inc(&test_counter_m);
36+
zassert_ok(ret, "Error incrementing counter");
37+
38+
zassert_equal(test_counter_m.value, 1, "Counter value is not 1");
39+
40+
ret = prometheus_counter_inc(&test_counter_m);
41+
zassert_ok(ret, "Error incrementing counter");
42+
43+
zassert_equal(test_counter_m.value, 2, "Counter value is not 2");
44+
}
45+
46+
ZTEST_SUITE(test_counter, NULL, NULL, NULL, NULL, NULL);
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
tests:
2+
# section.subsection
3+
prometheus.counter:
4+
depends_on: netif
5+
integration_platforms:
6+
- native_sim
7+
- qemu_x86
8+
platform_exclude:
9+
- native_posix
10+
- native_posix/native/64
11+
tags: prometheus
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# SPDX-License-Identifier: Apache-2.0
2+
3+
cmake_minimum_required(VERSION 3.20.0)
4+
5+
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
6+
7+
project(test_prometheus_formatter)
8+
9+
target_sources(app PRIVATE src/main.c)
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
CONFIG_LOG=y
2+
CONFIG_NET_LOG=y
3+
CONFIG_ZTEST=y
4+
CONFIG_ZTEST_STACK_SIZE=1024
5+
CONFIG_PROMETHEUS=y
6+
CONFIG_POSIX_API=y
7+
CONFIG_NETWORKING=y
8+
CONFIG_NET_SOCKETS=y
9+
CONFIG_HTTP_SERVER=y
10+
CONFIG_NET_TEST=y
11+
CONFIG_ENTROPY_GENERATOR=y
12+
CONFIG_TEST_RANDOM_GENERATOR=y

0 commit comments

Comments
 (0)