Princeton-CDH / cdh-ansible

CDH Ansible playbook repository
Apache License 2.0
1 stars 3 forks source link

Refactor: Convert build_npm Role to Use Molecule for Testing #245

Open kayiwa opened 1 week ago

kayiwa commented 1 week ago

This issue aims to improve the reliability and maintainability of the build_npm role by integrating Molecule for testing. Molecule provides a framework for creating and running automated tests for Ansible roles, ensuring they function as expected and remain compatible across different environments.

Tasks:

  1. Install Molecule:

    • If not already installed, install Molecule on the development machine: pip install molecule
    • This can also be in the Pipfile. (Look at the README for how python packages are managed) (add new molecule on requirements.txt (?))
  2. Initialize Molecule in the Role:

    • Navigate to the role directory: cd roles/build_dependencies]
    • Possible to borrow and example role like PUL
    • Initialize Molecule: molecule init
    • Choose the ansible verifier based on the role's requirements.
  3. Create molecule.yml:

    • Configure the testing environment in molecule.yml. This includes:
      • Selecting a base image.
      • Specifying provisioner options (Ansible version, inventory).
      • Configuring verifiers (test frameworks like Ansible, Testinfra).
      • Setup Github CI/CD
  4. Write Tests:

    • Create tests in the verify.yml file (or using other testing frameworks) to validate the role's functionality.
    • Use assertions to check if resources are correctly configured, files exist, services are running, and commands produce the expected output.
  5. Run Molecule Tests:

    • Execute molecule test to run the tests.
    • Analyze the test results and fix any failures or errors in the role.
  6. Integrate with CI/CD:

    • If applicable, integrate Molecule tests into the CI/CD pipeline to ensure that changes to the role do not break existing functionality.

Example molecule.yml (Docker):

---
dependency:
  name: galaxy
driver:
  name: docker
platforms:
  - name: instance
    image: ubuntu:latest
provisioner:
  name: ansible
  playbooks:
    converge: ../playbook.yml
verifier:
  name: ansible

Benefits:

Improved Reliability: Catch errors and regressions early through automated testing. Increased Maintainability: Ensure the role remains functional across different environments and Ansible versions. Simplified Development: Develop and test the role in an isolated environment. Enhanced Documentation: Molecule tests serve as executable documentation of the role's intended behavior.

Additional Notes:

kayiwa commented 4 days ago

related to #165