Skip to content

Commit ee59da8

Browse files
committed
Used filters
Signed-off-by: shlao <[email protected]>
1 parent f4f7b8b commit ee59da8

File tree

2 files changed

+22
-19
lines changed

2 files changed

+22
-19
lines changed

insights/specs/datasources/host_key_files.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,18 @@
44

55
import os
66

7+
from insights.specs import Specs
78
from insights.core.context import HostContext
8-
from insights.core.exceptions import SkipComponent
9+
from insights.core.filters import add_filter
910
from insights.core.plugins import datasource
11+
from insights.core.exceptions import SkipComponent
1012
from insights.core.spec_factory import DatasourceProvider
1113

12-
ERROR_KEY = "error: Could not load host key:"
13-
VAR_LOG_MSG = '/var/log/messages'
14+
ERROR_MSG = "error: Could not load host key:"
15+
add_filter(Specs.messages, ERROR_MSG)
1416

1517

16-
@datasource(HostContext)
18+
@datasource(Specs.messages, HostContext)
1719
def host_key_files(broker):
1820
"""
1921
This datasource reads '/var/log/messages' to check the host key path, and check
@@ -30,10 +32,10 @@ def host_key_files(broker):
3032
SkipComponent: When any exception occurs.
3133
"""
3234
error_loadings = set()
33-
with open(VAR_LOG_MSG, 'r', encoding='utf-8') as file:
34-
for line in file:
35-
if ERROR_KEY in line:
36-
error_loadings.add(line.split(ERROR_KEY)[-1].strip())
35+
messages = broker[Specs.messages].content
36+
for line in messages:
37+
if ERROR_MSG in line:
38+
error_loadings.add(line.split(ERROR_MSG)[-1].strip())
3739

3840
if not error_loadings:
3941
raise SkipComponent()
Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import pytest
22

3-
from mock.mock import patch, mock_open
3+
from mock.mock import patch, Mock
4+
from insights.specs import Specs
45
from insights.core.dr import SkipComponent
56
from insights.core.spec_factory import DatasourceProvider
67
from insights.specs.datasources.host_key_files import host_key_files
@@ -17,31 +18,31 @@
1718
""".strip()
1819

1920

20-
@patch(
21-
'insights.specs.datasources.host_key_files.open', new_callable=mock_open, read_data=VAR_LOGS
22-
)
2321
@patch('os.path.exists')
24-
def test_host_key_files(mock_exists, mock_ds_open):
22+
def test_host_key_files(mock_exists):
2523
files_dict = {
2624
'/etc/ssh/ssh_host_dsa_key': True,
2725
'/etc/ssh_host_dsa_key': False,
2826
}
2927
mock_exists.side_effect = lambda path: files_dict.get(path, False)
3028

31-
broker = {}
29+
messages = Mock()
30+
messages.content = VAR_LOGS.splitlines()
31+
broker = {Specs.messages: messages}
32+
3233
result = host_key_files(broker)
3334
assert result is not None
3435

3536
expected = DatasourceProvider(content=EXPECTED_RESULT, relative_path=RELATIVE_PATH)
3637
assert len(result.content) == len(expected.content) == 2
3738

3839

39-
@patch(
40-
'insights.specs.datasources.host_key_files.open', new_callable=mock_open, read_data=""
41-
)
4240
@patch('os.path.exists')
43-
def test_host_key_files_empty_log(mock_exists, mock_ds_open):
44-
broker = {}
41+
def test_host_key_files_empty_log(mock_exists):
42+
messages = Mock()
43+
messages.content = []
44+
broker = {Specs.messages: messages}
45+
4546
with pytest.raises(SkipComponent) as e:
4647
host_key_files(broker)
4748
assert 'SkipComponent' in str(e)

0 commit comments

Comments
 (0)