Skip to content

Testing the SCM Ansible Collection

This guide covers how to test the Palo Alto Networks Strata Cloud Manager Ansible Collection during development.

Testing Framework

The collection uses the following testing mechanisms:

  1. Linting and Style Checking:

  2. Ruff for Python linting and formatting

  3. isort for import sorting
  4. ansible-lint for Ansible-specific linting

  5. Functional Testing:

  6. Ansible playbooks for module testing

  7. Integration tests against SCM API

Setting Up Test Environment

Prerequisites

  • Python 3.11 or higher
  • Poetry installed
  • SCM credentials for integration testing

Initial Setup

# Install dependencies
poetry install

# Install pre-commit hooks
poetry run pre-commit install

Running Tests

Lint and Style Checks

# Run all linting checks
make lint

# Format code
make format

# Auto-fix linting issues
make fix

Functional Tests

Testing Individual Modules

To test a specific module, run the corresponding test playbook:

# Example: Test the address module
poetry run ansible-playbook tests/test_address.yaml

# Example: Test the address_group module
poetry run ansible-playbook tests/test_address_group.yaml

Configuring Test Credentials

The test playbooks load credentials from a vault-encrypted file:

  1. Create a tests/.vault_password file containing your vault password
  2. Create a tests/vault.yaml file with the following content:
client_id: "your_client_id"
client_secret: "your_client_secret"
tsg_id: "your_tsg_id"
  1. Encrypt the file:
ansible-vault encrypt tests/vault.yaml

Continuous Integration

The repository includes GitHub Actions workflows for automated testing:

  • Code Quality: Runs linting and style checks
  • Unit Tests: Runs unit tests with pytest
  • Integration Tests: Runs integration tests against SCM API

Writing Tests

Adding a New Test Playbook

  1. Create a new YAML file in the tests/ directory
  2. Include common setup tasks for required test objects
  3. Test your module's functionality:
  4. Create a resource
  5. Update a resource
  6. Verify idempotence
  7. Delete a resource
  8. Include cleanup tasks to remove test objects

Example test playbook structure:

---
- name: Test Module Name
  hosts: localhost
  gather_facts: false
  vars_files:
    - vault.yaml
  vars:
    provider:
      client_id: "{{ client_id }}"
      client_secret: "{{ client_secret }}"
      tsg_id: "{{ tsg_id }}"
      log_level: "INFO"
  tasks:
    # Setup tasks
    - name: Create prerequisites
      # ...

    # Test tasks
    - name: Create a resource
      cdot65.scm.module_name:
        provider: "{{ provider }}"
        name: "Test_Resource"
        # parameters...
        state: "present"
      register: create_result

    - name: Verify create result
      assert:
        that:
          - create_result.changed == true
          - create_result.resource.name == "Test_Resource"

    # Update test

    # Idempotence test

    # Delete test

    # Cleanup tasks

Test Best Practices

  1. Test All Module Functions:

  2. Create, update, delete operations

  3. Parameter validation
  4. Error handling

  5. Verify Idempotence:

  6. Run the same task twice and ensure the second run reports no changes

  7. Clean Up After Tests:

  8. Remove all test resources after testing

  9. Use always blocks to ensure cleanup happens even if tests fail

  10. Use Descriptive Names:

  11. Name test resources with a prefix to identify them as test objects

  12. Use descriptive task names to document what's being tested

  13. Register and Verify Results:

  14. Use register to capture task results

  15. Use assert tasks to verify expected behavior

Troubleshooting

Common Issues

  • Authentication Failures: Verify your SCM credentials in vault.yaml
  • API Rate Limiting: Add delays between API calls if hitting rate limits
  • Test Resource Conflicts: Ensure unique names for test resources

Debugging Tips

  • Enable verbose output with -v, -vv, or -vvv flags
  • Set provider log_level to "DEBUG" for detailed API logs
  • Use the debug module to inspect variables during playbook execution

For additional help, please open an issue on the GitHub repository.