2021-01-14 10:11:39 +08:00
|
|
|
from mock.mock import patch, MagicMock
|
2020-11-05 18:05:34 +08:00
|
|
|
import os
|
|
|
|
import ca_common
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
fake_container_binary = 'podman'
|
|
|
|
fake_container_image = 'docker.io/ceph/daemon:latest'
|
|
|
|
|
|
|
|
|
|
|
|
class TestCommon(object):
|
|
|
|
|
2020-11-26 03:51:09 +08:00
|
|
|
def setup_method(self):
|
|
|
|
self.fake_binary = 'ceph'
|
|
|
|
self.fake_cluster = 'ceph'
|
|
|
|
self.fake_container_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=' + self.fake_binary,
|
|
|
|
fake_container_image
|
|
|
|
]
|
|
|
|
|
2020-11-05 18:05:34 +08:00
|
|
|
@patch.dict(os.environ, {'CEPH_CONTAINER_BINARY': fake_container_binary})
|
|
|
|
def test_container_exec(self):
|
2020-11-26 03:51:09 +08:00
|
|
|
cmd = ca_common.container_exec(self.fake_binary, fake_container_image)
|
|
|
|
assert cmd == self.fake_container_cmd
|
2020-11-05 18:05:34 +08:00
|
|
|
|
|
|
|
def test_not_is_containerized(self):
|
|
|
|
assert ca_common.is_containerized() is None
|
|
|
|
|
|
|
|
@patch.dict(os.environ, {'CEPH_CONTAINER_IMAGE': fake_container_image})
|
|
|
|
def test_is_containerized(self):
|
|
|
|
assert ca_common.is_containerized() == fake_container_image
|
|
|
|
|
|
|
|
@pytest.mark.parametrize('image', [None, fake_container_image])
|
|
|
|
@patch.dict(os.environ, {'CEPH_CONTAINER_BINARY': fake_container_binary})
|
2022-11-29 16:47:58 +08:00
|
|
|
def test_pre_generate_cmd(self, image):
|
2020-11-05 18:05:34 +08:00
|
|
|
if image:
|
2020-11-26 03:51:09 +08:00
|
|
|
expected_cmd = self.fake_container_cmd
|
2020-11-05 18:05:34 +08:00
|
|
|
else:
|
2020-11-26 03:51:09 +08:00
|
|
|
expected_cmd = [self.fake_binary]
|
2020-11-05 18:05:34 +08:00
|
|
|
|
2022-11-29 16:47:58 +08:00
|
|
|
assert ca_common.pre_generate_cmd(self.fake_binary, image) == expected_cmd # noqa: E501
|
2020-11-05 18:05:34 +08:00
|
|
|
|
|
|
|
@pytest.mark.parametrize('image', [None, fake_container_image])
|
|
|
|
@patch.dict(os.environ, {'CEPH_CONTAINER_BINARY': fake_container_binary})
|
2022-11-29 16:47:58 +08:00
|
|
|
def test_generate_cmd(self, image):
|
2020-11-05 18:05:34 +08:00
|
|
|
sub_cmd = ['osd', 'pool']
|
|
|
|
args = ['create', 'foo']
|
|
|
|
if image:
|
2020-11-26 03:51:09 +08:00
|
|
|
expected_cmd = self.fake_container_cmd
|
2020-11-05 18:05:34 +08:00
|
|
|
else:
|
2020-11-26 03:51:09 +08:00
|
|
|
expected_cmd = [self.fake_binary]
|
2020-11-05 18:05:34 +08:00
|
|
|
|
|
|
|
expected_cmd.extend([
|
|
|
|
'-n', 'client.admin',
|
|
|
|
'-k', '/etc/ceph/ceph.client.admin.keyring',
|
|
|
|
'--cluster',
|
2020-11-26 03:51:09 +08:00
|
|
|
self.fake_cluster,
|
|
|
|
'osd', 'pool',
|
|
|
|
'create', 'foo'
|
|
|
|
])
|
2022-11-29 16:47:58 +08:00
|
|
|
assert ca_common.generate_cmd(sub_cmd=sub_cmd, args=args, cluster=self.fake_cluster, container_image=image) == expected_cmd # noqa: E501
|
2020-11-26 03:51:09 +08:00
|
|
|
|
|
|
|
@pytest.mark.parametrize('image', [None, fake_container_image])
|
|
|
|
@patch.dict(os.environ, {'CEPH_CONTAINER_BINARY': fake_container_binary})
|
2022-11-29 16:47:58 +08:00
|
|
|
def test_generate_cmd_different_cluster_name(self, image):
|
2020-11-26 03:51:09 +08:00
|
|
|
sub_cmd = ['osd', 'pool']
|
|
|
|
args = ['create', 'foo']
|
|
|
|
if image:
|
|
|
|
expected_cmd = self.fake_container_cmd
|
|
|
|
else:
|
|
|
|
expected_cmd = [self.fake_binary]
|
|
|
|
|
|
|
|
expected_cmd.extend([
|
|
|
|
'-n', 'client.admin',
|
|
|
|
'-k', '/etc/ceph/foo.client.admin.keyring',
|
|
|
|
'--cluster',
|
|
|
|
'foo',
|
|
|
|
'osd', 'pool',
|
|
|
|
'create', 'foo'
|
|
|
|
])
|
2022-11-29 16:47:58 +08:00
|
|
|
result = ca_common.generate_cmd(sub_cmd=sub_cmd, args=args, cluster='foo', container_image=image) # noqa: E501
|
2020-11-26 03:51:09 +08:00
|
|
|
assert result == expected_cmd
|
|
|
|
|
|
|
|
@pytest.mark.parametrize('image', [None, fake_container_image])
|
|
|
|
@patch.dict(os.environ, {'CEPH_CONTAINER_BINARY': fake_container_binary})
|
2022-11-29 16:47:58 +08:00
|
|
|
def test_generate_cmd_different_cluster_name_and_user(self, image):
|
2020-11-26 03:51:09 +08:00
|
|
|
sub_cmd = ['osd', 'pool']
|
|
|
|
args = ['create', 'foo']
|
|
|
|
if image:
|
|
|
|
expected_cmd = self.fake_container_cmd
|
|
|
|
else:
|
|
|
|
expected_cmd = [self.fake_binary]
|
|
|
|
|
|
|
|
expected_cmd.extend([
|
|
|
|
'-n', 'client.foo',
|
|
|
|
'-k', '/etc/ceph/foo.client.foo.keyring',
|
|
|
|
'--cluster',
|
|
|
|
'foo',
|
|
|
|
'osd', 'pool',
|
|
|
|
'create', 'foo'
|
|
|
|
])
|
2022-11-29 16:47:58 +08:00
|
|
|
result = ca_common.generate_cmd(sub_cmd=sub_cmd, args=args, cluster='foo', user='client.foo', container_image=image) # noqa: E501
|
2020-11-26 03:51:09 +08:00
|
|
|
assert result == expected_cmd
|
|
|
|
|
|
|
|
@pytest.mark.parametrize('image', [None, fake_container_image])
|
|
|
|
@patch.dict(os.environ, {'CEPH_CONTAINER_BINARY': fake_container_binary})
|
2022-11-29 16:47:58 +08:00
|
|
|
def test_generate_cmd_different_user(self, image):
|
2020-11-26 03:51:09 +08:00
|
|
|
sub_cmd = ['osd', 'pool']
|
|
|
|
args = ['create', 'foo']
|
|
|
|
if image:
|
|
|
|
expected_cmd = self.fake_container_cmd
|
|
|
|
else:
|
|
|
|
expected_cmd = [self.fake_binary]
|
|
|
|
|
|
|
|
expected_cmd.extend([
|
|
|
|
'-n', 'client.foo',
|
|
|
|
'-k', '/etc/ceph/ceph.client.foo.keyring',
|
|
|
|
'--cluster',
|
|
|
|
'ceph',
|
2020-11-05 18:05:34 +08:00
|
|
|
'osd', 'pool',
|
|
|
|
'create', 'foo'
|
|
|
|
])
|
2022-11-29 16:47:58 +08:00
|
|
|
result = ca_common.generate_cmd(sub_cmd=sub_cmd, args=args, user='client.foo', container_image=image) # noqa: E501
|
2020-11-26 03:51:09 +08:00
|
|
|
assert result == expected_cmd
|
2021-01-14 10:11:39 +08:00
|
|
|
|
|
|
|
@pytest.mark.parametrize('stdin', [None, 'foo'])
|
|
|
|
def test_exec_command(self, stdin):
|
|
|
|
fake_module = MagicMock()
|
|
|
|
rc = 0
|
|
|
|
stderr = ''
|
|
|
|
stdout = 'ceph version 1.2.3'
|
|
|
|
fake_module.run_command.return_value = 0, stdout, stderr
|
|
|
|
expected_cmd = [self.fake_binary, '--version']
|
2022-11-29 16:47:58 +08:00
|
|
|
_rc, _cmd, _out, _err = ca_common.exec_command(fake_module, expected_cmd, stdin=stdin) # noqa: E501
|
2021-01-14 10:11:39 +08:00
|
|
|
assert _rc == rc
|
|
|
|
assert _cmd == expected_cmd
|
|
|
|
assert _err == stderr
|
|
|
|
assert _out == stdout
|