From 6f90f0fa2efc830eec07373b94534e3c96f24945 Mon Sep 17 00:00:00 2001 From: Sebastian Wagner Date: Fri, 23 Jul 2021 02:36:27 +0200 Subject: [PATCH] pyhton-common: move some tests from cephadm/test_spec.py Cause they don't have any dependencies to cephadm Signed-off-by: Sebastian Wagner --- src/pybind/mgr/cephadm/tests/test_spec.py | 93 +------------------ .../ceph/tests/test_service_spec.py | 91 +++++++++++++++++- 2 files changed, 91 insertions(+), 93 deletions(-) diff --git a/src/pybind/mgr/cephadm/tests/test_spec.py b/src/pybind/mgr/cephadm/tests/test_spec.py index 7566aa99dd8..458958973c8 100644 --- a/src/pybind/mgr/cephadm/tests/test_spec.py +++ b/src/pybind/mgr/cephadm/tests/test_spec.py @@ -6,10 +6,8 @@ import json import pytest -import yaml - from ceph.deployment.service_spec import ServiceSpec, NFSServiceSpec, RGWSpec, \ - IscsiServiceSpec, AlertManagerSpec, HostPlacementSpec, CustomContainerSpec + IscsiServiceSpec, HostPlacementSpec, CustomContainerSpec from orchestrator import DaemonDescription, OrchestratorError @@ -589,92 +587,3 @@ def test_daemon_description_service_name(spec: ServiceSpec, else: with pytest.raises(OrchestratorError): dd.service_name() - - -def test_alertmanager_spec_1(): - spec = AlertManagerSpec() - assert spec.service_type == 'alertmanager' - assert isinstance(spec.user_data, dict) - assert len(spec.user_data.keys()) == 0 - - -def test_alertmanager_spec_2(): - spec = AlertManagerSpec(user_data={'default_webhook_urls': ['foo']}) - assert isinstance(spec.user_data, dict) - assert 'default_webhook_urls' in spec.user_data.keys() - - -def test_custom_container_spec(): - spec = CustomContainerSpec(service_id='hello-world', - image='docker.io/library/hello-world:latest', - entrypoint='/usr/bin/bash', - uid=1000, - gid=2000, - volume_mounts={'foo': '/foo'}, - args=['--foo'], - envs=['FOO=0815'], - bind_mounts=[ - [ - 'type=bind', - 'source=lib/modules', - 'destination=/lib/modules', - 'ro=true' - ] - ], - ports=[8080, 8443], - dirs=['foo', 'bar'], - files={ - 'foo.conf': 'foo\nbar', - 'bar.conf': ['foo', 'bar'] - }) - assert spec.service_type == 'container' - assert spec.entrypoint == '/usr/bin/bash' - assert spec.uid == 1000 - assert spec.gid == 2000 - assert spec.volume_mounts == {'foo': '/foo'} - assert spec.args == ['--foo'] - assert spec.envs == ['FOO=0815'] - assert spec.bind_mounts == [ - [ - 'type=bind', - 'source=lib/modules', - 'destination=/lib/modules', - 'ro=true' - ] - ] - assert spec.ports == [8080, 8443] - assert spec.dirs == ['foo', 'bar'] - assert spec.files == { - 'foo.conf': 'foo\nbar', - 'bar.conf': ['foo', 'bar'] - } - - -def test_custom_container_spec_config_json(): - spec = CustomContainerSpec(service_id='foo', image='foo', dirs=None) - config_json = spec.config_json() - for key in ['entrypoint', 'uid', 'gid', 'bind_mounts', 'dirs']: - assert key not in config_json - - -def test_ingress_spec(): - yaml_str = """service_type: ingress -service_id: rgw.foo -placement: - hosts: - - host1 - - host2 - - host3 -spec: - virtual_ip: 192.168.20.1/24 - backend_service: rgw.foo - frontend_port: 8080 - monitor_port: 8081 -""" - yaml_file = yaml.safe_load(yaml_str) - spec = ServiceSpec.from_json(yaml_file) - assert spec.service_type == "ingress" - assert spec.service_id == "rgw.foo" - assert spec.virtual_ip == "192.168.20.1/24" - assert spec.frontend_port == 8080 - assert spec.monitor_port == 8081 diff --git a/src/python-common/ceph/tests/test_service_spec.py b/src/python-common/ceph/tests/test_service_spec.py index a360f126323..e8620b01733 100644 --- a/src/python-common/ceph/tests/test_service_spec.py +++ b/src/python-common/ceph/tests/test_service_spec.py @@ -5,7 +5,8 @@ import yaml import pytest from ceph.deployment.service_spec import HostPlacementSpec, PlacementSpec, \ - ServiceSpec, RGWSpec, NFSServiceSpec, IscsiServiceSpec + ServiceSpec, RGWSpec, NFSServiceSpec, IscsiServiceSpec, AlertManagerSpec, \ + CustomContainerSpec from ceph.deployment.drive_group import DriveGroupSpec from ceph.deployment.hostspec import SpecValidationError @@ -242,6 +243,19 @@ spec: assert yaml.dump(object) == y assert yaml.dump(ServiceSpec.from_json(object.to_json())) == y +def test_alertmanager_spec_1(): + spec = AlertManagerSpec() + assert spec.service_type == 'alertmanager' + assert isinstance(spec.user_data, dict) + assert len(spec.user_data.keys()) == 0 + + +def test_alertmanager_spec_2(): + spec = AlertManagerSpec(user_data={'default_webhook_urls': ['foo']}) + assert isinstance(spec.user_data, dict) + assert 'default_webhook_urls' in spec.user_data.keys() + + @pytest.mark.parametrize("spec1, spec2, eq", [ ( @@ -335,3 +349,78 @@ def test_service_id_raises_invalid_char(s_type, s_id): with pytest.raises(SpecValidationError): spec = ServiceSpec.from_json(_get_dict_spec(s_type, s_id)) spec.validate() + +def test_custom_container_spec(): + spec = CustomContainerSpec(service_id='hello-world', + image='docker.io/library/hello-world:latest', + entrypoint='/usr/bin/bash', + uid=1000, + gid=2000, + volume_mounts={'foo': '/foo'}, + args=['--foo'], + envs=['FOO=0815'], + bind_mounts=[ + [ + 'type=bind', + 'source=lib/modules', + 'destination=/lib/modules', + 'ro=true' + ] + ], + ports=[8080, 8443], + dirs=['foo', 'bar'], + files={ + 'foo.conf': 'foo\nbar', + 'bar.conf': ['foo', 'bar'] + }) + assert spec.service_type == 'container' + assert spec.entrypoint == '/usr/bin/bash' + assert spec.uid == 1000 + assert spec.gid == 2000 + assert spec.volume_mounts == {'foo': '/foo'} + assert spec.args == ['--foo'] + assert spec.envs == ['FOO=0815'] + assert spec.bind_mounts == [ + [ + 'type=bind', + 'source=lib/modules', + 'destination=/lib/modules', + 'ro=true' + ] + ] + assert spec.ports == [8080, 8443] + assert spec.dirs == ['foo', 'bar'] + assert spec.files == { + 'foo.conf': 'foo\nbar', + 'bar.conf': ['foo', 'bar'] + } + + +def test_custom_container_spec_config_json(): + spec = CustomContainerSpec(service_id='foo', image='foo', dirs=None) + config_json = spec.config_json() + for key in ['entrypoint', 'uid', 'gid', 'bind_mounts', 'dirs']: + assert key not in config_json + + +def test_ingress_spec(): + yaml_str = """service_type: ingress +service_id: rgw.foo +placement: + hosts: + - host1 + - host2 + - host3 +spec: + virtual_ip: 192.168.20.1/24 + backend_service: rgw.foo + frontend_port: 8080 + monitor_port: 8081 +""" + yaml_file = yaml.safe_load(yaml_str) + spec = ServiceSpec.from_json(yaml_file) + assert spec.service_type == "ingress" + assert spec.service_id == "rgw.foo" + assert spec.virtual_ip == "192.168.20.1/24" + assert spec.frontend_port == 8080 + assert spec.monitor_port == 8081