algo/tests
Dan Guido 172fc348ef
Add test to detect inline comments in Jinja2 expressions within YAML files (#14817)
* Add test to detect inline comments in Jinja2 expressions within YAML files

This test would have caught the bug reported where inline comments (#)
within Jinja2 expressions in YAML task files caused Ansible template
errors. The test:

- Extracts and validates all Jinja2 expressions from YAML files
- Specifically detects inline comments within {{ }} and {% %} blocks
- Includes regression test for the exact reported bug pattern
- Avoids false positives (# in strings, escaped #, comments outside expressions)
- Focuses on the critical inline comment issue

The original bug was in roles/strongswan/tasks/openssl.yml where comments
like "# Per-deployment UUID..." were placed inside a Jinja2 expression,
causing "unexpected char '#'" errors during playbook execution.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>

* Refactor test to use pytest framework and add comprehensive edge cases

- Converted standalone script to proper pytest test functions
- Replaced main() with individual test functions using pytest assertions
- Added comprehensive edge case tests for inline comment detection:
  * Hash symbols in strings (should pass)
  * Escaped hashes (should pass)
  * Comments in control blocks (should fail)
  * Multi-line expressions with comments (should fail)
  * URL fragments and hex colors (should pass)
- Test functions now properly integrate with pytest:
  * test_regression_openssl_inline_comments() - regression test
  * test_edge_cases_inline_comments() - comprehensive edge cases
  * test_yaml_files_no_inline_comments() - scan all YAML files
  * test_openssl_file_specifically() - test the originally buggy file

This addresses the review feedback about pytest integration and adds
the suggested test cases for better coverage.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>

* Fix linter issues in test_yaml_jinja2_expressions.py

- Fixed trailing whitespace issues (W293)
- Applied ruff formatting for consistent code style
- All tests still pass after formatting changes

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>

* Add mutation testing guidance to CLAUDE.md

Added a section on writing effective tests that emphasizes the importance
of verifying that tests actually detect failure cases. This lightweight
mutation testing approach ensures:

- Tests catch the specific bugs they're designed to prevent
- We avoid false confidence from tests that always pass
- Test purposes are clear and documented
- Both success and failure cases are validated

The guidance includes a concrete example from our recent inline comment
detection test, showing how to verify both the problematic pattern
(should fail) and the fixed pattern (should pass).

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>

---------

Co-authored-by: Claude <noreply@anthropic.com>
2025-08-07 11:12:23 -07:00
..
fixtures feat: Add comprehensive performance optimizations to reduce deployment time by 30-60% 2025-08-03 16:42:17 -07:00
integration feat: Add comprehensive performance optimizations to reduce deployment time by 30-60% 2025-08-03 16:42:17 -07:00
legacy-lxd Implement self-bootstrapping uv setup to resolve issue #14776 (#14814) 2025-08-06 22:10:56 -07:00
unit Add test to detect inline comments in Jinja2 expressions within YAML files (#14817) 2025-08-07 11:12:23 -07:00
README.md Implement self-bootstrapping uv setup to resolve issue #14776 (#14814) 2025-08-06 22:10:56 -07:00
test-aws-credentials.yml feat: Add comprehensive performance optimizations to reduce deployment time by 30-60% 2025-08-03 16:42:17 -07:00
test-local-config.sh Implement self-bootstrapping uv setup to resolve issue #14776 (#14814) 2025-08-06 22:10:56 -07:00
test-wireguard-async.yml Fix IPv6 address selection on BSD systems (#14786) 2025-08-03 17:15:27 -07:00
test-wireguard-fix.yml Fix IPv6 address selection on BSD systems (#14786) 2025-08-03 17:15:27 -07:00
test-wireguard-real-async.yml Fix IPv6 address selection on BSD systems (#14786) 2025-08-03 17:15:27 -07:00
test_bsd_ipv6.yml Fix IPv6 address selection on BSD systems (#14786) 2025-08-03 17:15:27 -07:00
test_cloud_init_template.py feat: Add comprehensive performance optimizations to reduce deployment time by 30-60% 2025-08-03 16:42:17 -07:00
test_package_preinstall.py feat: Add comprehensive performance optimizations to reduce deployment time by 30-60% 2025-08-03 16:42:17 -07:00
validate_jinja2_templates.py Implement self-bootstrapping uv setup to resolve issue #14776 (#14814) 2025-08-06 22:10:56 -07:00

Algo VPN Test Suite

Current Test Coverage

What We Test Now

  1. Basic Sanity (test_basic_sanity.py)

    • Python version >= 3.11
    • pyproject.toml exists and has dependencies
    • config.cfg is valid YAML
    • Ansible playbook syntax
    • Shell scripts pass shellcheck
    • Dockerfile exists and is valid
  2. Docker Build (test_docker_build.py)

    • Docker image builds successfully
    • Container can start
    • Ansible is available in container
  3. Configuration Generation (test-local-config.sh)

    • Ansible templates render without errors
    • Basic configuration can be generated
  4. Config Validation (test_config_validation.py)

    • WireGuard config format validation
    • Base64 key format checking
    • IP address and CIDR notation
    • Mobile config XML validation
    • Port range validation
  5. Certificate Validation (test_certificate_validation.py)

    • OpenSSL availability
    • Certificate subject formats
    • Key file permissions (600)
    • Password complexity
    • IPsec cipher suite security
  6. User Management (test_user_management.py) - Addresses #14745, #14746, #14738, #14726

    • User list parsing from config
    • Server selection string parsing
    • SSH key preservation
    • CA password handling
    • User config path generation
    • Duplicate user detection
  7. OpenSSL Compatibility (test_openssl_compatibility.py) - Addresses #14755, #14718

    • OpenSSL version detection
    • Legacy flag support detection
    • Apple device key format compatibility
    • Certificate generation compatibility
    • PKCS#12 export for mobile devices
  8. Cloud Provider Configs (test_cloud_provider_configs.py) - Addresses #14752, #14730, #14762

    • Cloud provider configuration validation
    • Hetzner server type updates (cx11 → cx22)
    • Azure dependency compatibility
    • Region format validation
    • Server size naming conventions
    • OS image naming validation

What We DON'T Test Yet

1. VPN Functionality

  • WireGuard configuration validation
    • Private/public key generation
    • Client config file format
    • QR code generation
    • Mobile config profiles
  • IPsec configuration validation
    • Certificate generation and validation
    • StrongSwan config format
    • Apple profile generation
  • SSH tunnel configuration
    • Key generation
    • SSH config file format

2. Cloud Provider Integrations

  • DigitalOcean API interactions
  • AWS EC2/Lightsail deployments
  • Azure deployments
  • Google Cloud deployments
  • Other providers (Vultr, Hetzner, etc.)

3. User Management

  • Adding new users
  • Removing users
  • Updating user configurations

4. Advanced Features

  • DNS ad-blocking configuration
  • On-demand VPN settings
  • MTU calculations
  • IPv6 configuration

5. Security Validations

  • Certificate constraints
  • Key permissions
  • Password generation
  • Firewall rules

Potential Improvements

Short Term (Easy Wins)

  1. Add job names to fix zizmor warnings

  2. Test configuration file generation without deployment:

    def test_wireguard_config_format():
        # Generate a test config
        # Validate it has required sections
        # Check key format with regex
    
  3. Test user management scripts in isolation:

    # Test that update-users generates valid YAML
    ./algo update-users --dry-run
    
  4. Add XML validation for mobile configs:

    xmllint --noout generated_configs/*.mobileconfig
    

Medium Term

  1. Mock cloud provider APIs to test deployment logic
  2. Container-based integration tests using Docker Compose
  3. Test certificate generation without full deployment
  4. Validate generated configs against schemas

Long Term

  1. End-to-end tests with actual VPN connections (using network namespaces)
  2. Performance testing for large user counts
  3. Upgrade path testing (old configs → new configs)
  4. Multi-platform client testing

Security Improvements (from zizmor)

Current status: No security issues found

Recommendations:

  1. Add explicit job names for better workflow clarity
  2. Consider pinning Ubuntu runner versions to specific releases
  3. Add GITHUB_TOKEN with minimal permissions when needed for API checks

Test Philosophy

Our approach focuses on:

  1. Fast feedback - Tests run in < 3 minutes
  2. No flaky tests - Avoid complex networking setups
  3. Test what matters - Config generation, not VPN protocols
  4. Progressive enhancement - Start simple, add coverage gradually