|
| 1 | +# Integration Testing |
| 2 | + |
| 3 | +This document describes how to run integration tests for graft's external operators (Vault and NATS). |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +The integration tests verify that graft correctly interacts with external services like HashiCorp Vault and NATS. These tests use Docker containers to spin up real instances of the services, populate them with test data, and then run graft commands to verify the operators work correctly. |
| 8 | + |
| 9 | +## Prerequisites |
| 10 | + |
| 11 | +- Docker must be installed and running |
| 12 | +- The `graft` binary must be built (`make build`) |
| 13 | +- Ports 8200 (Vault) and 4222 (NATS) must be available |
| 14 | +- For NATS tests: `nats` CLI tool (will be auto-installed if missing) |
| 15 | + |
| 16 | +## Running Tests |
| 17 | + |
| 18 | +### Run All Integration Tests |
| 19 | + |
| 20 | +```bash |
| 21 | +make integration |
| 22 | +``` |
| 23 | + |
| 24 | +### Run Specific Tests |
| 25 | + |
| 26 | +```bash |
| 27 | +# Run only Vault tests |
| 28 | +scripts/integration --no-nats |
| 29 | + |
| 30 | +# Run only NATS tests |
| 31 | +scripts/integration --no-vault |
| 32 | + |
| 33 | +# Run with verbose output |
| 34 | +scripts/integration -v |
| 35 | + |
| 36 | +# Keep containers running after tests (for debugging) |
| 37 | +scripts/integration --keep |
| 38 | +``` |
| 39 | + |
| 40 | +## Test Coverage |
| 41 | + |
| 42 | +### Vault Operator Tests |
| 43 | + |
| 44 | +1. **Basic secret retrieval** - Fetches username/password from Vault |
| 45 | +2. **Secret with specific key** - Uses `:key` syntax to get specific fields |
| 46 | +3. **Multiple vault references** - Tests multiple operators in one document |
| 47 | +4. **Vault with defaults** - Tests `||` operator for fallback values |
| 48 | +5. **Invalid path handling** - Verifies error handling for missing secrets |
| 49 | +6. **Target-specific vault** - Tests `@target` syntax for multi-instance support |
| 50 | +7. **Vault in complex merge** - Tests vault operators across merged documents |
| 51 | + |
| 52 | +### NATS Operator Tests |
| 53 | + |
| 54 | +1. **Basic KV retrieval** - Fetches values from JetStream KV store |
| 55 | +2. **Object store YAML retrieval** - Fetches and parses YAML from object store |
| 56 | +3. **Multiple NATS references** - Tests multiple operators in one document |
| 57 | +4. **Invalid key handling** - Verifies error handling for missing keys |
| 58 | +5. **Target-specific NATS** - Tests `@target` syntax for multi-instance support |
| 59 | +6. **NATS with timeout** - Tests custom timeout configuration |
| 60 | + |
| 61 | +## Test Data |
| 62 | + |
| 63 | +### Vault Test Data |
| 64 | + |
| 65 | +The integration tests create the following secrets in Vault: |
| 66 | + |
| 67 | +- `secret/test/credentials` - Contains username, password, api_key |
| 68 | +- `secret/test/database` - Contains host, port, name |
| 69 | +- `secret/test/features` - Contains enabled (bool), max_users (int), tier (string) |
| 70 | + |
| 71 | +### NATS Test Data |
| 72 | + |
| 73 | +The integration tests create: |
| 74 | + |
| 75 | +**KV Store (`test-bucket`)**: |
| 76 | +- `config.host` = "api.example.com" |
| 77 | +- `config.port` = "8080" |
| 78 | +- `config.timeout` = "30" |
| 79 | +- `features.auth` = "enabled" |
| 80 | +- `features.cache` = "true" |
| 81 | + |
| 82 | +**Object Store (`test-objects`)**: |
| 83 | +- `config.yml` - A YAML file with version, app, and env fields |
| 84 | + |
| 85 | +## Environment Variables |
| 86 | + |
| 87 | +The integration tests use these environment variables: |
| 88 | + |
| 89 | +### Vault |
| 90 | +- `VAULT_ADDR` - Vault server address (default: http://localhost:8200) |
| 91 | +- `VAULT_TOKEN` - Vault authentication token |
| 92 | +- `VAULT_<TARGET>_ADDR` - Target-specific Vault address |
| 93 | +- `VAULT_<TARGET>_TOKEN` - Target-specific Vault token |
| 94 | + |
| 95 | +### NATS |
| 96 | +- `NATS_URL` - NATS server URL (default: nats://localhost:4222) |
| 97 | +- `NATS_TIMEOUT` - Connection timeout |
| 98 | +- `NATS_<TARGET>_URL` - Target-specific NATS URL |
| 99 | + |
| 100 | +## Output Format |
| 101 | + |
| 102 | +The tests use TAP (Test Anything Protocol) format for output: |
| 103 | + |
| 104 | +``` |
| 105 | +1..7 # Vault operator tests |
| 106 | +ok 1 - Vault basic secret retrieval |
| 107 | +ok 2 - Retrieved correct username |
| 108 | +ok 3 - Retrieved correct password |
| 109 | +... |
| 110 | +``` |
| 111 | + |
| 112 | +## Troubleshooting |
| 113 | + |
| 114 | +### Port Already in Use |
| 115 | + |
| 116 | +If you see "Port 8200 is already in use" or similar: |
| 117 | +- Check for running Vault/NATS instances: `docker ps` |
| 118 | +- Stop conflicting services or use different ports |
| 119 | + |
| 120 | +### Docker Not Found |
| 121 | + |
| 122 | +Ensure Docker is installed and the Docker daemon is running: |
| 123 | +```bash |
| 124 | +docker --version |
| 125 | +docker ps |
| 126 | +``` |
| 127 | + |
| 128 | +### Tests Fail to Connect |
| 129 | + |
| 130 | +If tests fail with connection errors: |
| 131 | +1. Check Docker is running |
| 132 | +2. Verify no firewall is blocking localhost connections |
| 133 | +3. Run with `-v` flag for verbose output |
| 134 | +4. Use `--keep` to inspect running containers |
| 135 | + |
| 136 | +### Debugging Failed Tests |
| 137 | + |
| 138 | +To debug failing tests: |
| 139 | +1. Run with verbose mode: `scripts/integration -v` |
| 140 | +2. Keep containers running: `scripts/integration --keep` |
| 141 | +3. Inspect container logs: `docker logs graft-test-vault-<PID>` |
| 142 | +4. Manually test with graft: `VAULT_ADDR=http://localhost:8200 VAULT_TOKEN=root-token-for-testing ./graft merge test.yml` |
0 commit comments