mirror of
https://github.com/OneUptime/oneuptime.git
synced 2026-04-06 00:32:12 +02:00
Terraform Provider E2E Tests
End-to-end tests for the OneUptime Terraform Provider. These tests validate that the generated Terraform provider works correctly against a running OneUptime instance.
Directory Structure
e2e-tests/
├── scripts/
│ ├── index.sh # Main entry point - orchestrates the full test flow
│ ├── setup-test-account.sh # Creates test user, project, and API key
│ ├── run-tests.sh # Builds provider and runs all test cases
│ ├── lib.sh # Shared library with common test utilities
│ └── cleanup.sh # Cleans up test artifacts and state files
└── tests/
├── 01-label/ # Label resource tests
├── 02-monitor-status/ # Monitor status resource tests
├── 03-incident-severity/ # Incident severity resource tests
├── ... # More test directories
└── XX-resource-name/ # Each test has main.tf, variables.tf, and verify.sh
Running Tests
Full E2E Test Suite (CI/CD)
The index.sh script runs the complete test flow:
./scripts/index.sh
This will:
- Set up
config.envfrom the example file - Start OneUptime services via Docker Compose
- Wait for services to be ready
- Install npm dependencies
- Generate the Terraform provider
- Create a test account with API key
- Run all Terraform tests
- Clean up on exit
Running Individual Scripts
If you already have OneUptime running locally:
# Set up test account and API key
./scripts/setup-test-account.sh
# Run the Terraform tests
./scripts/run-tests.sh
# Clean up after testing
./scripts/cleanup.sh
Test Flow
Each test case in tests/ follows this pattern:
terraform init- Initialize the Terraform configurationterraform plan- Create an execution planterraform apply- Create the resourcesverify.sh- Run API validation to verify resources were created correctlyterraform destroy- Clean up created resources- Verify deletion via API
Shared Library (lib.sh)
The scripts/lib.sh file provides common utility functions for verify.sh scripts:
Helper Functions
| Function | Description |
|---|---|
unwrap_value "$json" |
Unwrap API values from wrapper format (e.g., {"_type": "Color", "value": "#FF5733"} → #FF5733) |
get_output "name" |
Get a Terraform output value safely (returns empty string if not found) |
assert_not_empty "$value" "name" |
Assert that a value is not empty |
assert_equals "$expected" "$actual" "name" |
Assert two values are equal |
api_get_resource "/api/endpoint" "$id" '{"select": true}' |
Make an API call to get a resource |
verify_resource_exists "/api/endpoint" "$id" |
Verify a resource exists in the API |
validate_field "$response" "field" "$expected" |
Validate a field from API response (handles wrapper unwrapping) |
check_idempotency [strict] |
Run idempotency check (terraform plan should show no changes) |
print_header "Test Name" |
Print test header |
print_passed "Test Name" |
Print test passed message |
print_failed "Test Name" |
Print test failed message and exit |
Example verify.sh
#!/bin/bash
set -e
# Source common library
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
source "$SCRIPT_DIR/../../scripts/lib.sh"
print_header "My Resource Verification"
# Get terraform outputs
RESOURCE_ID=$(get_output resource_id)
EXPECTED_NAME=$(get_output resource_name)
# Verify resource exists
if ! verify_resource_exists "/api/my-resource" "$RESOURCE_ID"; then
print_failed "My Resource Verification"
fi
# Get full resource for validation
RESPONSE=$(api_get_resource "/api/my-resource" "$RESOURCE_ID" '{"_id": true, "name": true}')
# Validate fields
validation_failed=0
validate_field "$RESPONSE" "name" "$EXPECTED_NAME" || validation_failed=1
if [ $validation_failed -eq 1 ]; then
print_failed "My Resource Verification"
fi
# Check idempotency
check_idempotency true # strict mode - fails on any changes
print_passed "My Resource Verification"
Environment Variables
The following environment variables are used:
| Variable | Default | Description |
|---|---|---|
ONEUPTIME_URL |
http://localhost |
OneUptime instance URL |
TF_VAR_api_key |
(generated) | API key for authentication |
TF_VAR_project_id |
(generated) | Project ID for resources |
CI/CD
Tests run automatically via GitHub Actions on:
- Pull requests
- Pushes to
main,master, ordevelopbranches - Manual workflow dispatch
See .github/workflows/terraform-provider-e2e.yml for the workflow configuration.
Adding New Tests
- Create a new directory in
tests/with the naming conventionXX-resource-name/ - Add
main.tfwith the Terraform configuration - Add
variables.tfwith required variables - Add
verify.shfor API validation (source the shared library) - Test directories are auto-discovered by
run-tests.sh
Test Naming Convention
01-XX- Basic resource creation tests09-XX-crud- Full CRUD operation testsXX-server-defaults- Tests for server-computed default valuesXX-idempotency- Tests for idempotency verification