Skip to content

Commit 40155c8

Browse files
authored
Add caching for test collections (ansible#2792)
1 parent 3019e46 commit 40155c8

13 files changed

+62
-41
lines changed

.ansible-lint

+1-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ warn_list:
7676
# - all
7777

7878
# Offline mode disables installation of requirements.yml
79-
offline: false
79+
offline: true
8080

8181
# Return success if number of violations compared with previous git
8282
# commit has not increased. This feature works only in git

.github/workflows/release.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ jobs:
2222
with:
2323
python-version: 3.9
2424
- name: Install tox
25-
run: python3 -m pip install --user "tox>=4.0.0rc1"
25+
run: python3 -m pip install --user "tox>=4.0.0"
2626
- name: Check out src from Git
2727
uses: actions/checkout@v3
2828
with:

.github/workflows/tox.yml

+13-6
Original file line numberDiff line numberDiff line change
@@ -100,15 +100,22 @@ jobs:
100100
with:
101101
fetch-depth: 0 # needed by setuptools-scm
102102

103-
- name: Set caches
103+
- name: Set pre-commit cache
104104
uses: actions/cache@v3
105+
if: ${{ matrix.tox_env == 'lint' }}
105106
with:
106107
path: |
107-
~/.ansible/galaxy_cache
108-
~/.ansible/collections/ansible_collections
109-
~/.ansible/roles/
110108
~/.cache/pre-commit
111-
key: galaxy-${{ matrix.name || matrix.tox_env }}-${{ hashFiles('requirements.yml', '.pre-commit-config.yaml', 'pyproject.toml') }}
109+
key: pre-commit-${{ matrix.name || matrix.tox_env }}-${{ hashFiles('.pre-commit-config.yaml') }}
110+
111+
- name: Set galaxy cache
112+
uses: actions/cache@v3
113+
if: ${{ startsWith(matrix.tox_env, 'py') }}
114+
with:
115+
path: |
116+
examples/playbooks/collections/*.tar.gz
117+
examples/playbooks/collections/ansible_collections
118+
key: galaxy-${{ hashFiles('examples/playbooks/collections/requirements.yml') }}
112119

113120
- name: Set up Python ${{ matrix.python-version || '3.9' }}
114121
if: "!contains(matrix.shell, 'wsl')"
@@ -129,7 +136,7 @@ jobs:
129136
- name: Install tox
130137
run: |
131138
python3 -m pip install --upgrade pip
132-
python3 -m pip install --upgrade "tox>=4.0.0rc1"
139+
python3 -m pip install --upgrade "tox>=4.0.0"
133140
134141
- name: Log installed dists
135142
run: python3 -m pip freeze --all

.pre-commit-config.yaml

+4-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ exclude: >
1919
test/schemas/data/licenses.json|
2020
test/schemas/package-lock.json|
2121
test/schemas/negative_test|
22-
examples/broken/yaml-with-tabs/invalid-due-tabs.yaml
22+
examples/broken/yaml-with-tabs/invalid-due-tabs.yaml|
23+
examples/playbooks/collections/.*
2324
)$
2425
repos:
2526
- repo: meta
@@ -41,6 +42,7 @@ repos:
4142
examples/playbooks/with-umlaut-.*|
4243
examples/playbooks/with-skip-tag-id.yml|
4344
examples/yamllint/.*|
45+
examples/playbooks/collections/.*|
4446
test/fixtures/formatting-before/.*|
4547
test/schemas/data/.*|
4648
test/schemas/(negative_test|test)/.*\.md|
@@ -117,6 +119,7 @@ repos:
117119
examples/playbooks/templates/.*|
118120
examples/yamllint/.*|
119121
examples/other/some.j2.yaml|
122+
examples/playbooks/collections/.*|
120123
test/fixtures/formatting-before/.*
121124
)$
122125
files: \.(yaml|yml)$

ansible.cfg

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[defaults]
2+
collections_path = examples/playbooks/collections

conftest.py

+2-13
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,10 @@
1616
)
1717
sys.exit(1)
1818
# we need to be sure that we have the requirements installed as some tests
19-
# might depend on these.
19+
# might depend on these. This approach is compatible with GHA caching.
2020
try:
21-
from ansible_compat.prerun import get_cache_dir
22-
23-
cache_dir = get_cache_dir(".")
2421
subprocess.check_output(
25-
[
26-
"ansible-galaxy",
27-
"collection",
28-
"install",
29-
"-p",
30-
f"{cache_dir}/collections",
31-
"-r",
32-
"requirements.yml",
33-
],
22+
["./tools/install-reqs.sh"],
3423
stderr=subprocess.PIPE,
3524
text=True,
3625
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ansible_collections
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
collections:
2+
- name: community-molecule-0.1.0.tar.gz
3+
version: 0.1.0
4+
- name: ansible-posix-1.4.0.tar.gz
5+
version: 1.4.0
6+
- name: community-general-6.1.0.tar.gz
7+
version: 6.1.0

requirements.yml

-14
This file was deleted.

test/test_profiles.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
"""Tests for the --profile feature."""
2+
import platform
23
import subprocess
34
import sys
45

@@ -41,4 +42,9 @@ def test_profile_listing(capfd: CaptureFixture[str]) -> None:
4142
if sys.version_info < (3, 9):
4243
assert "ansible-lint is no longer tested" in err
4344
else:
44-
assert err == ""
45+
# On WSL we might see this warning on stderr:
46+
# [WARNING]: Ansible is being run in a world writable directory
47+
# WSL2 has "WSL2" in platform name but WSL1 has "microsoft":
48+
platform_name = platform.platform().lower()
49+
if all(word not in platform_name for word in ["wsl", "microsoft"]):
50+
assert err == "", platform_name

tools/install-reqs.sh

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#!/bin/bash
2+
set -euo pipefail
3+
pushd examples/playbooks/collections
4+
MISSING=()
5+
for COLLECTION in ansible.posix community.general community.molecule;
6+
do
7+
FILE=${COLLECTION//\./-}
8+
if test -n "$(find . -maxdepth 1 -name '$FILE*' -print -quit)"
9+
then
10+
echo "Already cached $FILE"
11+
else
12+
MISSING+=("${COLLECTION}")
13+
fi
14+
if [ ${#MISSING[@]} -ne 0 ]; then
15+
ansible-galaxy collection download -p . -v "${MISSING[@]}"
16+
fi
17+
done
18+
19+
echo "Install requirements.yml ..."
20+
cat requirements.yml
21+
ansible-galaxy collection install -r requirements.yml -p .
22+
popd

tools/test-setup.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ if [ -f "/usr/bin/apt-get" ]; then
1818
sudo apt-get remove -y ansible pipx || true
1919
# cspell:disable-next-line
2020
sudo apt-get install -y --no-install-recommends -o=Dpkg::Use-Pty=0 \
21-
gcc git python3-venv python3-pip python3-dev libyaml-dev
21+
curl gcc git python3-venv python3-pip python3-dev libyaml-dev
2222
# Some of these might be needed for compiling packages that do not yet
2323
# a binary for current platform, like pyyaml on py311
2424
# pip3 install -v --no-binary :all: --user pyyaml

tox.ini

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# spell-checker:ignore linkcheck basepython changedir envdir envlist envname envsitepackagesdir passenv setenv testenv toxinidir toxworkdir usedevelop doctrees envpython posargs
22
[tox]
3-
minversion = 4.0.0rc1
3+
minversion = 4.0.0
44
envlist =
55
lint
66
pkg
@@ -23,8 +23,6 @@ deps =
2323
commands =
2424
# safety measure to assure we do not accidentally run tests with broken dependencies
2525
{envpython} -m pip check
26-
# Needed at least for WSL
27-
ansible-galaxy collection install -r requirements.yml
2826
coverage run -m pytest {posargs:\
2927
-n auto \
3028
-ra \

0 commit comments

Comments
 (0)