Skip to content

Commit

Permalink
Merge pull request #134 from SEKOIA-IO/fix/typing
Browse files Browse the repository at this point in the history
fix: Typing issue and tests warnings
  • Loading branch information
Darkheir authored Sep 10, 2024
2 parents 8093ac9 + 49db660 commit 3a94f6c
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 8 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ repos:

- id: ruff
name: Format with Ruff
entry: poetry run ruff
entry: poetry run ruff check
language: system
types: [ python ]
args: [ --fix, --exit-non-zero-on-fix, . ]
Expand Down
8 changes: 6 additions & 2 deletions sekoia_automation/action.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
MissingActionArgumentFileError,
SendEventError,
)
from sekoia_automation.module import Module, ModuleItem
from sekoia_automation.module import LogLevelStr, Module, ModuleItem
from sekoia_automation.storage import UPLOAD_CHUNK_SIZE
from sekoia_automation.utils import chunks, returns

Expand Down Expand Up @@ -107,7 +107,11 @@ def execute(self) -> None:
self.send_results()

def log(
self, message: str, level: str = "debug", only_sentry: bool = True, **kwargs
self,
message: str,
level: LogLevelStr = "debug",
only_sentry: bool = True,
**kwargs,
) -> None:
"""Log a message with a specific level."""
self._logs.append(
Expand Down
10 changes: 8 additions & 2 deletions sekoia_automation/module.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from abc import ABC, abstractmethod
from functools import cached_property
from pathlib import Path
from typing import Any, cast
from typing import Any, Literal, cast

import requests
import sentry_sdk
Expand All @@ -25,6 +25,8 @@
get_as_model,
)

LogLevelStr = Literal["fatal", "critical", "error", "warning", "info", "debug"]


class Module:
MODULE_CONFIGURATION_FILE_NAME = "module_configuration"
Expand Down Expand Up @@ -380,7 +382,11 @@ def _ensure_data_path_set(self):
raise

def log(
self, message: str, level: str = "debug", only_sentry: bool = False, **kwargs
self,
message: str,
level: LogLevelStr = "debug",
only_sentry: bool = False,
**kwargs,
) -> None:
"""Log a message with a specific level."""
# Right now propagates to sentry only errors and warnings
Expand Down
4 changes: 2 additions & 2 deletions sekoia_automation/trigger.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
TriggerConfigurationError,
)
from sekoia_automation.metrics import PrometheusExporterThread, make_exporter
from sekoia_automation.module import Module, ModuleItem
from sekoia_automation.module import LogLevelStr, Module, ModuleItem
from sekoia_automation.timer import RepeatedTimer
from sekoia_automation.utils import (
capture_retry_error,
Expand Down Expand Up @@ -306,7 +306,7 @@ def log_exception(self, exception: Exception, **kwargs):
message = kwargs.get("message", "An exception occurred")
self.log(f"{message}\n{exception}", level="error", propagate=False)

def log(self, message: str, level: str = "info", *args, **kwargs) -> None:
def log(self, message: str, level: LogLevelStr = "info", *args, **kwargs) -> None:
if level == "critical" and self._critical_log_sent:
# Prevent sending multiple critical errors
level = "error"
Expand Down
2 changes: 1 addition & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def mocked_load_config(module, file_name, type_="str", non_exist_ok=False):
elif "callback" in file_name:
return FAKE_URL
elif "sentry" in file_name:
return "http://1234@localhost/123"
return None
else:
return DEFAULT_ARGUMENTS

Expand Down
67 changes: 67 additions & 0 deletions tests/test_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
# third parties
import pytest
from pydantic import BaseModel
from sentry_sdk import get_isolation_scope

# internal
from sekoia_automation.exceptions import CommandNotFoundError, ModuleConfigurationError
Expand Down Expand Up @@ -196,3 +197,69 @@ def test_configuration_setter_missing_required_secret():
with pytest.raises(expected_exception=ModuleConfigurationError):
module.configuration = {"not a secret": "some value"}
assert module.configuration == {secret_name: "some other value"}


def test_playbook_uuid():
module = Module()
with patch.object(
module, "load_config", return_value="5e57c739-391a-4eb3-b6be-7d15ca92d5ed"
):
assert module.playbook_uuid == "5e57c739-391a-4eb3-b6be-7d15ca92d5ed"


def test_playbook_run_uuid():
module = Module()
with patch.object(
module, "load_config", return_value="5e57c739-391a-4eb3-b6be-7d15ca92d5ed"
):
assert module.playbook_run_uuid == "5e57c739-391a-4eb3-b6be-7d15ca92d5ed"


def test_node_run_uuid():
module = Module()
with patch.object(
module, "load_config", return_value="5e57c739-391a-4eb3-b6be-7d15ca92d5ed"
):
assert module.node_run_uuid == "5e57c739-391a-4eb3-b6be-7d15ca92d5ed"


def test_trigger_configuration_uuid():
module = Module()
with patch.object(
module, "load_config", return_value="5e57c739-391a-4eb3-b6be-7d15ca92d5ed"
):
assert (
module.trigger_configuration_uuid == "5e57c739-391a-4eb3-b6be-7d15ca92d5ed"
)


def test_connector_configuration_uuid():
module = Module()
with patch.object(
module, "load_config", return_value="5e57c739-391a-4eb3-b6be-7d15ca92d5ed"
):
assert (
module.connector_configuration_uuid
== "5e57c739-391a-4eb3-b6be-7d15ca92d5ed"
)


def test_init_sentry():
module = Module()
module._community_uuid = "community"
module._playbook_uuid = "playbook"
module._playbook_run_uuid = "playbook_run"
module._node_run_uuid = "node_run"
module._trigger_configuration_uuid = "trigger_configuration"
module._connector_configuration_uuid = "connector_configuration"

with patch.object(
module, "_load_sentry_dsn", return_value="http://1234@localhost/1234"
):
module.init_sentry()
tags = get_isolation_scope()._tags
assert tags["community"] == "community"
assert tags["playbook_uuid"] == "playbook"
assert tags["playbook_run_uuid"] == "playbook_run"
assert tags["trigger_configuration_uuid"] == "trigger_configuration"
assert tags["connector_configuration_uuid"] == "connector_configuration"

0 comments on commit 3a94f6c

Please sign in to comment.