Skip to content

Commit 121bffa

Browse files
authored
Activate ruff rules on SSI/auto-injection (#6805)
1 parent bf8d7b7 commit 121bffa

11 files changed

Lines changed: 96 additions & 83 deletions

File tree

pyproject.toml

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -203,8 +203,6 @@ ignore = [
203203
"SIM108", # if-else-block-instead-of-if-exp: ternary operator nay be harder to read on long statements
204204
]
205205
"tests/auto_inject/*" = [
206-
"ANN001", # missing-type-function-argument: TODO
207-
"RUF059", # unused-unpacked-variable: TODO
208206
"SLF001", # private-member-access: TODO
209207
]
210208
"tests/fuzzer/*" = [
@@ -224,11 +222,9 @@ ignore = [
224222
"S605", # start-process-with-a-shell: test the test needs to run shell commands
225223
]
226224
"utils/_context/_scenarios/auto_injection.py" = [
227-
"ANN001", # missing-type-function-argument: TODO
228225
"PLC0415", # import-outside-top-level TODO
229226
]
230227
"utils/{_context/_scenarios/docker_ssi.py,docker_ssi/docker_ssi_matrix_utils.py}" = [
231-
"ANN001", # missing-type-function-argument: TODO
232228
"E501", # line-too-long: TODO
233229
"T201", # print: TODO
234230
"RET504", # unnecessary-assign: TODO
@@ -242,11 +238,6 @@ ignore = [
242238
"ANN205" # missing-return-type-static-method: obvious decorators
243239
]
244240
"utils/build/*" = ["ALL"] # mostly python weblog code. it may be a good idea to enable rules here
245-
"utils/docker_ssi/*" = [
246-
"ANN001", # missing-type-function-argument: TODO
247-
"ANN201", # missing-return-type-undocumented-public-function: TODO
248-
"ANN205", # issing-return-type-static-method: TODO
249-
]
250241

251242
"utils/{k8s_lib_injection/*,_context/_scenarios/k8s_lib_injection.py}" = [
252243
"ANN003", # missing-type-kwargs: TODO
@@ -288,7 +279,6 @@ ignore = [
288279
"UP017", # datetime-timezone-utc: TODO
289280
]
290281
"utils/scripts/ssi_wizards/aws_onboarding_wizard_utils.py" = [
291-
"ANN001", # missing-type-function-argument: TODO
292282
"PLR2004", # magic-value-comparison: TODO
293283
]
294284

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ setuptools==75.8.0
4242
shellcheck-py==0.11.0.1
4343
types-aiofiles==24.1.0.20241221
4444
types-mock==5.2.0.20250924
45+
types-paramiko==4.0.0.20250822
4546
types-protobuf==5.29.1.20241207
4647
types-python-dateutil==2.9.0.20241206
4748
types-PyYAML==6.0.12.20241230

tests/auto_inject/test_auto_inject_chaos.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88

99
class BaseAutoInjectChaos(base.AutoInjectBaseTest):
10-
def _test_removing_things(self, virtual_machine: _VirtualMachine, evil_command):
10+
def _test_removing_things(self, virtual_machine: _VirtualMachine, evil_command: str):
1111
"""Test break the installation and restore it.
1212
After breaking the installation, the app should be still working (but no sending traces to the backend).
1313
After breaking the installation, we can restart the app
@@ -64,7 +64,7 @@ def _test_removing_things(self, virtual_machine: _VirtualMachine, evil_command):
6464
logger.info("Restoring installation using the command:: ")
6565
apm_inject_restore = f"{prefix_env} {apm_inject_restore}"
6666
logger.info(apm_inject_restore)
67-
_, stdout, stderr = virtual_machine.get_ssh_connection().exec_command(apm_inject_restore)
67+
_, stdout, _stderr = virtual_machine.get_ssh_connection().exec_command(apm_inject_restore)
6868
stdout.channel.set_combine_stderr(True)
6969

7070
# Read the output line by line

tests/auto_inject/test_blocklist_auto_inject.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import uuid
2+
import paramiko
23
from scp import SCPClient
34

45
from utils import scenarios, context, features, irrelevant, logger
@@ -8,7 +9,7 @@
89
class _AutoInjectWorkloadSelectionBaseTest:
910
"""Base class to test workload selection policies on auto instrumentation."""
1011

11-
def _execute_remote_command(self, ssh_client, command):
12+
def _execute_remote_command(self, ssh_client: paramiko.SSHClient, command: str):
1213
"""Execute remote command and get remote log file from the vm. You can use this method using env variables or using injection config file"""
1314

1415
unique_log_name = f"host_injection_{uuid.uuid4()}.log"

tests/auto_inject/utils.py

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import time
2+
import paramiko
23
from utils.onboarding.weblog_interface import make_get_request, warmup_weblog, make_internal_get_request
34
from utils.onboarding.backend_interface import wait_backend_trace_id
45
from utils.onboarding.wait_for_tcp_port import wait_for_port
@@ -10,7 +11,12 @@
1011

1112
class AutoInjectBaseTest:
1213
def _test_install(
13-
self, virtual_machine, *, profile: bool = False, appsec: bool = False, origin_detection: bool = False
14+
self,
15+
virtual_machine: _VirtualMachine,
16+
*,
17+
profile: bool = False,
18+
appsec: bool = False,
19+
origin_detection: bool = False,
1420
):
1521
"""If there is a multicontainer app, we need to make a request to each app"""
1622

@@ -32,7 +38,7 @@ def _test_install(
3238
def _check_install(
3339
self,
3440
virtual_machine: _VirtualMachine,
35-
vm_context_url,
41+
vm_context_url: str,
3642
*,
3743
profile: bool = False,
3844
appsec: bool = False,
@@ -75,7 +81,7 @@ def _check_install(
7581
self._log_trace_debug_message(e, request_uuid)
7682
raise
7783

78-
def _appsec_validator(self, _, trace_data):
84+
def _appsec_validator(self, _: str, trace_data: dict):
7985
"""Validator for Appsec traces that checks if the trace contains an Appsec event."""
8086
root_id = trace_data["trace"]["root_id"]
8187
root_span = trace_data["trace"]["spans"][root_id]
@@ -102,7 +108,7 @@ def _appsec_validator(self, _, trace_data):
102108
logger.error("expected 'appsec.event' to be true in trace meta or at least one rule triggered")
103109
return False
104110

105-
def _container_tags_validator(self, _, trace_data):
111+
def _container_tags_validator(self, _: str, trace_data: dict):
106112
root_id = trace_data["trace"]["root_id"]
107113
root_span = trace_data["trace"]["spans"][root_id]
108114

@@ -127,14 +133,14 @@ def _log_trace_debug_message(self, exc: Exception, request_uuid: str) -> None:
127133
f"- A problem processing the intake in the backend (manually locate the trace id [{request_uuid}] in the DD console, using the system-tests organization)\n"
128134
)
129135

130-
def close_channel(self, channel) -> None:
136+
def close_channel(self, channel: paramiko.Channel) -> None:
131137
try:
132138
if not channel.eof_received:
133139
channel.close()
134140
except Exception as e:
135141
logger.error(f"Error closing the channel: {e}")
136142

137-
def execute_command(self, virtual_machine, command) -> str:
143+
def execute_command(self, virtual_machine: _VirtualMachine, command: str) -> str:
138144
# Env for the command
139145
prefix_env = ""
140146
for key, value in virtual_machine.get_command_environment().items():
@@ -165,7 +171,12 @@ def execute_command(self, virtual_machine, command) -> str:
165171
return command_output
166172

167173
def _test_uninstall_commands(
168-
self, virtual_machine, stop_weblog_command, start_weblog_command, uninstall_command, install_command
174+
self,
175+
virtual_machine: _VirtualMachine,
176+
stop_weblog_command: str,
177+
start_weblog_command: str,
178+
uninstall_command: str,
179+
install_command: str,
169180
):
170181
"""We can unistall the auto injection software. We can start the app again
171182
The weblog app should work but no sending traces to the backend.
@@ -228,7 +239,7 @@ def _test_uninstall_commands(
228239
self._test_install(virtual_machine)
229240
logger.info(f"Success _test_uninstall for : [{virtual_machine.name}]")
230241

231-
def _test_uninstall(self, virtual_machine):
242+
def _test_uninstall(self, virtual_machine: _VirtualMachine):
232243
header = "----------------------------------------------------------------------"
233244
vm_logger(context.scenario.host_log_folder, virtual_machine.name).info(
234245
f"{header} \n {header} \n Launching the uninstall for VM: {virtual_machine.name} \n {header} \n {header}"
@@ -250,7 +261,7 @@ def _test_uninstall(self, virtual_machine):
250261
virtual_machine, stop_weblog_command, start_weblog_command, uninstall_command, install_command
251262
)
252263

253-
def _test_no_world_writeable(self, virtual_machine):
264+
def _test_no_world_writeable(self, virtual_machine: _VirtualMachine):
254265
"""Checks that there are no world writeable files in /opt/datadog-packages/datadog-apm*"""
255266
logger = vm_logger(context.scenario.host_log_folder, virtual_machine.name)
256267
logger.info(

utils/_context/_scenarios/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1094,7 +1094,7 @@ class _Scenarios:
10941094
"DOCKER_SSI_APPSEC",
10951095
doc="Validates the installer and the ssi on a docker environment",
10961096
extra_env_vars={"DD_SERVICE": "payments-service"},
1097-
appsec_enabled="true",
1097+
appsec_enabled=True,
10981098
scenario_groups=[scenario_groups.all, scenario_groups.docker_ssi],
10991099
)
11001100
docker_ssi_crashtracking = DockerSSIScenario(

utils/_context/_scenarios/auto_injection.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from utils._logger import logger
88
from utils.onboarding.debug_vm import download_vm_logs
99
from utils.virtual_machine.virtual_machines import _VirtualMachine, load_virtual_machines
10-
from .core import Scenario
10+
from .core import Scenario, ScenarioGroup
1111

1212

1313
class _VirtualMachineScenario(Scenario):
@@ -19,10 +19,10 @@ def __init__(
1919
*,
2020
github_workflow: str,
2121
doc: str,
22-
vm_provision=None,
23-
agent_env=None,
24-
app_env=None,
25-
scenario_groups=None,
22+
vm_provision: str | None = None,
23+
agent_env: dict | None = None,
24+
app_env: dict | None = None,
25+
scenario_groups: list[ScenarioGroup] | None = None,
2626
) -> None:
2727
super().__init__(name, doc=doc, github_workflow=github_workflow, scenario_groups=scenario_groups)
2828
self.vm_provision_name = vm_provision
@@ -153,7 +153,7 @@ def fill_context(self):
153153
# different version
154154
del self.components[key]
155155

156-
def pytest_sessionfinish(self, session, exitstatus): # noqa: ARG002
156+
def pytest_sessionfinish(self, session: pytest.Session, exitstatus: int) -> None: # noqa: ARG002
157157
self.close_targets()
158158

159159
def close_targets(self):
@@ -227,13 +227,13 @@ def customize_feature_parity_dashboard(self, result: dict):
227227
class InstallerAutoInjectionScenario(_VirtualMachineScenario):
228228
def __init__(
229229
self,
230-
name,
231-
doc,
232-
vm_provision="installer-auto-inject",
233-
agent_env=None,
234-
app_env=None,
235-
scenario_groups=None,
236-
github_workflow=None,
230+
name: str,
231+
doc: str,
232+
vm_provision: str = "installer-auto-inject",
233+
agent_env: dict | None = None,
234+
app_env: dict | None = None,
235+
scenario_groups: list[ScenarioGroup] | None = None,
236+
github_workflow: str | None = None,
237237
) -> None:
238238
# Force full tracing without limits
239239
app_env_defaults = {

0 commit comments

Comments
 (0)