Skip to content

Commit

Permalink
Added tox for linting and testing (#191)
Browse files Browse the repository at this point in the history
Replaces Molecule Wrapper.
  • Loading branch information
freemanjp authored Jul 23, 2023
1 parent 4f4dc7f commit 3c974de
Show file tree
Hide file tree
Showing 40 changed files with 2,599 additions and 1,021 deletions.
3 changes: 3 additions & 0 deletions .ansible-lint
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
skip_list:
- '106' # Role name {} does not match \^[a-z][a-z0-9_]+$\ pattern'
exclude_paths:
- .tox/
- .venv/
46 changes: 46 additions & 0 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// For format details, see https://aka.ms/devcontainer.json. For config options, see the
// README at: https://github.com/devcontainers/templates/tree/main/src/python
{
"name": "ansible-role-golang",
"image": "mcr.microsoft.com/devcontainers/python:0-3.9",
"features": {
"ghcr.io/devcontainers/features/common-utils:2": {
"installZsh": true,
"configureZshAsDefaultShell": true,
"installOhMyZsh": true
},
"ghcr.io/devcontainers/features/docker-in-docker:2": {},
"ghcr.io/devcontainers/features/github-cli:1": {},
"ghcr.io/devcontainers-contrib/features/apt-get-packages:1": {
"packages": "fzf grc zoxide"
},
"ghcr.io/devcontainers-contrib/features/fd:1": {},
"ghcr.io/devcontainers-contrib/features/ripgrep:1": {},
"ghcr.io/devcontainers-contrib/features/starship:1": {},
"ghcr.io/devcontainers-contrib/features/zsh-plugins:0": {
"plugins": "autopep8 dirhistory docker fd fzf git-escape-magic gh git grc pip pipenv ripgrep starship sudo zoxide zsh-autosuggestions zsh-syntax-highlighting",
"omzPlugins": "https://github.com/zsh-users/zsh-autosuggestions https://github.com/zsh-users/zsh-syntax-highlighting.git"
}
},
"customizations": {
"vscode": {
"extensions": [
"DavidAnson.vscode-markdownlint",
"donjayamanne.githistory",
"EditorConfig.EditorConfig",
"github.vscode-github-actions",
"GitHub.vscode-pull-request-github",
"ldez.ignore-files",
"me-dutour-mathieu.vscode-github-actions",
"oderwat.indent-rainbow",
"redhat.ansible",
"streetsidesoftware.code-spell-checker",
"wholroyd.jinja"
]
}
},
"remoteEnv": {
"PATH": "/home/vscode/.local/bin:${containerEnv:PATH}"
},
"postCreateCommand": "pip3 install --user -r requirements/dev.txt"
}
10 changes: 9 additions & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,17 @@ indent_size = 4
trim_trailing_whitespace = true

# Files with a smaller indent
[*.yml]
[*.{md,yml}]
indent_size = 2

# Ansible Lint
[.ansible-lint]
indent_size = 2

# Jinja2 template files
[*.j2]
end_of_line = lf

# Devcontainer config
[devcontainer.json]
indent_style = tab
3 changes: 3 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
# to native line endings on checkout.
.gitignore text
.gitattributes text
.python-version text
*.xml text
*.yml text
*.yaml text
Expand All @@ -20,6 +21,8 @@
*.less text
*.scss text
*.cfg text
*.in text
*.ini text
LICENSE text

# Declare files that will always have CRLF line endings on checkout.
Expand Down
95 changes: 68 additions & 27 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,60 @@ on:
pull_request: {}

env:
MOLECULEW_USE_SYSTEM: 'true'
PY_COLORS: '1'
ANSIBLE_FORCE_COLOR: '1'

jobs:
lint:
name: Lint
runs-on: ubuntu-20.04

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

- name: Set up Python
id: setup-python
uses: actions/setup-python@v4
with:
python-version-file: '.python-version'

- name: Tox cache
id: tox-cache
uses: actions/cache@v3
with:
path: ~/.cache/pip
key: tox-${{ hashFiles('requirements/tox.txt') }}-py${{ steps.setup-python.outputs.python-version}}

- name: Lint cache
id: lint-cache
uses: actions/cache@v3
with:
path: .tox
key: lint-${{ hashFiles('requirements/lint.txt') }}-py${{ steps.setup-python.outputs.python-version}}

- name: Install tox
run: pip3 install --no-compile -r requirements/tox.txt

- name: Lint
run: tox run -e lint

- name: Compact lint cache
if: steps.lint-cache.outputs.cache-hit != 'true'
run: find .tox -name '__pycache__' -exec rm -rf {} +

- name: Compact tox cache
if: steps.tox-cache.outputs.cache-hit != 'true'
run: find ~/.cache/pip -name '__pycache__' -exec rm -rf {} +

test:
name: Molecule test
runs-on: ubuntu-20.04

strategy:
matrix:
ansible-version:
- '7.3.0' # max-ansible-test-version
- max
molecule-scenario:
- rocky
- debian-min
Expand All @@ -29,54 +70,54 @@ jobs:
- fedora
- opensuse
include:
- ansible-version: '5.10.0' # min-ansible-test-version
- ansible-version: min
molecule-scenario: default

env:
MOLECULEW_ANSIBLE: ${{ matrix.ansible-version }}

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

- name: Set up Python
id: setup-python
uses: actions/setup-python@v4
with:
python-version: '3.9'
python-version-file: '.python-version'

- name: Cache Molecule
id: molecule-cache
- name: Tox cache
id: tox-cache
uses: actions/cache@v3
with:
path: ~/.moleculew
key: Ansible-${{ matrix.ansible-version }}-${{ hashFiles('.moleculew/**') }}
path: ~/.cache/pip
key: tox-${{ hashFiles('requirements/tox.txt') }}-py${{ steps.setup-python.outputs.python-version}}

- name: Install dependencies
if: steps.molecule-cache.outputs.cache-hit != 'true'
run: ./moleculew wrapper-install

- name: Dependency versions
run: ./moleculew wrapper-versions

- name: Login to Docker Hub
if: '!github.event.pull_request || github.event.pull_request.head.repo.full_name == github.repository'
uses: docker/login-action@v2
- name: Molecule cache
id: molecule-cache
uses: actions/cache@v3
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
path: .tox
key: molecule-${{ hashFiles(format('requirements/ansible-{0}.txt', matrix.ansible-version)) }}-py${{ steps.setup-python.outputs.python-version}}

- name: Install tox
run: pip3 install --no-compile -r requirements/tox.txt

- name: Molecule test
run: ./moleculew test --scenario-name=${{ matrix.molecule-scenario }}
run: tox run -e ansible-${{ matrix.ansible-version }} -- --scenario-name=${{ matrix.molecule-scenario }}

- name: Compact cache
- name: Compact Molecule cache
if: steps.molecule-cache.outputs.cache-hit != 'true'
run: find ~/.moleculew -name '__pycache__' -exec rm -rf {} +
run: find .tox -name '__pycache__' -exec rm -rf {} +

- name: Compact tox cache
if: steps.tox-cache.outputs.cache-hit != 'true'
run: find ~/.cache/pip -name '__pycache__' -exec rm -rf {} +

test-all:
if: ${{ always() }}
name: Test (matrix)
runs-on: ubuntu-20.04
needs: test
needs:
- lint
- test
steps:
- name: Check test matrix status
run: "[[ '${{ needs.test.result }}' == 'success' ]] || exit 1"
11 changes: 9 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ fabric.properties

# *.iml
# modules.xml
# .idea/misc.xml
# .idea/misc.xml
# *.ipr


Expand Down Expand Up @@ -243,8 +243,15 @@ local.properties
### Custom rules ###
####################

### Molecule ###
### Molecule ###

__pycache__
.cache
.molecule


### Tox ###
.tox

### For development ###
.venv
1 change: 0 additions & 1 deletion .moleculew/ansible_lint_version

This file was deleted.

1 change: 0 additions & 1 deletion .moleculew/ansible_version

This file was deleted.

1 change: 0 additions & 1 deletion .moleculew/flake8_version

This file was deleted.

1 change: 0 additions & 1 deletion .moleculew/molecule_version

This file was deleted.

1 change: 0 additions & 1 deletion .moleculew/python_version

This file was deleted.

1 change: 0 additions & 1 deletion .moleculew/testinfra_version

This file was deleted.

1 change: 0 additions & 1 deletion .moleculew/yamllint_version

This file was deleted.

1 change: 1 addition & 0 deletions .python-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3.9
7 changes: 6 additions & 1 deletion .yamllint
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,9 @@ rules:
new-lines:
type: unix
trailing-spaces: disable
truthy: disable
truthy:
allowed-values: ['true', 'false', 'on']

ignore: |
.tox/
.venv/
40 changes: 13 additions & 27 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Role to install the [Apache Maven](https://maven.apache.org) build tool.
Requirements
------------

* Ansible >= 5 (Ansible Core >= 2.12)
* Ansible Core >= 2.12

* Linux Distribution

Expand Down Expand Up @@ -230,32 +230,18 @@ You can find more roles from GantSign on
Development & Testing
---------------------

This project uses [Molecule](http://molecule.readthedocs.io/) to aid in the
development and testing; the role is unit tested using
[Testinfra](http://testinfra.readthedocs.io/) and
[pytest](http://docs.pytest.org/).

To develop or test you'll need to have installed the following:

* Linux (e.g. [Ubuntu](http://www.ubuntu.com/))
* [Docker](https://www.docker.com/)
* [Python](https://www.python.org/) (including python-pip)
* [Ansible](https://www.ansible.com/)
* [Molecule](http://molecule.readthedocs.io/)

Because the above can be tricky to install, this project includes
[Molecule Wrapper](https://github.com/gantsign/molecule-wrapper). Molecule
Wrapper is a shell script that installs Molecule and it's dependencies (apart
from Linux) and then executes Molecule with the command you pass it.

To test this role using Molecule Wrapper run the following command from the
project root:

```bash
./moleculew test
```

Note: some of the dependencies need `sudo` permission to install.
This project uses the following tooling:
* [Molecule](http://molecule.readthedocs.io/) for orchestrating test scenarios
* [Testinfra](http://testinfra.readthedocs.io/) for testing the changes on the
remote
* [pytest](http://docs.pytest.org/) the testing framework
* [Tox](https://tox.wiki/en/latest/) manages Python virtual
environments for linting and testing
* [pip-tools](https://github.com/jazzband/pip-tools) for managing dependencies

A Visual Studio Code
[Dev Container](https://code.visualstudio.com/docs/devcontainers/containers) is
provided for developing and testing this role.

License
-------
Expand Down
6 changes: 0 additions & 6 deletions molecule/debian-max/molecule.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,6 @@ driver:

role_name_check: 2

lint: |
set -e
yamllint .
ansible-lint
flake8
platforms:
- name: ansible-role-maven-debian-max
image: debian:11
Expand Down
4 changes: 2 additions & 2 deletions molecule/debian-min/converge.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
update_cache: true
changed_when: false

- name: Install jre-headless 8
- name: Install jre-headless 11
become: true
ansible.builtin.apt:
name: openjdk-8-jre-headless
name: openjdk-11-jre-headless
state: present

roles:
Expand Down
6 changes: 0 additions & 6 deletions molecule/debian-min/molecule.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,6 @@ driver:

role_name_check: 2

lint: |
set -e
yamllint .
ansible-lint
flake8
platforms:
- name: ansible-role-maven-debian-min
image: debian:10
Expand Down
6 changes: 0 additions & 6 deletions molecule/default/molecule.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,6 @@ driver:

role_name_check: 2

lint: |
set -e
yamllint .
ansible-lint
flake8
platforms:
- name: ansible-role-maven-ubuntu-max
image: ubuntu:20.04
Expand Down
Loading

0 comments on commit 3c974de

Please sign in to comment.