Skip to content

Commit

Permalink
Fix ANSIBLE_LIBRARY variable handling (#3103)
Browse files Browse the repository at this point in the history
When we introduced the ability to pass down the user-specified
ANSIBLE_LIBRARY variable value to the molecule, we wrongly assumed
that the variable will always contain a single componet, which is not
always true.

This commit makes sure that we properly handle scenarios where users
specify multiple directories in the ANSIBLE_LIBRARY environment
variable.
  • Loading branch information
tadeboro committed May 14, 2021
1 parent 2347df5 commit 14e7575
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 19 deletions.
12 changes: 6 additions & 6 deletions .github/workflows/tox.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,17 @@ jobs:
- tox_env: lint
- tox_env: docs
- tox_env: py36
PREFIX: PYTEST_REQPASS=433
PREFIX: PYTEST_REQPASS=435
- tox_env: py37
PREFIX: PYTEST_REQPASS=433
PREFIX: PYTEST_REQPASS=435
- tox_env: py38
PREFIX: PYTEST_REQPASS=433
PREFIX: PYTEST_REQPASS=435
- tox_env: py39
PREFIX: PYTEST_REQPASS=433
PREFIX: PYTEST_REQPASS=435
- tox_env: py36-devel
PREFIX: PYTEST_REQPASS=433
PREFIX: PYTEST_REQPASS=435
- tox_env: py39-devel
PREFIX: PYTEST_REQPASS=433
PREFIX: PYTEST_REQPASS=435
- tox_env: packaging
- tox_env: eco
- tox_env: dockerfile
Expand Down
2 changes: 1 addition & 1 deletion src/molecule/provisioner/ansible.py
Original file line number Diff line number Diff line change
Expand Up @@ -940,7 +940,7 @@ def _get_modules_directories(self):
)

if os.environ.get("ANSIBLE_LIBRARY"):
paths.extend([util.abs_path(os.environ.get("ANSIBLE_LIBRARY"))])
paths.extend(map(util.abs_path, os.environ["ANSIBLE_LIBRARY"].split(":")))

return paths

Expand Down
39 changes: 27 additions & 12 deletions src/molecule/test/unit/provisioner/test_ansible.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import collections
import os
import re

import pytest

Expand Down Expand Up @@ -684,22 +685,36 @@ def test_get_plugin_directory(_instance):
assert ("molecule", "provisioner", "ansible", "plugins") == parts[-4:]


def test_get_modules_directories(_instance, monkeypatch):
result = _instance._get_modules_directories()[0]
parts = pytest.helpers.os_split(result)
x = ("molecule", "provisioner", "ansible", "plugins", "modules")
def test_get_modules_directories_default(_instance, monkeypatch):
monkeypatch.delenv("ANSIBLE_LIBRARY", raising=False)

paths = _instance._get_modules_directories()

assert len(paths) == 5
assert re.search(r"molecule/provisioner/ansible/plugins/modules$", paths[0])
assert re.search(r"\.cache/molecule/[^/]+/default/library$", paths[1])
assert re.search(r"/library$", paths[2])
assert re.search(r"\.ansible/plugins/modules$", paths[3])
assert re.search(r"/usr/share/ansible/plugins/modules$", paths[4])


def test_get_modules_directories_single_ansible_library(_instance, monkeypatch):
monkeypatch.setenv("ANSIBLE_LIBRARY", "/abs/path/lib")

paths = _instance._get_modules_directories()

assert len(paths) == 6
assert paths[-1] == "/abs/path/lib"

assert x == parts[-5:]

lib_prev = os.environ.get("ANSIBLE_LIBRARY")
monkeypatch.setenv("ANSIBLE_LIBRARY", "/foo/bar")
result = _instance._get_modules_directories()[-1]
monkeypatch.setenv("ANSIBLE_LIBRARY", lib_prev if lib_prev else "")
def test_get_modules_directories_multi_ansible_library(_instance, monkeypatch):
monkeypatch.setenv("ANSIBLE_LIBRARY", "relpath/lib:/abs/path/lib")

env_lib_result_parts = pytest.helpers.os_split(result)
env_lib_expected_parts = ("foo", "bar")
paths = _instance._get_modules_directories()

assert env_lib_result_parts == env_lib_expected_parts[-2:]
assert len(paths) == 7
assert paths[-2].endswith("relpath/lib")
assert paths[-1] == "/abs/path/lib"


def test_get_filter_plugin_directory(_instance):
Expand Down

0 comments on commit 14e7575

Please sign in to comment.