towerops-agent/.gitlab-ci.yml
Graham McIntire 0b3cc9121e
Add comprehensive unit test coverage (29.19%)
Add 73 unit tests covering all testable business logic:

- src/snmp/types.rs: 100% coverage (17/17 lines)
  * SnmpError display formatting
  * SnmpValue conversions (as_i64, as_f64)

- src/snmp/client.rs: 34.4% coverage (32/93 lines)
  * OID parsing/formatting/validation
  * SNMP value conversion for all types
  * Error mapping from snmp crate
  * Helper functions (starts_with, format_oid)

- src/ping.rs: 65.2% coverage (58/89 lines)
  * ICMP checksum calculation and verification
  * Echo request packet building
  * Reply packet parsing (raw and IP-wrapped)
  * IP header length extraction (IHL field)
  * Error handling for invalid packets

- src/version.rs: 54.0% coverage (27/50 lines)
  * Version parsing with optional 'v' prefix
  * Version comparison and sorting
  * Docker Hub response deserialization
  * Latest version extraction from tags

- src/websocket_client.rs: 7.7% coverage (17/221 lines)
  * SnmpValue to string conversion
  * Agent ID generation
  * Phoenix message serialization/deserialization
  * Helper functions (get_uptime_seconds, get_local_ip)

- src/main.rs: 20.0% coverage (11/55 lines)
  * SimpleLogger enabled() logic
  * HTTP/HTTPS to WebSocket URL conversion

- .gitlab-ci.yml: Add 'cargo test' to CI pipeline

Uncovered code requires integration testing:
- Network I/O (WebSocket, HTTP, Docker Hub API)
- System privileges (raw ICMP sockets)
- External services (SNMP devices, WebSocket servers)
- Runtime initialization (tokio main, logger setup)

All 73 tests pass. No test failures.
2026-01-19 15:07:00 -06:00

133 lines
4 KiB
YAML

# GitLab CI/CD Configuration for Towerops Agent
# Builds and publishes Docker image to Docker Hub
stages:
- test
- build
- release
variables:
# Docker Hub Registry
REGISTRY: docker.io
IMAGE_NAME: gmcintire/towerops-agent
# Docker BuildKit for better caching
DOCKER_BUILDKIT: 1
# Disable TLS for buildx compatibility
DOCKER_TLS_CERTDIR: ""
# Test stage - compile and check code
test:
stage: test
image: rust:1.83-alpine
before_script:
- apk add --no-cache musl-dev protobuf-dev
- rustup component add rustfmt clippy
script:
- cargo check --release
- cargo fmt -- --check
- cargo clippy -- -D warnings
- cargo test
only:
- branches
- merge_requests
cache:
key: ${CI_COMMIT_REF_SLUG}-cargo
paths:
- target/
# Build Docker image for branches (test build)
build:test:
stage: build
image: docker:24-dind
services:
- docker:24-dind
before_script:
- docker login -u $DOCKERHUB_USERNAME -p $DOCKERHUB_TOKEN
script:
- |
docker build \
--tag $REGISTRY/$IMAGE_NAME:$CI_COMMIT_REF_SLUG \
--tag $REGISTRY/$IMAGE_NAME:$CI_COMMIT_SHORT_SHA \
.
- docker push $REGISTRY/$IMAGE_NAME:$CI_COMMIT_REF_SLUG
- docker push $REGISTRY/$IMAGE_NAME:$CI_COMMIT_SHORT_SHA
only:
- branches
except:
- main
- tags
# Build and push for main branch (amd64 only for speed)
# Tags with timestamp for Watchtower auto-updates
build:main:
stage: build
image: docker:24-dind
services:
- docker:24-dind
before_script:
- echo "$DOCKERHUB_TOKEN" | docker login -u "$DOCKERHUB_USERNAME" --password-stdin
- docker buildx create --driver docker-container --name builder --use || docker buildx use builder
- docker buildx inspect --bootstrap
- apk add --no-cache git grep sed
# Generate version from git describe (includes commit count since last tag)
- VERSION=$(git describe --tags --always --dirty=-modified | sed 's/^v//')
- TIMESTAMP=$(date +%Y%m%d-%H%M%S)
- echo "Building version $VERSION at $TIMESTAMP"
script:
- |
docker buildx build \
--platform linux/amd64 \
--cache-from type=registry,ref=$REGISTRY/$IMAGE_NAME:buildcache \
--cache-to type=registry,ref=$REGISTRY/$IMAGE_NAME:buildcache,mode=max \
--tag $REGISTRY/$IMAGE_NAME:latest \
--tag $REGISTRY/$IMAGE_NAME:main \
--tag $REGISTRY/$IMAGE_NAME:$VERSION \
--tag $REGISTRY/$IMAGE_NAME:main-$CI_COMMIT_SHORT_SHA \
--tag $REGISTRY/$IMAGE_NAME:main-$TIMESTAMP \
--push \
.
- echo "IMAGE_TAG=$VERSION" >> build.env
- echo "Built and pushed with tags:"
- echo " - latest (for production)"
- echo " - main (for Watchtower tracking)"
- echo " - $VERSION (git describe)"
- echo " - main-$CI_COMMIT_SHORT_SHA (commit)"
- echo " - main-$TIMESTAMP (timestamp)"
artifacts:
reports:
dotenv: build.env
only:
- main
# Release build for tags (multi-architecture with caching)
# Note: Multi-arch builds are slow (~15-20 min) due to QEMU emulation for ARM64
release:multiarch:
stage: release
image: docker:24-dind
services:
- docker:24-dind
before_script:
- echo "$DOCKERHUB_TOKEN" | docker login -u "$DOCKERHUB_USERNAME" --password-stdin
- docker buildx create --driver docker-container --name multiarch-builder --use || docker buildx use multiarch-builder
- docker buildx inspect --bootstrap
script:
- |
VERSION=${CI_COMMIT_TAG#v}
docker buildx build \
--platform linux/amd64,linux/arm64 \
--cache-from type=registry,ref=$REGISTRY/$IMAGE_NAME:buildcache \
--cache-to type=registry,ref=$REGISTRY/$IMAGE_NAME:buildcache,mode=max \
--tag $REGISTRY/$IMAGE_NAME:$VERSION \
--tag $REGISTRY/$IMAGE_NAME:$CI_COMMIT_TAG \
--tag $REGISTRY/$IMAGE_NAME:latest \
--push \
.
echo "Released version: $VERSION"
echo "IMAGE_TAG=$VERSION" >> release.env
artifacts:
reports:
dotenv: release.env
only:
- tags