Skip to content

Commit

Permalink
Merge pull request #5072 from wazuh/4996-dtt1-iteration-3-workflow-en…
Browse files Browse the repository at this point in the history
…gine-unit-tests

DTT1 iteration 3 workflow engine unit tests
  • Loading branch information
fcaffieri authored Apr 9, 2024
2 parents be02ce5 + da889f3 commit f7c5df4
Show file tree
Hide file tree
Showing 14 changed files with 2,127 additions and 1 deletion.
35 changes: 35 additions & 0 deletions .github/workflows/workflow-engine-unit-tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
name: Workflow engine unit tests

on:
workflow_dispatch:
pull_request:
paths:
- '.github/workflows/workflow-engine-unit-tests.yml'
- 'deployability/modules/workflow_engine/**'

jobs:
build:
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
python-version: ['3.10']
env:
PYTHONPATH: /home/runner/work/wazuh-qa/wazuh-qa/deployability/modules
steps:
- uses: actions/checkout@v3

- name: Setup Python ${{ matrix.python-version }}
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}
cache: 'pip'
cache-dependency-path: 'deployability/modules/workflow_engine/requirements-dev.txt'

- name: Install dependencies
run: |
python -m pip install --upgrade pip wheel
pip install -r deployability/modules/workflow_engine/requirements-dev.txt --no-build-isolation
- name: Run workflow_engine tests
run: python -m pytest deployability/modules/workflow_engine
2 changes: 2 additions & 0 deletions deployability/modules/workflow_engine/requirements-dev.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-r ../../deps/requirements.txt
-r ../../deps/remote_requirements.txt
167 changes: 167 additions & 0 deletions deployability/modules/workflow_engine/tests/TESTING-README.md

Large diffs are not rendered by default.

75 changes: 75 additions & 0 deletions deployability/modules/workflow_engine/tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
# Copyright (C) 2015, Wazuh Inc.
# Created by Wazuh, Inc. <[email protected]>.
# This program is a free software; you can redistribute it and/or modify it under the terms of GPLv2
"""Common unit test fixtures."""
import graphlib

from unittest.mock import patch, MagicMock
import pytest

from workflow_engine.workflow_processor import DAG, WorkflowProcessor

DEFAULT_TASK_COLLECTION = [
{'task': 'task1', 'path': '/cmd1', 'args': [{"param1": "value1"}]},
{'task': 'task2', 'path': '/cmd2', 'args': [{"param1": "value1"}]},
{'task': 'task3', 'path': '/cmd3', 'args': [{"param1": "value1"}]},
]


@pytest.fixture
def logger_mock(request) -> MagicMock:
"""Fixture to mock common logger methods."""
logger_to_patch = request.param.get('logger_to_patch', "workflow_engine.workflow_processor.logger")
with patch(logger_to_patch) as l_mock:
patch.object(l_mock, 'warning')
patch.object(l_mock, 'info')
patch.object(l_mock, 'debug')
patch.object(l_mock, 'error')
yield l_mock


@pytest.fixture
def dag(request) -> DAG:
"""Create a mocked DAG instance."""
ret_dag: DAG
reverse = request.param.get('reverse', False)
task_collection = request.param.get('task_collection', DEFAULT_TASK_COLLECTION)
if request.param.get('patch', True):
execution_plan_dict = request.param.get('execution_plan_dict', {})
gl_dag = graphlib.TopologicalSorter()
dep_dict = {'task1': 'task2'}
with patch.object(gl_dag, 'prepare'), \
patch('workflow_engine.workflow_processor.DAG._DAG__build_dag',
return_value=(gl_dag, dep_dict)), \
patch('workflow_engine.workflow_processor.DAG._DAG__create_execution_plan',
return_value=execution_plan_dict):
ret_dag = DAG(task_collection=task_collection, reverse=reverse)
else:
ret_dag = DAG(task_collection=task_collection, reverse=reverse)

if finished_task_status := request.param.get('finished_task_status', False):
ret_dag.finished_tasks_status = finished_task_status

return ret_dag


@pytest.fixture
def w_processor(request) -> WorkflowProcessor:
"""Create a mocked WorkflowProcessor instance."""

workflow_file = request.param.get('workflow_file', 'workflow.yaml')
dry_run = request.param.get('dry_run', False)
threads = request.param.get('threads', 1)
log_level = request.param.get('log_level', 'info')
schema_file = request.param.get('schema_file', 'schema.yaml')
with patch("workflow_engine.workflow_processor.WorkflowFile") as file_mock:
workflow_file_instance = file_mock.return_value
workflow_file_instance.task_collection = request.param.get('task_collection', DEFAULT_TASK_COLLECTION)
if request.param.get('patch', True):
with patch('workflow_engine.workflow_processor.logger.setLevel'):
processor = WorkflowProcessor(workflow_file, dry_run, threads,
log_level, schema_file)
else:
processor = WorkflowProcessor(workflow_file, dry_run,
threads, log_level, schema_file)
return processor
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
# Copyright (C) 2015, Wazuh Inc.
# Created by Wazuh, Inc. <[email protected]>.
# This program is a free software; you can redistribute it and/or modify it under the terms of GPLv2
version: 0.1
description: This workflow is used to test agents deployment with a single manager.
variables:
agents-os:
- linux-ubuntu-22.04-amd64
manager-os: linux-ubuntu-22.04-amd64
infra-provider: vagrant
working-dir: /tmp/dtt1

tasks:
# Generic agent test task
- task: "run-agent-tests-{agent}"
description: "Run tests uninstall for the {agent} agent."
do:
this: process
with:
path: python3
args:
- modules/testing/main.py
- inventory: "{working-dir}/agent-{agent}/inventory.yaml"
- dependencies:
- manager: "{working-dir}/manager-{manager-os}/inventory.yaml"
- agent: "{working-dir}/agent-{agent}/inventory.yaml"
- tests: "install,register,stop"
- component: "agent"
- wazuh-version: "4.7.1"
- wazuh-revision: "40709"
depends-on:
- "provision-install-{agent}"
- "provision-manager"
foreach:
- variable: agents-os
as: agent

# Generic agent test task
- task: "run-agent-tests-uninstall-{agent}"
description: "Run tests uninstall for the {agent} agent."
do:
this: process
with:
path: python3
args:
- modules/testing/main.py
- inventory: "{working-dir}/agent-{agent}/inventory.yaml"
- dependencies:
- manager: "{working-dir}/manager-{manager-os}/inventory.yaml"
- tests: "uninstall"
- component: "agent"
- wazuh-version: "4.7.1"
- wazuh-revision: "40709"
depends-on:
- "run-agent-tests-{agent}"
- "provision-uninstall-{agent}"
foreach:
- variable: agents-os
as: agent

# Unique manager provision task
- task: "provision-manager"
description: "Provision the manager."
do:
this: process
with:
path: python3
args:
- modules/provision/main.py
- inventory-manager: "{working-dir}/manager-{manager-os}/inventory.yaml"
- install:
- component: wazuh-manager
type: aio
version: "4.7.0"
depends-on:
- "allocate-manager"

# Unique manager allocate task
- task: "allocate-manager"
description: "Allocate resources for the manager."
do:
this: process
with:
path: python3
args:
- modules/allocation/main.py
- action: create
- provider: "{infra-provider}"
- size: large
- composite-name: "{manager-os}"
- inventory-output: "{working-dir}/manager-{manager-os}/inventory.yaml"
- track-output: "{working-dir}/manager-{manager-os}/track.yaml"
cleanup:
this: process
with:
args:
- modules/allocation/main.py
- action: delete
- track-output: "{working-dir}/manager-{manager-os}/track.yaml"

# Generic agent provision task
- task: "provision-install-{agent}"
description: "Provision resources for the {agent} agent."
do:
this: process
with:
path: python3
args:
- modules/provision/main.py
- inventory-agent: "{working-dir}/agent-{agent}/inventory.yaml"
- inventory-manager: "{working-dir}/manager-{manager-os}/inventory.yaml"
- install:
- component: wazuh-agent
type: aio
version: "4.8.0"
live: False
depends-on:
- "allocate-{agent}"
- "provision-manager"
foreach:
- variable: agents-os
as: agent

# Generic agent provision task
- task: "provision-uninstall-{agent}"
description: "Provision resources for the {agent} agent."
do:
this: process
with:
path: python3
args:
- modules/provision/main.py
- inventory-agent: "{working-dir}/agent-{agent}/inventory.yaml"
- inventory-manager: "{working-dir}/manager-{manager-os}/inventory.yaml"
- uninstall:
- component: wazuh-agent
type: package
depends-on:
- "provision-install-{agent}"
foreach:
- variable: agents-os
as: agent

# Generic agent allocate task
- task: "allocate-{agent}"
description: "Allocate resources for the {agent} agent."
do:
this: process
with:
path: python3
args:
- modules/allocation/main.py
- action: create
- provider: "{infra-provider}"
- size: small
- composite-name: "{agent}"
- inventory-output: "{working-dir}/agent-{agent}/inventory.yaml"
- track-output: "{working-dir}/agent-{agent}/track.yaml"
cleanup:
this: process
with:
path: python3
args:
- modules/allocation/main.py
- action: delete
- track-output: "{working-dir}/agent-{agent}/track.yaml"
foreach:
- variable: agents-os
as: agent
Loading

0 comments on commit f7c5df4

Please sign in to comment.