Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add initial Resource Dispatcher tests #37

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion .github/workflows/resource-dispatcher-pr-checks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,19 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: vrutkovs/action-s2i@master
- uses: actions/setup-python@v2
with:
python-version: '3.8'
- name: Install Pipenv & Run Test
run: |
export PATH="$PATH:/home/runner/.local/bin"
python -m pip install --upgrade pip
pip install pipenv
cd resource-dispatcher
pipenv install
pipenv run pytest
- name: Build Image
uses: vrutkovs/action-s2i@master
with:
path: resource-dispatcher
base: registry.access.redhat.com/ubi8/python-38:latest
Expand Down
4 changes: 3 additions & 1 deletion resource-dispatcher/.gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
**/__pycache__/
tmp/
tmp/
.pytest_cache
*.pyc
1 change: 1 addition & 0 deletions resource-dispatcher/Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ ansible = "*"
kubernetes = "*"
inflect = "*"
six = "*"
pytest = "*"

[requires]
python_version = "3.8"
142 changes: 106 additions & 36 deletions resource-dispatcher/Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Empty file.
2 changes: 2 additions & 0 deletions resource-dispatcher/configuration/test/config_exists.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
hello: world
15 changes: 15 additions & 0 deletions resource-dispatcher/configuration/test/test_configuration.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import pytest
import os
import configuration


def test_exits_without_configuration():
with pytest.raises(SystemExit) as pytest_wrapped_e:
configuration.get_config()
assert pytest_wrapped_e.type == SystemExit


def test_get_config(monkeypatch):
monkeypatch.setenv("CONFIG_FILE", os.getcwd() + "/configuration/test/config_exists.yml")
config = configuration.get_config()
assert {"hello": "world"} == config
Empty file.
82 changes: 82 additions & 0 deletions resource-dispatcher/execution/test/test_ansible.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import pytest
import execution
import os

def test_ansible_basic(capsys):
task = {
"name": "Ansible Test",
"disable_error_handling": True,
"steps": [
{
"plugin": "ansible",
"params": {
"playbook_path": os.getcwd() + "/execution/test/test_playbook/site.yml",
"inventory_path": os.getcwd() + "/execution/test/test_playbook/inventory"
}
}
]
}

execution.task_function(task, context={})

captured = capsys.readouterr()
assert "Hello, world!" in captured.out


def test_ansible_reading_context(capsys):
task = {
"name": "Ansible Test",
"disable_error_handling": True,
"steps": [
{
"plugin": "script",
"params": {
"script": "context['test_var'] = 'Set via context!'"
}
},
{
"plugin": "ansible",
"params": {
"playbook_path": os.getcwd() + "/execution/test/test_playbook/site.yml",
"inventory_path": os.getcwd() + "/execution/test/test_playbook/inventory"
}
}
]
}

execution.task_function(task, context={})

captured = capsys.readouterr()
assert "Set via context!" in captured.out


def test_ansible_merging_params_from_context(capsys):
task = {
"name": "Ansible Test",
"disable_error_handling": True,
"steps": [
{
"plugin": "script",
"params": {
"script": "context['test_var'] = {'extra_vars': {'test_var_1': 'thing1'}}"
}
},
{
"plugin": "ansible",
"params_from_context": "test_var",
"params": {
"playbook_path": os.getcwd() + "/execution/test/test_playbook/site.yml",
"inventory_path": os.getcwd() + "/execution/test/test_playbook/inventory",
"extra_vars": {
"test_var_2": "thing2"
}
}
}
]
}

execution.task_function(task, context={})

captured = capsys.readouterr()
assert "thing1" in captured.out
assert "thing2" in captured.out
9 changes: 9 additions & 0 deletions resource-dispatcher/execution/test/test_execution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import pytest
from importlib.util import find_spec
import execution


def test_all_plugins_loadable():
plugins = ["ansible", "git", "kubernetes", "script"]
for plugin in plugins:
assert find_spec(f"execution.plugins.{plugin}") is not None
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ansible_connection: local
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[local]
localhost
16 changes: 16 additions & 0 deletions resource-dispatcher/execution/test/test_playbook/site.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---
- name: Test Playbook
hosts: local
gather_facts: false
tasks:
- debug:
msg: Hello, world!
- debug:
var: test_var
when: test_var is defined
- debug:
var: test_var_1
when: test_var_1 is defined
- debug:
var: test_var_2
when: test_var_2 is defined
Loading