Repository: ansible/awx
File: awx/main/tests/functional/models/test_inventory.py (Python)

Code before:

    def test_related_group_update(self, group):
        src = group.inventory_sources.create(name='foo', source='ec2')
        job = InventoryUpdate.objects.create(inventory_source=src, source=src.source)
        assert job.id in [jerb.id for jerb in group._get_related_jobs()]


@pytest.mark.django_db
class TestInventorySourceInjectors:
    def test_extra_credentials(self, project, credential):
        inventory_source = InventorySource.objects.create(name='foo', source='scm', source_project=project)
        inventory_source.credentials.add(credential)
        assert inventory_source.get_cloud_credential() == credential  # for serializer
        assert inventory_source.get_extra_credentials() == [credential]

        inventory_source.source = 'ec2'
        assert inventory_source.get_cloud_credential() == credential
        assert inventory_source.get_extra_credentials() == []

    def test_all_cloud_sources_covered(self):
        """Code in several places relies on the fact that the older
        discover_cloud_provider_plugin_names contains the same names as what are
        defined within the injectors
        """
        # slight exception case for constructed, because it has a FQCN but is not a cloud source
        assert set(discover_available_cloud_provider_plugin_names) | set(['constructed']) == set(InventorySource.injectors.keys())

    @pytest.mark.parametrize('source,filename', [('ec2', 'aws_ec2.yml'), ('openstack', 'openstack.yml'), ('gce', 'gcp_compute.yml')])
    def test_plugin_filenames(self, source, filename):
        """It is important that the filenames for inventory plugin files
        are named correctly, because Ansible will reject files that do
        not have these exact names
        """
        injector = InventorySource.injectors[source]()
        assert injector.filename == filename

    @pytest.mark.parametrize(
        'source,proper_name',
        [
            ('ec2', 'amazon.aws.aws_ec2'),
            ('openstack', 'openstack.cloud.openstack'),
            ('gce', 'google.cloud.gcp_compute'),
            ('azure_rm', 'azure.azcollection.azure_rm'),
            ('vmware', 'community.vmware.vmware_vm_inventory'),
            ('rhv', 'ovirt.ovirt.ovirt'),
            ('satellite6', 'theforeman.foreman.foreman'),
            ('insights', 'redhatinsights.insights.insights'),
            ('controller', 'awx.awx.tower'),
            ('terraform', 'cloud.terraform.terraform_state'),
        ],
    )

Review comment (suggestion):
```suggestion
        assert set(discover_available_cloud_provider_plugin_names()) | set(['constructed']) == set(InventorySource.injectors.keys())
```

Code after:

    def test_related_group_update(self, group):
        src = group.inventory_sources.create(name='foo', source='ec2')
        job = InventoryUpdate.objects.create(inventory_source=src, source=src.source)
        assert job.id in [jerb.id for jerb in group._get_related_jobs()]


@pytest.mark.django_db
class TestInventorySourceInjectors:
    def test_extra_credentials(self, project, credential):
        inventory_source = InventorySource.objects.create(name='foo', source='scm', source_project=project)
        inventory_source.credentials.add(credential)
        assert inventory_source.get_cloud_credential() == credential  # for serializer
        assert inventory_source.get_extra_credentials() == [credential]

        inventory_source.source = 'ec2'
        assert inventory_source.get_cloud_credential() == credential
        assert inventory_source.get_extra_credentials() == []

    def test_all_cloud_sources_covered(self):
        """Code in several places relies on the fact that the older
        discover_cloud_provider_plugin_names returns the same names as what are
        defined within the injectors
        """
        # slight exception case for constructed, because it has a FQCN but is not a cloud source
        assert set(discover_available_cloud_provider_plugin_names()) | set(['constructed']) == set(InventorySource.injectors.keys())

    @pytest.mark.parametrize('source,filename', [('ec2', 'aws_ec2.yml'), ('openstack', 'openstack.yml'), ('gce', 'gcp_compute.yml')])
    def test_plugin_filenames(self, source, filename):
        """It is important that the filenames for inventory plugin files
        are named correctly, because Ansible will reject files that do
        not have these exact names
        """
        injector = InventorySource.injectors[source]()
        assert injector.filename == filename

    @pytest.mark.parametrize(
        'source,proper_name',
        [
            ('ec2', 'amazon.aws.aws_ec2'),
            ('openstack', 'openstack.cloud.openstack'),
            ('gce', 'google.cloud.gcp_compute'),
            ('azure_rm', 'azure.azcollection.azure_rm'),
            ('vmware', 'community.vmware.vmware_vm_inventory'),
            ('rhv', 'ovirt.ovirt.ovirt'),
            ('satellite6', 'theforeman.foreman.foreman'),
            ('insights', 'redhatinsights.insights.insights'),
            ('controller', 'awx.awx.tower'),
            ('terraform', 'cloud.terraform.terraform_state'),
        ],
    )