Skip to content

Commit

Permalink
Merge pull request #43 from shantanoo-desai/develop
Browse files Browse the repository at this point in the history
tests: add Integrity Tests for Generated Files / Directories
  • Loading branch information
shantanoo-desai authored Apr 1, 2023
2 parents 6669e77 + 04fe8f6 commit 352158b
Show file tree
Hide file tree
Showing 12 changed files with 223 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ on:
- pull_request

jobs:
build:
lint:
runs-on: ubuntu-latest

steps:
Expand Down
46 changes: 46 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
---
name: Integrity Tests with Ansible

on:
push:
branches:
- "main"
pull_request:
branches:
- "main"

jobs:
tests:
runs-on: ubuntu-latest

strategy:
fail-fast: true
matrix:
python-version:
- "3.10"
- "3.9"
- "3.8"

steps:
- name: Checkout Codebase
uses: actions/checkout@v3

- name: Pull eclipse-mosquitto image
run: docker pull eclipse-mosquitto:2.0.15

- name: Set up python ${{ matrix.python-version }}
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}

- name: Install ansible
run: python -m pip install ansible docker passlib

- name: Generate the Default Komponist Stack
run: ansible-playbook -vv generate_stack.yml

- name: Test Integrity of Generated Directories
run: ansible-playbook tests/test_generated_directories.yml

- name: Test Integrity of Generated Files
run: ansible-playbook tests/test_generated_files.yml
4 changes: 2 additions & 2 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"yaml.schemas": {
"https://raw.githubusercontent.com/ansible/ansible-lint/main/src/ansiblelint/schemas/ansible.json#/$defs/playbook": "generate-stack.yml",
"https://raw.githubusercontent.com/ansible/ansible-lint/main/src/ansiblelint/schemas/ansible.json#/$defs/tasks": ["tasks/"],
"https://raw.githubusercontent.com/ansible/ansible-lint/main/src/ansiblelint/schemas/ansible.json#/$defs/playbook": ["generate-stack.yml", "tests/test_*.yml"],
"https://raw.githubusercontent.com/ansible/ansible-lint/main/src/ansiblelint/schemas/ansible.json#/$defs/tasks": ["tasks/*.yml", "tests/services/*.yml"],
"https://raw.githubusercontent.com/compose-spec/compose-spec/master/schema/compose-spec.json": ["docker-compose.*.yml", "docker-compose.yml"],
"https://json.schemastore.org/traefik-v2.json": "traefik.yml",
"https://json.schemastore.org/traefik-v2-file-provider.json": "dynamic.yml"
Expand Down
3 changes: 2 additions & 1 deletion ansible.cfg
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
[defaults]
ansible_managed = This file is generated by Komponist.%n
Generated On: %Y-%m-%d %H:%M:%S %n
Generated On: %Y-%m-%d %H:%M:%S %n
stdout_callback = yaml
1 change: 1 addition & 0 deletions tasks/configure-mosquitto.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
user: "1000"
state: started
command: mosquitto_passwd -U /mosquitto/config/users
pull: true
volumes:
- "{{ komponist.deploy_dir }}/mosquitto/users:/mosquitto/config/users"

Expand Down
11 changes: 11 additions & 0 deletions tests/services/test_config_grafana.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
- name: Get stats for Grafana Files in dedicated directory under deploy_dir
ansible.builtin.stat:
path: "{{ playbook_dir }}/../{{ komponist.deploy_dir }}/grafana/provisioning/datasources/datasources.yml"
register: test_grafana_datasources_stat

- name: Test if the Grafana Datasources File in dedicated directory exists under deploy_dir
ansible.builtin.assert:
that: test_grafana_datasources_stat.stat.exists and test_grafana_datasources_stat.stat.mode == '0755'
fail_msg: "FAIL: Datasources File DOES NOT exist in {{ komponist.deploy_dir }}/grafana/provisioning/datasources directory."
success_msg: "PASS: Datasources File exists in {{ komponist.deploy_dir }}/grafana/provisioning/datasources directory."
11 changes: 11 additions & 0 deletions tests/services/test_config_influxdb.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
- name: Get stats for InfluxDBv1/v2 environment file in dedicated directory under deploy_dir
ansible.builtin.stat:
path: "{{ playbook_dir }}/../{{ komponist.deploy_dir }}/{{ item }}/{{ item }}.env"
register: test_service_envfile_stat

- name: Test if the InfluxDBv1/v2 environment file in dedicated directory exists under deploy_dir
ansible.builtin.assert:
that: test_service_envfile_stat.stat.exists and test_service_envfile_stat.stat.mode == '0755'
fail_msg: "FAIL: {{ item }}.env DOES NOT exist in {{ komponist.deploy_dir }}/{{ item }} directory."
success_msg: "PASS: {{ item }}.env File exists in {{ komponist.deploy_dir }}/{{ item }} directory."
18 changes: 18 additions & 0 deletions tests/services/test_config_mosquitto.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
---
- name: Get stats for Mosquitto related configuration files
ansible.builtin.stat:
path: "{{ playbook_dir }}/../{{ komponist.deploy_dir }}/mosquitto/{{ item }}"
register: test_mosquitto_files_stat
loop:
- acl
- mosquitto.conf
- users

- name: Test if Mosquitto related configuration files exist in deploy_dir
ansible.builtin.assert:
that: item.stat.exists and item.stat.mode == '0755'
fail_msg: "FAIL: {{ item.item }} DOES NOT exist in {{ komponist.deploy_dir }}/mosquitto directory"
success_msg: "PASS: {{ item.item }} exists in {{ komponist.deploy_dir }}/mosquitto directory"
loop: "{{ test_mosquitto_files_stat.results | map('ansible.utils.keep_keys', ['item', 'exists', 'mode']) }}"
loop_control:
label: "{{ item.item }}"
11 changes: 11 additions & 0 deletions tests/services/test_config_nodered.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
- name: Get stats for Node-RED settings file
ansible.builtin.stat:
path: "{{ playbook_dir }}/../{{ komponist.deploy_dir }}/nodered/settings.js"
register: test_nodered_settings_file_stat

- name: Test if settings.js Exists under deploy_dir directory
ansible.builtin.assert:
that: test_nodered_settings_file_stat.stat.exists and test_nodered_settings_file_stat.stat.mode == '0755'
fail_msg: "FAIL: settings.js DOES NOT exist under {{ komponist.deploy_dir }}/nodered directory"
success_msg: "PASS: settings.js exists under {{ komponist.deploy_dir }}/nodered directory"
15 changes: 15 additions & 0 deletions tests/services/test_config_traefik.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
- name: Get stats for Traefik related configuration files
ansible.builtin.stat:
path: "{{ playbook_dir }}/../{{ komponist.deploy_dir }}/traefik/{{ item }}"
register: test_traefik_files_stat
loop:
- traefik.yml
- configurations/dynamic.yml

- name: Test if Traefik related configuration files in deploy_dir
ansible.builtin.assert:
that: item.stat.exists and item.stat.mode == '0755'
fail_msg: "FAIL: {{ item.item }} DOES NOT exist under {{ komponist.deploy_dir }}/traefik directory"
success_msg: "PASS: {{ item.item }} exists under {{ komponist.deploy_dir }}/traefik directory"
loop: "{{ test_traefik_files_stat.results | map('ansible.utils.keep_keys', ['item', 'exists', 'mode']) }}"
36 changes: 36 additions & 0 deletions tests/test_generated_directories.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
---
- name: Directory Structure Creation Tests
vars_files:
- "{{ playbook_dir }}/../vars/config.yml"
hosts: localhost
gather_facts: false

pre_tasks:
- name: Collect Stat Information for deploy_dir
ansible.builtin.stat:
path: "{{ playbook_dir }}/../{{ komponist.deploy_dir }}"
register: test_deploy_dir_state

- name: Collect Stat Information for Services under deploy_dir
ansible.builtin.stat:
path: "{{ playbook_dir }}/../{{ komponist.deploy_dir }}/{{ item }}"
register: test_deploy_dir_service_state
loop: "{{ komponist.configuration.keys() }}"

tasks:
- name: Test if deploy_dir directory is created
ansible.builtin.assert:
that:
- test_deploy_dir_state.stat.exists and test_deploy_dir_state.stat.isdir
fail_msg: "Cannot Find the {{ komponist.deploy_dir }} in the root of the repository."
success_msg: "{{ komponist.deploy_dir }} exists in the root of the repository."

- name: Test if the subdirectories for each Service exists under deploy_dir
ansible.builtin.assert:
that: item.stat.exists and item.stat.isdir and item.stat.mode == '0755'
fail_msg: "FAIL: {{ item.item }} does not exist under {{ komponist.deploy_dir }} directory"
success_msg: "PASS: {{ item.item }} exist under {{ komponist.deploy_dir }} directory"
quiet: false
loop: "{{ test_deploy_dir_service_state.results | map('ansible.utils.keep_keys', ['item', 'exists', 'isdir', 'mode']) }}"
loop_control:
label: "{{ item.item }}"
69 changes: 69 additions & 0 deletions tests/test_generated_files.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
---
- name: File Generation Tests
vars_files:
- "{{ playbook_dir }}/../vars/config.yml"
hosts: localhost
gather_facts: false
tasks:
- name: Check for .env File in deploy_dir
block:
- name: Get stats for .env in deploy_dir
ansible.builtin.stat:
path: "{{ playbook_dir }}/../{{ komponist.deploy_dir }}/.env"
register: test_compose_env_file_stat

- name: Check if .env file is generated
ansible.builtin.assert:
that: test_compose_env_file_stat.stat.exists and test_compose_env_file_stat.stat.mode == "0755"
fail_msg: "FAIL: .env File DOES NOT exist in {{ komponist.deploy_dir }} directory."
success_msg: "PASS: .env File exists in {{ komponist.deploy_dir }} directory."

- name: Check for Generated Docker Compose Services Files
block:
- name: Get stats for configured Docker Compose services
ansible.builtin.stat:
path: "{{ playbook_dir }}/../{{ komponist.deploy_dir }}/docker-compose.{{ item }}.yml"
loop: "{{ komponist.configuration.keys() }}"
register: test_compose_services_files_stat

- name: Check if respective Docker Compose Service Files exist
ansible.builtin.assert:
that: item.stat.exists and item.stat.mode
fail_msg: "FAIL: docker-compose.{{ item.item }}.yml DOES NOT exist in {{ komponist.deploy_dir }} directory."
success_msg: "PASS: docker-compose.{{ item.item }}.yml File exists in {{ komponist.deploy_dir }} directory."
loop: "{{ test_compose_services_files_stat.results | map('ansible.utils.keep_keys', ['item', 'exists', 'mode']) }}"
loop_control:
label: "{{ item.item }}"

- name: Check for Deployment-Ready docker-compose.yml File
block:
- name: Get stats for Deployment-Ready docker-compose.yml file
ansible.builtin.stat:
path: "{{ playbook_dir }}/../{{ komponist.deploy_dir }}/docker-compose.yml"
register: test_compose_deployment_file_stat

- name: Check if docker-compose.yml File exist
ansible.builtin.assert:
that: test_compose_deployment_file_stat.stat.exists and test_compose_deployment_file_stat.stat.mode == '0644'
fail_msg: "FAIL: docker-compose.yml DOES NOT exist in {{ komponist.deploy_dir }} directory."
success_msg: "PASS: docker-compose.yml File exists in {{ komponist.deploy_dir }} directory."

- name: Check InfluxDB V1/V2 Generated Files in deploy_dir
ansible.builtin.include_tasks: "{{ playbook_dir }}/services/test_config_influxdb.yml"
with_items: "{{ komponist.configuration.keys() | select('match', 'influxdbv[1-2]') | list }}"

- name: Check Mosquitto MQTT Generated Files in deploy_dir
ansible.builtin.include_tasks: "{{ playbook_dir }}/services/test_config_mosquitto.yml"
when: "'mosquitto' in komponist.configuration.keys()"

- name: Check Grafana Generated Files in deploy_dir
ansible.builtin.include_tasks: "{{ playbook_dir }}/services/test_config_grafana.yml"
when: "'grafana' in komponist.configuration.keys() and ('influxdbv1' in komponist.configuration.keys() or 'influxdbv2' in komponist.configuration.keys())"

- name: Check Node-RED Generated Files in deploy_dir
ansible.builtin.include_tasks: "{{ playbook_dir }}/services/test_config_nodered.yml"
when: "'nodered' in komponist.configuration.keys()"

- name: Check Traefik Generated Files in deploy_dir
ansible.builtin.include_tasks: "{{ playbook_dir }}/services/test_config_traefik.yml"
when: "'traefik' in komponist.configuration.keys()"

0 comments on commit 352158b

Please sign in to comment.