|
1 | 1 | import pytest
|
2 | 2 |
|
3 |
| -from mock.mock import patch, mock_open |
| 3 | +from mock.mock import patch, Mock |
| 4 | +from insights.specs import Specs |
4 | 5 | from insights.core.dr import SkipComponent
|
5 | 6 | from insights.core.spec_factory import DatasourceProvider
|
6 | 7 | from insights.specs.datasources.host_key_files import host_key_files
|
7 | 8 |
|
8 | 9 | VAR_LOGS = """
|
9 | 10 | Mar 16 19:30:54 host sshd[10644]: error: Could not load host key: /etc/ssh/ssh_host_dsa_key
|
| 11 | +Mar 16 19:30:54 host sshd[10645]: error: New host key: /etc/ssh_host_dsa_key |
10 | 12 | Mar 16 19:30:54 host sshd[10645]: error: Could not load host key: /etc/ssh_host_dsa_key
|
11 | 13 | """.strip()
|
12 | 14 |
|
|
17 | 19 | """.strip()
|
18 | 20 |
|
19 | 21 |
|
20 |
| -@patch( |
21 |
| - 'insights.specs.datasources.host_key_files.open', new_callable=mock_open, read_data=VAR_LOGS |
22 |
| -) |
23 | 22 | @patch('os.path.exists')
|
24 |
| -def test_host_key_files(mock_exists, mock_ds_open): |
| 23 | +def test_host_key_files(mock_exists): |
25 | 24 | files_dict = {
|
26 | 25 | '/etc/ssh/ssh_host_dsa_key': True,
|
27 | 26 | '/etc/ssh_host_dsa_key': False,
|
28 | 27 | }
|
29 | 28 | mock_exists.side_effect = lambda path: files_dict.get(path, False)
|
30 | 29 |
|
31 |
| - broker = {} |
| 30 | + messages = Mock() |
| 31 | + messages.content = VAR_LOGS.splitlines() |
| 32 | + broker = {Specs.messages: messages} |
| 33 | + |
32 | 34 | result = host_key_files(broker)
|
33 | 35 | assert result is not None
|
34 | 36 |
|
35 | 37 | expected = DatasourceProvider(content=EXPECTED_RESULT, relative_path=RELATIVE_PATH)
|
36 | 38 | assert len(result.content) == len(expected.content) == 2
|
37 | 39 |
|
38 | 40 |
|
39 |
| -@patch( |
40 |
| - 'insights.specs.datasources.host_key_files.open', new_callable=mock_open, read_data="" |
41 |
| -) |
42 | 41 | @patch('os.path.exists')
|
43 |
| -def test_host_key_files_empty_log(mock_exists, mock_ds_open): |
44 |
| - broker = {} |
| 42 | +def test_host_key_files_empty_log(mock_exists): |
| 43 | + messages = Mock() |
| 44 | + messages.content = [] |
| 45 | + broker = {Specs.messages: messages} |
| 46 | + |
45 | 47 | with pytest.raises(SkipComponent) as e:
|
46 | 48 | host_key_files(broker)
|
47 | 49 | assert 'SkipComponent' in str(e)
|
0 commit comments