]>
git.proxmox.com Git - ceph.git/blob - ceph/src/pybind/mgr/cephadm/tests/test_agent.py
1 from unittest
.mock
import MagicMock
2 from cephadm
.agent
import Root
5 class FakeDaemonDescription
:
6 def __init__(self
, ip
, ports
, hostname
, service_name
='', daemon_type
=''):
9 self
.hostname
= hostname
10 self
._service
_name
= service_name
11 self
.daemon_type
= daemon_type
13 def service_name(self
):
14 return self
._service
_name
18 def get_daemons_by_service(self
, service_type
):
19 if service_type
== 'ceph-exporter':
20 return [FakeDaemonDescription('1.2.3.4', [9926], 'node0'),
21 FakeDaemonDescription('1.2.3.5', [9926], 'node1')]
23 return [FakeDaemonDescription('1.2.3.4', [9100], 'node0'),
24 FakeDaemonDescription('1.2.3.5', [9200], 'node1')]
26 def get_daemons_by_type(self
, daemon_type
):
27 return [FakeDaemonDescription('1.2.3.4', [9100], 'node0', 'ingress', 'haproxy'),
28 FakeDaemonDescription('1.2.3.5', [9200], 'node1', 'ingress', 'haproxy')]
32 def get_addr(self
, name
: str):
36 class FakeServiceSpec
:
37 def __init__(self
, port
):
38 self
.monitor_port
= port
41 class FakeSpecDescription
:
42 def __init__(self
, port
):
43 self
.spec
= FakeServiceSpec(port
)
46 class FakeSpecStore():
47 def __init__(self
, mgr
):
49 self
._specs
= {'ingress': FakeSpecDescription(9049)}
51 def __contains__(self
, name
):
52 return name
in self
._specs
54 def __getitem__(self
, name
):
55 return self
._specs
['ingress']
61 self
.check_mon_command
= MagicMock(side_effect
=self
._check
_mon
_command
)
62 self
.mon_command
= MagicMock(side_effect
=self
._check
_mon
_command
)
63 self
.template
= MagicMock()
64 self
.log
= MagicMock()
65 self
.inventory
= FakeInventory()
66 self
.cache
= FakeCache()
67 self
.spec_store
= FakeSpecStore(self
)
69 def list_servers(self
):
73 'ceph_version': '16.2',
74 'services': [{'type': 'mgr'}, {'type': 'mon'}]},
76 'ceph_version': '16.2',
77 'services': [{'type': 'mgr'}, {'type': 'mon'}]}
82 def _check_mon_command(self
, cmd_dict
, inbuf
=None):
83 prefix
= cmd_dict
.get('prefix')
84 if prefix
== 'get-cmd':
85 return 0, self
.config
, ''
86 if prefix
== 'set-cmd':
87 self
.config
= cmd_dict
.get('value')
88 return 0, 'value set', ''
89 return -1, '', 'error'
91 def get_module_option_ex(self
, module
, option
, default_value
):
95 class TestCephadmService
:
97 def test_get_sd_config_prometheus(self
):
100 cfg
= root
.get_sd_config('mgr-prometheus')
102 # check response structure
105 assert 'labels' in entry
106 assert 'targets' in entry
109 assert cfg
[0]['targets'] == ['node0:9283', 'node1:9283']
111 def test_get_sd_config_node_exporter(self
):
114 cfg
= root
.get_sd_config('node-exporter')
116 # check response structure
119 assert 'labels' in entry
120 assert 'targets' in entry
123 assert cfg
[0]['targets'] == ['1.2.3.4:9100']
124 assert cfg
[0]['labels'] == {'instance': 'node0'}
125 assert cfg
[1]['targets'] == ['1.2.3.5:9200']
126 assert cfg
[1]['labels'] == {'instance': 'node1'}
128 def test_get_sd_config_alertmgr(self
):
131 cfg
= root
.get_sd_config('alertmanager')
133 # check response structure
136 assert 'labels' in entry
137 assert 'targets' in entry
140 assert cfg
[0]['targets'] == ['1.2.3.4:9100', '1.2.3.5:9200']
142 def test_get_sd_config_haproxy(self
):
145 cfg
= root
.get_sd_config('haproxy')
147 # check response structure
150 assert 'labels' in entry
151 assert 'targets' in entry
154 assert cfg
[0]['targets'] == ['1.2.3.4:9049']
155 assert cfg
[0]['labels'] == {'instance': 'ingress'}
157 def test_get_sd_config_ceph_exporter(self
):
160 cfg
= root
.get_sd_config('ceph-exporter')
162 # check response structure
165 assert 'labels' in entry
166 assert 'targets' in entry
169 assert cfg
[0]['targets'] == ['1.2.3.4:9926']
171 def test_get_sd_config_invalid_service(self
):
174 cfg
= root
.get_sd_config('invalid-service')