Skip to content

Commit

Permalink
Finish unit/component tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dkubek committed Feb 29, 2024
1 parent ce08b9c commit 84b1735
Show file tree
Hide file tree
Showing 9 changed files with 129 additions and 54 deletions.
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
import os

from leapp.actors import Actor
from leapp.tags import SecondPhaseTag, UnitTestWorkflowTag
from leapp.dialogs import Dialog
from leapp.dialogs.components import BooleanComponent, ChoiceComponent, NumberComponent, TextComponent
from leapp.tags import FirstPhaseTag, UnitTestWorkflowTag


class FirstActor(Actor):
name = 'first_actor'
description = 'No description has been provided for the first_actor actor.'
class DialogActor(Actor):
name = 'dialog_actor'
description = 'No description has been provided for the dialog_actor actor.'
consumes = ()
produces = ()
tags = (FirstPhaseTag, UnitTestWorkflowTag)
tags = (SecondPhaseTag, UnitTestWorkflowTag)
dialogs = (Dialog(
scope='unique_dialog_scope',
reason='Confirmation',
Expand All @@ -35,8 +33,4 @@ class FirstActor(Actor):
def process(self):
from leapp.libraries.common.test_helper import log_execution
log_execution(self)
if not self.configuration or self.configuration.value != 'unit-test':
self.report_error('Unit test failed due missing or invalid workflow provided configuration')
if os.environ.get('FirstActor-ReportError') == '1':
self.report_error("Unit test requested error")
self.get_answers(self.dialogs[0]).get('confirm', False)
31 changes: 31 additions & 0 deletions tests/data/leappdb-tests/actors/exitstatusactor/actor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import os

from leapp.actors import Actor
from leapp.tags import FirstPhaseTag, UnitTestWorkflowTag
from leapp.exceptions import StopActorExecution, StopActorExecutionError


class ExitStatusActor(Actor):
name = 'exit_status_actor'
description = 'No description has been provided for the exit_status_actor actor.'
consumes = ()
produces = ()
tags = (FirstPhaseTag, UnitTestWorkflowTag)

def process(self):
from leapp.libraries.common.test_helper import log_execution
log_execution(self)
if not self.configuration or self.configuration.value != 'unit-test':
self.report_error('Unit test failed due missing or invalid workflow provided configuration')

if os.environ.get('ExitStatusActor-Error') == 'StopActorExecution':
self.report_error('Unit test requested StopActorExecution error')
raise StopActorExecution

if os.environ.get('ExitStatusActor-Error') == 'StopActorExecutionError':
self.report_error('Unit test requested StopActorExecutionError error')
raise StopActorExecutionError('StopActorExecutionError message')

if os.environ.get('ExitStatusActor-Error') == 'UnhandledError':
self.report_error('Unit test requested unhandled error')
assert 0 == 1, '0 == 1'
14 changes: 0 additions & 14 deletions tests/data/leappdb-tests/actors/secondactor/actor.py

This file was deleted.

5 changes: 0 additions & 5 deletions tests/data/leappdb-tests/tags/fifthphase.py

This file was deleted.

5 changes: 0 additions & 5 deletions tests/data/leappdb-tests/tags/fourthphase.py

This file was deleted.

5 changes: 0 additions & 5 deletions tests/data/leappdb-tests/tags/thirdphase.py

This file was deleted.

13 changes: 9 additions & 4 deletions tests/scripts/test_dialog_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from leapp.config import get_config

_HOSTNAME = 'test-host.example.com'
_CONTEXT_NAME = 'test-context-name'
_CONTEXT_NAME = 'test-context-name-dialogdb'
_ACTOR_NAME = 'test-actor-name'
_PHASE_NAME = 'test-phase-name'
_DIALOG_SCOPE = 'test-dialog'
Expand Down Expand Up @@ -141,8 +141,11 @@ def test_save_dialog(monkeypatch):
monkeypatch.setenv('LEAPP_CURRENT_PHASE', _PHASE_NAME)
monkeypatch.setenv('LEAPP_EXECUTION_ID', _CONTEXT_NAME)
monkeypatch.setenv('LEAPP_HOSTNAME', _HOSTNAME)

e = store_dialog(_TEST_DIALOG, {})
monkeypatch.delenv('LEAPP_CURRENT_ACTOR')
monkeypatch.delenv('LEAPP_CURRENT_PHASE')
monkeypatch.delenv('LEAPP_EXECUTION_ID')
monkeypatch.delenv('LEAPP_HOSTNAME')

entry = fetch_dialog(e.dialog_id)
assert entry is not None
Expand All @@ -162,10 +165,10 @@ def test_save_dialog(monkeypatch):
assert component_metadata in entry_data['components']


def test_save_dialog_workflow(repository):
def test_save_dialog_workflow(monkeypatch, repository):
workflow = repository.lookup_workflow('LeappDBUnitTest')()
with tempfile.NamedTemporaryFile(mode='w') as stdin_dialog:
os.environ['LEAPP_TEST_EXECUTION_LOG'] = '/dev/null'
monkeypatch.setenv('LEAPP_TEST_EXECUTION_LOG', '/dev/null')
stdin_dialog.write('my answer\n')
stdin_dialog.write('yes\n')
stdin_dialog.write('42\n')
Expand All @@ -174,6 +177,8 @@ def test_save_dialog_workflow(repository):
with mock.patch('sys.stdin.fileno', return_value=stdin_dialog.fileno()):
workflow.run(skip_dialogs=False)

monkeypatch.delenv('LEAPP_TEST_EXECUTION_LOG', '/dev/null')

entry = fetch_dialog()
assert entry is not None
assert entry['scope'] == 'unique_dialog_scope'
Expand Down
68 changes: 68 additions & 0 deletions tests/scripts/test_exit_status.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import os
import json
import tempfile

import py
import pytest

from leapp.repository.scan import scan_repo
from leapp.config import get_config
from leapp.utils.audit import get_audit_entry

_HOSTNAME = 'test-host.example.com'
_CONTEXT_NAME = 'test-context-name-exit-status'
_ACTOR_NAME = 'test-actor-name'
_PHASE_NAME = 'test-phase-name'
_DIALOG_SCOPE = 'test-dialog'


@pytest.fixture(scope='module')
def repository():
repository_path = py.path.local(os.path.join(os.path.dirname(os.path.dirname(__file__)), 'data', 'leappdb-tests'))
with repository_path.as_cwd():
repo = scan_repo('.')
repo.load(resolve=True)
yield repo


def setup_module():
get_config().set('database', 'path', '/tmp/leapp-test.db')


def setup():
path = get_config().get('database', 'path')
if os.path.isfile(path):
os.unlink(path)


@pytest.mark.parametrize('error, code', [(None, 0), ('StopActorExecution', 0), ('StopActorExecutionError', 0),
('UnhandledError', 1)])
def test_exit_status_stopactorexecution(monkeypatch, repository, error, code):

workflow = repository.lookup_workflow('LeappDBUnitTest')()

if error is not None:
os.environ['ExitStatusActor-Error'] = error
else:
os.environ.pop('ExitStatusActor-Error', None)

with tempfile.NamedTemporaryFile() as test_log_file:
monkeypatch.setenv('LEAPP_TEST_EXECUTION_LOG', test_log_file.name)
monkeypatch.setenv('LEAPP_HOSTNAME', _HOSTNAME)
try:
workflow.run(skip_dialogs=True, context=_CONTEXT_NAME, until_actor='ExitStatusActor')
except BaseException:
pass

ans = get_audit_entry('actor-exit-status', _CONTEXT_NAME).pop()

assert ans is not None
assert ans['actor'] == 'exit_status_actor'
assert ans['context'] == _CONTEXT_NAME
assert ans['hostname'] == _HOSTNAME
data = json.loads(ans['data'])
assert data['exit_status'] == code


def teardown():
os.environ.pop('ExitStatusActor-Error', None)
26 changes: 16 additions & 10 deletions tests/scripts/test_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from leapp.config import get_config

_HOSTNAME = 'test-host.example.com'
_CONTEXT_NAME = 'test-context-name'
_CONTEXT_NAME = 'test-context-name-metadata'
_ACTOR_NAME = 'test-actor-name'
_PHASE_NAME = 'test-phase-name'
_DIALOG_SCOPE = 'test-dialog'
Expand All @@ -27,7 +27,7 @@
'class_name': 'FirstPhase',
'filter': {
'phase': 'FirstPhaseTag',
'tags': ['UnitTestWorkflowTag', 'UnitTestWorkflowTag']
'tags': ['UnitTestWorkflowTag']
},
'flags': {
'is_checkpoint': False,
Expand All @@ -44,7 +44,7 @@
'class_name': 'SecondPhase',
'filter': {
'phase': 'SecondPhaseTag',
'tags': ['UnitTestWorkflowTag', 'UnitTestWorkflowTag']
'tags': ['UnitTestWorkflowTag']
},
'flags': {
'is_checkpoint': False,
Expand Down Expand Up @@ -119,7 +119,7 @@ def test_save_empty_metadata():
assert entry['metadata'] == 'null'


def test_store_actor_metadata(repository_dir):
def test_store_actor_metadata(monkeypatch, repository_dir):
# ---
# Test store actor metadata without error
# ---
Expand All @@ -131,9 +131,11 @@ def test_store_actor_metadata(repository_dir):
with mock.patch('leapp.repository.actor_definition.get_actors', return_value=[True]):
definition._module = True

os.environ['LEAPP_EXECUTION_ID'] = _CONTEXT_NAME
os.environ['LEAPP_HOSTNAME'] = _HOSTNAME
monkeypatch.setenv('LEAPP_EXECUTION_ID', _CONTEXT_NAME)
monkeypatch.setenv('LEAPP_HOSTNAME', _HOSTNAME)
store_actor_metadata(definition, 'test-phase')
monkeypatch.delenv('LEAPP_EXECUTION_ID')
monkeypatch.delenv('LEAPP_HOSTNAME')

# ---
# Test retrieve correct actor metadata
Expand All @@ -160,22 +162,26 @@ def test_store_actor_metadata(repository_dir):
assert sorted(metadata['produces']) == sorted(_TEST_ACTOR_METADATA['produces'])


def test_workflow_metadata(repository):
def test_workflow_metadata(monkeypatch, repository):
# ---
# Test store workflow metadata without error
# ---
workflow = repository.lookup_workflow('LeappDBUnitTest')()

os.environ['LEAPP_EXECUTION_ID'] = _CONTEXT_NAME
os.environ['LEAPP_HOSTNAME'] = _HOSTNAME
monkeypatch.setenv('LEAPP_EXECUTION_ID', _CONTEXT_NAME)
monkeypatch.setenv('LEAPP_HOSTNAME', _HOSTNAME)
store_workflow_metadata(workflow)
monkeypatch.delenv('LEAPP_EXECUTION_ID')
monkeypatch.delenv('LEAPP_HOSTNAME')

# ---
# Test retrieve correct workflow metadata
# ---
entry = None
with get_connection(None) as conn:
cursor = conn.execute('SELECT * FROM metadata WHERE kind == "workflow" ORDER BY id DESC LIMIT 1;')
cursor = conn.execute(
'SELECT * FROM metadata WHERE kind == "workflow" AND context = ? ORDER BY id DESC LIMIT 1;',
(_CONTEXT_NAME,))
cursor.row_factory = dict_factory
entry = cursor.fetchone()

Expand Down

0 comments on commit 84b1735

Please sign in to comment.