mirror of
https://github.com/ansible/awx.git
synced 2026-02-01 17:48:10 -03:30
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:
@@ -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')
|
||||
|
||||
@@ -290,6 +290,17 @@ class TestGenericRun(TestJobExecution):
|
||||
args, cwd, env, stdout = call_args
|
||||
assert args[0] == 'bwrap'
|
||||
|
||||
def test_awx_task_env(self):
|
||||
patch = mock.patch('awx.main.tasks.settings.AWX_TASK_ENV', {'FOO': 'BAR'})
|
||||
patch.start()
|
||||
|
||||
self.task.run(self.pk)
|
||||
|
||||
assert self.run_pexpect.call_count == 1
|
||||
call_args, _ = self.run_pexpect.call_args_list[0]
|
||||
args, cwd, env, stdout = call_args
|
||||
assert env['FOO'] == 'BAR'
|
||||
|
||||
|
||||
class TestIsolatedExecution(TestJobExecution):
|
||||
|
||||
@@ -1035,6 +1046,17 @@ class TestJobCredentials(TestJobExecution):
|
||||
self.run_pexpect.side_effect = run_pexpect_side_effect
|
||||
self.task.run(self.pk)
|
||||
|
||||
def test_awx_task_env(self):
|
||||
patch = mock.patch('awx.main.tasks.settings.AWX_TASK_ENV', {'FOO': 'BAR'})
|
||||
patch.start()
|
||||
|
||||
self.task.run(self.pk)
|
||||
|
||||
assert self.run_pexpect.call_count == 1
|
||||
call_args, _ = self.run_pexpect.call_args_list[0]
|
||||
args, cwd, env, stdout = call_args
|
||||
assert env['FOO'] == 'BAR'
|
||||
|
||||
|
||||
class TestProjectUpdateCredentials(TestJobExecution):
|
||||
|
||||
@@ -1056,6 +1078,11 @@ class TestProjectUpdateCredentials(TestJobExecution):
|
||||
dict(scm_type='git'),
|
||||
dict(scm_type='hg'),
|
||||
dict(scm_type='svn'),
|
||||
],
|
||||
'test_awx_task_env': [
|
||||
dict(scm_type='git'),
|
||||
dict(scm_type='hg'),
|
||||
dict(scm_type='svn'),
|
||||
]
|
||||
}
|
||||
|
||||
@@ -1113,6 +1140,18 @@ class TestProjectUpdateCredentials(TestJobExecution):
|
||||
self.run_pexpect.side_effect = partial(run_pexpect_side_effect, private_data)
|
||||
self.task.run(self.pk)
|
||||
|
||||
def test_awx_task_env(self, scm_type):
|
||||
self.instance.scm_type = scm_type
|
||||
patch = mock.patch('awx.main.tasks.settings.AWX_TASK_ENV', {'FOO': 'BAR'})
|
||||
patch.start()
|
||||
|
||||
self.task.run(self.pk)
|
||||
|
||||
assert self.run_pexpect.call_count == 1
|
||||
call_args, _ = self.run_pexpect.call_args_list[0]
|
||||
args, cwd, env, stdout = call_args
|
||||
assert env['FOO'] == 'BAR'
|
||||
|
||||
|
||||
class TestInventoryUpdateCredentials(TestJobExecution):
|
||||
|
||||
@@ -1323,6 +1362,28 @@ class TestInventoryUpdateCredentials(TestJobExecution):
|
||||
self.run_pexpect.side_effect = run_pexpect_side_effect
|
||||
self.task.run(self.pk)
|
||||
|
||||
def test_awx_task_env(self):
|
||||
gce = CredentialType.defaults['gce']()
|
||||
self.instance.source = 'gce'
|
||||
self.instance.credential = Credential(
|
||||
pk=1,
|
||||
credential_type=gce,
|
||||
inputs = {
|
||||
'username': 'bob',
|
||||
'project': 'some-project',
|
||||
}
|
||||
)
|
||||
patch = mock.patch('awx.main.tasks.settings.AWX_TASK_ENV', {'FOO': 'BAR'})
|
||||
patch.start()
|
||||
|
||||
self.task.run(self.pk)
|
||||
|
||||
assert self.run_pexpect.call_count == 1
|
||||
call_args, _ = self.run_pexpect.call_args_list[0]
|
||||
args, cwd, env, stdout = call_args
|
||||
assert env['FOO'] == 'BAR'
|
||||
|
||||
|
||||
|
||||
def test_os_open_oserror():
|
||||
with pytest.raises(OSError):
|
||||
|
||||
@@ -2,7 +2,9 @@
|
||||
|
||||
# Copyright (c) 2017 Ansible, Inc.
|
||||
# All Rights Reserved.
|
||||
import os
|
||||
import pytest
|
||||
from uuid import uuid4
|
||||
|
||||
from awx.main.utils import common
|
||||
|
||||
@@ -15,3 +17,13 @@ from awx.main.utils import common
|
||||
])
|
||||
def test_parse_yaml_or_json(input_, output):
|
||||
assert common.parse_yaml_or_json(input_) == output
|
||||
|
||||
|
||||
def test_set_environ():
|
||||
key = str(uuid4())
|
||||
old_environ = os.environ.copy()
|
||||
with common.set_environ(**{key: 'bar'}):
|
||||
assert os.environ[key] == 'bar'
|
||||
assert set(os.environ.keys()) - set(old_environ.keys()) == set([key])
|
||||
assert os.environ == old_environ
|
||||
assert key not in os.environ
|
||||
|
||||
Reference in New Issue
Block a user