]> git.proxmox.com Git - ceph.git/blame - ceph/src/cephadm/tests/fixtures.py
import ceph pacific 16.2.5
[ceph.git] / ceph / src / cephadm / tests / fixtures.py
CommitLineData
f67539c2 1import mock
f67539c2 2import os
b3b6e05e 3import pytest
f67539c2
TL
4import time
5
b3b6e05e
TL
6from contextlib import contextmanager
7from pyfakefs import fake_filesystem
8
9from typing import Callable, Dict, List, Optional
10
11
12with mock.patch('builtins.open', create=True):
f67539c2
TL
13 from importlib.machinery import SourceFileLoader
14 cd = SourceFileLoader('cephadm', 'cephadm').load_module()
15
16
b3b6e05e
TL
17def mock_docker():
18 docker = mock.Mock(cd.Docker)
19 docker.path = '/usr/bin/docker'
20 return docker
21
22
23def mock_podman():
24 podman = mock.Mock(cd.Podman)
25 podman.path = '/usr/bin/podman'
26 podman.version = (2, 1, 0)
27 return podman
28
29
f67539c2
TL
30def _daemon_path():
31 return os.getcwd()
32
33
34def _mock_scrape_host(obj, interval):
35 try:
36 raise ValueError("wah")
37 except Exception as e:
38 obj._handle_thread_exception(e, 'host')
39
40
41def _mock_run(obj):
42 t = obj._create_thread(obj._scrape_host_facts, 'host', 5)
43 time.sleep(1)
44 if not t.is_alive():
45 obj.cephadm_cache.update_health('host', "inactive", "host thread stopped")
46
47
48@pytest.fixture
49def exporter():
50 with mock.patch('cephadm.CephadmDaemon.daemon_path', _daemon_path()), \
51 mock.patch('cephadm.CephadmDaemon.can_run', return_value=True), \
52 mock.patch('cephadm.CephadmDaemon.run', _mock_run), \
53 mock.patch('cephadm.CephadmDaemon._scrape_host_facts', _mock_scrape_host):
54
55 ctx = cd.CephadmContext()
56 exporter = cd.CephadmDaemon(ctx, fsid='foobar', daemon_id='test')
57 assert exporter.token == 'MyAccessToken'
58 yield exporter
b3b6e05e
TL
59
60
61@pytest.fixture()
62def cephadm_fs(
63 fs: fake_filesystem.FakeFilesystem,
64):
65 """
66 use pyfakefs to stub filesystem calls
67 """
68 uid = os.getuid()
69 gid = os.getgid()
70
71 with mock.patch('os.fchown'), \
72 mock.patch('cephadm.extract_uid_gid', return_value=(uid, gid)):
73
74 fs.create_dir(cd.DATA_DIR)
75 fs.create_dir(cd.LOG_DIR)
76 fs.create_dir(cd.LOCK_DIR)
77 fs.create_dir(cd.LOGROTATE_DIR)
78 fs.create_dir(cd.UNIT_DIR)
79
80 yield fs
81
82
83@contextmanager
84def with_cephadm_ctx(
85 cmd: List[str],
86 container_engine: Callable = mock_podman(),
87 list_networks: Optional[Dict[str,Dict[str,List[str]]]] = None,
88 hostname: Optional[str] = None,
89):
90 """
91 :param cmd: cephadm command argv
92 :param container_engine: container engine mock (podman or docker)
93 :param list_networks: mock 'list-networks' return
94 :param hostname: mock 'socket.gethostname' return
95 """
96 if not list_networks:
97 list_networks = {}
98 if not hostname:
99 hostname = 'host1'
100
101 with mock.patch('cephadm.get_parm'), \
102 mock.patch('cephadm.attempt_bind'), \
103 mock.patch('cephadm.call', return_value=('', '', 0)), \
104 mock.patch('cephadm.find_executable', return_value='foo'), \
105 mock.patch('cephadm.is_available', return_value=True), \
106 mock.patch('cephadm.json_loads_retry', return_value={'epoch' : 1}), \
107 mock.patch('cephadm.list_networks', return_value=list_networks), \
108 mock.patch('socket.gethostname', return_value=hostname):
109 ctx: cd.CephadmContext = cd.cephadm_init_ctx(cmd)
110 ctx.container_engine = container_engine
111 yield ctx