2020-11-16 23:11:20 +08:00
|
|
|
from mock.mock import patch
|
2023-03-08 00:06:48 +08:00
|
|
|
from ansible.module_utils.basic import AnsibleModule
|
2020-11-16 23:11:20 +08:00
|
|
|
import os
|
|
|
|
import pytest
|
|
|
|
import ca_test_common
|
|
|
|
import ceph_mgr_module
|
|
|
|
|
|
|
|
fake_cluster = 'ceph'
|
|
|
|
fake_container_binary = 'podman'
|
2022-12-06 20:14:07 +08:00
|
|
|
fake_container_image = 'quay.io/ceph/daemon:latest'
|
2020-11-16 23:11:20 +08:00
|
|
|
fake_module = 'noup'
|
|
|
|
fake_user = 'client.admin'
|
|
|
|
fake_keyring = '/etc/ceph/{}.{}.keyring'.format(fake_cluster, fake_user)
|
2023-03-08 00:06:48 +08:00
|
|
|
fake_mgr_module_ls_output = {"enabled_modules": ["iostat", "nfs", "restful"],
|
|
|
|
"disabled_modules": [{"name": "fake"}],
|
|
|
|
"always_on_modules": ["foo", "bar"]}
|
2020-11-16 23:11:20 +08:00
|
|
|
|
|
|
|
|
|
|
|
class TestCephMgrModuleModule(object):
|
|
|
|
|
|
|
|
@patch('ansible.module_utils.basic.AnsibleModule.fail_json')
|
|
|
|
def test_without_parameters(self, m_fail_json):
|
|
|
|
ca_test_common.set_module_args({})
|
|
|
|
m_fail_json.side_effect = ca_test_common.fail_json
|
|
|
|
|
|
|
|
with pytest.raises(ca_test_common.AnsibleFailJson) as result:
|
|
|
|
ceph_mgr_module.main()
|
|
|
|
|
|
|
|
result = result.value.args[0]
|
|
|
|
assert result['msg'] == 'missing required arguments: name'
|
|
|
|
|
|
|
|
@patch('ansible.module_utils.basic.AnsibleModule.exit_json')
|
|
|
|
def test_with_check_mode(self, m_exit_json):
|
|
|
|
ca_test_common.set_module_args({
|
|
|
|
'name': fake_module,
|
|
|
|
'_ansible_check_mode': True
|
|
|
|
})
|
|
|
|
m_exit_json.side_effect = ca_test_common.exit_json
|
|
|
|
|
|
|
|
with pytest.raises(ca_test_common.AnsibleExitJson) as result:
|
|
|
|
ceph_mgr_module.main()
|
|
|
|
|
|
|
|
result = result.value.args[0]
|
|
|
|
assert not result['changed']
|
2023-03-08 00:06:48 +08:00
|
|
|
assert result['cmd'] == ['ceph',
|
|
|
|
'-n',
|
|
|
|
fake_user,
|
|
|
|
'-k',
|
|
|
|
fake_keyring,
|
|
|
|
'--cluster',
|
|
|
|
fake_cluster,
|
|
|
|
'mgr', 'module',
|
|
|
|
'enable', fake_module]
|
2020-11-16 23:11:20 +08:00
|
|
|
assert result['rc'] == 0
|
|
|
|
assert not result['stdout']
|
|
|
|
assert not result['stderr']
|
|
|
|
|
2023-03-08 00:06:48 +08:00
|
|
|
@patch('ceph_mgr_module.get_mgr_initial_modules',
|
|
|
|
return_value=['restful', 'iostat', 'nfs'])
|
|
|
|
@patch('ceph_mgr_module.mgr_module_ls',
|
|
|
|
return_value=fake_mgr_module_ls_output)
|
2020-11-16 23:11:20 +08:00
|
|
|
@patch('ansible.module_utils.basic.AnsibleModule.exit_json')
|
|
|
|
@patch('ansible.module_utils.basic.AnsibleModule.run_command')
|
2023-03-08 00:06:48 +08:00
|
|
|
def test_with_failure(self,
|
|
|
|
m_run_command,
|
|
|
|
m_exit_json,
|
|
|
|
m_mgr_module_ls,
|
|
|
|
m_get_mgr_initial_modules):
|
2020-11-16 23:11:20 +08:00
|
|
|
ca_test_common.set_module_args({
|
|
|
|
'name': fake_module
|
|
|
|
})
|
|
|
|
m_exit_json.side_effect = ca_test_common.exit_json
|
|
|
|
stdout = ''
|
|
|
|
stderr = 'Error ENOENT: all mgr daemons do not support module \'{}\', pass --force to force enablement'.format(fake_module)
|
2023-03-08 00:06:48 +08:00
|
|
|
m_run_command.return_value = 123, stdout, stderr
|
2020-11-16 23:11:20 +08:00
|
|
|
|
|
|
|
with pytest.raises(ca_test_common.AnsibleExitJson) as result:
|
|
|
|
ceph_mgr_module.main()
|
|
|
|
|
|
|
|
result = result.value.args[0]
|
2023-03-08 00:06:48 +08:00
|
|
|
assert not result['changed']
|
|
|
|
assert result['cmd'] == [['ceph',
|
|
|
|
'-n',
|
|
|
|
fake_user,
|
|
|
|
'-k',
|
|
|
|
fake_keyring,
|
|
|
|
'--cluster',
|
|
|
|
fake_cluster,
|
|
|
|
'mgr', 'module',
|
|
|
|
'enable', fake_module]]
|
|
|
|
assert result['rc']
|
|
|
|
assert result['stderr'] == "failed to enable module(s): noup. Error message(s):\nError ENOENT: " \
|
|
|
|
"all mgr daemons do not support module 'noup', pass --force to force enablement"
|
2020-11-16 23:11:20 +08:00
|
|
|
|
2023-03-08 00:06:48 +08:00
|
|
|
@patch('ceph_mgr_module.get_mgr_initial_modules', return_value=['restful', 'iostat', 'nfs'])
|
|
|
|
@patch('ceph_mgr_module.mgr_module_ls', return_value=fake_mgr_module_ls_output)
|
2020-11-16 23:11:20 +08:00
|
|
|
@patch('ansible.module_utils.basic.AnsibleModule.exit_json')
|
|
|
|
@patch('ansible.module_utils.basic.AnsibleModule.run_command')
|
2023-03-08 00:06:48 +08:00
|
|
|
def test_enable_module(self, m_run_command, m_exit_json, m_mgr_module_ls, m_get_mgr_initial_modules):
|
2020-11-16 23:11:20 +08:00
|
|
|
ca_test_common.set_module_args({
|
|
|
|
'name': fake_module,
|
2023-03-08 00:06:48 +08:00
|
|
|
'state': 'enable'
|
2020-11-16 23:11:20 +08:00
|
|
|
})
|
|
|
|
m_exit_json.side_effect = ca_test_common.exit_json
|
|
|
|
stdout = ''
|
|
|
|
stderr = ''
|
|
|
|
rc = 0
|
|
|
|
m_run_command.return_value = rc, stdout, stderr
|
|
|
|
|
|
|
|
with pytest.raises(ca_test_common.AnsibleExitJson) as result:
|
|
|
|
ceph_mgr_module.main()
|
|
|
|
|
|
|
|
result = result.value.args[0]
|
|
|
|
assert result['changed']
|
2023-03-08 00:06:48 +08:00
|
|
|
assert result['cmd'] == []
|
|
|
|
assert not result['rc']
|
2020-11-16 23:11:20 +08:00
|
|
|
assert result['stderr'] == stderr
|
2023-03-08 00:06:48 +08:00
|
|
|
assert result['stdout'] == "Successfully enabled module(s): noup"
|
2020-11-16 23:11:20 +08:00
|
|
|
|
2023-03-08 00:06:48 +08:00
|
|
|
@patch('ceph_mgr_module.get_mgr_initial_modules', return_value=['restful', 'iostat', 'nfs'])
|
|
|
|
@patch('ceph_mgr_module.mgr_module_ls', return_value={"enabled_modules": ["iostat", "nfs", "restful", "noup"],
|
|
|
|
"disabled_modules": [{"name": "fake"}],
|
|
|
|
"always_on_modules": ["foo", "bar"]
|
|
|
|
})
|
2020-11-16 23:11:20 +08:00
|
|
|
@patch('ansible.module_utils.basic.AnsibleModule.exit_json')
|
|
|
|
@patch('ansible.module_utils.basic.AnsibleModule.run_command')
|
2023-03-08 00:06:48 +08:00
|
|
|
def test_already_enabled_module(self, m_run_command, m_exit_json, m_mgr_module_ls, m_get_mgr_initial_modules):
|
2020-11-16 23:11:20 +08:00
|
|
|
ca_test_common.set_module_args({
|
|
|
|
'name': fake_module,
|
|
|
|
})
|
|
|
|
m_exit_json.side_effect = ca_test_common.exit_json
|
2021-01-21 10:02:17 +08:00
|
|
|
stderr = 'module \'{}\' is already enabled'.format(fake_module)
|
|
|
|
stdout = ''
|
2020-11-16 23:11:20 +08:00
|
|
|
rc = 0
|
|
|
|
m_run_command.return_value = rc, stdout, stderr
|
|
|
|
|
|
|
|
with pytest.raises(ca_test_common.AnsibleExitJson) as result:
|
|
|
|
ceph_mgr_module.main()
|
|
|
|
|
|
|
|
result = result.value.args[0]
|
|
|
|
assert not result['changed']
|
2023-03-08 00:06:48 +08:00
|
|
|
assert result['cmd'] == []
|
|
|
|
assert not result['rc']
|
|
|
|
assert result['stderr'] == ''
|
|
|
|
assert result['stdout'] == 'Nothing to do.'
|
2020-11-16 23:11:20 +08:00
|
|
|
|
2023-03-08 00:06:48 +08:00
|
|
|
@patch('ceph_mgr_module.get_mgr_initial_modules', return_value=['restful', 'iostat', 'nfs'])
|
|
|
|
@patch('ceph_mgr_module.mgr_module_ls', return_value=fake_mgr_module_ls_output)
|
2020-11-16 23:11:20 +08:00
|
|
|
@patch('ansible.module_utils.basic.AnsibleModule.exit_json')
|
|
|
|
@patch('ansible.module_utils.basic.AnsibleModule.run_command')
|
2023-03-08 00:06:48 +08:00
|
|
|
def test_disable_module(self, m_run_command, m_exit_json, m_mgr_module_ls, m_get_mgr_initial_modules):
|
2020-11-16 23:11:20 +08:00
|
|
|
ca_test_common.set_module_args({
|
|
|
|
'name': fake_module,
|
|
|
|
'state': 'disable'
|
|
|
|
})
|
|
|
|
m_exit_json.side_effect = ca_test_common.exit_json
|
|
|
|
stdout = ''
|
|
|
|
stderr = ''
|
|
|
|
rc = 0
|
|
|
|
m_run_command.return_value = rc, stdout, stderr
|
|
|
|
|
|
|
|
with pytest.raises(ca_test_common.AnsibleExitJson) as result:
|
|
|
|
ceph_mgr_module.main()
|
|
|
|
|
|
|
|
result = result.value.args[0]
|
|
|
|
assert result['changed']
|
2023-03-08 00:06:48 +08:00
|
|
|
assert result['cmd'] == []
|
|
|
|
assert result['rc'] == rc
|
|
|
|
assert result['stderr'] == stderr
|
|
|
|
assert result['stdout'] == 'Successfully disabled module(s): noup'
|
|
|
|
|
|
|
|
@patch('ceph_mgr_module.get_mgr_initial_modules', return_value=['restful', 'iostat', 'nfs'])
|
|
|
|
@patch('ceph_mgr_module.mgr_module_ls', return_value=fake_mgr_module_ls_output)
|
|
|
|
@patch('ansible.module_utils.basic.AnsibleModule.exit_json')
|
|
|
|
@patch('ansible.module_utils.basic.AnsibleModule.run_command')
|
|
|
|
def test_disable_module_already_disabled(self, m_run_command, m_exit_json, m_mgr_module_ls, m_get_mgr_initial_modules):
|
|
|
|
ca_test_common.set_module_args({
|
|
|
|
'name': 'fake',
|
|
|
|
'state': 'disable'
|
|
|
|
})
|
|
|
|
m_exit_json.side_effect = ca_test_common.exit_json
|
|
|
|
stdout = ''
|
|
|
|
stderr = ''
|
|
|
|
rc = 0
|
|
|
|
m_run_command.return_value = rc, stdout, stderr
|
|
|
|
|
|
|
|
with pytest.raises(ca_test_common.AnsibleExitJson) as result:
|
|
|
|
ceph_mgr_module.main()
|
|
|
|
|
|
|
|
result = result.value.args[0]
|
|
|
|
assert not result['changed']
|
|
|
|
assert result['cmd'] == []
|
2020-11-16 23:11:20 +08:00
|
|
|
assert result['rc'] == rc
|
|
|
|
assert result['stderr'] == stderr
|
2023-03-08 00:06:48 +08:00
|
|
|
assert result['stdout'] == 'Skipped module(s): fake'
|
2020-11-16 23:11:20 +08:00
|
|
|
|
|
|
|
@patch.dict(os.environ, {'CEPH_CONTAINER_BINARY': fake_container_binary})
|
|
|
|
@patch.dict(os.environ, {'CEPH_CONTAINER_IMAGE': fake_container_image})
|
2023-03-08 00:06:48 +08:00
|
|
|
@patch('ceph_mgr_module.get_mgr_initial_modules', return_value=['restful', 'iostat', 'nfs'])
|
|
|
|
@patch('ceph_mgr_module.mgr_module_ls', return_value=fake_mgr_module_ls_output)
|
2020-11-16 23:11:20 +08:00
|
|
|
@patch('ansible.module_utils.basic.AnsibleModule.exit_json')
|
|
|
|
@patch('ansible.module_utils.basic.AnsibleModule.run_command')
|
2023-03-08 00:06:48 +08:00
|
|
|
def test_with_container(self, m_run_command, m_exit_json, m_mgr_module_ls, m_get_mgr_initial_modules):
|
2020-11-16 23:11:20 +08:00
|
|
|
ca_test_common.set_module_args({
|
|
|
|
'name': fake_module,
|
|
|
|
})
|
|
|
|
m_exit_json.side_effect = ca_test_common.exit_json
|
2023-03-08 00:06:48 +08:00
|
|
|
stderr = ''
|
2020-11-16 23:11:20 +08:00
|
|
|
rc = 0
|
2023-03-08 00:06:48 +08:00
|
|
|
m_run_command.return_value = rc, '', stderr
|
2020-11-16 23:11:20 +08:00
|
|
|
|
|
|
|
with pytest.raises(ca_test_common.AnsibleExitJson) as result:
|
|
|
|
ceph_mgr_module.main()
|
|
|
|
|
|
|
|
result = result.value.args[0]
|
|
|
|
assert result['changed']
|
2023-03-08 00:06:48 +08:00
|
|
|
assert result['cmd'] == [[fake_container_binary, 'run', '--rm', '--net=host',
|
|
|
|
'-v', '/etc/ceph:/etc/ceph:z',
|
|
|
|
'-v', '/var/lib/ceph/:/var/lib/ceph/:z',
|
|
|
|
'-v', '/var/log/ceph/:/var/log/ceph/:z',
|
|
|
|
'--entrypoint=ceph', fake_container_image,
|
|
|
|
'-n', fake_user, '-k', fake_keyring,
|
|
|
|
'--cluster', fake_cluster, 'mgr', 'module', 'enable', fake_module]]
|
2020-11-16 23:11:20 +08:00
|
|
|
assert result['rc'] == rc
|
2023-03-08 00:06:48 +08:00
|
|
|
assert result['stderr'] == ''
|
|
|
|
assert result['stdout'] == 'action = enable success for the following modules: noup'
|
|
|
|
|
|
|
|
@patch('ansible.module_utils.basic.AnsibleModule.run_command')
|
|
|
|
def test_get_run_dir(self, m_run_command):
|
|
|
|
m_run_command.return_value = 0, '/var/run/ceph', ''
|
|
|
|
assert ceph_mgr_module.get_run_dir(AnsibleModule, 'fake', 'fake') == '/var/run/ceph'
|
|
|
|
|
|
|
|
@patch('ansible.module_utils.basic.AnsibleModule.run_command')
|
|
|
|
def test_get_run_dir_fail(self, m_run_command):
|
|
|
|
m_run_command.return_value = 1, '', ''
|
|
|
|
with pytest.raises(RuntimeError):
|
|
|
|
ceph_mgr_module.get_run_dir(AnsibleModule, 'fake', 'fake')
|
|
|
|
|
|
|
|
@patch('ceph_mgr_module.get_run_dir', return_value='/var/run/ceph')
|
|
|
|
@patch('ansible.module_utils.basic.AnsibleModule.run_command')
|
|
|
|
def test_get_mgr_initial_modules(self, m_run_command, m_get_run_dir):
|
|
|
|
m_run_command.return_value = 0, '{"mgr_initial_modules":"foo bar"}', ''
|
|
|
|
m_get_run_dir.return_value = '/var/run/ceph'
|
|
|
|
assert ceph_mgr_module.get_mgr_initial_modules(AnsibleModule, 'ceph', None) == ["foo", "bar"]
|
|
|
|
|
|
|
|
@patch('ceph_mgr_module.get_run_dir', return_value='/var/run/ceph')
|
|
|
|
@patch('ansible.module_utils.basic.AnsibleModule.run_command')
|
|
|
|
def test_get_mgr_initial_modules_fail(self, m_run_command, m_get_run_dir):
|
|
|
|
m_run_command.return_value = 1, '', 'error'
|
|
|
|
with pytest.raises(RuntimeError):
|
|
|
|
ceph_mgr_module.get_mgr_initial_modules(AnsibleModule, 'ceph', None)
|
|
|
|
|
|
|
|
@patch('ansible.module_utils.basic.AnsibleModule.run_command')
|
|
|
|
def test_mgr_module_ls(self, m_run_command):
|
|
|
|
m_run_command.return_value = 0, '{}', ''
|
|
|
|
assert ceph_mgr_module.mgr_module_ls(AnsibleModule, 'ceph', None) == {}
|
|
|
|
|
|
|
|
@patch('ansible.module_utils.basic.AnsibleModule.run_command')
|
|
|
|
def test_mgr_module_ls_fail(self, m_run_command):
|
|
|
|
m_run_command.return_value = 1, '', 'Error'
|
|
|
|
with pytest.raises(RuntimeError):
|
|
|
|
ceph_mgr_module.mgr_module_ls(AnsibleModule, 'ceph', None)
|
|
|
|
|
|
|
|
@patch('ceph_mgr_module.get_mgr_initial_modules', return_value=['restful', 'iostat', 'nfs'])
|
|
|
|
@patch('ceph_mgr_module.mgr_module_ls',
|
|
|
|
return_value={"enabled_modules": ["iostat",
|
|
|
|
"nfs",
|
|
|
|
"restful",
|
|
|
|
"foobar",
|
|
|
|
"zabbix"],
|
|
|
|
"disabled_modules": [{"name": "fake"}],
|
|
|
|
"always_on_modules": ["foo", "bar"]})
|
|
|
|
@patch('ansible.module_utils.basic.AnsibleModule.exit_json')
|
|
|
|
@patch('ansible.module_utils.basic.AnsibleModule.run_command')
|
|
|
|
def test_mgr_module_auto(self, m_run_command, m_exit_json, m_mgr_module_ls,
|
|
|
|
m_get_mgr_initial_modules):
|
|
|
|
m_run_command.return_value = 0, '', ''
|
|
|
|
m_exit_json.side_effect = ca_test_common.exit_json
|
|
|
|
ca_test_common.set_module_args({
|
|
|
|
'name': ["foo", "bar"],
|
|
|
|
'state': 'auto'
|
|
|
|
})
|
|
|
|
|
|
|
|
with pytest.raises(ca_test_common.AnsibleExitJson) as result:
|
|
|
|
ceph_mgr_module.main()
|
|
|
|
result = result.value.args[0]
|
|
|
|
assert result['stdout'] == "action = enable success for the following modules: bar,foo\n" \
|
|
|
|
"action = disable success for the following modules: foobar,zabbix"
|
|
|
|
assert result['changed']
|
|
|
|
assert not result['rc']
|
|
|
|
|
|
|
|
def test_get_cmd_from_reports(self):
|
|
|
|
cmd = ceph_mgr_module.get_cmd_from_reports([("foo", 123, "", "Error", ['foo', 'bar'],),
|
|
|
|
("bar", 456, "", "Error", ['bar', 'foo'],)])
|
|
|
|
assert cmd == [['foo', 'bar'], ['bar', 'foo']]
|
|
|
|
|
|
|
|
@patch('ceph_mgr_module.get_mgr_initial_modules', return_value=['restful', 'iostat', 'nfs'])
|
|
|
|
@patch('ceph_mgr_module.mgr_module_ls', return_value=fake_mgr_module_ls_output)
|
|
|
|
@patch('ansible.module_utils.basic.AnsibleModule.exit_json')
|
|
|
|
@patch('ansible.module_utils.basic.AnsibleModule.run_command')
|
|
|
|
def test_enable_module_always_on(self, m_run_command, m_exit_json, m_mgr_module_ls, m_get_mgr_initial_modules):
|
|
|
|
ca_test_common.set_module_args({
|
|
|
|
'name': 'foo',
|
|
|
|
'state': 'enable'
|
|
|
|
})
|
|
|
|
m_exit_json.side_effect = ca_test_common.exit_json
|
|
|
|
stdout = ''
|
|
|
|
stderr = ''
|
|
|
|
rc = 0
|
|
|
|
m_run_command.return_value = rc, stdout, stderr
|
|
|
|
|
|
|
|
with pytest.raises(ca_test_common.AnsibleExitJson) as result:
|
|
|
|
ceph_mgr_module.main()
|
|
|
|
|
|
|
|
result = result.value.args[0]
|
|
|
|
assert not result['changed']
|
|
|
|
assert result['cmd'] == []
|
|
|
|
assert not result['rc']
|
|
|
|
assert result['stderr'] == ''
|
|
|
|
assert result['stdout'] == "Skipped module(s): foo"
|
|
|
|
|
|
|
|
@patch('ceph_mgr_module.get_mgr_initial_modules', return_value=['restful', 'iostat', 'nfs'])
|
|
|
|
@patch('ceph_mgr_module.mgr_module_ls', return_value=fake_mgr_module_ls_output)
|
|
|
|
@patch('ansible.module_utils.basic.AnsibleModule.exit_json')
|
|
|
|
@patch('ansible.module_utils.basic.AnsibleModule.run_command')
|
|
|
|
def test_enable_module_with_failure(self, m_run_command, m_exit_json, m_mgr_module_ls, m_get_mgr_initial_modules):
|
|
|
|
ca_test_common.set_module_args({
|
|
|
|
'name': ['fake', 'fake2'],
|
|
|
|
'state': 'enable'
|
|
|
|
})
|
|
|
|
m_exit_json.side_effect = ca_test_common.exit_json
|
|
|
|
rc = 1
|
|
|
|
m_run_command.return_value = rc, '', 'Error'
|
|
|
|
|
|
|
|
with pytest.raises(ca_test_common.AnsibleExitJson) as result:
|
|
|
|
ceph_mgr_module.main()
|
|
|
|
|
|
|
|
result = result.value.args[0]
|
|
|
|
assert not result['changed']
|
|
|
|
assert result['cmd'] == [['ceph',
|
|
|
|
'-n', 'client.admin',
|
|
|
|
'-k', '/etc/ceph/ceph.client.admin.keyring',
|
|
|
|
'--cluster', 'ceph', 'mgr', 'module',
|
|
|
|
'enable', 'fake'],
|
|
|
|
['ceph', '-n', 'client.admin',
|
|
|
|
'-k', '/etc/ceph/ceph.client.admin.keyring',
|
|
|
|
'--cluster', 'ceph', 'mgr', 'module',
|
|
|
|
'enable', 'fake2']]
|
|
|
|
assert result['rc']
|
|
|
|
assert result['stderr'] == 'Failed to enable module(s): fake,fake2'
|
|
|
|
assert result['stdout'] == ''
|