From 401c5671c892c56c42f5bf6c4ec68f98ef099b76 Mon Sep 17 00:00:00 2001 From: AlanCoding Date: Thu, 14 May 2020 20:21:35 -0400 Subject: [PATCH] Remove things from tests specific to inventory scripts --- .../test_inventory_source_injectors.py | 9 -- awx/main/tests/unit/test_tasks.py | 147 ++++-------------- 2 files changed, 27 insertions(+), 129 deletions(-) diff --git a/awx/main/tests/functional/test_inventory_source_injectors.py b/awx/main/tests/functional/test_inventory_source_injectors.py index 1dea5df0d0..7e35f9a51b 100644 --- a/awx/main/tests/functional/test_inventory_source_injectors.py +++ b/awx/main/tests/functional/test_inventory_source_injectors.py @@ -68,15 +68,6 @@ INI_TEST_VARS = { 'satellite6_want_hostcollections': True, 'satellite6_want_ansible_ssh_host': True, 'satellite6_want_facts': True - - }, - 'cloudforms': { - 'version': '2.4', - 'purge_actions': 'maybe', - 'clean_group_keys': 'this_key', - 'nest_tags': 'yes', - 'suffix': '.ppt', - 'prefer_ipv4': 'yes' }, 'rhv': { # options specific to the plugin 'ovirt_insecure': False, diff --git a/awx/main/tests/unit/test_tasks.py b/awx/main/tests/unit/test_tasks.py index 9a62e8112b..b711e6dcb1 100644 --- a/awx/main/tests/unit/test_tasks.py +++ b/awx/main/tests/unit/test_tasks.py @@ -1835,6 +1835,13 @@ class TestProjectUpdateCredentials(TestJobExecution): assert env['FOO'] == 'BAR' +@pytest.fixture +def mock_ansible_version(): + with mock.patch('awx.main.tasks._get_ansible_version', mock.MagicMock(return_value='2.10')) as _fixture: + yield _fixture + + +@pytest.mark.usefixtures("mock_ansible_version") class TestInventoryUpdateCredentials(TestJobExecution): @pytest.fixture def inventory_update(self): @@ -1852,17 +1859,11 @@ class TestInventoryUpdateCredentials(TestJobExecution): inventory_update.get_cloud_credential = mocker.Mock(return_value=None) inventory_update.get_extra_credentials = mocker.Mock(return_value=[]) - with mocker.patch('awx.main.tasks._get_ansible_version', mocker.MagicMock(return_value='2.7')): - private_data_files = task.build_private_data_files(inventory_update, private_data_dir) - env = task.build_env(inventory_update, private_data_dir, False, private_data_files) + private_data_files = task.build_private_data_files(inventory_update, private_data_dir) + env = task.build_env(inventory_update, private_data_dir, False, private_data_files) assert 'AWS_ACCESS_KEY_ID' not in env assert 'AWS_SECRET_ACCESS_KEY' not in env - assert 'EC2_INI_PATH' in env - - config = configparser.ConfigParser() - config.read(env['EC2_INI_PATH']) - assert 'ec2' in config.sections() @pytest.mark.parametrize('with_credential', [True, False]) def test_custom_source(self, with_credential, mocker, inventory_update, private_data_dir): @@ -1928,20 +1929,13 @@ class TestInventoryUpdateCredentials(TestJobExecution): inventory_update.get_cloud_credential = get_cred inventory_update.get_extra_credentials = mocker.Mock(return_value=[]) - # force test to use the ec2 script injection logic, as opposed to plugin - with mocker.patch('awx.main.tasks._get_ansible_version', mocker.MagicMock(return_value='2.7')): - private_data_files = task.build_private_data_files(inventory_update, private_data_dir) - env = task.build_env(inventory_update, private_data_dir, False, private_data_files) + private_data_files = task.build_private_data_files(inventory_update, private_data_dir) + env = task.build_env(inventory_update, private_data_dir, False, private_data_files) safe_env = build_safe_env(env) assert env['AWS_ACCESS_KEY_ID'] == 'bob' assert env['AWS_SECRET_ACCESS_KEY'] == 'secret' - assert 'EC2_INI_PATH' in env - - config = configparser.ConfigParser() - config.read(env['EC2_INI_PATH']) - assert 'ec2' in config.sections() assert safe_env['AWS_SECRET_ACCESS_KEY'] == tasks.HIDDEN_PASSWORD @@ -1961,9 +1955,8 @@ class TestInventoryUpdateCredentials(TestJobExecution): inventory_update.get_cloud_credential = get_cred inventory_update.get_extra_credentials = mocker.Mock(return_value=[]) - with mocker.patch('awx.main.tasks._get_ansible_version', mocker.MagicMock(return_value='2.7')): - private_data_files = task.build_private_data_files(inventory_update, private_data_dir) - env = task.build_env(inventory_update, private_data_dir, False, private_data_files) + private_data_files = task.build_private_data_files(inventory_update, private_data_dir) + env = task.build_env(inventory_update, private_data_dir, False, private_data_files) safe_env = {} credentials = task.build_credentials_list(inventory_update) @@ -1973,11 +1966,10 @@ class TestInventoryUpdateCredentials(TestJobExecution): credential, env, safe_env, [], private_data_dir ) - config = configparser.ConfigParser() - config.read(env['VMWARE_INI_PATH']) - assert config.get('vmware', 'username') == 'bob' - assert config.get('vmware', 'password') == 'secret' - assert config.get('vmware', 'server') == 'https://example.org' + env["VMWARE_USER"] == "bob", + env["VMWARE_PASSWORD"] == "secret", + env["VMWARE_HOST"] == "https://example.org", + env["VMWARE_VALIDATE_CERTS"] == "False", def test_azure_rm_source_with_tenant(self, private_data_dir, inventory_update, mocker): task = tasks.RunInventoryUpdate() @@ -2005,10 +1997,8 @@ class TestInventoryUpdateCredentials(TestJobExecution): 'group_by_resource_group': 'no' } - # force azure_rm inventory to use script injection logic, as opposed to plugin - with mocker.patch('awx.main.tasks._get_ansible_version', mocker.MagicMock(return_value='2.7')): - private_data_files = task.build_private_data_files(inventory_update, private_data_dir) - env = task.build_env(inventory_update, private_data_dir, False, private_data_files) + private_data_files = task.build_private_data_files(inventory_update, private_data_dir) + env = task.build_env(inventory_update, private_data_dir, False, private_data_files) safe_env = build_safe_env(env) @@ -2018,15 +2008,6 @@ class TestInventoryUpdateCredentials(TestJobExecution): assert env['AZURE_SUBSCRIPTION_ID'] == 'some-subscription' assert env['AZURE_CLOUD_ENVIRONMENT'] == 'foobar' - config = configparser.ConfigParser() - config.read(env['AZURE_INI_PATH']) - assert config.get('azure', 'include_powerstate') == 'yes' - assert config.get('azure', 'group_by_resource_group') == 'no' - assert config.get('azure', 'group_by_location') == 'yes' - assert 'group_by_security_group' not in config.items('azure') - assert config.get('azure', 'group_by_tag') == 'yes' - assert config.get('azure', 'locations') == 'north,south,east,west' - assert safe_env['AZURE_SECRET'] == tasks.HIDDEN_PASSWORD def test_azure_rm_source_with_password(self, private_data_dir, inventory_update, mocker): @@ -2055,10 +2036,8 @@ class TestInventoryUpdateCredentials(TestJobExecution): 'group_by_security_group': 'no' } - # force azure_rm inventory to use script injection logic, as opposed to plugin - with mocker.patch('awx.main.tasks._get_ansible_version', mocker.MagicMock(return_value='2.7')): - private_data_files = task.build_private_data_files(inventory_update, private_data_dir) - env = task.build_env(inventory_update, private_data_dir, False, private_data_files) + private_data_files = task.build_private_data_files(inventory_update, private_data_dir) + env = task.build_env(inventory_update, private_data_dir, False, private_data_files) safe_env = build_safe_env(env) @@ -2067,14 +2046,6 @@ class TestInventoryUpdateCredentials(TestJobExecution): assert env['AZURE_PASSWORD'] == 'secret' assert env['AZURE_CLOUD_ENVIRONMENT'] == 'foobar' - config = configparser.ConfigParser() - config.read(env['AZURE_INI_PATH']) - assert config.get('azure', 'include_powerstate') == 'yes' - assert config.get('azure', 'group_by_resource_group') == 'no' - assert config.get('azure', 'group_by_location') == 'yes' - assert config.get('azure', 'group_by_security_group') == 'no' - assert config.get('azure', 'group_by_tag') == 'yes' - assert 'locations' not in config.items('azure') assert safe_env['AZURE_PASSWORD'] == tasks.HIDDEN_PASSWORD def test_gce_source(self, inventory_update, private_data_dir, mocker): @@ -2118,18 +2089,6 @@ class TestInventoryUpdateCredentials(TestJobExecution): assert json_data['client_email'] == 'bob' assert json_data['project_id'] == 'some-project' - config = configparser.ConfigParser() - config.read(env['GCE_INI_PATH']) - assert 'cache' in config.sections() - assert config.getint('cache', 'cache_max_age') == 0 - - # Change the initial version of the inventory plugin to force use of script - with mock.patch('awx.main.models.inventory.gce.initial_version', None): - run('') - - inventory_update.source_regions = 'us-east-4' - run('us-east-4') - def test_openstack_source(self, inventory_update, private_data_dir, mocker): task = tasks.RunInventoryUpdate() openstack = CredentialType.defaults['openstack']() @@ -2200,60 +2159,12 @@ class TestInventoryUpdateCredentials(TestJobExecution): 'satellite6_want_facts': False } - with mocker.patch('awx.main.tasks._get_ansible_version', mocker.MagicMock(return_value='2.7')): - private_data_files = task.build_private_data_files(inventory_update, private_data_dir) - env = task.build_env(inventory_update, private_data_dir, False, private_data_files) - - config = configparser.ConfigParser() - config.read(env['FOREMAN_INI_PATH']) - assert config.get('foreman', 'url') == 'https://example.org' - assert config.get('foreman', 'user') == 'bob' - assert config.get('foreman', 'password') == 'secret' - assert config.get('ansible', 'group_patterns') == '[a,b,c]' - assert config.get('ansible', 'group_prefix') == 'hey_' - assert config.get('ansible', 'want_hostcollections') == 'True' - assert config.get('ansible', 'want_ansible_ssh_host') == 'True' - assert config.get('ansible', 'rich_params') == 'True' - assert config.get('ansible', 'want_facts') == 'False' - - def test_cloudforms_source(self, inventory_update, private_data_dir, mocker): - task = tasks.RunInventoryUpdate() - cloudforms = CredentialType.defaults['cloudforms']() - inventory_update.source = 'cloudforms' - - def get_cred(): - cred = Credential( - pk=1, - credential_type=cloudforms, - inputs = { - 'username': 'bob', - 'password': 'secret', - 'host': 'https://example.org' - } - ) - cred.inputs['password'] = encrypt_field( - cred, 'password' - ) - return cred - inventory_update.get_cloud_credential = get_cred - inventory_update.get_extra_credentials = mocker.Mock(return_value=[]) - - inventory_update.source_vars = '{"prefer_ipv4": True}' - private_data_files = task.build_private_data_files(inventory_update, private_data_dir) env = task.build_env(inventory_update, private_data_dir, False, private_data_files) - config = configparser.ConfigParser() - config.read(env['CLOUDFORMS_INI_PATH']) - assert config.get('cloudforms', 'url') == 'https://example.org' - assert config.get('cloudforms', 'username') == 'bob' - assert config.get('cloudforms', 'password') == 'secret' - assert config.get('cloudforms', 'ssl_verify') == 'false' - assert config.get('cloudforms', 'prefer_ipv4') == 'True' - - cache_path = config.get('cache', 'path') - assert cache_path.startswith(env['AWX_PRIVATE_DATA_DIR']) - assert os.path.isdir(cache_path) + env["FOREMAN_SERVER"] == "https://example.org", + env["FOREMAN_USER"] == "bob", + env["FOREMAN_PASSWORD"] == "secret", @pytest.mark.parametrize('verify', [True, False]) def test_tower_source(self, verify, inventory_update, private_data_dir, mocker): @@ -2275,16 +2186,13 @@ class TestInventoryUpdateCredentials(TestJobExecution): inventory_update.get_cloud_credential = get_cred inventory_update.get_extra_credentials = mocker.Mock(return_value=[]) - # force tower inventory source to use script injection logic, as opposed to plugin - with mocker.patch('awx.main.tasks._get_ansible_version', mocker.MagicMock(return_value='2.7')): - env = task.build_env(inventory_update, private_data_dir, False) + env = task.build_env(inventory_update, private_data_dir, False) safe_env = build_safe_env(env) assert env['TOWER_HOST'] == 'https://tower.example.org' assert env['TOWER_USERNAME'] == 'bob' assert env['TOWER_PASSWORD'] == 'secret' - assert env['TOWER_INVENTORY'] == '12345' if verify: assert env['TOWER_VERIFY_SSL'] == 'True' else: @@ -2339,9 +2247,8 @@ class TestInventoryUpdateCredentials(TestJobExecution): inventory_update.get_extra_credentials = mocker.Mock(return_value=[]) settings.AWX_TASK_ENV = {'FOO': 'BAR'} - with mocker.patch('awx.main.tasks._get_ansible_version', mocker.MagicMock(return_value='2.7')): - private_data_files = task.build_private_data_files(inventory_update, private_data_dir) - env = task.build_env(inventory_update, private_data_dir, False, private_data_files) + private_data_files = task.build_private_data_files(inventory_update, private_data_dir) + env = task.build_env(inventory_update, private_data_dir, False, private_data_files) assert env['FOO'] == 'BAR'