-
Notifications
You must be signed in to change notification settings - Fork 92
/
Copy pathtest_holmes_sync_toolsets.py
295 lines (227 loc) · 8.96 KB
/
test_holmes_sync_toolsets.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
from typing import Any, Dict
from unittest.mock import Mock, patch
import pytest
import subprocess
import os
from holmes.utils.holmes_sync_toolsets import holmes_sync_toolsets_status
from holmes.core.tools import (
Toolset,
ToolsetCommandPrerequisite,
ToolsetEnvironmentPrerequisite,
ToolsetStatusEnum,
ToolsetTag,
YAMLTool,
StaticPrerequisite,
)
from holmes.config import Config
@pytest.fixture
def mock_dal():
dal = Mock()
dal.account_id = "test-account"
dal.sync_toolsets = Mock()
return dal
@pytest.fixture
def mock_config():
config = Mock(spec=Config)
config.cluster_name = "test-cluster"
return config
class SampleToolset(Toolset):
def get_example_config(self) -> Dict[str, Any]:
return {}
@pytest.fixture
def sample_toolset():
return SampleToolset(
name="test-toolset",
description="Test toolset",
enabled=True,
tools=[
YAMLTool(name="test-tool", description="Test tool", command="echo test")
],
tags=[ToolsetTag.CORE],
)
def test_sync_toolsets_basic(mock_dal, mock_config, sample_toolset):
mock_config.create_tool_executor.return_value = Mock(toolsets=[sample_toolset])
holmes_sync_toolsets_status(mock_dal, mock_config)
mock_dal.sync_toolsets.assert_called_once()
call_args = mock_dal.sync_toolsets.call_args[0]
assert len(call_args[0]) == 1
toolset_data = call_args[0][0]
assert toolset_data["toolset_name"] == "test-toolset"
assert toolset_data["cluster_id"] == "test-cluster"
assert toolset_data["account_id"] == "test-account"
assert toolset_data["description"] == "Test toolset"
assert toolset_data["status"] == ToolsetStatusEnum.DISABLED
assert isinstance(toolset_data["updated_at"], str)
def test_sync_toolsets_no_cluster_name(mock_dal):
config = Mock(spec=Config)
config.cluster_name = None
with pytest.raises(Exception) as exc_info:
holmes_sync_toolsets_status(mock_dal, config)
assert "Cluster name is missing" in str(exc_info.value)
mock_dal.sync_toolsets.assert_not_called()
@patch(
"holmes.utils.holmes_sync_toolsets.render_default_installation_instructions_for_toolset"
)
def test_sync_toolsets_with_installation_instructions(
mock_render, mock_dal, mock_config, sample_toolset
):
mock_render.return_value = "Test installation instructions"
mock_config.create_tool_executor.return_value = Mock(toolsets=[sample_toolset])
holmes_sync_toolsets_status(mock_dal, mock_config)
mock_dal.sync_toolsets.assert_called_once()
toolset_data = mock_dal.sync_toolsets.call_args[0][0][0]
assert toolset_data["installation_instructions"] == "Test installation instructions"
mock_render.assert_called_once_with(sample_toolset)
@patch("subprocess.run")
def test_sync_toolsets_multiple(mock_subprocess_run, mock_dal, mock_config):
mock_subprocess_run.return_value = Mock(stdout="success", returncode=0)
toolset1 = SampleToolset(
name="toolset1",
description="First toolset",
enabled=True,
tools=[YAMLTool(name="tool1", description="Tool 1", command="echo 1")],
tags=[ToolsetTag.CORE],
)
toolset1.check_prerequisites()
toolset2 = SampleToolset(
name="toolset2",
description="Second toolset",
enabled=False,
tools=[YAMLTool(name="tool2", description="Tool 2", command="echo 2")],
tags=[ToolsetTag.CLI],
prerequisites=[
StaticPrerequisite(enabled=False, disabled_reason="Feature flag disabled")
],
)
toolset2.check_prerequisites()
mock_config.create_tool_executor.return_value = Mock(toolsets=[toolset1, toolset2])
holmes_sync_toolsets_status(mock_dal, mock_config)
mock_dal.sync_toolsets.assert_called_once()
toolsets_data = mock_dal.sync_toolsets.call_args[0][0]
assert len(toolsets_data) == 2
assert toolsets_data[0]["toolset_name"] == "toolset1"
assert toolsets_data[0]["status"] == ToolsetStatusEnum.ENABLED
assert toolsets_data[1]["toolset_name"] == "toolset2"
assert toolsets_data[1]["status"] == ToolsetStatusEnum.FAILED
@patch("subprocess.run")
def test_sync_toolsets_with_prerequisites_check(
mock_subprocess_run, mock_dal, mock_config
):
mock_subprocess_run.return_value = Mock(stdout="success", returncode=0)
toolset = SampleToolset(
name="test-toolset",
description="Test toolset",
enabled=True,
tools=[
YAMLTool(name="test-tool", description="Test tool", command="echo test")
],
tags=[ToolsetTag.CORE],
prerequisites=[],
)
toolset.check_prerequisites()
mock_config.create_tool_executor.return_value = Mock(toolsets=[toolset])
holmes_sync_toolsets_status(mock_dal, mock_config)
mock_dal.sync_toolsets.assert_called_once()
toolset_data = mock_dal.sync_toolsets.call_args[0][0][0]
assert toolset_data["status"] == ToolsetStatusEnum.ENABLED
assert toolset_data["error"] is None
@patch("subprocess.run")
def test_sync_toolsets_with_failed_prerequisites(
mock_subprocess_run, mock_dal, mock_config
):
mock_subprocess_run.side_effect = subprocess.CalledProcessError(
1, "some-failing-command", "error output"
)
toolset = SampleToolset(
name="test-toolset",
description="Test toolset",
enabled=True,
tools=[
YAMLTool(name="test-tool", description="Test tool", command="echo test")
],
tags=[ToolsetTag.CORE],
prerequisites=[ToolsetCommandPrerequisite(command="some-failing-command")],
)
toolset.check_prerequisites()
mock_config.create_tool_executor.return_value = Mock(toolsets=[toolset])
holmes_sync_toolsets_status(mock_dal, mock_config)
mock_dal.sync_toolsets.assert_called_once()
toolset_data = mock_dal.sync_toolsets.call_args[0][0][0]
assert toolset_data["status"] == ToolsetStatusEnum.FAILED
assert toolset_data["error"] is not None
assert "Prerequisites check failed" in toolset_data["error"]
@patch("subprocess.run")
def test_sync_toolsets_with_successful_prerequisites(
mock_subprocess_run, mock_dal, mock_config
):
mock_subprocess_run.return_value = Mock(stdout="success\n", returncode=0)
toolset = SampleToolset(
name="test-toolset",
description="Test toolset",
enabled=True,
tools=[
YAMLTool(name="test-tool", description="Test tool", command="echo test")
],
tags=[ToolsetTag.CORE],
prerequisites=[
ToolsetCommandPrerequisite(
command="echo success", expected_output="success"
)
],
)
toolset.check_prerequisites()
mock_config.create_tool_executor.return_value = Mock(toolsets=[toolset])
holmes_sync_toolsets_status(mock_dal, mock_config)
mock_dal.sync_toolsets.assert_called_once()
toolset_data = mock_dal.sync_toolsets.call_args[0][0][0]
assert toolset_data["status"] == ToolsetStatusEnum.ENABLED
assert toolset_data["error"] is None
@patch.dict(os.environ, {}, clear=True)
def test_sync_toolsets_with_missing_env_var_prerequisites(mock_dal, mock_config):
toolset = SampleToolset(
name="test-toolset",
description="Test toolset",
enabled=True,
tools=[
YAMLTool(name="test-tool", description="Test tool", command="echo test")
],
tags=[ToolsetTag.CORE],
prerequisites=[ToolsetEnvironmentPrerequisite(env=["NONEXISTENT_ENV_VAR"])],
)
toolset.check_prerequisites()
mock_config.create_tool_executor.return_value = Mock(toolsets=[toolset])
holmes_sync_toolsets_status(mock_dal, mock_config)
mock_dal.sync_toolsets.assert_called_once()
toolset_data = mock_dal.sync_toolsets.call_args[0][0][0]
assert toolset_data["status"] == ToolsetStatusEnum.FAILED
assert toolset_data["error"] is not None
assert "NONEXISTENT_ENV_VAR" in toolset_data["error"]
@patch("subprocess.run")
def test_sync_toolsets_with_command_output_mismatch(
mock_subprocess_run, mock_dal, mock_config
):
mock_subprocess_run.return_value = Mock(
stdout="wrong output\n", returncode=0, stderr=""
)
toolset = SampleToolset(
name="test-toolset",
description="Test toolset",
enabled=True,
tools=[
YAMLTool(name="test-tool", description="Test tool", command="echo test")
],
tags=[ToolsetTag.CORE],
prerequisites=[
ToolsetCommandPrerequisite(
command="some-command", expected_output="expected output"
)
],
)
toolset.check_prerequisites()
mock_config.create_tool_executor.return_value = Mock(toolsets=[toolset])
holmes_sync_toolsets_status(mock_dal, mock_config)
mock_dal.sync_toolsets.assert_called_once()
toolset_data = mock_dal.sync_toolsets.call_args[0][0][0]
assert toolset_data["status"] == ToolsetStatusEnum.FAILED
assert toolset_data["error"] is not None
assert "Prerequisites check gave wrong output" in toolset_data["error"]