mirror of
https://github.com/ansible/awx.git
synced 2026-03-20 18:37:39 -02:30
Merge pull request #6300 from chrismeyersfsu/feature-insights_proxy
insights proxy
This commit is contained in:
@@ -287,3 +287,17 @@ class TestControlledBySCM:
|
||||
r = options(reverse('api:inventory_inventory_sources_list', kwargs={'pk': scm_inventory.id}),
|
||||
admin_user, expect=200)
|
||||
assert 'POST' not in r.data['actions']
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
class TestInsightsCredential:
|
||||
def test_insights_credential(self, patch, insights_inventory, admin_user, insights_credential):
|
||||
patch(insights_inventory.get_absolute_url(),
|
||||
{'insights_credential': insights_credential.id}, admin_user,
|
||||
expect=200)
|
||||
|
||||
def test_non_insights_credential(self, patch, insights_inventory, admin_user, scm_credential):
|
||||
patch(insights_inventory.get_absolute_url(),
|
||||
{'insights_credential': scm_credential.id}, admin_user,
|
||||
expect=400)
|
||||
|
||||
|
||||
15
awx/main/tests/functional/api/test_project.py
Normal file
15
awx/main/tests/functional/api/test_project.py
Normal file
@@ -0,0 +1,15 @@
|
||||
import pytest
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
class TestInsightsCredential:
|
||||
def test_insights_credential(self, patch, insights_project, admin_user, insights_credential):
|
||||
patch(insights_project.get_absolute_url(),
|
||||
{'credential': insights_credential.id}, admin_user,
|
||||
expect=200)
|
||||
|
||||
def test_non_insights_credential(self, patch, insights_project, admin_user, scm_credential):
|
||||
patch(insights_project.get_absolute_url(),
|
||||
{'credential': scm_credential.id}, admin_user,
|
||||
expect=400)
|
||||
|
||||
@@ -178,6 +178,11 @@ def user_project(user):
|
||||
return Project.objects.create(name="test-user-project", created_by=owner, description="test-user-project-desc")
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def insights_project():
|
||||
return Project.objects.create(name="test-insights-project", scm_type="insights")
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def instance(settings):
|
||||
return Instance.objects.create(uuid=settings.SYSTEM_UUID, hostname="instance.example.org", capacity=100)
|
||||
@@ -216,6 +221,20 @@ def credentialtype_vault():
|
||||
return vault_type
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def credentialtype_scm():
|
||||
scm_type = CredentialType.defaults['scm']()
|
||||
scm_type.save()
|
||||
return scm_type
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def credentialtype_insights():
|
||||
insights_type = CredentialType.defaults['insights']()
|
||||
insights_type.save()
|
||||
return insights_type
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def credential(credentialtype_aws):
|
||||
return Credential.objects.create(credential_type=credentialtype_aws, name='test-cred',
|
||||
@@ -240,6 +259,18 @@ def machine_credential(credentialtype_ssh):
|
||||
inputs={'username': 'test_user', 'password': 'pas4word'})
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def scm_credential(credentialtype_scm):
|
||||
return Credential.objects.create(credential_type=credentialtype_scm, name='scm-cred',
|
||||
inputs={'username': 'optimus', 'password': 'prime'})
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def insights_credential(credentialtype_insights):
|
||||
return Credential.objects.create(credential_type=credentialtype_insights, name='insights-cred',
|
||||
inputs={'username': 'morocco_mole', 'password': 'secret_squirrel'})
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def org_credential(organization, credentialtype_aws):
|
||||
return Credential.objects.create(credential_type=credentialtype_aws, name='test-cred',
|
||||
@@ -252,6 +283,13 @@ def inventory(organization):
|
||||
return organization.inventories.create(name="test-inv")
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def insights_inventory(inventory):
|
||||
inventory.scm_type = 'insights'
|
||||
inventory.save()
|
||||
return inventory
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def scm_inventory_source(inventory, project):
|
||||
inv_src = InventorySource(
|
||||
|
||||
@@ -19,6 +19,7 @@ def test_default_cred_types():
|
||||
'azure_rm',
|
||||
'cloudforms',
|
||||
'gce',
|
||||
'insights',
|
||||
'net',
|
||||
'openstack',
|
||||
'satellite6',
|
||||
|
||||
@@ -8,6 +8,7 @@ from django.apps import apps
|
||||
from awx.main.models import Credential, CredentialType
|
||||
from awx.main.migrations._credentialtypes import migrate_to_v2_credentials
|
||||
from awx.main.utils.common import decrypt_field
|
||||
from awx.main.migrations._credentialtypes import _disassociate_non_insights_projects
|
||||
|
||||
EXAMPLE_PRIVATE_KEY = '-----BEGIN PRIVATE KEY-----\nxyz==\n-----END PRIVATE KEY-----'
|
||||
|
||||
@@ -15,12 +16,12 @@ EXAMPLE_PRIVATE_KEY = '-----BEGIN PRIVATE KEY-----\nxyz==\n-----END PRIVATE KEY-
|
||||
|
||||
|
||||
@contextmanager
|
||||
def migrate(credential, kind):
|
||||
def migrate(credential, kind, is_insights=False):
|
||||
with mock.patch.object(Credential, 'kind', kind), \
|
||||
mock.patch.object(Credential, 'objects', mock.Mock(
|
||||
get=lambda **kw: deepcopy(credential),
|
||||
all=lambda: [credential]
|
||||
)):
|
||||
all=lambda: [credential],
|
||||
)), mock.patch('awx.main.migrations._credentialtypes._is_insights_scm', return_value=is_insights):
|
||||
class Apps(apps.__class__):
|
||||
def get_model(self, app, model):
|
||||
if model == 'Credential':
|
||||
@@ -307,3 +308,40 @@ def test_azure_rm_migration():
|
||||
assert decrypt_field(cred, 'secret') == 'some-secret'
|
||||
assert cred.inputs['tenant'] == 'some-tenant'
|
||||
assert Credential.objects.count() == 1
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
def test_insights_migration():
|
||||
cred = Credential(name='My Credential')
|
||||
|
||||
with migrate(cred, 'scm', is_insights=True):
|
||||
cred.__dict__.update({
|
||||
'username': 'bob',
|
||||
'password': 'some-password',
|
||||
})
|
||||
|
||||
assert cred.credential_type.name == 'Insights Basic Auth'
|
||||
assert cred.inputs['username'] == 'bob'
|
||||
assert cred.inputs['password'].startswith('$encrypted$')
|
||||
|
||||
|
||||
@pytest.mark.skip(reason="Need some more mocking here or something.")
|
||||
@pytest.mark.django_db
|
||||
def test_insights_project_migration():
|
||||
cred1 = apps.get_model('main', 'Credential').objects.create(name='My Credential')
|
||||
cred2 = apps.get_model('main', 'Credential').objects.create(name='My Credential')
|
||||
projA1 = apps.get_model('main', 'Project').objects.create(name='Insights Project A1', scm_type='insights', credential=cred1)
|
||||
|
||||
projB1 = apps.get_model('main', 'Project').objects.create(name='Git Project B1', scm_type='git', credential=cred1)
|
||||
projB2 = apps.get_model('main', 'Project').objects.create(name='Git Project B2', scm_type='git', credential=cred1)
|
||||
|
||||
projC1 = apps.get_model('main', 'Project').objects.create(name='Git Project C1', scm_type='git', credential=cred2)
|
||||
|
||||
_disassociate_non_insights_projects(apps, cred1)
|
||||
_disassociate_non_insights_projects(apps, cred2)
|
||||
|
||||
assert apps.get_model('main', 'Project').objects.get(pk=projA1).credential is None
|
||||
assert apps.get_model('main', 'Project').objects.get(pk=projB1).credential is None
|
||||
assert apps.get_model('main', 'Project').objects.get(pk=projB2).credential is None
|
||||
assert apps.get_model('main', 'Project').objects.get(pk=projC1).credential == cred2
|
||||
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import mock
|
||||
import pytest
|
||||
import requests
|
||||
|
||||
from collections import namedtuple
|
||||
|
||||
@@ -8,6 +9,11 @@ from awx.api.views import (
|
||||
JobTemplateLabelList,
|
||||
JobTemplateSurveySpec,
|
||||
InventoryInventorySourcesUpdate,
|
||||
HostInsights,
|
||||
)
|
||||
|
||||
from awx.main.models import (
|
||||
Host,
|
||||
)
|
||||
|
||||
|
||||
@@ -117,3 +123,81 @@ class TestInventoryInventorySourcesUpdate:
|
||||
view = InventoryInventorySourcesUpdate()
|
||||
response = view.post(mock_request)
|
||||
assert response.data == expected
|
||||
|
||||
|
||||
class TestHostInsights():
|
||||
|
||||
@pytest.fixture
|
||||
def patch_parent(self, mocker):
|
||||
mocker.patch('awx.api.generics.GenericAPIView')
|
||||
|
||||
@pytest.mark.parametrize("status_code, exception, error, message", [
|
||||
(500, requests.exceptions.SSLError, 'SSLError while trying to connect to https://myexample.com/whocares/me/', None,),
|
||||
(504, requests.exceptions.Timeout, 'Request to https://myexample.com/whocares/me/ timed out.', None,),
|
||||
(500, requests.exceptions.RequestException, 'booo!', 'Unkown exception booo! while trying to GET https://myexample.com/whocares/me/'),
|
||||
])
|
||||
def test_get_insights_request_exception(self, patch_parent, mocker, status_code, exception, error, message):
|
||||
view = HostInsights()
|
||||
mocker.patch.object(view, '_get_insights', side_effect=exception(error))
|
||||
|
||||
(msg, code) = view.get_insights('https://myexample.com/whocares/me/', 'ignore', 'ignore')
|
||||
assert code == status_code
|
||||
assert msg['error'] == message or error
|
||||
|
||||
def test_get_insights_non_200(self, patch_parent, mocker):
|
||||
view = HostInsights()
|
||||
Response = namedtuple('Response', 'status_code content')
|
||||
mocker.patch.object(view, '_get_insights', return_value=Response(500, 'mock 500 err msg'))
|
||||
|
||||
(msg, code) = view.get_insights('https://myexample.com/whocares/me/', 'ignore', 'ignore')
|
||||
assert msg['error'] == 'Failed to gather reports and maintenance plans from Insights API at URL https://myexample.com/whocares/me/. Server responded with 500 status code and message mock 500 err msg'
|
||||
|
||||
def test_get_insights_malformed_json_content(self, patch_parent, mocker):
|
||||
view = HostInsights()
|
||||
|
||||
class Response():
|
||||
status_code = 200
|
||||
content = 'booo!'
|
||||
|
||||
def json(self):
|
||||
raise ValueError('we do not care what this is')
|
||||
|
||||
mocker.patch.object(view, '_get_insights', return_value=Response())
|
||||
|
||||
(msg, code) = view.get_insights('https://myexample.com/whocares/me/', 'ignore', 'ignore')
|
||||
assert msg['error'] == 'Expected JSON response from Insights but instead got booo!'
|
||||
assert code == 500
|
||||
|
||||
#def test_get_not_insights_host(self, patch_parent, mocker, mock_response_new):
|
||||
#def test_get_not_insights_host(self, patch_parent, mocker):
|
||||
def test_get_not_insights_host(self, mocker):
|
||||
|
||||
view = HostInsights()
|
||||
|
||||
host = Host()
|
||||
host.insights_system_id = None
|
||||
|
||||
mocker.patch.object(view, 'get_object', return_value=host)
|
||||
|
||||
resp = view.get(None)
|
||||
|
||||
assert resp.data['error'] == 'This host is not recognized as an Insights host.'
|
||||
assert resp.status_code == 404
|
||||
|
||||
def test_get_no_credential(self, patch_parent, mocker):
|
||||
view = HostInsights()
|
||||
|
||||
class MockInventory():
|
||||
insights_credential = None
|
||||
name = 'inventory_name_here'
|
||||
|
||||
class MockHost():
|
||||
insights_system_id = 'insights_system_id_value'
|
||||
inventory = MockInventory()
|
||||
|
||||
mocker.patch.object(view, 'get_object', return_value=MockHost())
|
||||
|
||||
resp = view.get(None)
|
||||
|
||||
assert resp.data['error'] == 'No Insights Credential found for the Inventory, "inventory_name_here", that this host belongs to.'
|
||||
assert resp.status_code == 404
|
||||
|
||||
Reference in New Issue
Block a user