]>
Commit | Line | Data |
---|---|---|
9f95a23c | 1 | import fnmatch |
f6b5b4d7 TL |
2 | from contextlib import contextmanager |
3 | ||
4 | from ceph.deployment.service_spec import PlacementSpec, ServiceSpec | |
adb31ebb | 5 | from ceph.utils import datetime_to_str, datetime_now |
f91f0fd5 | 6 | from cephadm.serve import CephadmServe |
f6b5b4d7 | 7 | |
9f95a23c | 8 | try: |
f91f0fd5 | 9 | from typing import Any, Iterator, List |
9f95a23c TL |
10 | except ImportError: |
11 | pass | |
9f95a23c TL |
12 | |
13 | from cephadm import CephadmOrchestrator | |
f67539c2 | 14 | from orchestrator import raise_if_exception, OrchResult, HostSpec |
9f95a23c TL |
15 | from tests import mock |
16 | ||
17 | ||
9f95a23c TL |
18 | def get_ceph_option(_, key): |
19 | return __file__ | |
20 | ||
21 | ||
22 | def _run_cephadm(ret): | |
23 | def foo(*args, **kwargs): | |
f91f0fd5 | 24 | return [ret], '', 0 |
9f95a23c TL |
25 | return foo |
26 | ||
27 | ||
28 | def match_glob(val, pat): | |
29 | ok = fnmatch.fnmatchcase(val, pat) | |
30 | if not ok: | |
31 | assert pat in val | |
32 | ||
33 | ||
f6b5b4d7 TL |
34 | @contextmanager |
35 | def with_cephadm_module(module_options=None, store=None): | |
36 | """ | |
37 | :param module_options: Set opts as if they were set before module.__init__ is called | |
38 | :param store: Set the store before module.__init__ is called | |
39 | """ | |
9f95a23c | 40 | with mock.patch("cephadm.module.CephadmOrchestrator.get_ceph_option", get_ceph_option),\ |
f6b5b4d7 TL |
41 | mock.patch("cephadm.services.osd.RemoveUtil._run_mon_cmd"), \ |
42 | mock.patch("cephadm.module.CephadmOrchestrator.get_osdmap"), \ | |
43 | mock.patch("cephadm.services.osd.OSDService.get_osdspec_affinity", return_value='test_spec'), \ | |
44 | mock.patch("cephadm.module.CephadmOrchestrator.remote"): | |
9f95a23c | 45 | |
f91f0fd5 | 46 | m = CephadmOrchestrator.__new__(CephadmOrchestrator) |
f6b5b4d7 TL |
47 | if module_options is not None: |
48 | for k, v in module_options.items(): | |
49 | m._ceph_set_module_option('cephadm', k, v) | |
50 | if store is None: | |
51 | store = {} | |
52 | if '_ceph_get/mon_map' not in store: | |
53 | m.mock_store_set('_ceph_get', 'mon_map', { | |
adb31ebb | 54 | 'modified': datetime_to_str(datetime_now()), |
f6b5b4d7 TL |
55 | 'fsid': 'foobar', |
56 | }) | |
57 | for k, v in store.items(): | |
58 | m._ceph_set_store(k, v) | |
59 | ||
9f95a23c TL |
60 | m.__init__('cephadm', 0, 0) |
61 | m._cluster_fsid = "fsid" | |
62 | yield m | |
63 | ||
64 | ||
65 | def wait(m, c): | |
f67539c2 TL |
66 | # type: (CephadmOrchestrator, OrchResult) -> Any |
67 | return raise_if_exception(c) | |
f6b5b4d7 TL |
68 | |
69 | ||
70 | @contextmanager | |
f91f0fd5 | 71 | def with_host(m: CephadmOrchestrator, name, refresh_hosts=True): |
f6b5b4d7 TL |
72 | # type: (CephadmOrchestrator, str) -> None |
73 | wait(m, m.add_host(HostSpec(hostname=name))) | |
f91f0fd5 TL |
74 | if refresh_hosts: |
75 | CephadmServe(m)._refresh_hosts_and_daemons() | |
f6b5b4d7 TL |
76 | yield |
77 | wait(m, m.remove_host(name)) | |
78 | ||
79 | ||
f67539c2 TL |
80 | def assert_rm_service(cephadm: CephadmOrchestrator, srv_name): |
81 | mon_or_mgr = cephadm.spec_store[srv_name].spec.service_type in ('mon', 'mgr') | |
82 | if mon_or_mgr: | |
83 | assert 'Unable' in wait(cephadm, cephadm.remove_service(srv_name)) | |
84 | return | |
f6b5b4d7 | 85 | assert wait(cephadm, cephadm.remove_service(srv_name)) == f'Removed service {srv_name}' |
f67539c2 TL |
86 | assert cephadm.spec_store[srv_name].deleted is not None |
87 | CephadmServe(cephadm)._check_daemons() | |
f91f0fd5 | 88 | CephadmServe(cephadm)._apply_all_services() |
f67539c2 TL |
89 | assert cephadm.spec_store[srv_name].deleted |
90 | unmanaged = cephadm.spec_store[srv_name].spec.unmanaged | |
91 | CephadmServe(cephadm)._purge_deleted_services() | |
92 | if not unmanaged: # cause then we're not deleting daemons | |
93 | assert srv_name not in cephadm.spec_store, f'{cephadm.spec_store[srv_name]!r}' | |
f6b5b4d7 TL |
94 | |
95 | ||
96 | @contextmanager | |
f67539c2 TL |
97 | def with_service(cephadm_module: CephadmOrchestrator, spec: ServiceSpec, meth=None, host: str = '') -> Iterator[List[str]]: |
98 | if spec.placement.is_empty() and host: | |
f6b5b4d7 | 99 | spec.placement = PlacementSpec(hosts=[host], count=1) |
f67539c2 TL |
100 | if meth is not None: |
101 | c = meth(cephadm_module, spec) | |
102 | assert wait(cephadm_module, c) == f'Scheduled {spec.service_name()} update...' | |
103 | else: | |
104 | c = cephadm_module.apply([spec]) | |
105 | assert wait(cephadm_module, c) == [f'Scheduled {spec.service_name()} update...'] | |
106 | ||
f6b5b4d7 TL |
107 | specs = [d.spec for d in wait(cephadm_module, cephadm_module.describe_service())] |
108 | assert spec in specs | |
109 | ||
f91f0fd5 | 110 | CephadmServe(cephadm_module)._apply_all_services() |
f6b5b4d7 TL |
111 | |
112 | dds = wait(cephadm_module, cephadm_module.list_daemons()) | |
f91f0fd5 | 113 | own_dds = [dd for dd in dds if dd.service_name() == spec.service_name()] |
f67539c2 TL |
114 | if host: |
115 | assert own_dds | |
f6b5b4d7 | 116 | |
f91f0fd5 | 117 | yield [dd.name() for dd in own_dds] |
f6b5b4d7 | 118 | |
f91f0fd5 | 119 | assert_rm_service(cephadm_module, spec.service_name()) |
f67539c2 TL |
120 | |
121 | ||
122 | def _deploy_cephadm_binary(host): | |
123 | def foo(*args, **kwargs): | |
124 | return True | |
125 | return foo |