Support AWX_TASK_ENV injection in task and notification invocations.

This change _only_ injects `AWS_TASK_ENV` into `os.environ`; it's up to
underlying libraries to be good citizens and actually respect things
like `HTTPS_PROXY`.

see: #3508
This commit is contained in:
Ryan Petrello
2017-07-06 10:22:59 -04:00
parent 92bc5fd3f0
commit 12d41e2deb
7 changed files with 136 additions and 3 deletions

View File

@@ -1,6 +1,10 @@
import mock
import pytest
from requests.adapters import HTTPAdapter
from requests.utils import select_proxy
from requests.exceptions import ConnectionError
from awx.api.versioning import reverse
from awx.main.models.notifications import NotificationTemplate, Notification
from awx.main.models.inventory import Inventory, InventorySource
@@ -129,3 +133,26 @@ def test_disallow_delete_when_notifications_pending(delete, user, notification_t
response = delete(url, user=u)
assert response.status_code == 405
@pytest.mark.django_db
def test_custom_environment_injection(post, user, organization):
u = user('admin-poster', True)
url = reverse('api:notification_template_list')
response = post(url,
dict(name="test-webhook",
description="test webhook",
organization=organization.id,
notification_type="webhook",
notification_configuration=dict(url="https://example.org",
headers={"Test": "Header"})),
u)
assert response.status_code == 201
template = NotificationTemplate.objects.get(pk=response.data['id'])
with pytest.raises(ConnectionError), \
mock.patch('django.conf.settings.AWX_TASK_ENV', {'HTTPS_PROXY': '192.168.50.100:1234'}), \
mock.patch.object(HTTPAdapter, 'send') as fake_send:
def _send_side_effect(request, **kw):
assert select_proxy(request.url, kw['proxies']) == '192.168.50.100:1234'
raise ConnectionError()
fake_send.side_effect = _send_side_effect
template.send('subject', 'message')