Skip to content

Commit bd93a46

Browse files
cephadm: use parsed_container_mem_usage in cephadm.py
Replace the use of _parse_mem_usage and related command calls with parsed_container_mem_usage. This needs a lot of tests to be updated so that the call functions that now originate in `container_engines.py` get mocked. funkypatch handles most of the extra work for us - but it needs to be added to many tests. Signed-off-by: John Mulligan <[email protected]>
1 parent 395b8d3 commit bd93a46

File tree

3 files changed

+63
-51
lines changed

3 files changed

+63
-51
lines changed

src/cephadm/cephadm.py

Lines changed: 3 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@
8888
Podman,
8989
check_container_engine,
9090
find_container_engine,
91+
parsed_container_mem_usage,
9192
pull_command,
9293
registry_login,
9394
)
@@ -98,7 +99,6 @@
9899
normalize_image_digest,
99100
try_convert_datetime,
100101
read_config,
101-
with_units_to_int,
102102
_extract_host_info_from_applied_spec,
103103
)
104104
from cephadmlib.file_utils import (
@@ -1633,12 +1633,7 @@ def _daemon_ls_subset(self) -> Dict[str, Dict[str, Any]]:
16331633
daemons: Dict[str, Dict[str, Any]] = {}
16341634
data_dir = self.ctx.data_dir
16351635
seen_memusage = {} # type: Dict[str, int]
1636-
out, err, code = call(
1637-
self.ctx,
1638-
[self.ctx.container_engine.path, 'stats', '--format', '{{.ID}},{{.MemUsage}}', '--no-stream'],
1639-
verbosity=CallVerbosity.DEBUG
1640-
)
1641-
seen_memusage_cid_len, seen_memusage = _parse_mem_usage(code, out)
1636+
seen_memusage_cid_len, seen_memusage = parsed_container_mem_usage(self.ctx)
16421637
# we need a mapping from container names to ids. Later we will convert daemon
16431638
# names to container names to get daemons container id to see if it has changed
16441639
out, err, code = call(
@@ -3452,12 +3447,7 @@ def list_daemons(
34523447
# keep track of memory and cpu usage we've seen
34533448
seen_memusage = {} # type: Dict[str, int]
34543449
seen_cpuperc = {} # type: Dict[str, str]
3455-
out, err, code = call(
3456-
ctx,
3457-
[container_path, 'stats', '--format', '{{.ID}},{{.MemUsage}}', '--no-stream'],
3458-
verbosity=CallVerbosity.QUIET
3459-
)
3460-
seen_memusage_cid_len, seen_memusage = _parse_mem_usage(code, out)
3450+
seen_memusage_cid_len, seen_memusage = parsed_container_mem_usage(ctx)
34613451

34623452
out, err, code = call(
34633453
ctx,
@@ -3667,24 +3657,6 @@ def list_daemons(
36673657
return ls
36683658

36693659

3670-
def _parse_mem_usage(code: int, out: str) -> Tuple[int, Dict[str, int]]:
3671-
# keep track of memory usage we've seen
3672-
seen_memusage = {} # type: Dict[str, int]
3673-
seen_memusage_cid_len = 0
3674-
if not code:
3675-
for line in out.splitlines():
3676-
(cid, usage) = line.split(',')
3677-
(used, limit) = usage.split(' / ')
3678-
try:
3679-
seen_memusage[cid] = with_units_to_int(used)
3680-
if not seen_memusage_cid_len:
3681-
seen_memusage_cid_len = len(cid)
3682-
except ValueError:
3683-
logger.info('unable to parse memory usage line\n>{}'.format(line))
3684-
pass
3685-
return seen_memusage_cid_len, seen_memusage
3686-
3687-
36883660
def _parse_cpu_perc(code: int, out: str) -> Tuple[int, Dict[str, str]]:
36893661
seen_cpuperc = {}
36903662
seen_cpuperc_cid_len = 0

src/cephadm/tests/test_agent.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,12 @@
33

44
import pytest
55

6-
from tests.fixtures import with_cephadm_ctx, cephadm_fs, import_cephadm
6+
from tests.fixtures import (
7+
cephadm_fs,
8+
funkypatch,
9+
import_cephadm,
10+
with_cephadm_ctx,
11+
)
712

813
from typing import Optional
914

@@ -182,7 +187,7 @@ def _ceph_volume_empty(_):
182187
out, _ = agent._ceph_volume(False)
183188

184189

185-
def test_agent_daemon_ls_subset(cephadm_fs):
190+
def test_agent_daemon_ls_subset(cephadm_fs, funkypatch):
186191
# Basing part of this test on some actual sample output
187192

188193
# Some sample "podman stats --format '{{.ID}},{{.MemUsage}}' --no-stream" output
@@ -225,10 +230,11 @@ def _fake_call(ctx, cmd, desc=None, verbosity=_cephadm.CallVerbosity.VERBOSE_ON_
225230
cephadm_fs.create_dir(f'/var/lib/ceph/{FSID}/mgr.host1.pntmho') # cephadm daemon
226231
cephadm_fs.create_dir(f'/var/lib/ceph/{FSID}/crash.host1') # cephadm daemon
227232

228-
with with_cephadm_ctx([]) as ctx:
233+
with with_cephadm_ctx([], mock_cephadm_call_fn=False) as ctx:
229234
ctx.fsid = FSID
230235
agent = _cephadm.CephadmAgent(ctx, FSID, AGENT_ID)
231-
_cephadm.call.side_effect = _fake_call
236+
_call = funkypatch.patch('cephadmlib.call_wrappers.call')
237+
_call.side_effect = _fake_call
232238
daemons = agent._daemon_ls_subset()
233239

234240
assert 'agent.host1' in daemons

src/cephadm/tests/test_cephadm.py

Lines changed: 50 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,9 @@ def test_check_required_global_args(self):
226226

227227
@mock.patch('cephadm.logger')
228228
def test_parse_mem_usage(self, _logger):
229-
len, summary = _cephadm._parse_mem_usage(0, 'c6290e3f1489,-- / --')
229+
from cephadmlib.container_engines import _parse_mem_usage
230+
231+
len, summary = _parse_mem_usage(0, 'c6290e3f1489,-- / --')
230232
assert summary == {}
231233

232234
def test_CustomValidation(self):
@@ -1665,7 +1667,9 @@ def test_fsid(self, fsid, err, cephadm_fs, funkypatch):
16651667

16661668
class TestShell(object):
16671669

1668-
def test_fsid(self, cephadm_fs):
1670+
def test_fsid(self, cephadm_fs, funkypatch):
1671+
_call = funkypatch.patch('cephadmlib.call_wrappers.call', force=True)
1672+
_call.side_effect = lambda *args, **kwargs: ('', '', 0)
16691673
fsid = '00000000-0000-0000-0000-0000deadbeef'
16701674

16711675
cmd = ['shell', '--fsid', fsid]
@@ -1699,7 +1703,10 @@ def test_fsid(self, cephadm_fs):
16991703
assert retval == 1
17001704
assert ctx.fsid == None
17011705

1702-
def test_name(self, cephadm_fs):
1706+
def test_name(self, cephadm_fs, funkypatch):
1707+
_call = funkypatch.patch('cephadmlib.call_wrappers.call', force=True)
1708+
_call.side_effect = lambda *args, **kwargs: ('', '', 0)
1709+
17031710
cmd = ['shell', '--name', 'foo']
17041711
with with_cephadm_ctx(cmd) as ctx:
17051712
retval = _cephadm.command_shell(ctx)
@@ -1718,7 +1725,10 @@ def test_name(self, cephadm_fs):
17181725
retval = _cephadm.command_shell(ctx)
17191726
assert retval == 0
17201727

1721-
def test_config(self, cephadm_fs):
1728+
def test_config(self, cephadm_fs, funkypatch):
1729+
_call = funkypatch.patch('cephadmlib.call_wrappers.call', force=True)
1730+
_call.side_effect = lambda *args, **kwargs: ('', '', 0)
1731+
17221732
cmd = ['shell']
17231733
with with_cephadm_ctx(cmd) as ctx:
17241734
retval = _cephadm.command_shell(ctx)
@@ -1737,7 +1747,10 @@ def test_config(self, cephadm_fs):
17371747
assert retval == 0
17381748
assert ctx.config == 'foo'
17391749

1740-
def test_keyring(self, cephadm_fs):
1750+
def test_keyring(self, cephadm_fs, funkypatch):
1751+
_call = funkypatch.patch('cephadmlib.call_wrappers.call', force=True)
1752+
_call.side_effect = lambda *args, **kwargs: ('', '', 0)
1753+
17411754
cmd = ['shell']
17421755
with with_cephadm_ctx(cmd) as ctx:
17431756
retval = _cephadm.command_shell(ctx)
@@ -1756,24 +1769,33 @@ def test_keyring(self, cephadm_fs):
17561769
assert retval == 0
17571770
assert ctx.keyring == 'foo'
17581771

1759-
@mock.patch('cephadm.CephContainer')
1760-
def test_mount_no_dst(self, _ceph_container, cephadm_fs):
1772+
def test_mount_no_dst(self, cephadm_fs, funkypatch):
1773+
_ceph_container = funkypatch.patch('cephadm.CephContainer')
1774+
_call = funkypatch.patch('cephadmlib.call_wrappers.call', force=True)
1775+
_call.side_effect = lambda *args, **kwargs: ('', '', 0)
1776+
17611777
cmd = ['shell', '--mount', '/etc/foo']
17621778
with with_cephadm_ctx(cmd) as ctx:
17631779
retval = _cephadm.command_shell(ctx)
17641780
assert retval == 0
17651781
assert _ceph_container.call_args.kwargs['volume_mounts']['/etc/foo'] == '/mnt/foo'
17661782

1767-
@mock.patch('cephadm.CephContainer')
1768-
def test_mount_with_dst_no_opt(self, _ceph_container, cephadm_fs):
1783+
def test_mount_with_dst_no_opt(self, cephadm_fs, funkypatch):
1784+
_ceph_container = funkypatch.patch('cephadm.CephContainer')
1785+
_call = funkypatch.patch('cephadmlib.call_wrappers.call', force=True)
1786+
_call.side_effect = lambda *args, **kwargs: ('', '', 0)
1787+
17691788
cmd = ['shell', '--mount', '/etc/foo:/opt/foo/bar']
17701789
with with_cephadm_ctx(cmd) as ctx:
17711790
retval = _cephadm.command_shell(ctx)
17721791
assert retval == 0
17731792
assert _ceph_container.call_args.kwargs['volume_mounts']['/etc/foo'] == '/opt/foo/bar'
17741793

1775-
@mock.patch('cephadm.CephContainer')
1776-
def test_mount_with_dst_and_opt(self, _ceph_container, cephadm_fs):
1794+
def test_mount_with_dst_and_opt(self, cephadm_fs, funkypatch):
1795+
_ceph_container = funkypatch.patch('cephadm.CephContainer')
1796+
_call = funkypatch.patch('cephadmlib.call_wrappers.call', force=True)
1797+
_call.side_effect = lambda *args, **kwargs: ('', '', 0)
1798+
17771799
cmd = ['shell', '--mount', '/etc/foo:/opt/foo/bar:Z']
17781800
with with_cephadm_ctx(cmd) as ctx:
17791801
retval = _cephadm.command_shell(ctx)
@@ -1790,7 +1812,10 @@ def _get_cmd(*args):
17901812
'--', 'inventory', '--format', 'json'
17911813
]
17921814

1793-
def test_noop(self, cephadm_fs):
1815+
def test_noop(self, cephadm_fs, funkypatch):
1816+
_call = funkypatch.patch('cephadmlib.call_wrappers.call', force=True)
1817+
_call.side_effect = lambda *args, **kwargs: ('', '', 0)
1818+
17941819
cmd = self._get_cmd()
17951820
with with_cephadm_ctx(cmd) as ctx:
17961821
_cephadm.command_ceph_volume(ctx)
@@ -1799,7 +1824,10 @@ def test_noop(self, cephadm_fs):
17991824
assert ctx.keyring == None
18001825
assert ctx.config_json == None
18011826

1802-
def test_fsid(self, cephadm_fs):
1827+
def test_fsid(self, cephadm_fs, funkypatch):
1828+
_call = funkypatch.patch('cephadmlib.call_wrappers.call', force=True)
1829+
_call.side_effect = lambda *args, **kwargs: ('', '', 0)
1830+
18031831
fsid = '00000000-0000-0000-0000-0000deadbeef'
18041832

18051833
cmd = self._get_cmd('--fsid', fsid)
@@ -1830,7 +1858,10 @@ def test_fsid(self, cephadm_fs):
18301858
_cephadm.command_ceph_volume(ctx)
18311859
assert ctx.fsid == None
18321860

1833-
def test_config(self, cephadm_fs):
1861+
def test_config(self, cephadm_fs, funkypatch):
1862+
_call = funkypatch.patch('cephadmlib.call_wrappers.call', force=True)
1863+
_call.side_effect = lambda *args, **kwargs: ('', '', 0)
1864+
18341865
cmd = self._get_cmd('--config', 'foo')
18351866
with with_cephadm_ctx(cmd) as ctx:
18361867
err = r'No such file or directory'
@@ -1843,7 +1874,10 @@ def test_config(self, cephadm_fs):
18431874
_cephadm.command_ceph_volume(ctx)
18441875
assert ctx.config == 'bar'
18451876

1846-
def test_keyring(self, cephadm_fs):
1877+
def test_keyring(self, cephadm_fs, funkypatch):
1878+
_call = funkypatch.patch('cephadmlib.call_wrappers.call', force=True)
1879+
_call.side_effect = lambda *args, **kwargs: ('', '', 0)
1880+
18471881
cmd = self._get_cmd('--keyring', 'foo')
18481882
with with_cephadm_ctx(cmd) as ctx:
18491883
err = r'No such file or directory'
@@ -2260,7 +2294,7 @@ def test_error(self, funkypatch):
22602294
funkypatch.patch('cephadm.logger')
22612295
_giifi = funkypatch.patch('cephadm.get_image_info_from_inspect')
22622296
_giifi.return_value = {}
2263-
_call = funkypatch.patch('cephadmlib.call_wrappers.call')
2297+
_call = funkypatch.patch('cephadmlib.call_wrappers.call', force=True)
22642298
ctx = _cephadm.CephadmContext()
22652299
ctx.container_engine = mock_podman()
22662300
ctx.insecure = False

0 commit comments

Comments
 (0)