Skip to content

Commit

Permalink
Add caching for test collections (#2792)
Browse files Browse the repository at this point in the history
  • Loading branch information
ssbarnea authored Dec 14, 2022
1 parent 3019e46 commit 40155c8
Show file tree
Hide file tree
Showing 13 changed files with 62 additions and 41 deletions.
2 changes: 1 addition & 1 deletion .ansible-lint
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ warn_list:
# - all

# Offline mode disables installation of requirements.yml
offline: false
offline: true

# Return success if number of violations compared with previous git
# commit has not increased. This feature works only in git
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ jobs:
with:
python-version: 3.9
- name: Install tox
run: python3 -m pip install --user "tox>=4.0.0rc1"
run: python3 -m pip install --user "tox>=4.0.0"
- name: Check out src from Git
uses: actions/checkout@v3
with:
Expand Down
19 changes: 13 additions & 6 deletions .github/workflows/tox.yml
Original file line number Diff line number Diff line change
Expand Up @@ -100,15 +100,22 @@ jobs:
with:
fetch-depth: 0 # needed by setuptools-scm

- name: Set caches
- name: Set pre-commit cache
uses: actions/cache@v3
if: ${{ matrix.tox_env == 'lint' }}
with:
path: |
~/.ansible/galaxy_cache
~/.ansible/collections/ansible_collections
~/.ansible/roles/
~/.cache/pre-commit
key: galaxy-${{ matrix.name || matrix.tox_env }}-${{ hashFiles('requirements.yml', '.pre-commit-config.yaml', 'pyproject.toml') }}
key: pre-commit-${{ matrix.name || matrix.tox_env }}-${{ hashFiles('.pre-commit-config.yaml') }}

- name: Set galaxy cache
uses: actions/cache@v3
if: ${{ startsWith(matrix.tox_env, 'py') }}
with:
path: |
examples/playbooks/collections/*.tar.gz
examples/playbooks/collections/ansible_collections
key: galaxy-${{ hashFiles('examples/playbooks/collections/requirements.yml') }}

- name: Set up Python ${{ matrix.python-version || '3.9' }}
if: "!contains(matrix.shell, 'wsl')"
Expand All @@ -129,7 +136,7 @@ jobs:
- name: Install tox
run: |
python3 -m pip install --upgrade pip
python3 -m pip install --upgrade "tox>=4.0.0rc1"
python3 -m pip install --upgrade "tox>=4.0.0"
- name: Log installed dists
run: python3 -m pip freeze --all
Expand Down
5 changes: 4 additions & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ exclude: >
test/schemas/data/licenses.json|
test/schemas/package-lock.json|
test/schemas/negative_test|
examples/broken/yaml-with-tabs/invalid-due-tabs.yaml
examples/broken/yaml-with-tabs/invalid-due-tabs.yaml|
examples/playbooks/collections/.*
)$
repos:
- repo: meta
Expand All @@ -41,6 +42,7 @@ repos:
examples/playbooks/with-umlaut-.*|
examples/playbooks/with-skip-tag-id.yml|
examples/yamllint/.*|
examples/playbooks/collections/.*|
test/fixtures/formatting-before/.*|
test/schemas/data/.*|
test/schemas/(negative_test|test)/.*\.md|
Expand Down Expand Up @@ -117,6 +119,7 @@ repos:
examples/playbooks/templates/.*|
examples/yamllint/.*|
examples/other/some.j2.yaml|
examples/playbooks/collections/.*|
test/fixtures/formatting-before/.*
)$
files: \.(yaml|yml)$
Expand Down
2 changes: 2 additions & 0 deletions ansible.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[defaults]
collections_path = examples/playbooks/collections
15 changes: 2 additions & 13 deletions conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,10 @@
)
sys.exit(1)
# we need to be sure that we have the requirements installed as some tests
# might depend on these.
# might depend on these. This approach is compatible with GHA caching.
try:
from ansible_compat.prerun import get_cache_dir

cache_dir = get_cache_dir(".")
subprocess.check_output(
[
"ansible-galaxy",
"collection",
"install",
"-p",
f"{cache_dir}/collections",
"-r",
"requirements.yml",
],
["./tools/install-reqs.sh"],
stderr=subprocess.PIPE,
text=True,
)
Expand Down
1 change: 1 addition & 0 deletions examples/playbooks/collections/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ansible_collections
7 changes: 7 additions & 0 deletions examples/playbooks/collections/requirements.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
collections:
- name: community-molecule-0.1.0.tar.gz
version: 0.1.0
- name: ansible-posix-1.4.0.tar.gz
version: 1.4.0
- name: community-general-6.1.0.tar.gz
version: 6.1.0
14 changes: 0 additions & 14 deletions requirements.yml

This file was deleted.

8 changes: 7 additions & 1 deletion test/test_profiles.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Tests for the --profile feature."""
import platform
import subprocess
import sys

Expand Down Expand Up @@ -41,4 +42,9 @@ def test_profile_listing(capfd: CaptureFixture[str]) -> None:
if sys.version_info < (3, 9):
assert "ansible-lint is no longer tested" in err
else:
assert err == ""
# On WSL we might see this warning on stderr:
# [WARNING]: Ansible is being run in a world writable directory
# WSL2 has "WSL2" in platform name but WSL1 has "microsoft":
platform_name = platform.platform().lower()
if all(word not in platform_name for word in ["wsl", "microsoft"]):
assert err == "", platform_name
22 changes: 22 additions & 0 deletions tools/install-reqs.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/bin/bash
set -euo pipefail
pushd examples/playbooks/collections
MISSING=()
for COLLECTION in ansible.posix community.general community.molecule;
do
FILE=${COLLECTION//\./-}
if test -n "$(find . -maxdepth 1 -name '$FILE*' -print -quit)"
then
echo "Already cached $FILE"
else
MISSING+=("${COLLECTION}")
fi
if [ ${#MISSING[@]} -ne 0 ]; then
ansible-galaxy collection download -p . -v "${MISSING[@]}"
fi
done

echo "Install requirements.yml ..."
cat requirements.yml
ansible-galaxy collection install -r requirements.yml -p .
popd
2 changes: 1 addition & 1 deletion tools/test-setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ if [ -f "/usr/bin/apt-get" ]; then
sudo apt-get remove -y ansible pipx || true
# cspell:disable-next-line
sudo apt-get install -y --no-install-recommends -o=Dpkg::Use-Pty=0 \
gcc git python3-venv python3-pip python3-dev libyaml-dev
curl gcc git python3-venv python3-pip python3-dev libyaml-dev
# Some of these might be needed for compiling packages that do not yet
# a binary for current platform, like pyyaml on py311
# pip3 install -v --no-binary :all: --user pyyaml
Expand Down
4 changes: 1 addition & 3 deletions tox.ini
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# spell-checker:ignore linkcheck basepython changedir envdir envlist envname envsitepackagesdir passenv setenv testenv toxinidir toxworkdir usedevelop doctrees envpython posargs
[tox]
minversion = 4.0.0rc1
minversion = 4.0.0
envlist =
lint
pkg
Expand All @@ -23,8 +23,6 @@ deps =
commands =
# safety measure to assure we do not accidentally run tests with broken dependencies
{envpython} -m pip check
# Needed at least for WSL
ansible-galaxy collection install -r requirements.yml
coverage run -m pytest {posargs:\
-n auto \
-ra \
Expand Down

0 comments on commit 40155c8

Please sign in to comment.