Skip to content

Commit

Permalink
test: Makefile target to run against local Prometheus
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
pvoborni committed Oct 9, 2023
1 parent eea261c commit f4e2137
Show file tree
Hide file tree
Showing 7 changed files with 199 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -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
64 changes: 64 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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..."
Expand All @@ -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:
Expand Down Expand Up @@ -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
81 changes: 81 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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
```
14 changes: 14 additions & 0 deletions hack/create-cert.sh
Original file line number Diff line number Diff line change
@@ -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
17 changes: 17 additions & 0 deletions hack/host-metering.conf
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions hack/prometheus.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
---
16 changes: 16 additions & 0 deletions hack/test-cert.cnf
Original file line number Diff line number Diff line change
@@ -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[email protected]
CN = test-host.host-metering.test

[ req_ext ]
subjectAltName = DNS:test-host.host-metering.test

0 comments on commit f4e2137

Please sign in to comment.