Skip to content

Commit 0ba2a92

Browse files
authored
Add unit and functional test suite with 95%+ coverage (#25)
Add unit and functional test suite with 95%+ coverage Summary Adds a comprehensive functional test suite to enable safe refactoring and ensure core business functionality works as expected. This provides regression protection for the metrics-processor codebase. Changes Test Infrastructure Added shared test fixtures in tests/fixtures/ (configs, graphite responses, helpers) Implemented custom assertion helpers for better error messages Configured cargo-tarpaulin for coverage measurement Test Coverage (95.34% library coverage) Core Metric Flag Evaluation: 11 unit tests for Lt/Gt/Eq operators, boundary conditions, null handling Service Health Aggregation: 11 tests for boolean expressions, weighted scoring, OR/AND operators Configuration Processing: 11 tests for template substitution, environment expansion, threshold overrides API Endpoints: 10 tests for REST handlers, error responses, Graphite compatibility Graphite Integration: 17 tests for query building, JSON parsing, error handling Coverage by Module Module Coverage src/config.rs 100.00% src/types.rs 98.55% src/common.rs 94.67% src/graphite.rs 94.37% src/api/v1.rs 92.31% Documentation Added doc/testing.md with test execution guide Updated README.md with test instructions Added Testing section to documentation SUMMARY.md Build & CI Fixed Makefile coverage targets to exclude binary files Added --lib --tests --exclude-files 'src/bin/*' flags to coverage commands Coverage threshold enforcement at 95% Test Execution # Run all tests cargo test # Run with coverage make coverage-check # Generate HTML coverage report make coverage-html ## Metrics - Total tests: 70+ (62 unit + 8 integration) - Execution time: < 1 second - Coverage: 95.34% (target: 95%) ## Related spec and documents - Spec: specs/002-functional-test-suite/spec.md - Plan: specs/002-functional-test-suite/plan.md - Tasks: specs/002-functional-test-suite/tasks.md Reviewed-by: Ilia Bakhterev Reviewed-by: Sergei Martynov
1 parent 2d3bb90 commit 0ba2a92

26 files changed

Lines changed: 5586 additions & 61 deletions

.dockerignore

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Git
2+
.git
3+
.gitignore
4+
5+
# CI/CD
6+
.github
7+
zuul.yaml
8+
.pre-commit-config.yaml
9+
10+
# IDE/Editor
11+
.vscode/
12+
.idea/
13+
*.iml
14+
*.swp
15+
*.swo
16+
17+
# Rust build artifacts
18+
target/
19+
**/*.rs.bk
20+
21+
# Documentation
22+
docs/
23+
doc/
24+
README.md
25+
CONTRIBUTING.md
26+
LICENSE
27+
28+
# Test artifacts
29+
tests/
30+
coverage/
31+
tarpaulin-report.html
32+
cobertura.xml
33+
34+
# Configuration
35+
config.yaml
36+
conf.d/
37+
38+
# Logs
39+
*.log
40+
41+
# Specs and planning
42+
specs/
43+
.specify/
44+
45+
# Environment
46+
.env*

.github/workflows/ci.yml

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
name: Execute tests and coverage
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
- master
8+
pull_request:
9+
10+
env:
11+
CARGO_TERM_COLOR: 'always'
12+
13+
jobs:
14+
test:
15+
runs-on: ubuntu-latest
16+
container:
17+
image: rust:latest
18+
steps:
19+
- uses: actions/checkout@v4
20+
21+
# Temporarily disabled linting and formatting checks, to be re-enabled later.
22+
# - name: Check formatting
23+
# run: make fmt-check
24+
#
25+
# - name: Run linter
26+
# run: make lint
27+
28+
- name: Run tests
29+
run: make test
30+
31+
coverage:
32+
runs-on: ubuntu-latest
33+
container:
34+
image: xd009642/tarpaulin:0.35.1
35+
options: --security-opt seccomp=unconfined
36+
needs: test
37+
steps:
38+
- uses: actions/checkout@v4
39+
40+
41+
- name: Run tests with coverage
42+
run: make coverage-check

.github/workflows/mdbook.yml

Lines changed: 0 additions & 60 deletions
This file was deleted.

.gitignore

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,9 @@ conf.d
2222
*.iml
2323

2424
# Ignore documentation output folder
25-
docs/
25+
docs/
26+
27+
# Coverage reports
28+
coverage/
29+
tarpaulin-report.html
30+
cobertura.xml

Makefile

Lines changed: 219 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,219 @@
1+
# Makefile for cloudmon-metrics
2+
# Rust project build, test, and quality automation
3+
4+
.PHONY: all build build-release build-convertor build-reporter \
5+
test test-verbose coverage coverage-html \
6+
fmt fmt-check lint lint-fix clean check \
7+
doc doc-serve doc-open doc-api help install-tools
8+
9+
# Binary names
10+
CONVERTOR_BIN = cloudmon-metrics-convertor
11+
REPORTER_BIN = cloudmon-metrics-reporter
12+
13+
# Tool versions
14+
TARPAULIN_VERSION = 0.35.1
15+
16+
# Default target
17+
all: fmt-check lint test build
18+
19+
# ============================================================================
20+
# Build targets
21+
# ============================================================================
22+
23+
## Build all debug binaries
24+
build:
25+
cargo build
26+
27+
## Build all release binaries (optimized)
28+
build-release:
29+
cargo build --release
30+
31+
## Build only the convertor binary (debug)
32+
build-convertor:
33+
cargo build --bin $(CONVERTOR_BIN)
34+
35+
## Build only the reporter binary (debug)
36+
build-reporter:
37+
cargo build --bin $(REPORTER_BIN)
38+
39+
## Build only the convertor binary (release)
40+
build-convertor-release:
41+
cargo build --release --bin $(CONVERTOR_BIN)
42+
43+
## Build only the reporter binary (release)
44+
build-reporter-release:
45+
cargo build --release --bin $(REPORTER_BIN)
46+
47+
## Check code compiles without producing binaries (faster)
48+
check:
49+
cargo check
50+
51+
# ============================================================================
52+
# Test targets
53+
# ============================================================================
54+
55+
## Run all tests (including binary targets)
56+
test:
57+
cargo test --all-targets
58+
59+
## Run tests with verbose output
60+
test-verbose:
61+
cargo test -- --nocapture
62+
63+
## Run tests with specific filter (usage: make test-filter FILTER=test_name)
64+
test-filter:
65+
cargo test $(FILTER)
66+
67+
# ============================================================================
68+
# Code coverage
69+
# ============================================================================
70+
71+
## Run tests with coverage (requires cargo-tarpaulin)
72+
coverage:
73+
cargo tarpaulin --lib --tests --exclude-files 'src/bin/*' --out Stdout --skip-clean
74+
75+
## Generate HTML coverage report
76+
coverage-html:
77+
cargo tarpaulin --lib --tests --exclude-files 'src/bin/*' --out Html --output-dir target/coverage --skip-clean
78+
@echo "Coverage report generated at target/coverage/tarpaulin-report.html"
79+
80+
## Run coverage with 95% threshold enforcement (library code only)
81+
coverage-check:
82+
cargo tarpaulin --lib --tests --exclude-files 'src/bin/*' --fail-under 95 --skip-clean
83+
84+
# ============================================================================
85+
# Code formatting
86+
# ============================================================================
87+
88+
## Format code using rustfmt
89+
fmt:
90+
cargo fmt
91+
92+
## Check formatting without making changes
93+
fmt-check:
94+
cargo fmt -- --check
95+
96+
# ============================================================================
97+
# Linting
98+
# ============================================================================
99+
100+
## Run clippy linter with warnings as errors
101+
lint:
102+
cargo clippy -- -D warnings
103+
104+
## Run clippy and automatically fix warnings where possible
105+
lint-fix:
106+
cargo clippy --fix --allow-dirty --allow-staged
107+
108+
# ============================================================================
109+
# Documentation
110+
# ============================================================================
111+
112+
## Build mdbook documentation
113+
doc:
114+
mdbook build doc/
115+
116+
## Serve documentation locally with live reload
117+
doc-serve:
118+
mdbook serve doc/
119+
120+
## Open documentation in browser
121+
doc-open:
122+
mdbook build doc/ --open
123+
124+
## Generate Rust API documentation
125+
doc-api:
126+
cargo doc --no-deps
127+
128+
## Generate and open Rust API documentation in browser
129+
doc-api-open:
130+
cargo doc --no-deps --open
131+
132+
## Clean generated documentation
133+
doc-clean:
134+
rm -rf docs/*
135+
136+
# ============================================================================
137+
# Cleanup
138+
# ============================================================================
139+
140+
## Clean build artifacts
141+
clean:
142+
cargo clean
143+
144+
## Clean and remove Cargo.lock (full clean)
145+
clean-all: clean
146+
rm -f Cargo.lock
147+
148+
# ============================================================================
149+
# Development helpers
150+
# ============================================================================
151+
152+
## Install required development tools
153+
install-tools:
154+
rustup component add rustfmt clippy
155+
cargo install cargo-tarpaulin --version $(TARPAULIN_VERSION)
156+
cargo install mdbook mdbook-mermaid mdbook-linkcheck
157+
158+
## Run all quality checks (CI simulation)
159+
ci: fmt-check lint test coverage-check
160+
161+
## Watch for changes and run tests (requires cargo-watch)
162+
watch:
163+
cargo watch -x test
164+
165+
## Update dependencies
166+
update:
167+
cargo update
168+
169+
# ============================================================================
170+
# Help
171+
# ============================================================================
172+
173+
## Show this help message
174+
help:
175+
@echo "Available targets:"
176+
@echo ""
177+
@echo " Build:"
178+
@echo " build - Build all debug binaries"
179+
@echo " build-release - Build all release binaries (optimized)"
180+
@echo " build-convertor - Build convertor binary (debug)"
181+
@echo " build-reporter - Build reporter binary (debug)"
182+
@echo " build-convertor-release - Build convertor binary (release)"
183+
@echo " build-reporter-release - Build reporter binary (release)"
184+
@echo " check - Check code compiles without producing binaries"
185+
@echo ""
186+
@echo " Test:"
187+
@echo " test - Run all tests"
188+
@echo " test-verbose - Run tests with verbose output"
189+
@echo " test-filter - Run tests matching FILTER (usage: make test-filter FILTER=name)"
190+
@echo ""
191+
@echo " Coverage:"
192+
@echo " coverage - Run tests with coverage report"
193+
@echo " coverage-html - Generate HTML coverage report"
194+
@echo " coverage-check - Run coverage with 95% threshold"
195+
@echo ""
196+
@echo " Code Quality:"
197+
@echo " fmt - Format code"
198+
@echo " fmt-check - Check code formatting"
199+
@echo " lint - Run clippy linter"
200+
@echo " lint-fix - Fix linter warnings automatically"
201+
@echo ""
202+
@echo " Documentation:"
203+
@echo " doc - Build mdbook documentation"
204+
@echo " doc-serve - Serve documentation locally with live reload"
205+
@echo " doc-open - Build and open documentation in browser"
206+
@echo " doc-api - Generate Rust API documentation"
207+
@echo " doc-api-open - Generate and open Rust API docs in browser"
208+
@echo " doc-clean - Clean generated documentation"
209+
@echo ""
210+
@echo " Utilities:"
211+
@echo " clean - Clean build artifacts"
212+
@echo " clean-all - Clean everything including Cargo.lock"
213+
@echo " install-tools - Install required development tools"
214+
@echo " ci - Run all CI checks (fmt, lint, test, coverage)"
215+
@echo " watch - Watch for changes and run tests"
216+
@echo " update - Update dependencies"
217+
@echo ""
218+
@echo " Default (all):"
219+
@echo " fmt-check lint test build"

0 commit comments

Comments
 (0)