Skip to content

Commit

Permalink
Assured dependency installed collections are isolated (#2875)
Browse files Browse the repository at this point in the history
  • Loading branch information
ssbarnea authored Oct 12, 2020
1 parent 7cd3af4 commit daf4feb
Show file tree
Hide file tree
Showing 10 changed files with 36 additions and 64 deletions.
9 changes: 8 additions & 1 deletion molecule/dependency/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"""Base Dependency Module."""

import abc
import os
import time

import sh
Expand Down Expand Up @@ -95,13 +96,19 @@ def default_options(self): # pragma: no cover
:return: dict
"""

@abc.abstractproperty
@property
def default_env(self): # pragma: no cover
"""
Get default env variables provided to ``cmd`` as a dict.
:return: dict
"""
env = util.merge_dicts(os.environ, self._config.env)
# inject ephemeral_directory on top of path
env[self._config.ansible_collections_path] = os.path.join(
self._config.scenario.ephemeral_directory, "collections"
)
return env

@property
def name(self):
Expand Down
7 changes: 1 addition & 6 deletions molecule/dependency/shell.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,11 @@
# DEALINGS IN THE SOFTWARE.
"""Shell Dependency Module."""

import os
import shlex

import sh

from molecule import logger, util
from molecule import logger
from molecule.dependency import base

LOG = logger.get_logger(__name__)
Expand Down Expand Up @@ -87,10 +86,6 @@ def command(self):
def default_options(self):
return {}

@property
def default_env(self):
return util.merge_dicts(os.environ, self._config.env)

def bake(self):
"""
Bake a ``shell`` command so it's ready to execute and returns None.
Expand Down
3 changes: 3 additions & 0 deletions molecule/provisioner/ansible.py
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,9 @@ def default_options(self):
def default_env(self):
# Finds if the current project is part of an ansible_collections hierarchy
collection_indicator = "ansible_collections"
# isolating test environment by injects ephemeral scenario directory on
# top of the collection_path_list. This prevents dependency commands
# from installing dependencies to user list of collections.
collections_path_list = [
util.abs_path(
os.path.join(self._config.scenario.ephemeral_directory, "collections")
Expand Down
46 changes: 9 additions & 37 deletions molecule/test/functional/test_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,9 @@
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.

import os

import pytest
import sh

from molecule.scenario import ephemeral_directory


@pytest.fixture
def scenario_to_test(request):
Expand Down Expand Up @@ -103,45 +99,21 @@ def test_command_create(scenario_to_test, with_scenario, scenario_name):
@pytest.mark.parametrize(
"scenario_to_test, driver_name, scenario_name",
[
("dependency", "delegated", "ansible-galaxy"),
pytest.param("dependency", "delegated", "shell", id="shell"),
pytest.param("dependency", "delegated", "ansible-galaxy", id="galaxy"),
],
indirect=["scenario_to_test", "driver_name", "scenario_name"],
)
def test_command_dependency_ansible_galaxy(
request, scenario_to_test, with_scenario, scenario_name
):
options = {"scenario_name": scenario_name}
cmd = sh.molecule.bake("dependency", **options)
pytest.helpers.run_command(cmd)

# Not testing the outcome because we dot effectively install any role,
# currently we cannot do this in offline mode.
# dependency_role = os.path.join(
# ephemeral_directory("molecule"),
# "dependency",
# "ansible-galaxy",
# "roles",
# "timezone",
# )
# assert os.path.isdir(dependency_role)


@pytest.mark.parametrize(
"scenario_to_test, driver_name, scenario_name",
[("dependency", "delegated", "shell")],
indirect=["scenario_to_test", "driver_name", "scenario_name"],
)
def test_command_dependency_shell(
request, scenario_to_test, with_scenario, scenario_name
):
def test_command_dependency(request, scenario_to_test, with_scenario, scenario_name):
options = {"scenario_name": scenario_name}
cmd = sh.molecule.bake("dependency", **options)
pytest.helpers.run_command(cmd)
result = pytest.helpers.run_command(cmd, log=False)
assert result.exit_code == 0

dependency_role = os.path.join(
ephemeral_directory("molecule"), "dependency", "shell", "roles", "timezone"
)
assert os.path.isdir(dependency_role)
# Validate that depdendency worked by running converge, which make use
cmd = sh.molecule.bake("converge", **options)
result = pytest.helpers.run_command(cmd, log=False)
assert result.exit_code == 0


@pytest.mark.extensive
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,8 @@
- name: Converge
hosts: all
gather_facts: false
roles:
- molecule
tasks:

- name: Validate that collection was installed
debug:
msg: "{{ 'foo' | community.molecule.header }}"
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
# See https://docs.ansible.com/ansible/latest/galaxy/user_guide.html#installing-roles-and-collections-from-the-same-requirements-yml-file
[]
collections:
- community.molecule
roles: []
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,8 @@
- name: Converge
hosts: all
gather_facts: false
roles:
- molecule
tasks:

- name: Validate that collection was installed
debug:
msg: "{{ 'foo' | community.molecule.header }}"
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
---
dependency:
name: shell
command: $MOLECULE_SCENARIO_DIRECTORY/run.bash
command: >
ansible-galaxy collection install -p "${MOLECULE_EPHEMERAL_DIRECTORY}/collections" community.molecule
driver:
name: delegated
platforms:
Expand Down

This file was deleted.

12 changes: 0 additions & 12 deletions molecule/test/scenarios/dependency/molecule/shell/run.bash

This file was deleted.

0 comments on commit daf4feb

Please sign in to comment.