From 320e44ded51d47e4382ee048adbb8da92d25bbff Mon Sep 17 00:00:00 2001 From: Petr Vobornik Date: Fri, 6 Oct 2023 14:07:34 +0000 Subject: [PATCH] test: Makefile target to run against local Prometheus Adds several Makefile targets, but mainly: - `make test-daemon` - `make clean-pod` Which can be used by developers to build&run host-metering locally within couple seconds againts a Prometheus server that is spawned in a podman container/pod. The run can be cleaned-up via. ``` make clean make clean-pod ``` `README.md` is updated to reflect this + several basic stuff that were missing. Signed-off-by: Petr Vobornik --- .gitignore | 6 +++ Makefile | 64 ++++++++++++++++++++++++++++++++ README.md | 81 +++++++++++++++++++++++++++++++++++++++++ hack/create-cert.sh | 14 +++++++ hack/host-metering.conf | 17 +++++++++ hack/prometheus.yml | 1 + hack/test-cert.cnf | 16 ++++++++ 7 files changed, 199 insertions(+) create mode 100755 hack/create-cert.sh create mode 100644 hack/host-metering.conf create mode 100644 hack/prometheus.yml create mode 100644 hack/test-cert.cnf diff --git a/.gitignore b/.gitignore index 9c0b076..07374e8 100644 --- a/.gitignore +++ b/.gitignore @@ -5,8 +5,14 @@ contrib/rpm/host-metering.spec contrib/selinux/tmp/ contrib/selinux/host-metering.pp +# testing +hack/test-cert.crt +hack/test-cert.key +hack/cpumetrics/ + # Coverage coverage.* # Binary host-metering +dist/host-metering diff --git a/Makefile b/Makefile index acadb59..588cb0e 100644 --- a/Makefile +++ b/Makefile @@ -12,6 +12,10 @@ RPMTOPDIR := $(DISTDIR)/rpmbuild GO := go TESTDIR := $(CURDIR)/test +CONTAINER_POD := host-metering-pod +DASHBOARD_URL = http://localhost:9090/graph?g0.expr=system_cpu_logical_count&g0.tab=0&g0.range_input=1m + +# Test .PHONY: test test: @echo "Running the unit tests..." @@ -31,12 +35,69 @@ test: @cat coverage.txt # Build +.PHONY: build +build: + @echo "Building the project..." + $(GO) build -o $(DISTDIR)/$(PROJECT) + .PHONY: build-selinux build-selinux: @echo "Building SELinux policy..." cd contrib/selinux && \ make -f /usr/share/selinux/devel/Makefile $(PROJECT).pp || exit +# Functional testing (manual or automatic) + +.PHONY: test-daemon +test-daemon: cert build prometheus + @echo "Running the $(PROJECT) in deamon mode..." + + PATH=$(TESTDIR)/bin:$(PATH) \ + $(DISTDIR)/$(PROJECT) --config hack/host-metering.conf daemon + +cert: hack/test-cert.crt hack/test-cert.key + +hack/test-cert.crt hack/test-cert.key: + @echo "Generating test certificates..." + cd hack && ./create-cert.sh + +# Containers + +.PHONY: container-pod +container-pod: + if podman pod exists $(CONTAINER_POD); then \ + echo "Pod $(CONTAINER_POD) exists."; \ + exit 0; \ + else \ + echo "Creating the $(CONTAINER_POD)..."; \ + podman pod create --replace -p 9090:9090 ${CONTAINER_POD}; \ + fi + +.PHONY: prometheus +prometheus: container-pod + @echo "See the dashboard at: ${DASHBOARD_URL}" + + if podman ps --filter "name=hm-prometheus" --format '{{.Names}}' | grep -q "hm-prometheus"; then \ + echo "Prometheus is already running."; \ + exit 0; \ + else \ + echo "Starting Prometheus..."; \ + podman run --pod ${CONTAINER_POD} \ + --name hm-prometheus \ + -d \ + -v ./hack/prometheus.yml:/etc/prometheus/prometheus.yml:Z \ + prometheus/prometheus \ + --config.file=/etc/prometheus/prometheus.yml \ + --storage.tsdb.path=/prometheus \ + --web.console.libraries=/usr/share/prometheus/console_libraries \ + --web.console.templates=/usr/share/prometheus/consoles \ + --web.enable-remote-write-receiver; \ + fi + +.PHONY: clean-pod +clean-pod: + podman pod rm -f ${CONTAINER_POD} + # Release .PHONY: version version: @@ -120,3 +181,6 @@ clean: rm -rf $(CURDIR)/$(PROJECT) rm -rf $(CURDIR)/contrib/selinux/tmp rm -rf $(CURDIR)/contrib/selinux/*.pp + rm -rf $(CURDIR)/hack/cpumetrics + rm -f $(CURDIR)/hack/test-cert.crt + rm -f $(CURDIR)/hack/test-cert.key diff --git a/README.md b/README.md index ddee06b..1d7babc 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,84 @@ # Host Metering Host metering client. + +## Usage + +``` +$ host-metering daemon +``` + +Or if run from RPMs as a service: + +``` +# yum install host-metering +# systemctl enable host-metering +# systemctl start host-metering +``` + +See output/log: + +``` +# journalctl -feu host-metering +``` + +## RPM repository + +RPM builds of `main` branch are available at COPR: https://copr.fedorainfracloud.org/coprs/pvoborni/host-metering/ + +## Build + +``` +$ go build +``` + +rpms via mock: + +``` +$ make mock # EPEL 7 +$ make mock-8 # CentOS Stream 8 +$ make mock-9 # CentOS Stream 9 +``` + +rpms directly via `rpmbuild` + +``` +$ make rpm +``` + +## Testing + +### Unit tests + +To run go unit test: +``` +$ make test +``` + +### Local run / development + +``` +$ make test-daemon +``` + +It will run `host-metering` with: +* mocked `subscriptin-manager` +* test configuration file that is lowering intervals and using the following: +* test certificate +* custom path for metrics WAL +* Prometheus server started in a podman container + +Query Prometheus, e.g., via command: + +``` +$ curl 'http://localhost:9090/api/v1/query?query=system_cpu_logical_count' | jq +``` + +Or visit the Prometheus Web UI at http://localhost:9090/graph?g0.expr=system_cpu_logical_count&g0.tab=0&g0.range_input=1m + +### Clean-up + +``` +$ make clean # clean build&test files +$ make clean-pod # destroy podman pod +``` diff --git a/hack/create-cert.sh b/hack/create-cert.sh new file mode 100755 index 0000000..2575081 --- /dev/null +++ b/hack/create-cert.sh @@ -0,0 +1,14 @@ + +# Execute this script in the directory with test-cert.cnf +# Purpose: Create a self-signed certificate for testing + +openssl genrsa -out test-cert.key + +# To create a CSR for submitting to CA +# openssl req -new -key test-cert.key -out test-cert.csr -config test-cert.cnf + +# Create a self-signed certificate +openssl req -x509 -new -key test-cert.key -out test-cert.crt -config test-cert.cnf + +# Show the certificate +openssl x509 -in test-cert.crt -text -noout \ No newline at end of file diff --git a/hack/host-metering.conf b/hack/host-metering.conf new file mode 100644 index 0000000..0591c82 --- /dev/null +++ b/hack/host-metering.conf @@ -0,0 +1,17 @@ +# Test host-metering configuration file which writes to a local prometheus +# and uses a test certificate and key. + +# E.g. could be used as: +# ./host-metering -config hack/host-metering.conf daemon + +[host-metering] +write_url=http://localhost:9090/api/v1/write +write_interval_sec=5 +host_cert_path=./hack/test-cert.crt +host_cert_key_path=./hack/test-cert.key +collect_interval_sec=0 +label_refresh_interval_sec=10 +write_retry_attempts=4 +write_retry_interval_sec=2 +metrics_wal_path=./hack/cpumetrics +log_level=DEBUG diff --git a/hack/prometheus.yml b/hack/prometheus.yml new file mode 100644 index 0000000..ed97d53 --- /dev/null +++ b/hack/prometheus.yml @@ -0,0 +1 @@ +--- diff --git a/hack/test-cert.cnf b/hack/test-cert.cnf new file mode 100644 index 0000000..75449ed --- /dev/null +++ b/hack/test-cert.cnf @@ -0,0 +1,16 @@ +[req] +prompt = no +req_extensions = req_ext +distinguished_name = dn + +[ dn ] +C=US +ST=North Carolina +L=Raleigh +O=Milton +OU=Information Technology +emailAddress=test@host-metering.test +CN = test-host.host-metering.test + +[ req_ext ] +subjectAltName = DNS:test-host.host-metering.test \ No newline at end of file