From 7c493577ba88700979f69be1393e67086e0d8b61 Mon Sep 17 00:00:00 2001 From: Matt Clay Date: Mon, 2 Mar 2020 10:40:34 -0800 Subject: [PATCH] Unit test cleanup to assist with migration. (#67920) * Move linode unit tests to match module layout. * Fix location of netapp module_utils unit tests. * Update sanity ignores. --- test/sanity/ignore.txt | 6 +- test/units/module_utils/test_netapp.py | 57 ++++++++++++++++ test/units/modules/cloud/linode/conftest.py | 67 ++++++++++++++++++ .../{linode_v4 => linode}/test_linode_v4.py | 0 .../units/modules/cloud/linode_v4/__init__.py | 0 .../units/modules/cloud/linode_v4/conftest.py | 68 ------------------- .../modules/storage/netapp/test_netapp.py | 62 ----------------- 7 files changed, 125 insertions(+), 135 deletions(-) rename test/units/modules/cloud/{linode_v4 => linode}/test_linode_v4.py (100%) delete mode 100644 test/units/modules/cloud/linode_v4/__init__.py delete mode 100644 test/units/modules/cloud/linode_v4/conftest.py delete mode 100644 test/units/modules/storage/netapp/test_netapp.py diff --git a/test/sanity/ignore.txt b/test/sanity/ignore.txt index c839554d868..1781a1ecc08 100644 --- a/test/sanity/ignore.txt +++ b/test/sanity/ignore.txt @@ -8381,7 +8381,6 @@ test/units/module_utils/test_hetzner.py future-import-boilerplate test/units/module_utils/test_hetzner.py metaclass-boilerplate test/units/module_utils/test_kubevirt.py future-import-boilerplate test/units/module_utils/test_kubevirt.py metaclass-boilerplate -test/units/module_utils/test_netapp.py future-import-boilerplate test/units/module_utils/test_text.py future-import-boilerplate test/units/module_utils/test_utm_utils.py future-import-boilerplate test/units/module_utils/test_utm_utils.py metaclass-boilerplate @@ -8404,9 +8403,7 @@ test/units/modules/cloud/kubevirt/test_kubevirt_vm.py metaclass-boilerplate test/units/modules/cloud/linode/conftest.py future-import-boilerplate test/units/modules/cloud/linode/conftest.py metaclass-boilerplate test/units/modules/cloud/linode/test_linode.py metaclass-boilerplate -test/units/modules/cloud/linode_v4/conftest.py future-import-boilerplate -test/units/modules/cloud/linode_v4/conftest.py metaclass-boilerplate -test/units/modules/cloud/linode_v4/test_linode_v4.py metaclass-boilerplate +test/units/modules/cloud/linode/test_linode_v4.py metaclass-boilerplate test/units/modules/cloud/misc/test_terraform.py future-import-boilerplate test/units/modules/cloud/misc/test_terraform.py metaclass-boilerplate test/units/modules/cloud/misc/virt_net/conftest.py future-import-boilerplate @@ -8690,7 +8687,6 @@ test/units/modules/storage/netapp/test_na_ontap_vscan_on_demand_task.py future-i test/units/modules/storage/netapp/test_na_ontap_vscan_on_demand_task.py metaclass-boilerplate test/units/modules/storage/netapp/test_na_ontap_vscan_scanner_pool.py future-import-boilerplate test/units/modules/storage/netapp/test_na_ontap_vscan_scanner_pool.py metaclass-boilerplate -test/units/modules/storage/netapp/test_netapp.py metaclass-boilerplate test/units/modules/storage/netapp/test_netapp_e_alerts.py future-import-boilerplate test/units/modules/storage/netapp/test_netapp_e_asup.py future-import-boilerplate test/units/modules/storage/netapp/test_netapp_e_auditlog.py future-import-boilerplate diff --git a/test/units/module_utils/test_netapp.py b/test/units/module_utils/test_netapp.py index ead33093362..828eaf96b45 100644 --- a/test/units/module_utils/test_netapp.py +++ b/test/units/module_utils/test_netapp.py @@ -2,8 +2,15 @@ # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) +from __future__ import absolute_import, division, print_function __metaclass__ = type +import pytest + +from ansible.module_utils.ansible_release import __version__ as ansible_version + +import ansible.module_utils.netapp as netapp_utils + try: from unittest.mock import patch, mock_open except ImportError: @@ -104,3 +111,53 @@ class NetappTest(ModuleTestCase): with self.assertRaisesRegexp(AnsibleFailJson, r"Protocol must be http or https."): base = StubNetAppESeriesModule() base._check_web_services_version() + + +class MockONTAPConnection(object): + ''' mock a server connection to ONTAP host ''' + + def __init__(self, kind=None, parm1=None): + ''' save arguments ''' + self.type = kind + self.parm1 = parm1 + self.xml_in = None + self.xml_out = None + + def invoke_successfully(self, xml, enable_tunneling): # pylint: disable=unused-argument + ''' mock invoke_successfully returning xml data ''' + self.xml_in = xml + if self.type == 'vserver': + xml = self.build_vserver_info(self.parm1) + self.xml_out = xml + return xml + + @staticmethod + def build_vserver_info(vserver): + ''' build xml data for vserser-info ''' + xml = netapp_utils.zapi.NaElement('xml') + attributes = netapp_utils.zapi.NaElement('attributes-list') + attributes.add_node_with_children('vserver-info', + **{'vserver-name': vserver}) + xml.add_child_elem(attributes) + return xml + + +@pytest.mark.skipif(not netapp_utils.has_netapp_lib(), reason="skipping as missing required netapp_lib") +def test_ems_log_event_version(): + ''' validate Ansible version is correctly read ''' + source = 'unittest' + server = MockONTAPConnection() + netapp_utils.ems_log_event(source, server) + xml = server.xml_in + version = xml.get_child_content('app-version') + assert version == ansible_version + print("Ansible version: %s" % ansible_version) + + +@pytest.mark.skipif(not netapp_utils.has_netapp_lib(), reason="skipping as missing required netapp_lib") +def test_get_cserver(): + ''' validate cluster vserser name is correctly retrieved ''' + svm_name = 'svm1' + server = MockONTAPConnection('vserver', svm_name) + cserver = netapp_utils.get_cserver(server) + assert cserver == svm_name diff --git a/test/units/modules/cloud/linode/conftest.py b/test/units/modules/cloud/linode/conftest.py index a7400b82db8..9a7d7371f5a 100644 --- a/test/units/modules/cloud/linode/conftest.py +++ b/test/units/modules/cloud/linode/conftest.py @@ -11,3 +11,70 @@ def auth(monkeypatch): def patched_test_echo(dummy): return [] monkeypatch.setattr('linode.api.Api.test_echo', patched_test_echo) + + +@pytest.fixture +def access_token(monkeypatch): + monkeypatch.setenv('LINODE_ACCESS_TOKEN', 'barfoo') + + +@pytest.fixture +def no_access_token_in_env(monkeypatch): + try: + monkeypatch.delenv('LINODE_ACCESS_TOKEN') + except KeyError: + pass + + +@pytest.fixture +def default_args(): + return {'state': 'present', 'label': 'foo'} + + +@pytest.fixture +def mock_linode(): + class Linode(): + def delete(self, *args, **kwargs): + pass + + @property + def _raw_json(self): + return { + "alerts": { + "cpu": 90, + "io": 10000, + "network_in": 10, + "network_out": 10, + "transfer_quota": 80 + }, + "backups": { + "enabled": False, + "schedule": { + "day": None, + "window": None, + } + }, + "created": "2018-09-26T08:12:33", + "group": "Foobar Group", + "hypervisor": "kvm", + "id": 10480444, + "image": "linode/centos7", + "ipv4": [ + "130.132.285.233" + ], + "ipv6": "2a82:7e00::h03c:46ff:fe04:5cd2/64", + "label": "lin-foo", + "region": "eu-west", + "specs": { + "disk": 25600, + "memory": 1024, + "transfer": 1000, + "vcpus": 1 + }, + "status": "running", + "tags": [], + "type": "g6-nanode-1", + "updated": "2018-09-26T10:10:14", + "watchdog_enabled": True + } + return Linode() diff --git a/test/units/modules/cloud/linode_v4/test_linode_v4.py b/test/units/modules/cloud/linode/test_linode_v4.py similarity index 100% rename from test/units/modules/cloud/linode_v4/test_linode_v4.py rename to test/units/modules/cloud/linode/test_linode_v4.py diff --git a/test/units/modules/cloud/linode_v4/__init__.py b/test/units/modules/cloud/linode_v4/__init__.py deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/test/units/modules/cloud/linode_v4/conftest.py b/test/units/modules/cloud/linode_v4/conftest.py deleted file mode 100644 index 5dadafc5f72..00000000000 --- a/test/units/modules/cloud/linode_v4/conftest.py +++ /dev/null @@ -1,68 +0,0 @@ -import pytest - - -@pytest.fixture -def access_token(monkeypatch): - monkeypatch.setenv('LINODE_ACCESS_TOKEN', 'barfoo') - - -@pytest.fixture -def no_access_token_in_env(monkeypatch): - try: - monkeypatch.delenv('LINODE_ACCESS_TOKEN') - except KeyError: - pass - - -@pytest.fixture -def default_args(): - return {'state': 'present', 'label': 'foo'} - - -@pytest.fixture -def mock_linode(): - class Linode(): - def delete(self, *args, **kwargs): - pass - - @property - def _raw_json(self): - return { - "alerts": { - "cpu": 90, - "io": 10000, - "network_in": 10, - "network_out": 10, - "transfer_quota": 80 - }, - "backups": { - "enabled": False, - "schedule": { - "day": None, - "window": None, - } - }, - "created": "2018-09-26T08:12:33", - "group": "Foobar Group", - "hypervisor": "kvm", - "id": 10480444, - "image": "linode/centos7", - "ipv4": [ - "130.132.285.233" - ], - "ipv6": "2a82:7e00::h03c:46ff:fe04:5cd2/64", - "label": "lin-foo", - "region": "eu-west", - "specs": { - "disk": 25600, - "memory": 1024, - "transfer": 1000, - "vcpus": 1 - }, - "status": "running", - "tags": [], - "type": "g6-nanode-1", - "updated": "2018-09-26T10:10:14", - "watchdog_enabled": True - } - return Linode() diff --git a/test/units/modules/storage/netapp/test_netapp.py b/test/units/modules/storage/netapp/test_netapp.py deleted file mode 100644 index 028027bae52..00000000000 --- a/test/units/modules/storage/netapp/test_netapp.py +++ /dev/null @@ -1,62 +0,0 @@ -# Copyright (c) 2018 NetApp -# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) - -''' unit tests for module_utils netapp.py ''' -from __future__ import absolute_import, division, print_function - -import pytest - -from ansible.module_utils.ansible_release import __version__ as ansible_version - -import ansible.module_utils.netapp as netapp_utils - -if not netapp_utils.has_netapp_lib(): - pytestmark = pytest.mark.skip("skipping as missing required netapp_lib") - - -class MockONTAPConnection(object): - ''' mock a server connection to ONTAP host ''' - - def __init__(self, kind=None, parm1=None): - ''' save arguments ''' - self.type = kind - self.parm1 = parm1 - self.xml_in = None - self.xml_out = None - - def invoke_successfully(self, xml, enable_tunneling): # pylint: disable=unused-argument - ''' mock invoke_successfully returning xml data ''' - self.xml_in = xml - if self.type == 'vserver': - xml = self.build_vserver_info(self.parm1) - self.xml_out = xml - return xml - - @staticmethod - def build_vserver_info(vserver): - ''' build xml data for vserser-info ''' - xml = netapp_utils.zapi.NaElement('xml') - attributes = netapp_utils.zapi.NaElement('attributes-list') - attributes.add_node_with_children('vserver-info', - **{'vserver-name': vserver}) - xml.add_child_elem(attributes) - return xml - - -def test_ems_log_event_version(): - ''' validate Ansible version is correctly read ''' - source = 'unittest' - server = MockONTAPConnection() - netapp_utils.ems_log_event(source, server) - xml = server.xml_in - version = xml.get_child_content('app-version') - assert version == ansible_version - print("Ansible version: %s" % ansible_version) - - -def test_get_cserver(): - ''' validate cluster vserser name is correctly retrieved ''' - svm_name = 'svm1' - server = MockONTAPConnection('vserver', svm_name) - cserver = netapp_utils.get_cserver(server) - assert cserver == svm_name