Merge pull request #42289 from mgfritch/cephadm-test-create-daemon-dirs-prom

cephadm: use pyfakefs during test_create_daemon_dirs_prometheus

Reviewed-by: Patrick Seidensal <pseidensal@suse.com>
This commit is contained in:
Sebastian Wagner 2021-07-20 17:35:09 +02:00 committed by GitHub
commit 6be59759e3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 21 deletions

View File

@ -2200,7 +2200,9 @@ def create_daemon_dirs(ctx, fsid, daemon_type, daemon_id, uid, gid,
f.write(keyring)
if daemon_type in Monitoring.components.keys():
config_json: Dict[str, Any] = get_parm(ctx.config_json)
config_json: Dict[str, Any] = dict()
if 'config_json' in ctx:
config_json = get_parm(ctx.config_json)
# Set up directories specific to the monitoring component
config_dir = ''

View File

@ -1,6 +1,7 @@
# type: ignore
import errno
import json
import mock
import os
import pytest
@ -1168,14 +1169,7 @@ class TestMonitoring(object):
version = cd.Monitoring.get_version(ctx, 'container_id', daemon_type)
assert version == '0.16.1'
@mock.patch('cephadm.os.fchown')
@mock.patch('cephadm.get_parm')
@mock.patch('cephadm.makedirs')
@mock.patch('cephadm.open')
@mock.patch('cephadm.make_log_dir')
@mock.patch('cephadm.make_data_dir')
def test_create_daemon_dirs_prometheus(self, make_data_dir, make_log_dir, _open, makedirs,
get_parm, fchown):
def test_create_daemon_dirs_prometheus(self, cephadm_fs):
"""
Ensures the required and optional files given in the configuration are
created and mapped correctly inside the container. Tests absolute and
@ -1186,15 +1180,14 @@ class TestMonitoring(object):
daemon_type = 'prometheus'
uid, gid = 50, 50
daemon_id = 'home'
ctx = mock.Mock()
ctx = cd.CephadmContext()
ctx.data_dir = '/somedir'
files = {
ctx.config_json = json.dumps({
'files': {
'prometheus.yml': 'foo',
'/etc/prometheus/alerting/ceph_alerts.yml': 'bar'
}
}
get_parm.return_value = files
})
cd.create_daemon_dirs(ctx,
fsid,
@ -1211,14 +1204,17 @@ class TestMonitoring(object):
daemon_type=daemon_type,
daemon_id=daemon_id
)
assert _open.call_args_list == [
mock.call('{}/etc/prometheus/prometheus.yml'.format(prefix), 'w',
encoding='utf-8'),
mock.call('{}/etc/prometheus/alerting/ceph_alerts.yml'.format(prefix), 'w',
encoding='utf-8'),
]
assert mock.call().__enter__().write('foo') in _open.mock_calls
assert mock.call().__enter__().write('bar') in _open.mock_calls
expected = {
'etc/prometheus/prometheus.yml': 'foo',
'etc/prometheus/alerting/ceph_alerts.yml': 'bar',
}
for file,content in expected.items():
file = os.path.join(prefix, file)
assert os.path.exists(file)
with open(file) as f:
assert f.read() == content
class TestBootstrap(object):