Skip to content

Commit

Permalink
Implement schema_validator unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Marcelo Ariel Hamra committed Mar 7, 2024
1 parent 11dacea commit 6b56e0a
Show file tree
Hide file tree
Showing 4 changed files with 234 additions and 4 deletions.
7 changes: 4 additions & 3 deletions deployability/modules/workflow_engine/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"""Common unit test fixtures."""
import graphlib

from unittest.mock import patch
from unittest.mock import patch, MagicMock
import pytest

from workflow_engine.workflow_processor import DAG, WorkflowProcessor, WorkflowFile
Expand All @@ -17,9 +17,10 @@


@pytest.fixture
def logger_mock():
def logger_mock(request) -> MagicMock:
"""Fixture to mock common logger methods."""
with patch("workflow_engine.workflow_processor.logger") as l_mock:
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')
Expand Down
170 changes: 170 additions & 0 deletions deployability/modules/workflow_engine/tests/data/wf-ok.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
# Copyright (C) 2015, Wazuh Inc.
# Created by Wazuh, Inc. <info@wazuh.com>.
# 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:
path: python3
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Copyright (C) 2015-2021, Wazuh Inc.
# Created by Wazuh, Inc. <info@wazuh.com>.
# This program is free software; you can redistribute it and/or modify it under the terms of GPLv2
"""SchemaValidator unit tests."""
from unittest.mock import patch, MagicMock, call, mock_open
import json
from ruamel.yaml import YAML
from pathlib import Path
import pytest

from workflow_engine.schema_validator import SchemaValidator

@pytest.mark.parametrize('logger_mock',
[{'logger_to_patch':'workflow_engine.schema_validator.logger'}],
indirect=True)
def test_schema_validator_constructor(logger_mock: MagicMock):
"""Test SchemaValidator constructor normal flow."""
schema_path = Path(__file__).parent.parent / 'schemas' / 'schema_v1.json'
with open(schema_path, 'r') as schema_file:
schema_data = json.load(schema_file)

wf_file_path = Path(__file__).parent / 'data' / 'wf-ok.yml'
with open(wf_file_path, 'r') as file:
yaml = YAML(typ='safe', pure=True)
yaml_data = yaml.load(file)

validator = SchemaValidator(schema_path, wf_file_path)
assert validator.schema_data == schema_data
assert validator.yaml_data == yaml_data
calls = [call(f"Loading schema file: {schema_path}"),
call(f"Loading yaml file: {wf_file_path}")]
logger_mock.debug.assert_has_calls(calls)


def test_preprocess_data():
pass

def test_validate_schema():
pass
Original file line number Diff line number Diff line change
Expand Up @@ -179,4 +179,24 @@ def side_effect(s, e, v = None):

def test_workflow_file_static_workflow_validation():
"""Test WorkflowFile.__static_workflow_validation."""

wf = MagicMock()
wf.task_collection = [{"task": "task 1", "param": "1"},
{"task": "task 2", "param": "2", 'depends-on': ['task 1']}
]
WorkflowFile._WorkflowFile__static_workflow_validation(wf)


@pytest.mark.parametrize('task_collection, error_msg', [
([{"task": "task 1", "param": "1"},
{"task": "task 1", "param": "2", 'depends-on': ['task 1']}],
'Duplicated task names: task 1'),
([{"task": "task 1", "param": "1", 'depends-on': ['task 3', 'task 4']},
{"task": "task 2", "param": "2", 'depends-on': ['task 3']}],
'Tasks do not exist: task 3, task 4')
])
def test_workflow_file_static_workflow_validation_ko(task_collection: dict, error_msg: str):
"""Test WorkflowFile.__static_workflow_validation."""
wf = MagicMock()
wf.task_collection = task_collection
with pytest.raises(ValueError, match=error_msg):
WorkflowFile._WorkflowFile__static_workflow_validation(wf)

0 comments on commit 6b56e0a

Please sign in to comment.