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 <mustafa.kus@sparsetechnology.com>
This commit is contained in:
Mustafa Abdullah Kus 2024-10-12 23:17:07 +03:00 committed by Carles Cufí
commit abea54e574
24 changed files with 506 additions and 0 deletions

View file

@ -0,0 +1,9 @@
# SPDX-License-Identifier: Apache-2.0
cmake_minimum_required(VERSION 3.20.0)
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
project(test_prometheus_collector)
target_sources(app PRIVATE src/main.c)

View file

@ -0,0 +1,12 @@
CONFIG_LOG=y
CONFIG_NET_LOG=y
CONFIG_ZTEST=y
CONFIG_ZTEST_STACK_SIZE=1024
CONFIG_PROMETHEUS=y
CONFIG_POSIX_API=y
CONFIG_NETWORKING=y
CONFIG_NET_SOCKETS=y
CONFIG_HTTP_SERVER=y
CONFIG_NET_TEST=y
CONFIG_ENTROPY_GENERATOR=y
CONFIG_TEST_RANDOM_GENERATOR=y

View file

@ -0,0 +1,56 @@
/*
* Copyright (c) 2024 Mustafa Abdullah Kus, Sparse Technology
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <zephyr/ztest.h>
#include <zephyr/net/prometheus/counter.h>
#include <zephyr/net/prometheus/collector.h>
struct prometheus_metric test_counter_metric = {
.type = PROMETHEUS_COUNTER,
.name = "test_counter",
.description = "Test counter",
.num_labels = 1,
.labels = {{
.key = "test",
.value = "counter",
}},
};
PROMETHEUS_COUNTER_DEFINE(test_counter_m, &test_counter_metric);
PROMETHEUS_COLLECTOR_DEFINE(test_custom_collector);
/**
* @brief Test prometheus_counter_inc
*
* @details The test shall increment the counter value by 1 and check if the
* value is incremented correctly.
*
* @details The test shall register the counter to the collector and check if the
* counter is found in the collector.
*/
ZTEST(test_collector, test_prometheus_collector_register)
{
int ret;
struct prometheus_counter *counter;
prometheus_collector_register_metric(&test_custom_collector, test_counter_m.base);
counter = (struct prometheus_counter *)prometheus_collector_get_metric(
&test_custom_collector, "test_counter");
zassert_equal(counter, &test_counter_m, "Counter not found in collector");
zassert_equal(test_counter_m.value, 0, "Counter value is not 0");
ret = prometheus_counter_inc(counter);
zassert_ok(ret, "Error incrementing counter");
zassert_equal(counter->value, 1, "Counter value is not 1");
}
ZTEST_SUITE(test_collector, NULL, NULL, NULL, NULL, NULL);

View file

@ -0,0 +1,11 @@
tests:
# section.subsection
prometheus.collector:
depends_on: netif
integration_platforms:
- native_sim
- qemu_x86
platform_exclude:
- native_posix
- native_posix/native/64
tags: prometheus

View file

@ -0,0 +1,9 @@
# SPDX-License-Identifier: Apache-2.0
cmake_minimum_required(VERSION 3.20.0)
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
project(test_prometheus_counter)
target_sources(app PRIVATE src/main.c)

View file

@ -0,0 +1,11 @@
CONFIG_LOG=y
CONFIG_ZTEST=y
CONFIG_TEST_RANDOM_GENERATOR=y
CONFIG_ZTEST_STACK_SIZE=1024
CONFIG_PROMETHEUS=y
CONFIG_POSIX_API=y
CONFIG_NETWORKING=y
CONFIG_NET_SOCKETS=y
CONFIG_NET_LOG=y
CONFIG_HTTP_SERVER=y
CONFIG_NET_TEST=y

View file

@ -0,0 +1,46 @@
/*
* Copyright (c) 2024 Mustafa Abdullah Kus, Sparse Technology
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <zephyr/ztest.h>
#include <zephyr/net/prometheus/counter.h>
struct prometheus_metric test_counter_metric = {
.type = PROMETHEUS_COUNTER,
.name = "test_counter",
.description = "Test counter",
.num_labels = 1,
.labels = {{
.key = "test",
.value = "counter",
}},
};
PROMETHEUS_COUNTER_DEFINE(test_counter_m, &test_counter_metric);
/**
* @brief Test prometheus_counter_inc
* @details The test shall increment the counter value by 1 and check if the
* value is incremented correctly.
*/
ZTEST(test_counter, test_prometheus_counter_inc)
{
int ret;
zassert_equal(test_counter_m.value, 0, "Counter value is not 0");
ret = prometheus_counter_inc(&test_counter_m);
zassert_ok(ret, "Error incrementing counter");
zassert_equal(test_counter_m.value, 1, "Counter value is not 1");
ret = prometheus_counter_inc(&test_counter_m);
zassert_ok(ret, "Error incrementing counter");
zassert_equal(test_counter_m.value, 2, "Counter value is not 2");
}
ZTEST_SUITE(test_counter, NULL, NULL, NULL, NULL, NULL);

View file

@ -0,0 +1,11 @@
tests:
# section.subsection
prometheus.counter:
depends_on: netif
integration_platforms:
- native_sim
- qemu_x86
platform_exclude:
- native_posix
- native_posix/native/64
tags: prometheus

View file

@ -0,0 +1,9 @@
# SPDX-License-Identifier: Apache-2.0
cmake_minimum_required(VERSION 3.20.0)
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
project(test_prometheus_formatter)
target_sources(app PRIVATE src/main.c)

View file

@ -0,0 +1,12 @@
CONFIG_LOG=y
CONFIG_NET_LOG=y
CONFIG_ZTEST=y
CONFIG_ZTEST_STACK_SIZE=1024
CONFIG_PROMETHEUS=y
CONFIG_POSIX_API=y
CONFIG_NETWORKING=y
CONFIG_NET_SOCKETS=y
CONFIG_HTTP_SERVER=y
CONFIG_NET_TEST=y
CONFIG_ENTROPY_GENERATOR=y
CONFIG_TEST_RANDOM_GENERATOR=y

View file

@ -0,0 +1,65 @@
/*
* Copyright (c) 2024 Mustafa Abdullah Kus, Sparse Technology
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <zephyr/ztest.h>
#include <zephyr/net/prometheus/counter.h>
#include <zephyr/net/prometheus/collector.h>
#include <zephyr/net/prometheus/formatter.h>
#define MAX_BUFFER_SIZE 256
struct prometheus_metric test_counter_metric = {
.type = PROMETHEUS_COUNTER,
.name = "test_counter",
.description = "Test counter",
.num_labels = 1,
.labels = {{
.key = "test",
.value = "counter",
}},
};
PROMETHEUS_COUNTER_DEFINE(test_counter_m, &test_counter_metric);
PROMETHEUS_COLLECTOR_DEFINE(test_custom_collector);
/**
* @brief Test Prometheus formatter
* @details The test shall increment the counter value by 1 and check if the
* value is incremented correctly. It shall then format the metric and compare
* it with the expected output.
*/
ZTEST(test_formatter, test_prometheus_formatter_simple)
{
int ret;
char formatted[MAX_BUFFER_SIZE];
struct prometheus_counter *counter;
char exposed[] = "# HELP test_counter Test counter\n"
"# TYPE test_counter counter\n"
"test_counter{test=\"counter\"} 1\n";
prometheus_collector_register_metric(&test_custom_collector, test_counter_m.base);
counter = (struct prometheus_counter *)prometheus_collector_get_metric(
&test_custom_collector, "test_counter");
zassert_equal(counter, &test_counter_m, "Counter not found in collector");
zassert_equal(test_counter_m.value, 0, "Counter value is not 0");
ret = prometheus_counter_inc(&test_counter_m);
zassert_ok(ret, "Error incrementing counter");
zassert_equal(counter->value, 1, "Counter value is not 1");
ret = prometheus_format_exposition(&test_custom_collector, formatted, sizeof(formatted));
zassert_ok(ret, "Error formatting exposition data");
zassert_equal(strcmp(formatted, exposed), 0, "Exposition format is not as expected");
}
ZTEST_SUITE(test_formatter, NULL, NULL, NULL, NULL, NULL);

View file

@ -0,0 +1,11 @@
tests:
# section.subsection
prometheus.formatter:
depends_on: netif
integration_platforms:
- native_sim
- qemu_x86
platform_exclude:
- native_posix
- native_posix/native/64
tags: prometheus

View file

@ -0,0 +1,9 @@
# SPDX-License-Identifier: Apache-2.0
cmake_minimum_required(VERSION 3.20.0)
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
project(test_prometheus_gauge)
target_sources(app PRIVATE src/main.c)

View file

@ -0,0 +1,11 @@
CONFIG_LOG=y
CONFIG_ZTEST=y
CONFIG_TEST_RANDOM_GENERATOR=y
CONFIG_ZTEST_STACK_SIZE=1024
CONFIG_PROMETHEUS=y
CONFIG_POSIX_API=y
CONFIG_NETWORKING=y
CONFIG_NET_SOCKETS=y
CONFIG_NET_LOG=y
CONFIG_HTTP_SERVER=y
CONFIG_NET_TEST=y

View file

@ -0,0 +1,50 @@
/*
* Copyright (c) 2024 Mustafa Abdullah Kus, Sparse Technology
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <zephyr/ztest.h>
#include <zephyr/net/prometheus/gauge.h>
struct prometheus_metric test_gauge_metric = {
.type = PROMETHEUS_GAUGE,
.name = "test_gauge",
.description = "Test gauge",
.num_labels = 1,
.labels = {{
.key = "test",
.value = "gauge",
}},
};
PROMETHEUS_GAUGE_DEFINE(test_gauge_m, &test_gauge_metric);
/**
* @brief Test prometheus_gauge_set
*
* @details The test shall set the gauge value to 1 and check if the
* value is set correctly.
*
* @details The test shall set the gauge value to 2 and check if the
* value is set correctly.
*/
ZTEST(test_gauge, test_gauge_set)
{
int ret;
zassert_equal(test_gauge_m.value, 0, "Gauge value is not 0");
ret = prometheus_gauge_set(&test_gauge_m, 1);
zassert_ok(ret, "Error setting gauge");
zassert_equal(test_gauge_m.value, 1, "Gauge value is not 1");
ret = prometheus_gauge_set(&test_gauge_m, 2);
zassert_ok(ret, "Error setting gauge");
zassert_equal(test_gauge_m.value, 2, "Gauge value is not 2");
}
ZTEST_SUITE(test_gauge, NULL, NULL, NULL, NULL, NULL);

View file

@ -0,0 +1,11 @@
tests:
# section.subsection
prometheus.gauge:
depends_on: netif
integration_platforms:
- native_sim
- qemu_x86
platform_exclude:
- native_posix
- native_posix/native/64
tags: prometheus

View file

@ -0,0 +1,9 @@
# SPDX-License-Identifier: Apache-2.0
cmake_minimum_required(VERSION 3.20.0)
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
project(test_prometheus_histogram)
target_sources(app PRIVATE src/main.c)

View file

@ -0,0 +1,12 @@
CONFIG_LOG=y
CONFIG_ZTEST=y
CONFIG_TEST_RANDOM_GENERATOR=y
CONFIG_ZTEST_STACK_SIZE=1024
CONFIG_PROMETHEUS=y
CONFIG_POSIX_API=y
CONFIG_NETWORKING=y
CONFIG_NET_SOCKETS=y
CONFIG_NET_LOG=y
CONFIG_HTTP_SERVER=y
CONFIG_NET_TEST=y
CONFIG_FPU=y

View file

@ -0,0 +1,50 @@
/*
* Copyright (c) 2024 Mustafa Abdullah Kus, Sparse Technology
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <zephyr/ztest.h>
#include <zephyr/net/prometheus/histogram.h>
struct prometheus_metric test_histogram_metric = {
.type = PROMETHEUS_HISTOGRAM,
.name = "test_histogram",
.description = "Test histogram",
.num_labels = 1,
.labels = {{
.key = "test",
.value = "histogram",
}},
};
PROMETHEUS_HISTOGRAM_DEFINE(test_histogram_m, &test_histogram_metric);
/**
* @brief Test prometheus_histogram_observe
*
* @details The test shall observe the histogram value by 1 and check if the
* value is incremented correctly.
*
* @details The test shall observe the histogram value by 2 and check if the
* value is incremented correctly.
*/
ZTEST(test_histogram, test_histogram_observe)
{
int ret;
zassert_equal(test_histogram_m.sum, 0, "Histogram value is not 0");
ret = prometheus_histogram_observe(&test_histogram_m, 1);
zassert_ok(ret, "Error observing histogram");
zassert_equal(test_histogram_m.sum, 1.0, "Histogram value is not 1");
ret = prometheus_histogram_observe(&test_histogram_m, 2);
zassert_ok(ret, "Error observing histogram");
zassert_equal(test_histogram_m.sum, 3.0, "Histogram value is not 2");
}
ZTEST_SUITE(test_histogram, NULL, NULL, NULL, NULL, NULL);

View file

@ -0,0 +1,11 @@
tests:
# section.subsection
prometheus.histogram:
depends_on: netif
integration_platforms:
- native_sim
- qemu_x86
platform_exclude:
- native_posix
- native_posix/native/64
tags: prometheus

View file

@ -0,0 +1,9 @@
# SPDX-License-Identifier: Apache-2.0
cmake_minimum_required(VERSION 3.20.0)
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
project(test_prometheus_summary)
target_sources(app PRIVATE src/main.c)

View file

@ -0,0 +1,11 @@
CONFIG_LOG=y
CONFIG_ZTEST=y
CONFIG_TEST_RANDOM_GENERATOR=y
CONFIG_ZTEST_STACK_SIZE=1024
CONFIG_PROMETHEUS=y
CONFIG_POSIX_API=y
CONFIG_NETWORKING=y
CONFIG_NET_SOCKETS=y
CONFIG_NET_LOG=y
CONFIG_HTTP_SERVER=y
CONFIG_NET_TEST=y

View file

@ -0,0 +1,50 @@
/*
* Copyright (c) 2024 Mustafa Abdullah Kus, Sparse Technology
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <zephyr/ztest.h>
#include <zephyr/net/prometheus/summary.h>
struct prometheus_metric test_summary_metric = {
.type = PROMETHEUS_SUMMARY,
.name = "test_summary",
.description = "Test summary",
.num_labels = 1,
.labels = {{
.key = "test",
.value = "summary",
}},
};
PROMETHEUS_SUMMARY_DEFINE(test_summary_m, &test_summary_metric);
/**
* @brief Test prometheus_summary_observe
*
* @details The test shall observe the histogram value by 1 and check if the
* value is incremented correctly.
*
* @details The test shall observe the histogram value by 2 and check if the
* value is incremented correctly.
*/
ZTEST(test_summary, test_summary_observe)
{
int ret;
zassert_equal(test_summary_m.sum, 0, "Histogram value is not 0");
ret = prometheus_summary_observe(&test_summary_m, 1);
zassert_ok(ret, "Error observing histogram");
zassert_equal(test_summary_m.sum, 1, "Histogram value is not 1");
ret = prometheus_summary_observe(&test_summary_m, 2);
zassert_ok(ret, "Error observing histogram");
zassert_equal(test_summary_m.sum, 3, "Histogram value is not 3");
}
ZTEST_SUITE(test_summary, NULL, NULL, NULL, NULL, NULL);

View file

@ -0,0 +1,11 @@
tests:
# section.subsection
prometheus.summary:
depends_on: netif
integration_platforms:
- native_sim
- qemu_x86
platform_exclude:
- native_posix
- native_posix/native/64
tags: prometheus