Skip to content

Commit

Permalink
Add support for global molecule configuration
Browse files Browse the repository at this point in the history
This patch adds the support for global molecule configuration at the
project level. It detects if a `.config/molecule/config.yml` file exists
at the root of the project and allows us to determine the driver name if
the scenarios configuration files didn't override the default one coming
from the global one.

Fixes #88

Signed-off-by: Gael Chamoulaud (Strider) <gchamoul@redhat.com>
  • Loading branch information
strider committed May 6, 2021
1 parent 6b28c10 commit 015e76c
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 2 deletions.
26 changes: 25 additions & 1 deletion src/tox_ansible/ansible/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
from ..tox_molecule_case import ToxMoleculeCase
from .scenario import Scenario

LOCAL_CONFIG_FILE = ".config/molecule/config.yml"


class Ansible(object):
"""A generalized class that handles interactions between the plugin and
Expand Down Expand Up @@ -44,6 +46,26 @@ def is_ansible(self):
path.join(self.directory, "galaxy.yml")
)

@property
def is_global_molecule_config(self):
"""Determine if there is a shared molecule configuration file at the
project level.
:return: The path of the <project>/.config/molecule/config.yml file.
None, otherwise."""
config_file_path = path.join(self.directory, LOCAL_CONFIG_FILE)
if path.isfile(config_file_path):
return config_file_path
return None

@property
def molecule_global_config(self):
"""Reads the .config/molecule/config.yml file if it exists. Adds it to
the self.molecule_global_config field."""
if self.is_global_molecule_config:
return load_yaml(self.is_global_molecule_config)
return None

@property
def scenarios(self):
"""Recursively searches the potential Ansible directory and looks for any
Expand Down Expand Up @@ -74,7 +96,9 @@ def scenarios(self):
if branch in self.options.ignore_paths:
ignored = True
if not ignored:
self._scenarios.append(Scenario(path.relpath(base_dir, self.directory)))
self._scenarios.append(Scenario(path.relpath(base_dir,
self.directory),
self.molecule_global_config))
return self._scenarios

@property
Expand Down
8 changes: 7 additions & 1 deletion src/tox_ansible/ansible/scenario.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@
class Scenario(object):
"""Knows about scenarios."""

def __init__(self, directory):
def __init__(self, directory, global_config=None):
self.directory = directory
self.scenario_file = path.join(self.directory, "molecule.yml")
self.global_config = global_config

def __str__(self):
return "{}".format(self.name)
Expand All @@ -32,10 +33,15 @@ def name(self):
@property
def driver(self):
"""Reads the driver for this scenario, if one is defined.
If there is no driver found in the scenario configuration and if there
is a global configuration, the driver coming from the global
configuration will be taken
:return: Driver name defined in molecule.yml or None"""
if self.config and "driver" in self.config and "name" in self.config["driver"]:
return self.config["driver"]["name"]
elif self.global_config:
return self.global_config["driver"]["name"]
return None

@property
Expand Down
7 changes: 7 additions & 0 deletions tests/test_scenario.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,10 @@ def test_no_driver(no_driver):
s = Scenario(no_driver)
assert s.name == "no_driver"
assert s.driver is None


def test_no_driver_with_global_config(no_driver):
global_config = {'driver': {'name': 'podman'}}
s = Scenario(no_driver, global_config)
assert s.name == "no_driver"
assert s.driver == "podman"

0 comments on commit 015e76c

Please sign in to comment.