]>
Commit | Line | Data |
---|---|---|
9f95a23c TL |
1 | import time |
2 | import fnmatch | |
3 | try: | |
4 | from typing import Any | |
5 | except ImportError: | |
6 | pass | |
7 | import pytest | |
8 | ||
9 | from cephadm import CephadmOrchestrator | |
10 | from orchestrator import raise_if_exception, Completion | |
11 | from tests import mock | |
12 | ||
13 | ||
14 | def set_store(self, k, v): | |
15 | if v is None: | |
16 | del self._store[k] | |
17 | else: | |
18 | self._store[k] = v | |
19 | ||
20 | ||
21 | def get_store(self, k): | |
22 | return self._store.get(k, None) | |
23 | ||
24 | ||
25 | def get_store_prefix(self, prefix): | |
26 | return { | |
27 | k: v for k, v in self._store.items() | |
28 | if k.startswith(prefix) | |
29 | } | |
30 | ||
31 | ||
32 | def get_ceph_option(_, key): | |
33 | return __file__ | |
34 | ||
35 | ||
36 | def _run_cephadm(ret): | |
37 | def foo(*args, **kwargs): | |
38 | return ret, '', 0 | |
39 | return foo | |
40 | ||
41 | ||
42 | def match_glob(val, pat): | |
43 | ok = fnmatch.fnmatchcase(val, pat) | |
44 | if not ok: | |
45 | assert pat in val | |
46 | ||
47 | ||
48 | def mon_command(*args, **kwargs): | |
49 | return 0, '', '' | |
50 | ||
51 | ||
52 | @pytest.yield_fixture() | |
53 | def cephadm_module(): | |
54 | with mock.patch("cephadm.module.CephadmOrchestrator.get_ceph_option", get_ceph_option),\ | |
55 | mock.patch("cephadm.module.CephadmOrchestrator._configure_logging", lambda *args: None),\ | |
56 | mock.patch("cephadm.module.CephadmOrchestrator.remote"),\ | |
57 | mock.patch("cephadm.module.CephadmOrchestrator.set_store", set_store), \ | |
58 | mock.patch("cephadm.module.CephadmOrchestrator.get_store", get_store),\ | |
59 | mock.patch("cephadm.module.CephadmOrchestrator._run_cephadm", _run_cephadm('[]')), \ | |
60 | mock.patch("cephadm.module.HostCache.save_host"), \ | |
61 | mock.patch("cephadm.module.HostCache.rm_host"), \ | |
62 | mock.patch("cephadm.module.CephadmOrchestrator.send_command"), \ | |
63 | mock.patch("cephadm.module.CephadmOrchestrator.mon_command", mon_command), \ | |
64 | mock.patch("cephadm.module.CephadmOrchestrator.get_store_prefix", get_store_prefix): | |
65 | ||
66 | CephadmOrchestrator._register_commands('') | |
67 | CephadmOrchestrator._register_options('') | |
68 | m = CephadmOrchestrator.__new__ (CephadmOrchestrator) | |
69 | m._root_logger = mock.MagicMock() | |
70 | m._store = { | |
71 | 'ssh_config': '', | |
72 | 'ssh_identity_key': '', | |
73 | 'ssh_identity_pub': '', | |
74 | 'inventory': {}, | |
75 | 'upgrade_state': None, | |
76 | } | |
77 | m.__init__('cephadm', 0, 0) | |
78 | m._cluster_fsid = "fsid" | |
79 | yield m | |
80 | ||
81 | ||
82 | def wait(m, c): | |
83 | # type: (CephadmOrchestrator, Completion) -> Any | |
84 | m.process([c]) | |
85 | ||
86 | try: | |
87 | import pydevd # if in debugger | |
88 | in_debug = True | |
89 | except ImportError: | |
90 | in_debug = False | |
91 | ||
92 | if in_debug: | |
93 | while True: # don't timeout | |
94 | if c.is_finished: | |
95 | raise_if_exception(c) | |
96 | return c.result | |
97 | time.sleep(0.1) | |
98 | else: | |
99 | for i in range(30): | |
100 | if i % 10 == 0: | |
101 | m.process([c]) | |
102 | if c.is_finished: | |
103 | raise_if_exception(c) | |
104 | return c.result | |
105 | time.sleep(0.1) | |
106 | assert False, "timeout" + str(c._state) |