Skip to content

Commit

Permalink
Add test
Browse files Browse the repository at this point in the history
  • Loading branch information
ethinot committed Sep 11, 2024
1 parent 7043c64 commit cef2eb3
Show file tree
Hide file tree
Showing 11 changed files with 311 additions and 14 deletions.
7 changes: 7 additions & 0 deletions .ansible-lint
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---

profile: production

exclude_paths:
- .github
- .manala
21 changes: 21 additions & 0 deletions .github/workflows/lint.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name: Lint

on:
pull_request:
workflow_dispatch:

jobs:
lint:
name: Lint
runs-on: ubuntu-24.04
steps:

- name: Checkout
uses: actions/checkout@v4

- name: Set up system
uses: ./.manala/github/system/setup

- name: Lint
run: |
make lint VERBOSE=1
38 changes: 38 additions & 0 deletions .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
name: Test

on:
pull_request:
workflow_dispatch:

jobs:
test:
name: Test
runs-on: ubuntu-24.04
steps:

- name: Checkout
uses: actions/checkout@v4

- name: Set up system
uses: ./.manala/github/system/setup

- name: Sanity
run: |
make test.sanity VERBOSE=1
- name: Units
run: |
make test.units VERBOSE=1 COVERAGE=1
- name: Integration
run: |
make test.integration VERBOSE=1 COVERAGE=1
- name: Coverage
run: |
make test.coverage VERBOSE=1
- name: Codecov
uses: codecov/codecov-action@v2
with:
fail_ci_if_error: false
14 changes: 14 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Changelog

All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]


### Added

- Initial release

82 changes: 69 additions & 13 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,72 @@

include .manala/Makefile

#########
# Setup #
#########

# ## Project - Setup
# setup: setup.ansible

# ## Project - Setup ansible
# setup.ansible: SHELL := $(MANALA_DOCKER_SHELL)
# setup.ansible:
# ansible-galaxy install \
# --verbose \
# -r requirements.yaml
########
# Lint #
########

## Lint - Lint collection [VERBOSE]
lint:
$(call manala_docker_shell, ansible-lint \
$(if $(VERBOSE), -v) \
--force-color \
)
.PHONY: lint

########
# Test #
########

## Test - Run all tests (but coverage)
test: test.sanity test.units test.integration
.PHONY: test

## Test - Run sanity tests [VERBOSE]
test.sanity:
$(call manala_docker_shell, ansible-test sanity \
--requirements \
--venv \
--python 3.11 \
$(if $(VERBOSE), --verbose) \
--color yes \
--exclude .github/ \
--exclude .manala/ \
)
.PHONY: test.sanity

## Test - Run units tests [VERBOSE|COVERAGE]
test.units:
$(call manala_docker_shell, ansible-test units \
--requirements \
--venv \
--python 3.11 \
$(if $(VERBOSE), --verbose) \
$(if $(COVERAGE), --coverage) \
--color yes \
)
.PHONY: test.units

## Test - Run integration tests [VERBOSE|COVERAGE]
test.integration:
$(call manala_docker_shell, ansible-test integration \
--requirements \
--venv \
--python 3.11 \
$(if $(VERBOSE), --verbose) \
$(if $(COVERAGE), --coverage) \
--color yes \
)
.PHONY: test.integration

## Test - Run coverage [VERBOSE]
test.coverage:
$(call manala_docker_shell, ansible-test coverage xml \
--requirements \
--venv \
--python 3.11 \
--group-by command \
--group-by version \
$(if $(VERBOSE), --verbose) \
--color yes \
)
.PHONY: test.coverage
2 changes: 1 addition & 1 deletion plugins/modules/path.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
description:
- Handle path
author:
- Manala (contact@manala.io)
- Manala (@manala)
'''

EXAMPLES = '''
Expand Down
2 changes: 2 additions & 0 deletions tests/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
__pycache__
/output
1 change: 1 addition & 0 deletions tests/integration/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
inventory
148 changes: 148 additions & 0 deletions tests/integration/targets/path/tasks/main.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
---

###########
# Present #
###########

- name: Present
tags: present
vars:
path: /tmp/integration/path/present
block:
- name: Present | Setup path
ansible.builtin.file: # noqa: risky-file-permissions
path: "{{ path }}"
state: "{{ item }}"
loop: [absent, directory]
- name: Present | Converge
manala.path.path:
path: "{{ [path, item.path] | ansible.builtin.path_join }}"
state: present
loop:
- path: foo
- name: Present | Stats
ansible.builtin.stat:
path: "{{ [path, item.path] | ansible.builtin.path_join }}"
register: stats
loop:
- path: foo
- name: Present | Verify
ansible.builtin.assert:
that:
- stats.results[0].stat.exists is true
- stats.results[0].stat.isdir is true

##########
# Absent #
##########

- name: Absent
tags: absent
vars:
path: /tmp/integration/path/absent
block:
- name: Absent | Setup path
ansible.builtin.file: # noqa: risky-file-permissions
path: "{{ path }}"
state: "{{ item }}"
loop: [absent, directory]
- name: Absent | Prepare files
ansible.builtin.file: # noqa: risky-file-permissions
path: "{{ [path, item.path] | ansible.builtin.path_join }}"
state: touch
loop:
- path: foo
- name: Absent | Converge
manala.path.path:
path: "{{ [path, item.path] | ansible.builtin.path_join }}"
state: absent
loop:
- path: foo
- name: Absent | Stats
ansible.builtin.stat:
path: "{{ [path, item.path] | ansible.builtin.path_join }}"
register: stats
loop:
- path: foo
- name: Absent | Verify
ansible.builtin.assert:
that:
- stats.results[0].stat.exists is false

###########
# Content #
###########

- name: Content
tags: content
vars:
path: /tmp/integration/path/content
block:
- name: Content | Setup path
ansible.builtin.file: # noqa: risky-file-permissions
path: "{{ path }}"
state: "{{ item }}"
loop: [absent, directory]
- name: Content | Converge
manala.path.path:
path: "{{ [path, item.path] | ansible.builtin.path_join }}"
content: "{{ item.content }}"
loop:
- path: foo
content: foo
- name: Content | Stats
ansible.builtin.stat:
path: "{{ [path, item.path] | ansible.builtin.path_join }}"
register: stats
loop:
- path: foo
- name: Content | Contents
ansible.builtin.slurp:
src: "{{ [path, item.path] | ansible.builtin.path_join }}"
register: contents
loop:
- path: foo
- name: Content | Verify
ansible.builtin.assert:
that:
- stats.results[0].stat.exists is true
- contents.results[0].content | b64decode == 'foo'

############
# Template #
############

- name: Template
tags: template
vars:
path: /tmp/integration/path/template
block:
- name: Template | Setup path
ansible.builtin.file: # noqa: risky-file-permissions
path: "{{ path }}"
state: "{{ item }}"
loop: [absent, directory]
- name: Template | Converge
manala.path.path:
path: "{{ [path, item.path] | ansible.builtin.path_join }}"
template: "{{ item.template }}"
loop:
- path: foo
template: foo.j2
- name: Template | Stats
ansible.builtin.stat:
path: "{{ [path, item.path] | ansible.builtin.path_join }}"
register: stats
loop:
- path: foo
- name: Template | Contents
ansible.builtin.slurp:
src: "{{ [path, item.path] | ansible.builtin.path_join }}"
register: contents
loop:
- path: foo
- name: Template | Verify
ansible.builtin.assert:
that:
- stats.results[0].stat.exists is true
- contents.results[0].content | b64decode == 'foo'
1 change: 1 addition & 0 deletions tests/integration/targets/path/templates/foo.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
foo
9 changes: 9 additions & 0 deletions tests/unit/plugins/test_path.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import unittest


class Test(unittest.TestCase):

def test_nothing(self):
self.nothing = None
self.assertIsNone(self.nothing)
# To continue

0 comments on commit cef2eb3

Please sign in to comment.