Skip to content

Commit

Permalink
Adopt molecule-containers plugin (#68)
Browse files Browse the repository at this point in the history
  • Loading branch information
ssbarnea authored Jan 8, 2023
1 parent a7333cd commit bbaf82e
Show file tree
Hide file tree
Showing 15 changed files with 183 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/tox.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ jobs:
runs-on: ubuntu-22.04
needs: pre
env:
PYTEST_REQPASS: 11
PYTEST_REQPASS: 13
strategy:
fail-fast: false
matrix: ${{ fromJson(needs.pre.outputs.matrix) }}
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ podman = [

[project.entry-points."molecule.driver"]
azure = "molecule_plugins.azure.driver:Azure"
containers = "molecule_plugins.containers.driver:Container"
docker = "molecule_plugins.docker.driver:Docker"
ec2 = "molecule_plugins.ec2.driver:EC2"
gce = "molecule_plugins.gce.driver:GCE"
Expand Down
1 change: 1 addition & 0 deletions src/molecule_plugins/containers/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""Molecule Containers Drivers."""
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"molecule_directory": "molecule",
"role_name": "OVERRIDDEN",
"scenario_name": "OVERRIDDEN"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
- name: Converge
hosts: all
# Disabled gather as it seems broken on podman
# https://github.com/ansible-community/molecule-podman/issues/2
gather_facts: false
tasks:
- name: Test implementation
debug:
msg: Add test implementation here
54 changes: 54 additions & 0 deletions src/molecule_plugins/containers/driver.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
"""Containers Driver Module."""
from __future__ import absolute_import

import inspect
import os
import shutil
from typing import Dict

from molecule import logger

_logger = logger.get_logger(__name__)

# Preference logic for picking backend driver to be used.
drivers = os.environ.get("MOLECULE_CONTAINERS_BACKEND", "podman,docker").split(",")
for driver in drivers:
if shutil.which(driver):
break
else:
driver = drivers[0]

# Logic for picking backend is subject to change.
if driver == "docker":
from molecule_plugins.docker.driver import Docker as DriverBackend
elif driver == "podman":
from molecule_plugins.podman.driver import Podman as DriverBackend
else:
raise NotImplementedError(f"Driver {driver} is not supported.")
_logger.debug("Containers driver will use %s backend", driver)


class Container(DriverBackend):
"""
Container Driver Class.
This class aims to provide an agnostic container enginer implementation,
which should allow users to consume whichever enginer they have available.
""" # noqa

def __init__(self, config=None):
"""Construct Container."""
super().__init__(config)
self._name = "containers"
# Assure that _path points to the driver we would be using, or
# molecule will fail to find the embedded playbooks.
self._path = os.path.abspath(os.path.dirname(inspect.getfile(DriverBackend)))

@property
def required_collections(self) -> Dict[str, str]:
"""Return collections dict containing names and versions required."""
return {
"ansible.posix": "1.3.0",
"community.docker": "1.9.1",
"containers.podman": "1.8.1",
}
1 change: 1 addition & 0 deletions test/containers/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""Molecule Containers Driver Tests."""
9 changes: 9 additions & 0 deletions test/containers/functional/.ansible-lint
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# ansible-lint config for functional testing, used to bypass expected metadata
# errors in molecule-generated roles. Loaded via the metadata_lint_update
# pytest helper. For reference, see "E7xx - metadata" in:
# https://docs.ansible.com/ansible-lint/rules/default_rules.html
skip_list:
# metadata/701 - Role info should contain platforms
- '701'
# metadata/703 - Should change default metadata: <field>"
- '703'
1 change: 1 addition & 0 deletions test/containers/functional/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""Functional Tests."""
23 changes: 23 additions & 0 deletions test/containers/functional/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Copyright (c) 2015-2018 Cisco Systems, Inc.
# Copyright (c) 2018 Red Hat, Inc.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to
# deal in the Software without restriction, including without limitation the
# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
# sell copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.
"""PyTest Config."""

from molecule.test.conftest import * # noqa pylint: disable=wildcard-import,unused-wildcard-import
53 changes: 53 additions & 0 deletions test/containers/functional/test_containers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# Copyright (c) 2015-2018 Cisco Systems, Inc.
# Copyright (c) 2018 Red Hat, Inc.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to
# deal in the Software without restriction, including without limitation the
# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
# sell copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.
"""Functional Tests."""
import os

from molecule import logger
from molecule.test.conftest import change_dir_to, molecule_directory
from molecule.util import run_command

LOG = logger.get_logger(__name__)


def test_command_init_scenario(temp_dir):
"""Verify that we can initialize a new scenario with this driver."""
with change_dir_to(temp_dir):
scenario_directory = os.path.join(molecule_directory(), "default")
cmd = [
"molecule",
"init",
"scenario",
"default",
"--driver-name",
"containers",
]
result = run_command(cmd)
assert result.returncode == 0

assert os.path.isdir(scenario_directory)

# we do not run the full "test" sequence because lint will fail, check
# is shorter but comprehensive enough to test the most important
# functionality: destroy, dependency, create, prepare, converge
cmd = ["molecule", "check", "-s", "default"]
result = run_command(cmd)
assert result.returncode == 0
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
- name: Converge
hosts: all
gather_facts: false
become: true
tasks:
- name: Display bogus message
ansible.builtin.debug:
msg: seems ok
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
driver:
name: containers
platforms:
- name: instance
provisioner:
name: ansible
7 changes: 7 additions & 0 deletions test/containers/test_driver.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
"""Unit Tests."""
from molecule import api


def test_driver_is_detected():
"""Check that driver is recognized."""
assert "containers" in [str(d) for d in api.drivers()]
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ passenv =
CI
CURL_CA_BUNDLE
DOCKER_*
PYTEST_OPTIONS
PYTEST_*
REQUESTS_CA_BUNDLE
SSH_AUTH_SOCK
SSL_CERT_FILE
Expand Down

0 comments on commit bbaf82e

Please sign in to comment.