Skip to content

Commit 32fe8aa

Browse files
cephadm: replace get_container_stats in cephadm.py
Replace the existing get_container_stats with a version from container_types.py that returns the parsed results of the container status command (as a ContainerInfo, None on error). Fix up a bunch of tests. Signed-off-by: John Mulligan <[email protected]>
1 parent 3154cd2 commit 32fe8aa

File tree

2 files changed

+62
-65
lines changed

2 files changed

+62
-65
lines changed

src/cephadm/cephadm.py

Lines changed: 18 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -147,8 +147,9 @@
147147
InitContainer,
148148
SidecarContainer,
149149
extract_uid_gid,
150-
is_container_running,
150+
get_container_stats,
151151
get_mgr_images,
152+
is_container_running,
152153
)
153154
from cephadmlib.decorators import (
154155
deprecated_command,
@@ -511,10 +512,11 @@ def daemon_name_or_type(daemon: Dict[str, str]) -> str:
511512
version=version)
512513
else:
513514
d_type, d_id = matching_daemons[0]['name'].split('.', 1)
514-
out, _, code = get_container_stats(ctx, ctx.container_engine.path, ctx.fsid, d_type, d_id)
515-
if not code:
516-
(container_id, image_name, image_id, start, version) = out.strip().split(',')
517-
return ContainerInfo(container_id, image_name, image_id, start, version)
515+
cinfo = get_container_stats(
516+
ctx, DaemonIdentity(ctx.fsid, d_type, d_id)
517+
)
518+
if cinfo:
519+
return cinfo
518520
return None
519521

520522

@@ -3490,10 +3492,17 @@ def list_daemons(
34903492
version = None
34913493
start_stamp = None
34923494

3493-
out, err, code = get_container_stats(ctx, container_path, fsid, daemon_type, daemon_id)
3494-
if not code:
3495-
(container_id, image_name, image_id, start,
3496-
version) = out.strip().split(',')
3495+
cinfo = get_container_stats(
3496+
ctx,
3497+
DaemonIdentity(fsid, daemon_type, daemon_id),
3498+
container_path=container_path
3499+
)
3500+
if cinfo:
3501+
container_id = cinfo.container_id
3502+
image_name = cinfo.image_name
3503+
image_id = cinfo.image_id
3504+
start = cinfo.start
3505+
version = cinfo.version
34973506
image_id = normalize_container_id(image_id)
34983507
daemon_type = name.split('.', 1)[0]
34993508
start_stamp = try_convert_datetime(start)
@@ -3638,24 +3647,6 @@ def get_daemon_description(ctx, fsid, name, detail=False, legacy_dir=None):
36383647
raise Error('Daemon not found: {}. See `cephadm ls`'.format(name))
36393648

36403649

3641-
def get_container_stats(ctx: CephadmContext, container_path: str, fsid: str, daemon_type: str, daemon_id: str) -> Tuple[str, str, int]:
3642-
"""returns container id, image name, image id, created time, and ceph version if available"""
3643-
c = CephContainer.for_daemon(
3644-
ctx, DaemonIdentity(fsid, daemon_type, daemon_id), 'bash'
3645-
)
3646-
out, err, code = '', '', -1
3647-
for name in (c.cname, c.old_cname):
3648-
cmd = [
3649-
container_path, 'inspect',
3650-
'--format', '{{.Id}},{{.Config.Image}},{{.Image}},{{.Created}},{{index .Config.Labels "io.ceph.version"}}',
3651-
name
3652-
]
3653-
out, err, code = call(ctx, cmd, verbosity=CallVerbosity.QUIET)
3654-
if not code:
3655-
break
3656-
return out, err, code
3657-
3658-
36593650
def get_container_stats_by_image_name(ctx: CephadmContext, container_path: str, image_name: str) -> Tuple[str, str, int]:
36603651
"""returns image id, created time, and ceph version if available"""
36613652
out, err, code = '', '', -1

src/cephadm/tests/test_cephadm.py

Lines changed: 44 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -650,9 +650,8 @@ def test_infer_local_ceph_image(self, _logger, _listdir):
650650
{'name': 'mon.ceph-node-0', 'fsid': '00000000-0000-0000-0000-0000deadbeef'},
651651
{'name': 'mgr.ceph-node-0', 'fsid': '00000000-0000-0000-0000-0000deadbeef'},
652652
],
653-
("935b549714b8f007c6a4e29c758689cf9e8e69f2e0f51180506492974b90a972,registry.hub.docker.com/rkachach/ceph:custom-v0.5,666bbfa87e8df05702d6172cae11dd7bc48efb1d94f1b9e492952f19647199a4,2022-04-19 13:45:20.97146228 +0000 UTC,",
654-
"",
655-
0),
653+
("935b549714b8f007c6a4e29c758689cf9e8e69f2e0f51180506492974b90a972", "registry.hub.docker.com/rkachach/ceph:custom-v0.5", "666bbfa87e8df05702d6172cae11dd7bc48efb1d94f1b9e492952f19647199a4", "2022-04-19 13:45:20.97146228 +0000 UTC", ""
654+
),
656655
_cephadm.ContainerInfo('935b549714b8f007c6a4e29c758689cf9e8e69f2e0f51180506492974b90a972',
657656
'registry.hub.docker.com/rkachach/ceph:custom-v0.5',
658657
'666bbfa87e8df05702d6172cae11dd7bc48efb1d94f1b9e492952f19647199a4',
@@ -667,9 +666,8 @@ def test_infer_local_ceph_image(self, _logger, _listdir):
667666
{'name': 'mgr.ceph-node-0', 'fsid': '00000000-0000-0000-0000-0000deadbeef'},
668667
{'name': 'mon.ceph-node-0', 'fsid': '00000000-0000-0000-0000-0000deadbeef'},
669668
],
670-
("935b549714b8f007c6a4e29c758689cf9e8e69f2e0f51180506492974b90a972,registry.hub.docker.com/rkachach/ceph:custom-v0.5,666bbfa87e8df05702d6172cae11dd7bc48efb1d94f1b9e492952f19647199a4,2022-04-19 13:45:20.97146228 +0000 UTC,",
671-
"",
672-
0),
669+
("935b549714b8f007c6a4e29c758689cf9e8e69f2e0f51180506492974b90a972", "registry.hub.docker.com/rkachach/ceph:custom-v0.5", "666bbfa87e8df05702d6172cae11dd7bc48efb1d94f1b9e492952f19647199a4", "2022-04-19 13:45:20.97146228 +0000 UTC", ""
670+
),
673671
_cephadm.ContainerInfo('935b549714b8f007c6a4e29c758689cf9e8e69f2e0f51180506492974b90a972',
674672
'registry.hub.docker.com/rkachach/ceph:custom-v0.5',
675673
'666bbfa87e8df05702d6172cae11dd7bc48efb1d94f1b9e492952f19647199a4',
@@ -684,9 +682,8 @@ def test_infer_local_ceph_image(self, _logger, _listdir):
684682
{'name': 'mon.ceph-node-0', 'fsid': '10000000-0000-0000-0000-0000deadbeef'},
685683
{'name': 'mon.ceph-node-0', 'fsid': '00000000-0000-0000-0000-0000deadbeef'},
686684
],
687-
("935b549714b8f007c6a4e29c758689cf9e8e69f2e0f51180506492974b90a972,registry.hub.docker.com/rkachach/ceph:custom-v0.5,666bbfa87e8df05702d6172cae11dd7bc48efb1d94f1b9e492952f19647199a4,2022-04-19 13:45:20.97146228 +0000 UTC,",
688-
"",
689-
0),
685+
("935b549714b8f007c6a4e29c758689cf9e8e69f2e0f51180506492974b90a972", "registry.hub.docker.com/rkachach/ceph:custom-v0.5", "666bbfa87e8df05702d6172cae11dd7bc48efb1d94f1b9e492952f19647199a4", "2022-04-19 13:45:20.97146228 +0000 UTC", ""
686+
),
690687
_cephadm.ContainerInfo('935b549714b8f007c6a4e29c758689cf9e8e69f2e0f51180506492974b90a972',
691688
'registry.hub.docker.com/rkachach/ceph:custom-v0.5',
692689
'666bbfa87e8df05702d6172cae11dd7bc48efb1d94f1b9e492952f19647199a4',
@@ -701,9 +698,7 @@ def test_infer_local_ceph_image(self, _logger, _listdir):
701698
{'name': 'mon.ceph-node-0', 'fsid': '00000000-FFFF-0000-0000-0000deadbeef'},
702699
{'name': 'mon.ceph-node-0', 'fsid': '00000000-0000-0000-0000-0000deadbeef'},
703700
],
704-
("",
705-
"",
706-
127),
701+
None,
707702
None
708703
),
709704
# get container info by name (bad container stats: 127 code)
@@ -714,9 +709,7 @@ def test_infer_local_ceph_image(self, _logger, _listdir):
714709
{'name': 'mgr.ceph-node-0', 'fsid': '00000000-0000-0000-0000-0000deadbeef'},
715710
{'name': 'mon.ceph-node-0', 'fsid': '00000000-0000-0000-0000-0000deadbeef'},
716711
],
717-
("",
718-
"",
719-
127),
712+
None,
720713
None
721714
),
722715
# get container info by invalid name (doens't contain '.')
@@ -727,9 +720,8 @@ def test_infer_local_ceph_image(self, _logger, _listdir):
727720
{'name': 'mon.ceph-node-0', 'fsid': '00000000-0000-0000-0000-0000deadbeef'},
728721
{'name': 'mon.ceph-node-0', 'fsid': '00000000-0000-0000-0000-0000deadbeef'},
729722
],
730-
("935b549714b8f007c6a4e29c758689cf9e8e69f2e0f51180506492974b90a972,registry.hub.docker.com/rkachach/ceph:custom-v0.5,666bbfa87e8df05702d6172cae11dd7bc48efb1d94f1b9e492952f19647199a4,2022-04-19 13:45:20.97146228 +0000 UTC,",
731-
"",
732-
0),
723+
("935b549714b8f007c6a4e29c758689cf9e8e69f2e0f51180506492974b90a972", "registry.hub.docker.com/rkachach/ceph:custom-v0.5", "666bbfa87e8df05702d6172cae11dd7bc48efb1d94f1b9e492952f19647199a4", "2022-04-19 13:45:20.97146228 +0000 UTC", ""
724+
),
733725
None
734726
),
735727
# get container info by invalid name (empty)
@@ -740,9 +732,8 @@ def test_infer_local_ceph_image(self, _logger, _listdir):
740732
{'name': 'mon.ceph-node-0', 'fsid': '00000000-0000-0000-0000-0000deadbeef'},
741733
{'name': 'mon.ceph-node-0', 'fsid': '00000000-0000-0000-0000-0000deadbeef'},
742734
],
743-
("935b549714b8f007c6a4e29c758689cf9e8e69f2e0f51180506492974b90a972,registry.hub.docker.com/rkachach/ceph:custom-v0.5,666bbfa87e8df05702d6172cae11dd7bc48efb1d94f1b9e492952f19647199a4,2022-04-19 13:45:20.97146228 +0000 UTC,",
744-
"",
745-
0),
735+
("935b549714b8f007c6a4e29c758689cf9e8e69f2e0f51180506492974b90a972", "registry.hub.docker.com/rkachach/ceph:custom-v0.5", "666bbfa87e8df05702d6172cae11dd7bc48efb1d94f1b9e492952f19647199a4", "2022-04-19 13:45:20.97146228 +0000 UTC", ""
736+
),
746737
None
747738
),
748739
# get container info by invalid type (empty)
@@ -753,9 +744,8 @@ def test_infer_local_ceph_image(self, _logger, _listdir):
753744
{'name': 'mon.ceph-node-0', 'fsid': '00000000-0000-0000-0000-0000deadbeef'},
754745
{'name': 'mon.ceph-node-0', 'fsid': '00000000-0000-0000-0000-0000deadbeef'},
755746
],
756-
("935b549714b8f007c6a4e29c758689cf9e8e69f2e0f51180506492974b90a972,registry.hub.docker.com/rkachach/ceph:custom-v0.5,666bbfa87e8df05702d6172cae11dd7bc48efb1d94f1b9e492952f19647199a4,2022-04-19 13:45:20.97146228 +0000 UTC,",
757-
"",
758-
0),
747+
("935b549714b8f007c6a4e29c758689cf9e8e69f2e0f51180506492974b90a972", "registry.hub.docker.com/rkachach/ceph:custom-v0.5", "666bbfa87e8df05702d6172cae11dd7bc48efb1d94f1b9e492952f19647199a4", "2022-04-19 13:45:20.97146228 +0000 UTC", ""
748+
),
759749
None
760750
),
761751
# get container info by name: no match (invalid fsid)
@@ -766,9 +756,8 @@ def test_infer_local_ceph_image(self, _logger, _listdir):
766756
{'name': 'mon.ceph-node-0', 'fsid': '00000000-1111-0000-0000-0000deadbeef'},
767757
{'name': 'mon.ceph-node-0', 'fsid': '00000000-2222-0000-0000-0000deadbeef'},
768758
],
769-
("935b549714b8f007c6a4e29c758689cf9e8e69f2e0f51180506492974b90a972,registry.hub.docker.com/rkachach/ceph:custom-v0.5,666bbfa87e8df05702d6172cae11dd7bc48efb1d94f1b9e492952f19647199a4,2022-04-19 13:45:20.97146228 +0000 UTC,",
770-
"",
771-
0),
759+
("935b549714b8f007c6a4e29c758689cf9e8e69f2e0f51180506492974b90a972", "registry.hub.docker.com/rkachach/ceph:custom-v0.5", "666bbfa87e8df05702d6172cae11dd7bc48efb1d94f1b9e492952f19647199a4", "2022-04-19 13:45:20.97146228 +0000 UTC", ""
760+
),
772761
None
773762
),
774763
# get container info by name: no match
@@ -788,19 +777,36 @@ def test_infer_local_ceph_image(self, _logger, _listdir):
788777
None
789778
),
790779
])
791-
@mock.patch('cephadm.logger')
792-
def test_get_container_info(self, _logger, daemon_filter, by_name, daemon_list, container_stats, output):
780+
def test_get_container_info(
781+
self,
782+
daemon_filter,
783+
by_name,
784+
daemon_list,
785+
container_stats,
786+
output,
787+
funkypatch,
788+
):
793789
ctx = _cephadm.CephadmContext()
794790
ctx.fsid = '00000000-0000-0000-0000-0000deadbeef'
795791
ctx.container_engine = mock_podman()
796-
with mock.patch('cephadm.list_daemons', return_value=daemon_list):
797-
with mock.patch('cephadm.get_container_stats', return_value=container_stats):
798-
assert _cephadm.get_container_info(ctx, daemon_filter, by_name) == output
799-
800-
@mock.patch('cephadm.list_daemons')
801-
@mock.patch('cephadm.get_container_stats')
802-
@mock.patch('cephadm.get_container_stats_by_image_name')
803-
def test_get_container_info_daemon_down(self, _get_stats_by_name, _get_stats, _list_daemons):
792+
funkypatch.patch('cephadm.list_daemons').return_value = daemon_list
793+
cinfo = (
794+
_cephadm.ContainerInfo(*container_stats)
795+
if container_stats
796+
else None
797+
)
798+
funkypatch.patch(
799+
'cephadmlib.container_types.get_container_stats'
800+
).return_value = cinfo
801+
assert (
802+
_cephadm.get_container_info(ctx, daemon_filter, by_name) == output
803+
)
804+
805+
def test_get_container_info_daemon_down(self, funkypatch):
806+
_get_stats_by_name = funkypatch.patch('cephadm.get_container_stats_by_image_name')
807+
_get_stats = funkypatch.patch('cephadmlib.container_types.get_container_stats')
808+
_list_daemons = funkypatch.patch('cephadm.list_daemons')
809+
804810
ctx = _cephadm.CephadmContext()
805811
ctx.fsid = '5e39c134-dfc5-11ee-a344-5254000ee071'
806812
ctx.container_engine = mock_podman()
@@ -859,7 +865,7 @@ def test_get_container_info_daemon_down(self, _get_stats_by_name, _get_stats, _l
859865
# than it partially being taken from the list_daemons output
860866
up_osd_json = copy.deepcopy(down_osd_json)
861867
up_osd_json['state'] = 'running'
862-
_get_stats.return_value = (('container_id,image_name,image_id,the_past,'), '', 0)
868+
_get_stats.return_value = _cephadm.ContainerInfo('container_id', 'image_name','image_id','the_past','')
863869
_list_daemons.return_value = [down_osd_json, up_osd_json]
864870

865871
expected_container_info = _cephadm.ContainerInfo(

0 commit comments

Comments
 (0)