From c652ae74795252f875594b09627064d97ff2a762 Mon Sep 17 00:00:00 2001 From: Sebastian Wagner Date: Mon, 20 Dec 2021 11:37:40 +0100 Subject: [PATCH] python-common: move test_invalid_snmp_gateway_spec from mgr/cephadm Let's keep the tests in the same package where the class is defined. Signed-off-by: Sebastian Wagner --- src/pybind/mgr/cephadm/tests/test_spec.py | 237 ------------------ .../ceph/tests/test_service_spec.py | 229 +++++++++++++++++ 2 files changed, 229 insertions(+), 237 deletions(-) diff --git a/src/pybind/mgr/cephadm/tests/test_spec.py b/src/pybind/mgr/cephadm/tests/test_spec.py index f92c6ac10b2..436aae5d6f2 100644 --- a/src/pybind/mgr/cephadm/tests/test_spec.py +++ b/src/pybind/mgr/cephadm/tests/test_spec.py @@ -9,7 +9,6 @@ import pytest from ceph.deployment.service_spec import ServiceSpec, NFSServiceSpec, RGWSpec, \ IscsiServiceSpec, HostPlacementSpec, CustomContainerSpec -from ceph.deployment.hostspec import SpecValidationError from orchestrator import DaemonDescription, OrchestratorError @@ -647,239 +646,3 @@ snmp_version: V3 def test_valid_snmp_gateway_spec(yaml_doc: YAMLdoc, snmp_type: str): spec = ServiceSpec.from_json(yaml_doc.content) spec.validate() - - -@pytest.mark.parametrize( - "yaml_doc,expected_error", [ - (YAMLdoc(""" ---- -service_type: snmp-gateway -service_name: snmp-gateway -placement: - count: 1 -spec: - credentials: - snmp_community: public -snmp_destination: 192.168.1.42:162 -snmp_version: V4 -"""), 'snmp_version unsupported. Must be one of V2c, V3'), - (YAMLdoc(""" ---- -service_type: snmp-gateway -service_name: snmp-gateway -placement: - count: 1 -spec: - credentials: - snmp_community: public -port: 9464 -snmp_destination: 192.168.1.42:162 -"""), 'Missing SNMP version (snmp_version)'), - (YAMLdoc(""" ---- -service_type: snmp-gateway -service_name: snmp-gateway -placement: - count: 1 -spec: - credentials: - snmp_v3_auth_username: myuser - snmp_v3_auth_password: mypassword -port: 9464 -auth_protocol: wah -snmp_destination: 192.168.1.42:162 -snmp_version: V3 -"""), 'auth_protocol unsupported. Must be one of MD5, SHA'), - (YAMLdoc(""" ---- -service_type: snmp-gateway -service_name: snmp-gateway -placement: - count: 1 -spec: - credentials: - snmp_v3_auth_username: myuser - snmp_v3_auth_password: mypassword - snmp_v3_priv_password: mysecret -port: 9464 -auth_protocol: SHA -privacy_protocol: weewah -snmp_destination: 192.168.1.42:162 -snmp_version: V3 -"""), 'privacy_protocol unsupported. Must be one of DES, AES'), - (YAMLdoc(""" ---- -service_type: snmp-gateway -service_name: snmp-gateway -placement: - count: 1 -spec: - credentials: - snmp_v3_auth_username: myuser - snmp_v3_auth_password: mypassword - snmp_v3_priv_password: mysecret -port: 9464 -auth_protocol: SHA -privacy_protocol: AES -snmp_destination: 192.168.1.42:162 -snmp_version: V3 -"""), 'Must provide an engine_id for SNMP V3 notifications'), - (YAMLdoc(""" ---- -service_type: snmp-gateway -service_name: snmp-gateway -placement: - count: 1 -spec: - credentials: - snmp_community: public -port: 9464 -snmp_destination: 192.168.1.42 -snmp_version: V2c -"""), 'SNMP destination (snmp_destination) type (IPv4) is invalid. Must be either: IPv4:Port, Name:Port'), - (YAMLdoc(""" ---- -service_type: snmp-gateway -service_name: snmp-gateway -placement: - count: 1 -spec: - credentials: - snmp_v3_auth_username: myuser - snmp_v3_auth_password: mypassword - snmp_v3_priv_password: mysecret -port: 9464 -auth_protocol: SHA -privacy_protocol: AES -engine_id: bogus -snmp_destination: 192.168.1.42:162 -snmp_version: V3 -"""), 'engine_id must be a string containing 10-64 hex characters. Its length must be divisible by 2'), - (YAMLdoc(""" ---- -service_type: snmp-gateway -service_name: snmp-gateway -placement: - count: 1 -spec: - credentials: - snmp_v3_auth_username: myuser - snmp_v3_auth_password: mypassword -port: 9464 -auth_protocol: SHA -engine_id: 8000C53F0000000000 -snmp_version: V3 -"""), 'SNMP destination (snmp_destination) must be provided'), - (YAMLdoc(""" ---- -service_type: snmp-gateway -service_name: snmp-gateway -placement: - count: 1 -spec: - credentials: - snmp_v3_auth_username: myuser - snmp_v3_auth_password: mypassword - snmp_v3_priv_password: mysecret -port: 9464 -auth_protocol: SHA -privacy_protocol: AES -engine_id: 8000C53F0000000000 -snmp_destination: my.imaginary.snmp-host -snmp_version: V3 -"""), 'SNMP destination (snmp_destination) is invalid: DNS lookup failed'), - (YAMLdoc(""" ---- -service_type: snmp-gateway -service_name: snmp-gateway -placement: - count: 1 -spec: - credentials: - snmp_v3_auth_username: myuser - snmp_v3_auth_password: mypassword - snmp_v3_priv_password: mysecret -port: 9464 -auth_protocol: SHA -privacy_protocol: AES -engine_id: 8000C53F0000000000 -snmp_destination: 10.79.32.10:fred -snmp_version: V3 -"""), 'SNMP destination (snmp_destination) is invalid: Port must be numeric'), - (YAMLdoc(""" ---- -service_type: snmp-gateway -service_name: snmp-gateway -placement: - count: 1 -spec: - credentials: - snmp_v3_auth_username: myuser - snmp_v3_auth_password: mypassword - snmp_v3_priv_password: mysecret -port: 9464 -auth_protocol: SHA -privacy_protocol: AES -engine_id: 8000C53 -snmp_destination: 10.79.32.10:162 -snmp_version: V3 -"""), 'engine_id must be a string containing 10-64 hex characters. Its length must be divisible by 2'), - (YAMLdoc(""" ---- -service_type: snmp-gateway -service_name: snmp-gateway -placement: - count: 1 -spec: - credentials: - snmp_v3_auth_username: myuser - snmp_v3_auth_password: mypassword - snmp_v3_priv_password: mysecret -port: 9464 -auth_protocol: SHA -privacy_protocol: AES -engine_id: 8000C53DOH! -snmp_destination: 10.79.32.10:162 -snmp_version: V3 -"""), 'engine_id must be a string containing 10-64 hex characters. Its length must be divisible by 2'), - (YAMLdoc(""" ---- -service_type: snmp-gateway -service_name: snmp-gateway -placement: - count: 1 -spec: - credentials: - snmp_v3_auth_username: myuser - snmp_v3_auth_password: mypassword - snmp_v3_priv_password: mysecret -port: 9464 -auth_protocol: SHA -privacy_protocol: AES -engine_id: 8000C53FCA7344403DC611EC9B985254002537A6C53FCA7344403DC6112537A60 -snmp_destination: 10.79.32.10:162 -snmp_version: V3 -"""), 'engine_id must be a string containing 10-64 hex characters. Its length must be divisible by 2'), - (YAMLdoc(""" ---- -service_type: snmp-gateway -service_name: snmp-gateway -placement: - count: 1 -spec: - credentials: - snmp_v3_auth_username: myuser - snmp_v3_auth_password: mypassword - snmp_v3_priv_password: mysecret -port: 9464 -auth_protocol: SHA -privacy_protocol: AES -engine_id: 8000C53F00000 -snmp_destination: 10.79.32.10:162 -snmp_version: V3 -"""), 'engine_id must be a string containing 10-64 hex characters. Its length must be divisible by 2'), - ]) -def test_invalid_snmp_gateway_spec(yaml_doc: YAMLdoc, expected_error: str): - with pytest.raises(SpecValidationError) as e: - ServiceSpec.from_json(yaml_doc.content) - assert str(e.value) == expected_error diff --git a/src/python-common/ceph/tests/test_service_spec.py b/src/python-common/ceph/tests/test_service_spec.py index 555ba9b8941..fd284f48133 100644 --- a/src/python-common/ceph/tests/test_service_spec.py +++ b/src/python-common/ceph/tests/test_service_spec.py @@ -1,5 +1,7 @@ # flake8: noqa import json +import re + import yaml import pytest @@ -589,6 +591,233 @@ spec: auth_protocol: BIZARRE snmp_version: V3 """, "auth_protocol unsupported. Must be one of MD5, SHA"), + (""" +--- +service_type: snmp-gateway +service_name: snmp-gateway +placement: + count: 1 +spec: + credentials: + snmp_community: public + snmp_destination: 192.168.1.42:162 + snmp_version: V4 +""", 'snmp_version unsupported. Must be one of V2c, V3'), + (""" +--- +service_type: snmp-gateway +service_name: snmp-gateway +placement: + count: 1 +spec: + credentials: + snmp_community: public + port: 9464 + snmp_destination: 192.168.1.42:162 +""", re.escape('Missing SNMP version (snmp_version)')), + (""" +--- +service_type: snmp-gateway +service_name: snmp-gateway +placement: + count: 1 +spec: + credentials: + snmp_v3_auth_username: myuser + snmp_v3_auth_password: mypassword + port: 9464 + auth_protocol: wah + snmp_destination: 192.168.1.42:162 + snmp_version: V3 +""", 'auth_protocol unsupported. Must be one of MD5, SHA'), + (""" +--- +service_type: snmp-gateway +service_name: snmp-gateway +placement: + count: 1 +spec: + credentials: + snmp_v3_auth_username: myuser + snmp_v3_auth_password: mypassword + snmp_v3_priv_password: mysecret + port: 9464 + auth_protocol: SHA + privacy_protocol: weewah + snmp_destination: 192.168.1.42:162 + snmp_version: V3 +""", 'privacy_protocol unsupported. Must be one of DES, AES'), + (""" +--- +service_type: snmp-gateway +service_name: snmp-gateway +placement: + count: 1 +spec: + credentials: + snmp_v3_auth_username: myuser + snmp_v3_auth_password: mypassword + snmp_v3_priv_password: mysecret + port: 9464 + auth_protocol: SHA + privacy_protocol: AES + snmp_destination: 192.168.1.42:162 + snmp_version: V3 +""", 'Must provide an engine_id for SNMP V3 notifications'), + (""" +--- +service_type: snmp-gateway +service_name: snmp-gateway +placement: + count: 1 +spec: + credentials: + snmp_community: public + port: 9464 + snmp_destination: 192.168.1.42 + snmp_version: V2c +""", re.escape('SNMP destination (snmp_destination) type (IPv4) is invalid. Must be either: IPv4:Port, Name:Port')), + (""" +--- +service_type: snmp-gateway +service_name: snmp-gateway +placement: + count: 1 +spec: + credentials: + snmp_v3_auth_username: myuser + snmp_v3_auth_password: mypassword + snmp_v3_priv_password: mysecret + port: 9464 + auth_protocol: SHA + privacy_protocol: AES + engine_id: bogus + snmp_destination: 192.168.1.42:162 + snmp_version: V3 +""", 'engine_id must be a string containing 10-64 hex characters. Its length must be divisible by 2'), + (""" +--- +service_type: snmp-gateway +service_name: snmp-gateway +placement: + count: 1 +spec: + credentials: + snmp_v3_auth_username: myuser + snmp_v3_auth_password: mypassword + port: 9464 + auth_protocol: SHA + engine_id: 8000C53F0000000000 + snmp_version: V3 +""", re.escape('SNMP destination (snmp_destination) must be provided')), + (""" +--- +service_type: snmp-gateway +service_name: snmp-gateway +placement: + count: 1 +spec: + credentials: + snmp_v3_auth_username: myuser + snmp_v3_auth_password: mypassword + snmp_v3_priv_password: mysecret + port: 9464 + auth_protocol: SHA + privacy_protocol: AES + engine_id: 8000C53F0000000000 + snmp_destination: my.imaginary.snmp-host + snmp_version: V3 +""", re.escape('SNMP destination (snmp_destination) is invalid: DNS lookup failed')), + (""" +--- +service_type: snmp-gateway +service_name: snmp-gateway +placement: + count: 1 +spec: + credentials: + snmp_v3_auth_username: myuser + snmp_v3_auth_password: mypassword + snmp_v3_priv_password: mysecret + port: 9464 + auth_protocol: SHA + privacy_protocol: AES + engine_id: 8000C53F0000000000 + snmp_destination: 10.79.32.10:fred + snmp_version: V3 +""", re.escape('SNMP destination (snmp_destination) is invalid: Port must be numeric')), + (""" +--- +service_type: snmp-gateway +service_name: snmp-gateway +placement: + count: 1 +spec: + credentials: + snmp_v3_auth_username: myuser + snmp_v3_auth_password: mypassword + snmp_v3_priv_password: mysecret + port: 9464 + auth_protocol: SHA + privacy_protocol: AES + engine_id: 8000C53 + snmp_destination: 10.79.32.10:162 + snmp_version: V3 +""", 'engine_id must be a string containing 10-64 hex characters. Its length must be divisible by 2'), + (""" +--- +service_type: snmp-gateway +service_name: snmp-gateway +placement: + count: 1 +spec: + credentials: + snmp_v3_auth_username: myuser + snmp_v3_auth_password: mypassword + snmp_v3_priv_password: mysecret + port: 9464 + auth_protocol: SHA + privacy_protocol: AES + engine_id: 8000C53DOH! + snmp_destination: 10.79.32.10:162 + snmp_version: V3 +""", 'engine_id must be a string containing 10-64 hex characters. Its length must be divisible by 2'), + (""" +--- +service_type: snmp-gateway +service_name: snmp-gateway +placement: + count: 1 +spec: + credentials: + snmp_v3_auth_username: myuser + snmp_v3_auth_password: mypassword + snmp_v3_priv_password: mysecret + port: 9464 + auth_protocol: SHA + privacy_protocol: AES + engine_id: 8000C53FCA7344403DC611EC9B985254002537A6C53FCA7344403DC6112537A60 + snmp_destination: 10.79.32.10:162 + snmp_version: V3 +""", 'engine_id must be a string containing 10-64 hex characters. Its length must be divisible by 2'), + (""" +--- +service_type: snmp-gateway +service_name: snmp-gateway +placement: + count: 1 +spec: + credentials: + snmp_v3_auth_username: myuser + snmp_v3_auth_password: mypassword + snmp_v3_priv_password: mysecret + port: 9464 + auth_protocol: SHA + privacy_protocol: AES + engine_id: 8000C53F00000 + snmp_destination: 10.79.32.10:162 + snmp_version: V3 +""", 'engine_id must be a string containing 10-64 hex characters. Its length must be divisible by 2'), ]) def test_service_spec_validation_error(y, error_match): data = yaml.safe_load(y) -- 2.39.5