diff options
Diffstat (limited to 'test/units/modules/packaging/os')
-rw-r--r-- | test/units/modules/packaging/os/test_apk.py | 31 | ||||
-rw-r--r-- | test/units/modules/packaging/os/test_redhat_subscription.py | 1221 | ||||
-rw-r--r-- | test/units/modules/packaging/os/test_rhn_channel.py | 141 | ||||
-rw-r--r-- | test/units/modules/packaging/os/test_rhn_register.py | 284 | ||||
-rw-r--r-- | test/units/modules/packaging/os/test_rhsm_release.py | 141 |
5 files changed, 0 insertions, 1818 deletions
diff --git a/test/units/modules/packaging/os/test_apk.py b/test/units/modules/packaging/os/test_apk.py deleted file mode 100644 index 96bae54532..0000000000 --- a/test/units/modules/packaging/os/test_apk.py +++ /dev/null @@ -1,31 +0,0 @@ -from units.compat import mock -from units.compat import unittest - -from ansible.modules.packaging.os import apk - - -class TestApkQueryLatest(unittest.TestCase): - - def setUp(self): - self.module_names = [ - 'bash', - 'g++', - ] - - @mock.patch('ansible.modules.packaging.os.apk.AnsibleModule') - def test_not_latest(self, mock_module): - apk.APK_PATH = "" - for module_name in self.module_names: - command_output = module_name + '-2.0.0-r1 < 3.0.0-r2 ' - mock_module.run_command.return_value = (0, command_output, None) - command_result = apk.query_latest(mock_module, module_name) - self.assertFalse(command_result) - - @mock.patch('ansible.modules.packaging.os.apk.AnsibleModule') - def test_latest(self, mock_module): - apk.APK_PATH = "" - for module_name in self.module_names: - command_output = module_name + '-2.0.0-r1 = 2.0.0-r1 ' - mock_module.run_command.return_value = (0, command_output, None) - command_result = apk.query_latest(mock_module, module_name) - self.assertTrue(command_result) diff --git a/test/units/modules/packaging/os/test_redhat_subscription.py b/test/units/modules/packaging/os/test_redhat_subscription.py deleted file mode 100644 index b2a11894fa..0000000000 --- a/test/units/modules/packaging/os/test_redhat_subscription.py +++ /dev/null @@ -1,1221 +0,0 @@ -# Author: Jiri Hnidek (jhnidek@redhat.com) -# -# 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 json - -from ansible.module_utils import basic -from ansible.modules.packaging.os import redhat_subscription - -import pytest - -TESTED_MODULE = redhat_subscription.__name__ - - -@pytest.fixture -def patch_redhat_subscription(mocker): - """ - Function used for mocking some parts of redhat_subscribtion module - """ - mocker.patch('ansible.modules.packaging.os.redhat_subscription.RegistrationBase.REDHAT_REPO') - mocker.patch('ansible.modules.packaging.os.redhat_subscription.isfile', return_value=False) - mocker.patch('ansible.modules.packaging.os.redhat_subscription.unlink', return_value=True) - mocker.patch('ansible.modules.packaging.os.redhat_subscription.AnsibleModule.get_bin_path', - return_value='/testbin/subscription-manager') - - -@pytest.mark.parametrize('patch_ansible_module', [{}], indirect=['patch_ansible_module']) -@pytest.mark.usefixtures('patch_ansible_module') -def test_without_required_parameters(capfd, patch_redhat_subscription): - """ - Failure must occurs when all parameters are missing - """ - with pytest.raises(SystemExit): - redhat_subscription.main() - out, err = capfd.readouterr() - results = json.loads(out) - assert results['failed'] - assert 'state is present but any of the following are missing' in results['msg'] - - -TEST_CASES = [ - # Test the case, when the system is already registered - [ - { - 'state': 'present', - 'server_hostname': 'subscription.rhsm.redhat.com', - 'username': 'admin', - 'password': 'admin', - 'org_id': 'admin' - }, - { - 'id': 'test_already_registered_system', - 'run_command.calls': [ - ( - # Calling of following command will be asserted - ['/testbin/subscription-manager', 'identity'], - # Was return code checked? - {'check_rc': False}, - # Mock of returned code, stdout and stderr - (0, 'system identity: b26df632-25ed-4452-8f89-0308bfd167cb', '') - ) - ], - 'changed': False, - 'msg': 'System already registered.' - } - ], - # Test simple registration using username and password - [ - { - 'state': 'present', - 'server_hostname': 'satellite.company.com', - 'username': 'admin', - 'password': 'admin', - }, - { - 'id': 'test_registeration_username_password', - 'run_command.calls': [ - ( - ['/testbin/subscription-manager', 'identity'], - {'check_rc': False}, - (1, '', '') - ), - ( - ['/testbin/subscription-manager', 'config', '--server.hostname=satellite.company.com'], - {'check_rc': True}, - (0, '', '') - ), - ( - ['/testbin/subscription-manager', 'register', - '--serverurl', 'satellite.company.com', - '--username', 'admin', - '--password', 'admin'], - {'check_rc': True, 'expand_user_and_vars': False}, - (0, '', '') - ) - ], - 'changed': True, - 'msg': "System successfully registered to 'satellite.company.com'." - } - ], - # Test unregistration, when system is unregistered - [ - { - 'state': 'absent', - 'server_hostname': 'subscription.rhsm.redhat.com', - 'username': 'admin', - 'password': 'admin', - }, - { - 'id': 'test_unregisteration', - 'run_command.calls': [ - ( - ['/testbin/subscription-manager', 'identity'], - {'check_rc': False}, - (0, 'system identity: b26df632-25ed-4452-8f89-0308bfd167cb', '') - ), - ( - ['/testbin/subscription-manager', 'unsubscribe', '--all'], - {'check_rc': True}, - (0, '', '') - ), - ( - ['/testbin/subscription-manager', 'unregister'], - {'check_rc': True}, - (0, '', '') - ) - ], - 'changed': True, - 'msg': "System successfully unregistered from subscription.rhsm.redhat.com." - } - ], - # Test unregistration of already unregistered system - [ - { - 'state': 'absent', - 'server_hostname': 'subscription.rhsm.redhat.com', - 'username': 'admin', - 'password': 'admin', - }, - { - 'id': 'test_unregisteration_of_unregistered_system', - 'run_command.calls': [ - ( - ['/testbin/subscription-manager', 'identity'], - {'check_rc': False}, - (1, 'This system is not yet registered.', '') - ) - ], - 'changed': False, - 'msg': "System already unregistered." - } - ], - # Test registration using activation key - [ - { - 'state': 'present', - 'server_hostname': 'satellite.company.com', - 'activationkey': 'some-activation-key', - 'org_id': 'admin' - }, - { - 'id': 'test_registeration_activation_key', - 'run_command.calls': [ - ( - ['/testbin/subscription-manager', 'identity'], - {'check_rc': False}, - (1, 'This system is not yet registered.', '') - ), - ( - ['/testbin/subscription-manager', 'config', '--server.hostname=satellite.company.com'], - {'check_rc': True}, - (0, '', '') - ), - ( - [ - '/testbin/subscription-manager', - 'register', - '--serverurl', 'satellite.company.com', - '--org', 'admin', - '--activationkey', 'some-activation-key' - ], - {'check_rc': True, 'expand_user_and_vars': False}, - (0, '', '') - ) - ], - 'changed': True, - 'msg': "System successfully registered to 'satellite.company.com'." - } - ], - # Test of registration using username and password with auto-attach option - [ - { - 'state': 'present', - 'username': 'admin', - 'password': 'admin', - 'org_id': 'admin', - 'auto_attach': 'true' - }, - { - 'id': 'test_registeration_username_password_auto_attach', - 'run_command.calls': [ - ( - ['/testbin/subscription-manager', 'identity'], - {'check_rc': False}, - (1, 'This system is not yet registered.', '') - ), - ( - [ - '/testbin/subscription-manager', - 'register', - '--org', 'admin', - '--auto-attach', - '--username', 'admin', - '--password', 'admin' - ], - {'check_rc': True, 'expand_user_and_vars': False}, - (0, '', '') - ) - ], - 'changed': True, - 'msg': "System successfully registered to 'None'." - } - ], - # Test of force registration despite the system is already registered - [ - { - 'state': 'present', - 'username': 'admin', - 'password': 'admin', - 'org_id': 'admin', - 'force_register': 'true' - }, - { - 'id': 'test_force_registeration_username_password', - 'run_command.calls': [ - ( - ['/testbin/subscription-manager', 'identity'], - {'check_rc': False}, - (0, 'This system already registered.', '') - ), - ( - [ - '/testbin/subscription-manager', - 'register', - '--force', - '--org', 'admin', - '--username', 'admin', - '--password', 'admin' - ], - {'check_rc': True, 'expand_user_and_vars': False}, - (0, '', '') - ) - ], - 'changed': True, - 'msg': "System successfully registered to 'None'." - } - ], - # Test of registration using username, password and proxy options - [ - { - 'state': 'present', - 'username': 'admin', - 'password': 'admin', - 'org_id': 'admin', - 'force_register': 'true', - 'server_proxy_hostname': 'proxy.company.com', - 'server_proxy_port': '12345', - 'server_proxy_user': 'proxy_user', - 'server_proxy_password': 'secret_proxy_password' - }, - { - 'id': 'test_registeration_username_password_proxy_options', - 'run_command.calls': [ - ( - ['/testbin/subscription-manager', 'identity'], - {'check_rc': False}, - (0, 'This system already registered.', '') - ), - ( - [ - '/testbin/subscription-manager', - 'config', - '--server.proxy_hostname=proxy.company.com', - '--server.proxy_password=secret_proxy_password', - '--server.proxy_port=12345', - '--server.proxy_user=proxy_user' - ], - {'check_rc': True}, - (0, '', '') - ), - ( - [ - '/testbin/subscription-manager', - 'register', - '--force', - '--org', 'admin', - '--proxy', 'proxy.company.com:12345', - '--proxyuser', 'proxy_user', - '--proxypassword', 'secret_proxy_password', - '--username', 'admin', - '--password', 'admin' - ], - {'check_rc': True, 'expand_user_and_vars': False}, - (0, '', '') - ) - ], - 'changed': True, - 'msg': "System successfully registered to 'None'." - } - ], - # Test of registration using username and password and attach to pool - [ - { - 'state': 'present', - 'username': 'admin', - 'password': 'admin', - 'org_id': 'admin', - 'pool': 'ff8080816b8e967f016b8e99632804a6' - }, - { - 'id': 'test_registeration_username_password_pool', - 'run_command.calls': [ - ( - ['/testbin/subscription-manager', 'identity'], - {'check_rc': False}, - (1, 'This system is not yet registered.', '') - ), - ( - [ - '/testbin/subscription-manager', - 'register', - '--org', 'admin', - '--username', 'admin', - '--password', 'admin' - ], - {'check_rc': True, 'expand_user_and_vars': False}, - (0, '', '') - ), - ( - [ - 'subscription-manager list --available', - {'check_rc': True, 'environ_update': {'LANG': 'C', 'LC_ALL': 'C', 'LC_MESSAGES': 'C'}}, - (0, - ''' -+-------------------------------------------+ - Available Subscriptions -+-------------------------------------------+ -Subscription Name: SP Server Premium (S: Premium, U: Production, R: SP Server) -Provides: SP Server Bits -SKU: sp-server-prem-prod -Contract: 0 -Pool ID: ff8080816b8e967f016b8e99632804a6 -Provides Management: Yes -Available: 5 -Suggested: 1 -Service Type: L1-L3 -Roles: SP Server -Service Level: Premium -Usage: Production -Add-ons: -Subscription Type: Standard -Starts: 06/25/19 -Ends: 06/24/20 -Entitlement Type: Physical -''', ''), - ] - ), - ( - 'subscription-manager attach --pool ff8080816b8e967f016b8e99632804a6', - {'check_rc': True}, - (0, '', '') - ) - ], - 'changed': True, - 'msg': "System successfully registered to 'None'." - } - ], - # Test of registration using username and password and attach to pool ID and quantities - [ - { - 'state': 'present', - 'username': 'admin', - 'password': 'admin', - 'org_id': 'admin', - 'pool_ids': [{'ff8080816b8e967f016b8e99632804a6': 2}, {'ff8080816b8e967f016b8e99747107e9': 4}] - }, - { - 'id': 'test_registeration_username_password_pool_ids_quantities', - 'run_command.calls': [ - ( - ['/testbin/subscription-manager', 'identity'], - {'check_rc': False}, - (1, 'This system is not yet registered.', '') - ), - ( - [ - '/testbin/subscription-manager', - 'register', - '--org', 'admin', - '--username', 'admin', - '--password', 'admin' - ], - {'check_rc': True, 'expand_user_and_vars': False}, - (0, '', '') - ), - ( - [ - 'subscription-manager list --available', - {'check_rc': True, 'environ_update': {'LANG': 'C', 'LC_ALL': 'C', 'LC_MESSAGES': 'C'}}, - (0, - ''' -+-------------------------------------------+ - Available Subscriptions -+-------------------------------------------+ -Subscription Name: SP Smart Management (A: ADDON1) -Provides: SP Addon 1 bits -SKU: sp-with-addon-1 -Contract: 1 -Pool ID: ff8080816b8e967f016b8e99747107e9 -Provides Management: Yes -Available: 10 -Suggested: 1 -Service Type: -Roles: -Service Level: -Usage: -Add-ons: ADDON1 -Subscription Type: Standard -Starts: 25.6.2019 -Ends: 24.6.2020 -Entitlement Type: Physical - -Subscription Name: SP Server Premium (S: Premium, U: Production, R: SP Server) -Provides: SP Server Bits -SKU: sp-server-prem-prod -Contract: 0 -Pool ID: ff8080816b8e967f016b8e99632804a6 -Provides Management: Yes -Available: 5 -Suggested: 1 -Service Type: L1-L3 -Roles: SP Server -Service Level: Premium -Usage: Production -Add-ons: -Subscription Type: Standard -Starts: 06/25/19 -Ends: 06/24/20 -Entitlement Type: Physical -''', '') - ] - ), - ( - [ - '/testbin/subscription-manager', - 'attach', - '--pool', 'ff8080816b8e967f016b8e99632804a6', - '--quantity', '2' - ], - {'check_rc': True}, - (0, '', '') - ), - ( - [ - '/testbin/subscription-manager', - 'attach', - '--pool', 'ff8080816b8e967f016b8e99747107e9', - '--quantity', '4' - ], - {'check_rc': True}, - (0, '', '') - ) - ], - 'changed': True, - 'msg': "System successfully registered to 'None'." - } - ], - # Test of registration using username and password and attach to pool ID without quantities - [ - { - 'state': 'present', - 'username': 'admin', - 'password': 'admin', - 'org_id': 'admin', - 'pool_ids': ['ff8080816b8e967f016b8e99632804a6', 'ff8080816b8e967f016b8e99747107e9'] - }, - { - 'id': 'test_registeration_username_password_pool_ids', - 'run_command.calls': [ - ( - ['/testbin/subscription-manager', 'identity'], - {'check_rc': False}, - (1, 'This system is not yet registered.', '') - ), - ( - [ - '/testbin/subscription-manager', - 'register', - '--org', 'admin', - '--username', 'admin', - '--password', 'admin' - ], - {'check_rc': True, 'expand_user_and_vars': False}, - (0, '', '') - ), - ( - [ - 'subscription-manager list --available', - {'check_rc': True, 'environ_update': {'LANG': 'C', 'LC_ALL': 'C', 'LC_MESSAGES': 'C'}}, - (0, - ''' -+-------------------------------------------+ - Available Subscriptions -+-------------------------------------------+ -Subscription Name: SP Smart Management (A: ADDON1) -Provides: SP Addon 1 bits -SKU: sp-with-addon-1 -Contract: 1 -Pool ID: ff8080816b8e967f016b8e99747107e9 -Provides Management: Yes -Available: 10 -Suggested: 1 -Service Type: -Roles: -Service Level: -Usage: -Add-ons: ADDON1 -Subscription Type: Standard -Starts: 25.6.2019 -Ends: 24.6.2020 -Entitlement Type: Physical - -Subscription Name: SP Server Premium (S: Premium, U: Production, R: SP Server) -Provides: SP Server Bits -SKU: sp-server-prem-prod -Contract: 0 -Pool ID: ff8080816b8e967f016b8e99632804a6 -Provides Management: Yes -Available: 5 -Suggested: 1 -Service Type: L1-L3 -Roles: SP Server -Service Level: Premium -Usage: Production -Add-ons: -Subscription Type: Standard -Starts: 06/25/19 -Ends: 06/24/20 -Entitlement Type: Physical -''', '') - ] - ), - ( - [ - '/testbin/subscription-manager', - 'attach', - '--pool', 'ff8080816b8e967f016b8e99632804a6' - ], - {'check_rc': True}, - (0, '', '') - ), - ( - [ - '/testbin/subscription-manager', - 'attach', - '--pool', 'ff8080816b8e967f016b8e99747107e9' - ], - {'check_rc': True}, - (0, '', '') - ) - ], - 'changed': True, - 'msg': "System successfully registered to 'None'." - } - ], - # Test of registration using username and password and attach to pool ID (one pool) - [ - { - 'state': 'present', - 'username': 'admin', - 'password': 'admin', - 'org_id': 'admin', - 'pool_ids': ['ff8080816b8e967f016b8e99632804a6'] - }, - { - 'id': 'test_registeration_username_password_one_pool_id', - 'run_command.calls': [ - ( - ['/testbin/subscription-manager', 'identity'], - {'check_rc': False}, - (1, 'This system is not yet registered.', '') - ), - ( - [ - '/testbin/subscription-manager', - 'register', - '--org', 'admin', - '--username', 'admin', - '--password', 'admin' - ], - {'check_rc': True, 'expand_user_and_vars': False}, - (0, '', '') - ), - ( - [ - 'subscription-manager list --available', - {'check_rc': True, 'environ_update': {'LANG': 'C', 'LC_ALL': 'C', 'LC_MESSAGES': 'C'}}, - (0, - ''' -+-------------------------------------------+ - Available Subscriptions -+-------------------------------------------+ -Subscription Name: SP Smart Management (A: ADDON1) -Provides: SP Addon 1 bits -SKU: sp-with-addon-1 -Contract: 1 -Pool ID: ff8080816b8e967f016b8e99747107e9 -Provides Management: Yes -Available: 10 -Suggested: 1 -Service Type: -Roles: -Service Level: -Usage: -Add-ons: ADDON1 -Subscription Type: Standard -Starts: 25.6.2019 -Ends: 24.6.2020 -Entitlement Type: Physical - -Subscription Name: SP Server Premium (S: Premium, U: Production, R: SP Server) -Provides: SP Server Bits -SKU: sp-server-prem-prod -Contract: 0 -Pool ID: ff8080816b8e967f016b8e99632804a6 -Provides Management: Yes -Available: 5 -Suggested: 1 -Service Type: L1-L3 -Roles: SP Server -Service Level: Premium -Usage: Production -Add-ons: -Subscription Type: Standard -Starts: 06/25/19 -Ends: 06/24/20 -Entitlement Type: Physical -''', '') - ] - ), - ( - [ - '/testbin/subscription-manager', - 'attach', - '--pool', 'ff8080816b8e967f016b8e99632804a6', - ], - {'check_rc': True}, - (0, '', '') - ) - ], - 'changed': True, - 'msg': "System successfully registered to 'None'." - } - ], - # Test attaching different set of pool IDs - [ - { - 'state': 'present', - 'username': 'admin', - 'password': 'admin', - 'org_id': 'admin', - 'pool_ids': [{'ff8080816b8e967f016b8e99632804a6': 2}, {'ff8080816b8e967f016b8e99747107e9': 4}] - }, - { - 'id': 'test_attaching_different_pool_ids', - 'run_command.calls': [ - ( - ['/testbin/subscription-manager', 'identity'], - {'check_rc': False}, - (0, 'system identity: b26df632-25ed-4452-8f89-0308bfd167cb', ''), - ), - ( - 'subscription-manager list --consumed', - {'check_rc': True, 'environ_update': {'LANG': 'C', 'LC_ALL': 'C', 'LC_MESSAGES': 'C'}}, - (0, ''' -+-------------------------------------------+ - Consumed Subscriptions -+-------------------------------------------+ -Subscription Name: Multi-Attribute Stackable (4 cores, no content) -Provides: Multi-Attribute Limited Product (no content) -SKU: cores4-multiattr -Contract: 1 -Account: 12331131231 -Serial: 7807912223970164816 -Pool ID: ff8080816b8e967f016b8e995f5103b5 -Provides Management: No -Active: True -Quantity Used: 1 -Service Type: Level 3 -Roles: -Service Level: Premium -Usage: -Add-ons: -Status Details: Subscription is current -Subscription Type: Stackable -Starts: 06/25/19 -Ends: 06/24/20 -Entitlement Type: Physical -''', '') - ), - ( - [ - '/testbin/subscription-manager', - 'unsubscribe', - '--serial=7807912223970164816', - ], - {'check_rc': True}, - (0, '', '') - ), - ( - [ - 'subscription-manager list --available', - {'check_rc': True, 'environ_update': {'LANG': 'C', 'LC_ALL': 'C', 'LC_MESSAGES': 'C'}}, - (0, - ''' -+-------------------------------------------+ - Available Subscriptions -+-------------------------------------------+ -Subscription Name: SP Smart Management (A: ADDON1) -Provides: SP Addon 1 bits -SKU: sp-with-addon-1 -Contract: 1 -Pool ID: ff8080816b8e967f016b8e99747107e9 -Provides Management: Yes -Available: 10 -Suggested: 1 -Service Type: -Roles: -Service Level: -Usage: -Add-ons: ADDON1 -Subscription Type: Standard -Starts: 25.6.2019 -Ends: 24.6.2020 -Entitlement Type: Physical - -Subscription Name: SP Server Premium (S: Premium, U: Production, R: SP Server) -Provides: SP Server Bits -SKU: sp-server-prem-prod -Contract: 0 -Pool ID: ff8080816b8e967f016b8e99632804a6 -Provides Management: Yes -Available: 5 -Suggested: 1 -Service Type: L1-L3 -Roles: SP Server -Service Level: Premium -Usage: Production -Add-ons: -Subscription Type: Standard -Starts: 06/25/19 -Ends: 06/24/20 -Entitlement Type: Physical - -Subscription Name: Multi-Attribute Stackable (4 cores, no content) -Provides: Multi-Attribute Limited Product (no content) -SKU: cores4-multiattr -Contract: 1 -Pool ID: ff8080816b8e967f016b8e995f5103b5 -Provides Management: No -Available: 10 -Suggested: 1 -Service Type: Level 3 -Roles: -Service Level: Premium -Usage: -Add-ons: -Subscription Type: Stackable -Starts: 11.7.2019 -Ends: 10.7.2020 -Entitlement Type: Physical -''', '') - ] - ), - ( - [ - '/testbin/subscription-manager', - 'attach', - '--pool', 'ff8080816b8e967f016b8e99632804a6', - '--quantity', '2' - ], - {'check_rc': True}, - (0, '', '') - ), - ( - [ - '/testbin/subscription-manager', - 'attach', - '--pool', 'ff8080816b8e967f016b8e99747107e9', - '--quantity', '4' - ], - {'check_rc': True}, - (0, '', '') - ) - ], - 'changed': True, - } - ] -] - - -TEST_CASES_IDS = [item[1]['id'] for item in TEST_CASES] - - -@pytest.mark.parametrize('patch_ansible_module, testcase', TEST_CASES, ids=TEST_CASES_IDS, indirect=['patch_ansible_module']) -@pytest.mark.usefixtures('patch_ansible_module') -def test_redhat_subscribtion(mocker, capfd, patch_redhat_subscription, testcase): - """ - Run unit tests for test cases listen in TEST_CASES - """ - - # Mock function used for running commands first - call_results = [item[2] for item in testcase['run_command.calls']] - mock_run_command = mocker.patch.object( - basic.AnsibleModule, - 'run_command', - side_effect=call_results) - - # Try to run test case - with pytest.raises(SystemExit): - redhat_subscription.main() - - out, err = capfd.readouterr() - results = json.loads(out) - - assert 'changed' in results - assert results['changed'] == testcase['changed'] - if 'msg' in results: - assert results['msg'] == testcase['msg'] - - assert basic.AnsibleModule.run_command.call_count == len(testcase['run_command.calls']) - if basic.AnsibleModule.run_command.call_count: - call_args_list = [(item[0][0], item[1]) for item in basic.AnsibleModule.run_command.call_args_list] - expected_call_args_list = [(item[0], item[1]) for item in testcase['run_command.calls']] - assert call_args_list == expected_call_args_list - - -SYSPURPOSE_TEST_CASES = [ - # Test setting syspurpose attributes (system is already registered) - # and synchronization with candlepin server - [ - { - 'state': 'present', - 'server_hostname': 'subscription.rhsm.redhat.com', - 'username': 'admin', - 'password': 'admin', - 'org_id': 'admin', - 'syspurpose': { - 'role': 'AwesomeOS', - 'usage': 'Production', - 'service_level_agreement': 'Premium', - 'addons': ['ADDON1', 'ADDON2'], - 'sync': True - } - }, - { - 'id': 'test_setting_syspurpose_attributes', - 'existing_syspurpose': {}, - 'expected_syspurpose': { - 'role': 'AwesomeOS', - 'usage': 'Production', - 'service_level_agreement': 'Premium', - 'addons': ['ADDON1', 'ADDON2'], - }, - 'run_command.calls': [ - ( - ['/testbin/subscription-manager', 'identity'], - {'check_rc': False}, - (0, 'system identity: b26df632-25ed-4452-8f89-0308bfd167cb', '') - ), - ( - ['/testbin/subscription-manager', 'status'], - {'check_rc': False}, - (0, ''' -+-------------------------------------------+ - System Status Details -+-------------------------------------------+ -Overall Status: Current - -System Purpose Status: Matched -''', '') - ) - ], - 'changed': True, - 'msg': 'Syspurpose attributes changed.' - } - ], - # Test setting unspupported attributes - [ - { - 'state': 'present', - 'server_hostname': 'subscription.rhsm.redhat.com', - 'username': 'admin', - 'password': 'admin', - 'org_id': 'admin', - 'syspurpose': { - 'foo': 'Bar', - 'role': 'AwesomeOS', - 'usage': 'Production', - 'service_level_agreement': 'Premium', - 'addons': ['ADDON1', 'ADDON2'], - 'sync': True - } - }, - { - 'id': 'test_setting_syspurpose_wrong_attributes', - 'existing_syspurpose': {}, - 'expected_syspurpose': {}, - 'run_command.calls': [], - 'failed': True - } - ], - # Test setting addons not a list - [ - { - 'state': 'present', - 'server_hostname': 'subscription.rhsm.redhat.com', - 'username': 'admin', - 'password': 'admin', - 'org_id': 'admin', - 'syspurpose': { - 'role': 'AwesomeOS', - 'usage': 'Production', - 'service_level_agreement': 'Premium', - 'addons': 'ADDON1', - 'sync': True - } - }, - { - 'id': 'test_setting_syspurpose_addons_not_list', - 'existing_syspurpose': {}, - 'expected_syspurpose': { - 'role': 'AwesomeOS', - 'usage': 'Production', - 'service_level_agreement': 'Premium', - 'addons': ['ADDON1'] - }, - 'run_command.calls': [ - ( - ['/testbin/subscription-manager', 'identity'], - {'check_rc': False}, - (0, 'system identity: b26df632-25ed-4452-8f89-0308bfd167cb', '') - ), - ( - ['/testbin/subscription-manager', 'status'], - {'check_rc': False}, - (0, ''' -+-------------------------------------------+ - System Status Details -+-------------------------------------------+ -Overall Status: Current - -System Purpose Status: Matched -''', '') - ) - ], - 'changed': True, - 'msg': 'Syspurpose attributes changed.' - } - ], - # Test setting syspurpose attributes (system is already registered) - # without synchronization with candlepin server. Some syspurpose attributes were set - # in the past - [ - { - 'state': 'present', - 'server_hostname': 'subscription.rhsm.redhat.com', - 'username': 'admin', - 'password': 'admin', - 'org_id': 'admin', - 'syspurpose': { - 'role': 'AwesomeOS', - 'service_level_agreement': 'Premium', - 'addons': ['ADDON1', 'ADDON2'], - 'sync': False - } - }, - { - 'id': 'test_changing_syspurpose_attributes', - 'existing_syspurpose': { - 'role': 'CoolOS', - 'usage': 'Production', - 'service_level_agreement': 'Super', - 'addons': [], - 'foo': 'bar' - }, - 'expected_syspurpose': { - 'role': 'AwesomeOS', - 'service_level_agreement': 'Premium', - 'addons': ['ADDON1', 'ADDON2'], - 'foo': 'bar' - }, - 'run_command.calls': [ - ( - ['/testbin/subscription-manager', 'identity'], - {'check_rc': False}, - (0, 'system identity: b26df632-25ed-4452-8f89-0308bfd167cb', '') - ), - ], - 'changed': True, - 'msg': 'Syspurpose attributes changed.' - } - ], - # Test trying to set syspurpose attributes (system is already registered) - # without synchronization with candlepin server. Some syspurpose attributes were set - # in the past. Syspurpose attributes are same as before - [ - { - 'state': 'present', - 'server_hostname': 'subscription.rhsm.redhat.com', - 'username': 'admin', - 'password': 'admin', - 'org_id': 'admin', - 'syspurpose': { - 'role': 'AwesomeOS', - 'service_level_agreement': 'Premium', - 'addons': ['ADDON1', 'ADDON2'], - 'sync': False - } - }, - { - 'id': 'test_not_changing_syspurpose_attributes', - 'existing_syspurpose': { - 'role': 'AwesomeOS', - 'service_level_agreement': 'Premium', - 'addons': ['ADDON1', 'ADDON2'], - }, - 'expected_syspurpose': { - 'role': 'AwesomeOS', - 'service_level_agreement': 'Premium', - 'addons': ['ADDON1', 'ADDON2'], - }, - 'run_command.calls': [ - ( - ['/testbin/subscription-manager', 'identity'], - {'check_rc': False}, - (0, 'system identity: b26df632-25ed-4452-8f89-0308bfd167cb', '') - ), - ], - 'changed': False, - 'msg': 'System already registered.' - } - ], - # Test of registration using username and password with auto-attach option, when - # syspurpose attributes are set - [ - { - 'state': 'present', - 'username': 'admin', - 'password': 'admin', - 'org_id': 'admin', - 'auto_attach': 'true', - 'syspurpose': { - 'role': 'AwesomeOS', - 'usage': 'Testing', - 'service_level_agreement': 'Super', - 'addons': ['ADDON1'], - 'sync': False - }, - }, - { - 'id': 'test_registeration_username_password_auto_attach_syspurpose', - 'existing_syspurpose': None, - 'expected_syspurpose': { - 'role': 'AwesomeOS', - 'usage': 'Testing', - 'service_level_agreement': 'Super', - 'addons': ['ADDON1'], - }, - 'run_command.calls': [ - ( - ['/testbin/subscription-manager', 'identity'], - {'check_rc': False}, - (1, 'This system is not yet registered.', '') - ), - ( - [ - '/testbin/subscription-manager', - 'register', - '--org', 'admin', - '--auto-attach', - '--username', 'admin', - '--password', 'admin' - ], - {'check_rc': True, 'expand_user_and_vars': False}, - (0, '', '') - ) - ], - 'changed': True, - 'msg': "System successfully registered to 'None'." - } - ], - # Test of registration using username and password with auto-attach option, when - # syspurpose attributes are set. Syspurpose attributes are also synchronized - # in this case - [ - { - 'state': 'present', - 'username': 'admin', - 'password': 'admin', - 'org_id': 'admin', - 'auto_attach': 'true', - 'syspurpose': { - 'role': 'AwesomeOS', - 'usage': 'Testing', - 'service_level_agreement': 'Super', - 'addons': ['ADDON1'], - 'sync': True - }, - }, - { - 'id': 'test_registeration_username_password_auto_attach_syspurpose_sync', - 'existing_syspurpose': None, - 'expected_syspurpose': { - 'role': 'AwesomeOS', - 'usage': 'Testing', - 'service_level_agreement': 'Super', - 'addons': ['ADDON1'], - }, - 'run_command.calls': [ - ( - ['/testbin/subscription-manager', 'identity'], - {'check_rc': False}, - (1, 'This system is not yet registered.', '') - ), - ( - [ - '/testbin/subscription-manager', - 'register', - '--org', 'admin', - '--auto-attach', - '--username', 'admin', - '--password', 'admin' - ], - {'check_rc': True, 'expand_user_and_vars': False}, - (0, '', '') - ), - ( - ['/testbin/subscription-manager', 'status'], - {'check_rc': False}, - (0, ''' -+-------------------------------------------+ - System Status Details -+-------------------------------------------+ -Overall Status: Current - -System Purpose Status: Matched -''', '') - ) - ], - 'changed': True, - 'msg': "System successfully registered to 'None'." - } - ], -] - - -SYSPURPOSE_TEST_CASES_IDS = [item[1]['id'] for item in SYSPURPOSE_TEST_CASES] - - -@pytest.mark.parametrize('patch_ansible_module, testcase', SYSPURPOSE_TEST_CASES, ids=SYSPURPOSE_TEST_CASES_IDS, indirect=['patch_ansible_module']) -@pytest.mark.usefixtures('patch_ansible_module') -def test_redhat_subscribtion_syspurpose(mocker, capfd, patch_redhat_subscription, patch_ansible_module, testcase, tmpdir): - """ - Run unit tests for test cases listen in SYSPURPOSE_TEST_CASES (syspurpose specific cases) - """ - - # Mock function used for running commands first - call_results = [item[2] for item in testcase['run_command.calls']] - mock_run_command = mocker.patch.object( - basic.AnsibleModule, - 'run_command', - side_effect=call_results) - - mock_syspurpose_file = tmpdir.mkdir("syspurpose").join("syspurpose.json") - # When there there are some existing syspurpose attributes specified, then - # write them to the file first - if testcase['existing_syspurpose'] is not None: - mock_syspurpose_file.write(json.dumps(testcase['existing_syspurpose'])) - else: - mock_syspurpose_file.write("{}") - - redhat_subscription.SysPurpose.SYSPURPOSE_FILE_PATH = str(mock_syspurpose_file) - - # Try to run test case - with pytest.raises(SystemExit): - redhat_subscription.main() - - out, err = capfd.readouterr() - results = json.loads(out) - - if 'failed' in testcase: - assert results['failed'] == testcase['failed'] - else: - assert 'changed' in results - assert results['changed'] == testcase['changed'] - if 'msg' in results: - assert results['msg'] == testcase['msg'] - - mock_file_content = mock_syspurpose_file.read_text("utf-8") - current_syspurpose = json.loads(mock_file_content) - assert current_syspurpose == testcase['expected_syspurpose'] - - assert basic.AnsibleModule.run_command.call_count == len(testcase['run_command.calls']) - if basic.AnsibleModule.run_command.call_count: - call_args_list = [(item[0][0], item[1]) for item in basic.AnsibleModule.run_command.call_args_list] - expected_call_args_list = [(item[0], item[1]) for item in testcase['run_command.calls']] - assert call_args_list == expected_call_args_list diff --git a/test/units/modules/packaging/os/test_rhn_channel.py b/test/units/modules/packaging/os/test_rhn_channel.py deleted file mode 100644 index 28ca99fa3a..0000000000 --- a/test/units/modules/packaging/os/test_rhn_channel.py +++ /dev/null @@ -1,141 +0,0 @@ -# -*- coding: utf-8 -*- -# Copyright (c) 2017 Pierre-Louis Bonicoli <pierre-louis@libregerbil.fr> -# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) - -import json - -from ansible.modules.packaging.os import rhn_channel - -import pytest - - -pytestmark = pytest.mark.usefixtures('patch_ansible_module') - - -@pytest.mark.parametrize('patch_ansible_module', [{}], indirect=['patch_ansible_module']) -def test_without_required_parameters(capfd): - with pytest.raises(SystemExit): - rhn_channel.main() - out, err = capfd.readouterr() - results = json.loads(out) - assert results['failed'] - assert 'missing required arguments' in results['msg'] - - -TESTED_MODULE = rhn_channel.__name__ -TEST_CASES = [ - [ - # add channel already added, check that result isn't changed - { - 'name': 'rhel-x86_64-server-6', - 'sysname': 'server01', - 'url': 'https://rhn.redhat.com/rpc/api', - 'user': 'user', - 'password': 'pass', - }, - { - 'calls': [ - ('auth.login', ['X' * 43]), - ('system.listUserSystems', - [[{'last_checkin': '2017-08-06 19:49:52.0', 'id': '0123456789', 'name': 'server01'}]]), - ('channel.software.listSystemChannels', - [[{'channel_name': 'Red Hat Enterprise Linux Server (v. 6 for 64-bit x86_64)', 'channel_label': 'rhel-x86_64-server-6'}]]), - ('auth.logout', [1]), - ], - 'changed': False, - 'msg': 'Channel rhel-x86_64-server-6 already exists', - } - ], - [ - # add channel, check that result is changed - { - 'name': 'rhel-x86_64-server-6-debuginfo', - 'sysname': 'server01', - 'url': 'https://rhn.redhat.com/rpc/api', - 'user': 'user', - 'password': 'pass', - }, - { - 'calls': [ - ('auth.login', ['X' * 43]), - ('system.listUserSystems', - [[{'last_checkin': '2017-08-06 19:49:52.0', 'id': '0123456789', 'name': 'server01'}]]), - ('channel.software.listSystemChannels', - [[{'channel_name': 'Red Hat Enterprise Linux Server (v. 6 for 64-bit x86_64)', 'channel_label': 'rhel-x86_64-server-6'}]]), - ('channel.software.listSystemChannels', - [[{'channel_name': 'Red Hat Enterprise Linux Server (v. 6 for 64-bit x86_64)', 'channel_label': 'rhel-x86_64-server-6'}]]), - ('system.setChildChannels', [1]), - ('auth.logout', [1]), - ], - 'changed': True, - 'msg': 'Channel rhel-x86_64-server-6-debuginfo added', - } - ], - [ - # remove inexistent channel, check that result isn't changed - { - 'name': 'rhel-x86_64-server-6-debuginfo', - 'state': 'absent', - 'sysname': 'server01', - 'url': 'https://rhn.redhat.com/rpc/api', - 'user': 'user', - 'password': 'pass', - }, - { - 'calls': [ - ('auth.login', ['X' * 43]), - ('system.listUserSystems', - [[{'last_checkin': '2017-08-06 19:49:52.0', 'id': '0123456789', 'name': 'server01'}]]), - ('channel.software.listSystemChannels', - [[{'channel_name': 'Red Hat Enterprise Linux Server (v. 6 for 64-bit x86_64)', 'channel_label': 'rhel-x86_64-server-6'}]]), - ('auth.logout', [1]), - ], - 'changed': False, - 'msg': 'Not subscribed to channel rhel-x86_64-server-6-debuginfo.', - } - ], - [ - # remove channel, check that result is changed - { - 'name': 'rhel-x86_64-server-6-debuginfo', - 'state': 'absent', - 'sysname': 'server01', - 'url': 'https://rhn.redhat.com/rpc/api', - 'user': 'user', - 'password': 'pass', - }, - { - 'calls': [ - ('auth.login', ['X' * 43]), - ('system.listUserSystems', - [[{'last_checkin': '2017-08-06 19:49:52.0', 'id': '0123456789', 'name': 'server01'}]]), - ('channel.software.listSystemChannels', [[ - {'channel_name': 'RHEL Server Debuginfo (v.6 for x86_64)', 'channel_label': 'rhel-x86_64-server-6-debuginfo'}, - {'channel_name': 'Red Hat Enterprise Linux Server (v. 6 for 64-bit x86_64)', 'channel_label': 'rhel-x86_64-server-6'} - ]]), - ('channel.software.listSystemChannels', [[ - {'channel_name': 'RHEL Server Debuginfo (v.6 for x86_64)', 'channel_label': 'rhel-x86_64-server-6-debuginfo'}, - {'channel_name': 'Red Hat Enterprise Linux Server (v. 6 for 64-bit x86_64)', 'channel_label': 'rhel-x86_64-server-6'} - ]]), - ('system.setChildChannels', [1]), - ('auth.logout', [1]), - ], - 'changed': True, - 'msg': 'Channel rhel-x86_64-server-6-debuginfo removed' - } - ] -] - - -@pytest.mark.parametrize('patch_ansible_module, testcase', TEST_CASES, indirect=['patch_ansible_module']) -def test_rhn_channel(capfd, mocker, testcase, mock_request): - """Check 'msg' and 'changed' results""" - - with pytest.raises(SystemExit): - rhn_channel.main() - - out, err = capfd.readouterr() - results = json.loads(out) - assert results['changed'] == testcase['changed'] - assert results['msg'] == testcase['msg'] - assert not testcase['calls'] # all calls should have been consumed diff --git a/test/units/modules/packaging/os/test_rhn_register.py b/test/units/modules/packaging/os/test_rhn_register.py deleted file mode 100644 index 9d56ec8a78..0000000000 --- a/test/units/modules/packaging/os/test_rhn_register.py +++ /dev/null @@ -1,284 +0,0 @@ -import json -import os - -from units.compat.mock import mock_open -from ansible.module_utils import basic -from ansible.module_utils._text import to_native -import ansible.module_utils.six -from ansible.module_utils.six.moves import xmlrpc_client -from ansible.modules.packaging.os import rhn_register - -import pytest - - -SYSTEMID = """<?xml version="1.0"?> -<params> -<param> -<value><struct> -<member> -<name>system_id</name> -<value><string>ID-123456789</string></value> -</member> -</struct></value> -</param> -</params> -""" - - -def skipWhenAllModulesMissing(modules): - """Skip the decorated test unless one of modules is available.""" - for module in modules: - try: - __import__(module) - return False - except ImportError: - continue - - return True - - -orig_import = __import__ - - -@pytest.fixture -def import_libxml(mocker): - def mock_import(name, *args, **kwargs): - if name in ['libxml2', 'libxml']: - raise ImportError() - else: - return orig_import(name, *args, **kwargs) - - if ansible.module_utils.six.PY3: - mocker.patch('builtins.__import__', side_effect=mock_import) - else: - mocker.patch('__builtin__.__import__', side_effect=mock_import) - - -@pytest.fixture -def patch_rhn(mocker): - load_config_return = { - 'serverURL': 'https://xmlrpc.rhn.redhat.com/XMLRPC', - 'systemIdPath': '/etc/sysconfig/rhn/systemid' - } - - mocker.patch.object(rhn_register.Rhn, 'load_config', return_value=load_config_return) - mocker.patch.object(rhn_register, 'HAS_UP2DATE_CLIENT', mocker.PropertyMock(return_value=True)) - - -@pytest.mark.skipif(skipWhenAllModulesMissing(['libxml2', 'libxml']), reason='none are available: libxml2, libxml') -def test_systemid_with_requirements(capfd, mocker, patch_rhn): - """Check 'msg' and 'changed' results""" - - mocker.patch.object(rhn_register.Rhn, 'enable') - mock_isfile = mocker.patch('os.path.isfile', return_value=True) - mocker.patch('ansible.modules.packaging.os.rhn_register.open', mock_open(read_data=SYSTEMID), create=True) - rhn = rhn_register.Rhn() - assert '123456789' == to_native(rhn.systemid) - - -@pytest.mark.parametrize('patch_ansible_module', [{}], indirect=['patch_ansible_module']) -@pytest.mark.usefixtures('patch_ansible_module') -def test_systemid_requirements_missing(capfd, mocker, patch_rhn, import_libxml): - """Check that missing dependencies are detected""" - - mocker.patch('os.path.isfile', return_value=True) - mocker.patch('ansible.modules.packaging.os.rhn_register.open', mock_open(read_data=SYSTEMID), create=True) - - with pytest.raises(SystemExit): - rhn_register.main() - - out, err = capfd.readouterr() - results = json.loads(out) - assert results['failed'] - assert 'Missing arguments' in results['msg'] - - -@pytest.mark.parametrize('patch_ansible_module', [{}], indirect=['patch_ansible_module']) -@pytest.mark.usefixtures('patch_ansible_module') -def test_without_required_parameters(capfd, patch_rhn): - """Failure must occurs when all parameters are missing""" - - with pytest.raises(SystemExit): - rhn_register.main() - out, err = capfd.readouterr() - results = json.loads(out) - assert results['failed'] - assert 'Missing arguments' in results['msg'] - - -TESTED_MODULE = rhn_register.__name__ -TEST_CASES = [ - [ - # Registering an unregistered host with channels - { - 'channels': 'rhel-x86_64-server-6', - 'username': 'user', - 'password': 'pass', - }, - { - 'calls': [ - ('auth.login', ['X' * 43]), - ('channel.software.listSystemChannels', - [[{'channel_name': 'Red Hat Enterprise Linux Server (v. 6 for 64-bit x86_64)', 'channel_label': 'rhel-x86_64-server-6'}]]), - ('channel.software.setSystemChannels', [1]), - ('auth.logout', [1]), - ], - 'is_registered': False, - 'is_registered.call_count': 1, - 'enable.call_count': 1, - 'systemid.call_count': 2, - 'changed': True, - 'msg': "System successfully registered to 'rhn.redhat.com'.", - 'run_command.call_count': 1, - 'run_command.call_args': '/usr/sbin/rhnreg_ks', - 'request_called': True, - 'unlink.call_count': 0, - } - ], - [ - # Registering an unregistered host without channels - { - 'activationkey': 'key', - 'username': 'user', - 'password': 'pass', - }, - { - 'calls': [ - ], - 'is_registered': False, - 'is_registered.call_count': 1, - 'enable.call_count': 1, - 'systemid.call_count': 0, - 'changed': True, - 'msg': "System successfully registered to 'rhn.redhat.com'.", - 'run_command.call_count': 1, - 'run_command.call_args': '/usr/sbin/rhnreg_ks', - 'request_called': False, - 'unlink.call_count': 0, - } - ], - [ - # Register an host already registered, check that result is unchanged - { - 'activationkey': 'key', - 'username': 'user', - 'password': 'pass', - }, - { - 'calls': [ - ], - 'is_registered': True, - 'is_registered.call_count': 1, - 'enable.call_count': 0, - 'systemid.call_count': 0, - 'changed': False, - 'msg': 'System already registered.', - 'run_command.call_count': 0, - 'request_called': False, - 'unlink.call_count': 0, - }, - ], - [ - # Unregister an host, check that result is changed - { - 'activationkey': 'key', - 'username': 'user', - 'password': 'pass', - 'state': 'absent', - }, - { - 'calls': [ - ('auth.login', ['X' * 43]), - ('system.deleteSystems', [1]), - ('auth.logout', [1]), - ], - 'is_registered': True, - 'is_registered.call_count': 1, - 'enable.call_count': 0, - 'systemid.call_count': 1, - 'changed': True, - 'msg': 'System successfully unregistered from rhn.redhat.com.', - 'run_command.call_count': 0, - 'request_called': True, - 'unlink.call_count': 1, - } - ], - [ - # Unregister a unregistered host (systemid missing) locally, check that result is unchanged - { - 'activationkey': 'key', - 'username': 'user', - 'password': 'pass', - 'state': 'absent', - }, - { - 'calls': [], - 'is_registered': False, - 'is_registered.call_count': 1, - 'enable.call_count': 0, - 'systemid.call_count': 0, - 'changed': False, - 'msg': 'System already unregistered.', - 'run_command.call_count': 0, - 'request_called': False, - 'unlink.call_count': 0, - } - - ], - [ - # Unregister an unknown host (an host with a systemid available locally, check that result contains failed - { - 'activationkey': 'key', - 'username': 'user', - 'password': 'pass', - 'state': 'absent', - }, - { - 'calls': [ - ('auth.login', ['X' * 43]), - ('system.deleteSystems', xmlrpc_client.Fault(1003, 'The following systems were NOT deleted: 123456789')), - ('auth.logout', [1]), - ], - 'is_registered': True, - 'is_registered.call_count': 1, - 'enable.call_count': 0, - 'systemid.call_count': 1, - 'failed': True, - 'msg': "Failed to unregister: <Fault 1003: 'The following systems were NOT deleted: 123456789'>", - 'run_command.call_count': 0, - 'request_called': True, - 'unlink.call_count': 0, - } - ], -] - - -@pytest.mark.parametrize('patch_ansible_module, testcase', TEST_CASES, indirect=['patch_ansible_module']) -@pytest.mark.usefixtures('patch_ansible_module') -def test_register_parameters(mocker, capfd, mock_request, patch_rhn, testcase): - # successful execution, no output - mocker.patch.object(basic.AnsibleModule, 'run_command', return_value=(0, '', '')) - mock_is_registered = mocker.patch.object(rhn_register.Rhn, 'is_registered', mocker.PropertyMock(return_value=testcase['is_registered'])) - mocker.patch.object(rhn_register.Rhn, 'enable') - mock_systemid = mocker.patch.object(rhn_register.Rhn, 'systemid', mocker.PropertyMock(return_value=12345)) - mocker.patch('os.unlink', return_value=True) - - with pytest.raises(SystemExit): - rhn_register.main() - - assert basic.AnsibleModule.run_command.call_count == testcase['run_command.call_count'] - if basic.AnsibleModule.run_command.call_count: - assert basic.AnsibleModule.run_command.call_args[0][0][0] == testcase['run_command.call_args'] - - assert mock_is_registered.call_count == testcase['is_registered.call_count'] - assert rhn_register.Rhn.enable.call_count == testcase['enable.call_count'] - assert mock_systemid.call_count == testcase['systemid.call_count'] - assert xmlrpc_client.Transport.request.called == testcase['request_called'] - assert os.unlink.call_count == testcase['unlink.call_count'] - - out, err = capfd.readouterr() - results = json.loads(out) - assert results.get('changed') == testcase.get('changed') - assert results.get('failed') == testcase.get('failed') - assert results['msg'] == testcase['msg'] - assert not testcase['calls'] # all calls should have been consumed diff --git a/test/units/modules/packaging/os/test_rhsm_release.py b/test/units/modules/packaging/os/test_rhsm_release.py deleted file mode 100644 index f652bfa64d..0000000000 --- a/test/units/modules/packaging/os/test_rhsm_release.py +++ /dev/null @@ -1,141 +0,0 @@ -# (c) 2018, Sean Myers <sean.myers@redhat.com> -# 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 - -from units.compat.mock import call, patch -from ansible.modules.packaging.os import rhsm_release -from units.modules.utils import ( - AnsibleExitJson, AnsibleFailJson, ModuleTestCase, set_module_args) - - -class RhsmRepositoryReleaseModuleTestCase(ModuleTestCase): - module = rhsm_release - - def setUp(self): - super(RhsmRepositoryReleaseModuleTestCase, self).setUp() - - # Mainly interested that the subscription-manager calls are right - # based on the module args, so patch out run_command in the module. - # returns (rc, out, err) structure - self.mock_run_command = patch('ansible.modules.packaging.os.rhsm_release.' - 'AnsibleModule.run_command') - self.module_main_command = self.mock_run_command.start() - - # Module does a get_bin_path check before every run_command call - self.mock_get_bin_path = patch('ansible.modules.packaging.os.rhsm_release.' - 'AnsibleModule.get_bin_path') - self.get_bin_path = self.mock_get_bin_path.start() - self.get_bin_path.return_value = '/testbin/subscription-manager' - - def tearDown(self): - self.mock_run_command.stop() - self.mock_get_bin_path.stop() - super(RhsmRepositoryReleaseModuleTestCase, self).tearDown() - - def module_main(self, exit_exc): - with self.assertRaises(exit_exc) as exc: - self.module.main() - return exc.exception.args[0] - - def test_release_set(self): - # test that the module attempts to change the release when the current - # release is not the same as the user-specific target release - set_module_args({'release': '7.5'}) - self.module_main_command.side_effect = [ - # first call, get_release: returns different version so set_release is called - (0, '7.4', ''), - # second call, set_release: just needs to exit with 0 rc - (0, '', ''), - ] - - result = self.module_main(AnsibleExitJson) - - self.assertTrue(result['changed']) - self.assertEqual('7.5', result['current_release']) - self.module_main_command.assert_has_calls([ - call('/testbin/subscription-manager release --show', check_rc=True), - call('/testbin/subscription-manager release --set 7.5', check_rc=True), - ]) - - def test_release_set_idempotent(self): - # test that the module does not attempt to change the release when - # the current release matches the user-specified target release - set_module_args({'release': '7.5'}) - self.module_main_command.side_effect = [ - # first call, get_release: returns same version, set_release is not called - (0, '7.5', ''), - ] - - result = self.module_main(AnsibleExitJson) - - self.assertFalse(result['changed']) - self.assertEqual('7.5', result['current_release']) - self.module_main_command.assert_has_calls([ - call('/testbin/subscription-manager release --show', check_rc=True), - ]) - - def test_release_unset(self): - # test that the module attempts to change the release when the current - # release is not the same as the user-specific target release - set_module_args({'release': None}) - self.module_main_command.side_effect = [ - # first call, get_release: returns version so set_release is called - (0, '7.5', ''), - # second call, set_release: just needs to exit with 0 rc - (0, '', ''), - ] - - result = self.module_main(AnsibleExitJson) - - self.assertTrue(result['changed']) - self.assertIsNone(result['current_release']) - self.module_main_command.assert_has_calls([ - call('/testbin/subscription-manager release --show', check_rc=True), - call('/testbin/subscription-manager release --unset', check_rc=True), - ]) - - def test_release_unset_idempotent(self): - # test that the module attempts to change the release when the current - # release is not the same as the user-specific target release - set_module_args({'release': None}) - self.module_main_command.side_effect = [ - # first call, get_release: returns no version, set_release is not called - (0, 'Release not set', ''), - ] - - result = self.module_main(AnsibleExitJson) - - self.assertFalse(result['changed']) - self.assertIsNone(result['current_release']) - self.module_main_command.assert_has_calls([ - call('/testbin/subscription-manager release --show', check_rc=True), - ]) - - def test_release_insane(self): - # test that insane values for release trigger fail_json - insane_value = 'this is an insane release value' - set_module_args({'release': insane_value}) - - result = self.module_main(AnsibleFailJson) - - # also ensure that the fail msg includes the insane value - self.assertIn(insane_value, result['msg']) - - def test_release_matcher(self): - # throw a few values at the release matcher -- only sane_values should match - sane_values = ['1Server', '10Server', '1.10', '10.0'] - insane_values = [ - '6server', # lowercase 's' - '100Server', # excessively long 'x' component - '100.0', # excessively long 'x' component - '6.100', # excessively long 'y' component - '100.100', # excessively long 'x' and 'y' components - ] - - matches = self.module.release_matcher.findall(' '.join(sane_values + insane_values)) - - # matches should be returned in the same order they were parsed, - # so sorting shouldn't be necessary here - self.assertEqual(matches, sane_values) |