move code linting to a stricter pep8-esque auto-formatting tool, black

This commit is contained in:
Ryan Petrello
2021-03-19 12:44:51 -04:00
parent 9b702e46fe
commit c2ef0a6500
671 changed files with 20538 additions and 21924 deletions

View File

@@ -13,26 +13,18 @@ from rest_framework.exceptions import ParseError
from awx.main.utils import common
from awx.main.models import (
Job,
AdHocCommand,
InventoryUpdate,
ProjectUpdate,
SystemJob,
WorkflowJob,
Inventory,
JobTemplate,
UnifiedJobTemplate,
UnifiedJob
from awx.main.models import Job, AdHocCommand, InventoryUpdate, ProjectUpdate, SystemJob, WorkflowJob, Inventory, JobTemplate, UnifiedJobTemplate, UnifiedJob
@pytest.mark.parametrize(
'input_, output',
[
({"foo": "bar"}, {"foo": "bar"}),
('{"foo": "bar"}', {"foo": "bar"}),
('---\nfoo: bar', {"foo": "bar"}),
(4399, {}),
],
)
@pytest.mark.parametrize('input_, output', [
({"foo": "bar"}, {"foo": "bar"}),
('{"foo": "bar"}', {"foo": "bar"}),
('---\nfoo: bar', {"foo": "bar"}),
(4399, {}),
])
def test_parse_yaml_or_json(input_, output):
assert common.parse_yaml_or_json(input_) == output
@@ -48,7 +40,6 @@ def test_recursive_vars_not_allowed():
class TestParserExceptions:
@staticmethod
def json_error(data):
try:
@@ -103,7 +94,7 @@ TEST_MODELS = [
(UnifiedJob, 'unified_job'),
(Inventory, 'inventory'),
(JobTemplate, 'job_template'),
(UnifiedJobTemplate, 'unified_job_template')
(UnifiedJobTemplate, 'unified_job_template'),
]
@@ -120,9 +111,7 @@ def test_get_model_for_invalid_type():
@pytest.mark.django_db
@pytest.mark.parametrize("model_type,model_class", [
(name, cls) for cls, name in TEST_MODELS
])
@pytest.mark.parametrize("model_type,model_class", [(name, cls) for cls, name in TEST_MODELS])
def test_get_model_for_valid_type(model_type, model_class):
assert common.get_model_for_type(model_type) == model_class
@@ -130,6 +119,7 @@ def test_get_model_for_valid_type(model_type, model_class):
@pytest.fixture
def memoized_function(mocker, mock_cache):
with mock.patch('awx.main.utils.common.get_memoize_cache', return_value=mock_cache):
@common.memoize(track_function=True)
def myfunction(key, value):
if key not in myfunction.calls:
@@ -141,6 +131,7 @@ def memoized_function(mocker, mock_cache):
return value
else:
return '%s called %s times' % (value, myfunction.calls[key])
myfunction.calls = dict()
return myfunction
@@ -178,16 +169,14 @@ def test_memoize_delete(memoized_function, mock_cache):
def test_memoize_parameter_error():
with pytest.raises(common.IllegalArgumentError):
@common.memoize(cache_key='foo', track_function=True)
def fn():
return
def test_extract_ansible_vars():
my_dict = {
"foobar": "baz",
"ansible_connetion_setting": "1928"
}
my_dict = {"foobar": "baz", "ansible_connetion_setting": "1928"}
redacted, var_list = common.extract_ansible_vars(json.dumps(my_dict))
assert var_list == set(['ansible_connetion_setting'])
assert redacted == {"foobar": "baz"}

View File

@@ -58,9 +58,10 @@ def test_decrypt_field_with_undefined_attr_raises_expected_exception():
class TestSurveyReversibilityValue:
'''
"""
Tests to enforce the contract with survey password question encrypted values
'''
"""
_key = encryption.get_encryption_key('value', None)
def test_encrypt_empty_string(self):

View File

@@ -1,4 +1,3 @@
# Python
import pytest
from unittest import mock
@@ -11,18 +10,20 @@ from awx.main.models import Host
from django.db.models import Q
@pytest.mark.parametrize('params, logger_name, expected', [
# skip all records if enabled_flag = False
({'enabled_flag': False}, 'awx.main', False),
# skip all records if the host is undefined
({'enabled_flag': True}, 'awx.main', False),
# skip all records if underlying logger is used by handlers themselves
({'enabled_flag': True}, 'awx.main.utils.handlers', False),
({'enabled_flag': True, 'enabled_loggers': ['awx']}, 'awx.main', True),
({'enabled_flag': True, 'enabled_loggers': ['abc']}, 'awx.analytics.xyz', False),
({'enabled_flag': True, 'enabled_loggers': ['xyz']}, 'awx.analytics.xyz', True),
])
@pytest.mark.parametrize(
'params, logger_name, expected',
[
# skip all records if enabled_flag = False
({'enabled_flag': False}, 'awx.main', False),
# skip all records if the host is undefined
({'enabled_flag': True}, 'awx.main', False),
# skip all records if underlying logger is used by handlers themselves
({'enabled_flag': True}, 'awx.main.utils.handlers', False),
({'enabled_flag': True, 'enabled_loggers': ['awx']}, 'awx.main', True),
({'enabled_flag': True, 'enabled_loggers': ['abc']}, 'awx.analytics.xyz', False),
({'enabled_flag': True, 'enabled_loggers': ['xyz']}, 'awx.analytics.xyz', True),
],
)
def test_base_logging_handler_skip_log(params, logger_name, expected, dummy_log_record):
filter = ExternalLoggerEnabled(**params)
dummy_log_record.name = logger_name
@@ -30,7 +31,6 @@ def test_base_logging_handler_skip_log(params, logger_name, expected, dummy_log_
class Field(object):
def __init__(self, name, related_model=None, __prevent_search__=None):
self.name = name
self.related_model = related_model
@@ -38,11 +38,8 @@ class Field(object):
class Meta(object):
def __init__(self, fields):
self._fields = {
f.name: f for f in fields
}
self._fields = {f.name: f for f in fields}
self.object_name = 'Host'
self.fields_map = {}
self.fields = self._fields.values()
@@ -59,164 +56,197 @@ class mockObjects:
class mockUser:
def __init__(self):
print("Host user created")
self._meta = Meta(fields=[
Field(name='password', __prevent_search__=True)
])
self._meta = Meta(fields=[Field(name='password', __prevent_search__=True)])
class mockHost:
def __init__(self):
print("Host mock created")
self.objects = mockObjects()
fields = [
Field(name='name'),
Field(name='description'),
Field(name='created_by', related_model=mockUser())
]
fields = [Field(name='name'), Field(name='description'), Field(name='created_by', related_model=mockUser())]
self._meta = Meta(fields=fields)
@mock.patch('awx.main.utils.filters.get_model', return_value=mockHost())
class TestSmartFilterQueryFromString():
@mock.patch(
'awx.api.filters.get_fields_from_path',
lambda model, path: ([model], path) # disable field filtering, because a__b isn't a real Host field
class TestSmartFilterQueryFromString:
@mock.patch('awx.api.filters.get_fields_from_path', lambda model, path: ([model], path)) # disable field filtering, because a__b isn't a real Host field
@pytest.mark.parametrize(
"filter_string,q_expected",
[
('facts__facts__blank=""', Q(**{u"facts__facts__blank": u""})),
('"facts__facts__ space "="f"', Q(**{u"facts__facts__ space ": u"f"})),
('"facts__facts__ e "=no_quotes_here', Q(**{u"facts__facts__ e ": u"no_quotes_here"})),
('a__b__c=3', Q(**{u"a__b__c": 3})),
('a__b__c=3.14', Q(**{u"a__b__c": 3.14})),
('a__b__c=true', Q(**{u"a__b__c": True})),
('a__b__c=false', Q(**{u"a__b__c": False})),
('a__b__c=null', Q(**{u"a__b__c": None})),
('ansible_facts__a="true"', Q(**{u"ansible_facts__contains": {u"a": u"true"}})),
('ansible_facts__a__exact="true"', Q(**{u"ansible_facts__contains": {u"a": u"true"}})),
# ('"a__b\"__c"="true"', Q(**{u"a__b\"__c": "true"})),
# ('a__b\"__c="true"', Q(**{u"a__b\"__c": "true"})),
],
)
@pytest.mark.parametrize("filter_string,q_expected", [
('facts__facts__blank=""', Q(**{u"facts__facts__blank": u""})),
('"facts__facts__ space "="f"', Q(**{u"facts__facts__ space ": u"f"})),
('"facts__facts__ e "=no_quotes_here', Q(**{u"facts__facts__ e ": u"no_quotes_here"})),
('a__b__c=3', Q(**{u"a__b__c": 3})),
('a__b__c=3.14', Q(**{u"a__b__c": 3.14})),
('a__b__c=true', Q(**{u"a__b__c": True})),
('a__b__c=false', Q(**{u"a__b__c": False})),
('a__b__c=null', Q(**{u"a__b__c": None})),
('ansible_facts__a="true"', Q(**{u"ansible_facts__contains": {u"a": u"true"}})),
('ansible_facts__a__exact="true"', Q(**{u"ansible_facts__contains": {u"a": u"true"}})),
#('"a__b\"__c"="true"', Q(**{u"a__b\"__c": "true"})),
#('a__b\"__c="true"', Q(**{u"a__b\"__c": "true"})),
])
def test_query_generated(self, mock_get_host_model, filter_string, q_expected):
q = SmartFilter.query_from_string(filter_string)
assert str(q) == str(q_expected)
@pytest.mark.parametrize("filter_string", [
'ansible_facts__facts__facts__blank='
'ansible_facts__a__b__c__ space =ggg',
])
@pytest.mark.parametrize(
"filter_string",
[
'ansible_facts__facts__facts__blank=' 'ansible_facts__a__b__c__ space =ggg',
],
)
def test_invalid_filter_strings(self, mock_get_host_model, filter_string):
with pytest.raises(RuntimeError) as e:
SmartFilter.query_from_string(filter_string)
assert str(e.value) == u"Invalid query " + filter_string
@pytest.mark.parametrize("filter_string", [
'created_by__password__icontains=pbkdf2'
'search=foo or created_by__password__icontains=pbkdf2',
'created_by__password__icontains=pbkdf2 or search=foo',
])
@pytest.mark.parametrize(
"filter_string",
[
'created_by__password__icontains=pbkdf2' 'search=foo or created_by__password__icontains=pbkdf2',
'created_by__password__icontains=pbkdf2 or search=foo',
],
)
def test_forbidden_filter_string(self, mock_get_host_model, filter_string):
with pytest.raises(Exception) as e:
SmartFilter.query_from_string(filter_string)
"Filtering on password is not allowed." in str(e)
@pytest.mark.parametrize("filter_string,q_expected", [
(u'(a=abc\u1F5E3def)', Q(**{u"a": u"abc\u1F5E3def"})),
(u'(ansible_facts__a=abc\u1F5E3def)', Q(**{u"ansible_facts__contains": {u"a": u"abc\u1F5E3def"}})),
])
@pytest.mark.parametrize(
"filter_string,q_expected",
[
(u'(a=abc\u1F5E3def)', Q(**{u"a": u"abc\u1F5E3def"})),
(u'(ansible_facts__a=abc\u1F5E3def)', Q(**{u"ansible_facts__contains": {u"a": u"abc\u1F5E3def"}})),
],
)
def test_unicode(self, mock_get_host_model, filter_string, q_expected):
q = SmartFilter.query_from_string(filter_string)
assert str(q) == str(q_expected)
@pytest.mark.parametrize("filter_string,q_expected", [
('(a=b)', Q(**{u"a": u"b"})),
('a=b and c=d', Q(**{u"a": u"b"}) & Q(**{u"c": u"d"})),
('(a=b and c=d)', Q(**{u"a": u"b"}) & Q(**{u"c": u"d"})),
('a=b or c=d', Q(**{u"a": u"b"}) | Q(**{u"c": u"d"})),
('(a=b and c=d) or (e=f)', (Q(**{u"a": u"b"}) & Q(**{u"c": u"d"})) | (Q(**{u"e": u"f"}))),
(
'a=b or a=d or a=e or a=z and b=h and b=i and b=j and b=k',
Q(**{u"a": u"b"}) | Q(**{u"a": u"d"}) | Q(**{u"a": u"e"}) | Q(**{u"a": u"z"}) &
Q(**{u"b": u"h"}) & Q(**{u"b": u"i"}) & Q(**{u"b": u"j"}) & Q(**{u"b": u"k"})
)
])
@pytest.mark.parametrize(
"filter_string,q_expected",
[
('(a=b)', Q(**{u"a": u"b"})),
('a=b and c=d', Q(**{u"a": u"b"}) & Q(**{u"c": u"d"})),
('(a=b and c=d)', Q(**{u"a": u"b"}) & Q(**{u"c": u"d"})),
('a=b or c=d', Q(**{u"a": u"b"}) | Q(**{u"c": u"d"})),
('(a=b and c=d) or (e=f)', (Q(**{u"a": u"b"}) & Q(**{u"c": u"d"})) | (Q(**{u"e": u"f"}))),
(
'a=b or a=d or a=e or a=z and b=h and b=i and b=j and b=k',
Q(**{u"a": u"b"})
| Q(**{u"a": u"d"})
| Q(**{u"a": u"e"})
| Q(**{u"a": u"z"}) & Q(**{u"b": u"h"}) & Q(**{u"b": u"i"}) & Q(**{u"b": u"j"}) & Q(**{u"b": u"k"}),
),
],
)
def test_boolean_parenthesis(self, mock_get_host_model, filter_string, q_expected):
q = SmartFilter.query_from_string(filter_string)
assert str(q) == str(q_expected)
@pytest.mark.parametrize("filter_string,q_expected", [
('ansible_facts__a__b__c[]=3', Q(**{u"ansible_facts__contains": {u"a": {u"b": {u"c": [3]}}}})),
('ansible_facts__a__b__c[]=3.14', Q(**{u"ansible_facts__contains": {u"a": {u"b": {u"c": [3.14]}}}})),
('ansible_facts__a__b__c[]=true', Q(**{u"ansible_facts__contains": {u"a": {u"b": {u"c": [True]}}}})),
('ansible_facts__a__b__c[]=false', Q(**{u"ansible_facts__contains": {u"a": {u"b": {u"c": [False]}}}})),
('ansible_facts__a__b__c[]="true"', Q(**{u"ansible_facts__contains": {u"a": {u"b": {u"c": [u"true"]}}}})),
('ansible_facts__a__b__c[]="hello world"', Q(**{u"ansible_facts__contains": {u"a": {u"b": {u"c": [u"hello world"]}}}})),
('ansible_facts__a__b__c[]__d[]="foobar"', Q(**{u"ansible_facts__contains": {u"a": {u"b": {u"c": [{u"d": [u"foobar"]}]}}}})),
('ansible_facts__a__b__c[]__d="foobar"', Q(**{u"ansible_facts__contains": {u"a": {u"b": {u"c": [{u"d": u"foobar"}]}}}})),
('ansible_facts__a__b__c[]__d__e="foobar"', Q(**{u"ansible_facts__contains": {u"a": {u"b": {u"c": [{u"d": {u"e": u"foobar"}}]}}}})),
('ansible_facts__a__b__c[]__d__e[]="foobar"', Q(**{u"ansible_facts__contains": {u"a": {u"b": {u"c": [{u"d": {u"e": [u"foobar"]}}]}}}})),
('ansible_facts__a__b__c[]__d__e__f[]="foobar"', Q(**{u"ansible_facts__contains": {u"a": {u"b": {u"c": [{u"d": {u"e": {u"f": [u"foobar"]}}}]}}}})),
(
'(ansible_facts__a__b__c[]__d__e__f[]="foobar") and (ansible_facts__a__b__c[]__d__e[]="foobar")',
Q(**{ u"ansible_facts__contains": {u"a": {u"b": {u"c": [{u"d": {u"e": {u"f": [u"foobar"]}}}]}}}}) &
Q(**{u"ansible_facts__contains": {u"a": {u"b": {u"c": [{u"d": {u"e": [u"foobar"]}}]}}}})),
#('"a__b\"__c"="true"', Q(**{u"a__b\"__c": "true"})),
#('a__b\"__c="true"', Q(**{u"a__b\"__c": "true"})),
])
@pytest.mark.parametrize(
"filter_string,q_expected",
[
('ansible_facts__a__b__c[]=3', Q(**{u"ansible_facts__contains": {u"a": {u"b": {u"c": [3]}}}})),
('ansible_facts__a__b__c[]=3.14', Q(**{u"ansible_facts__contains": {u"a": {u"b": {u"c": [3.14]}}}})),
('ansible_facts__a__b__c[]=true', Q(**{u"ansible_facts__contains": {u"a": {u"b": {u"c": [True]}}}})),
('ansible_facts__a__b__c[]=false', Q(**{u"ansible_facts__contains": {u"a": {u"b": {u"c": [False]}}}})),
('ansible_facts__a__b__c[]="true"', Q(**{u"ansible_facts__contains": {u"a": {u"b": {u"c": [u"true"]}}}})),
('ansible_facts__a__b__c[]="hello world"', Q(**{u"ansible_facts__contains": {u"a": {u"b": {u"c": [u"hello world"]}}}})),
('ansible_facts__a__b__c[]__d[]="foobar"', Q(**{u"ansible_facts__contains": {u"a": {u"b": {u"c": [{u"d": [u"foobar"]}]}}}})),
('ansible_facts__a__b__c[]__d="foobar"', Q(**{u"ansible_facts__contains": {u"a": {u"b": {u"c": [{u"d": u"foobar"}]}}}})),
('ansible_facts__a__b__c[]__d__e="foobar"', Q(**{u"ansible_facts__contains": {u"a": {u"b": {u"c": [{u"d": {u"e": u"foobar"}}]}}}})),
('ansible_facts__a__b__c[]__d__e[]="foobar"', Q(**{u"ansible_facts__contains": {u"a": {u"b": {u"c": [{u"d": {u"e": [u"foobar"]}}]}}}})),
('ansible_facts__a__b__c[]__d__e__f[]="foobar"', Q(**{u"ansible_facts__contains": {u"a": {u"b": {u"c": [{u"d": {u"e": {u"f": [u"foobar"]}}}]}}}})),
(
'(ansible_facts__a__b__c[]__d__e__f[]="foobar") and (ansible_facts__a__b__c[]__d__e[]="foobar")',
Q(**{u"ansible_facts__contains": {u"a": {u"b": {u"c": [{u"d": {u"e": {u"f": [u"foobar"]}}}]}}}})
& Q(**{u"ansible_facts__contains": {u"a": {u"b": {u"c": [{u"d": {u"e": [u"foobar"]}}]}}}}),
),
# ('"a__b\"__c"="true"', Q(**{u"a__b\"__c": "true"})),
# ('a__b\"__c="true"', Q(**{u"a__b\"__c": "true"})),
],
)
def test_contains_query_generated(self, mock_get_host_model, filter_string, q_expected):
q = SmartFilter.query_from_string(filter_string)
assert str(q) == str(q_expected)
@pytest.mark.parametrize("filter_string,q_expected", [
#('a__b__c[]="true"', Q(**{u"a__b__c__contains": u"\"true\""})),
('ansible_facts__a="true"', Q(**{u"ansible_facts__contains": {u"a": u"true"}})),
#('"a__b\"__c"="true"', Q(**{u"a__b\"__c": "true"})),
#('a__b\"__c="true"', Q(**{u"a__b\"__c": "true"})),
])
@pytest.mark.parametrize(
"filter_string,q_expected",
[
# ('a__b__c[]="true"', Q(**{u"a__b__c__contains": u"\"true\""})),
('ansible_facts__a="true"', Q(**{u"ansible_facts__contains": {u"a": u"true"}})),
# ('"a__b\"__c"="true"', Q(**{u"a__b\"__c": "true"})),
# ('a__b\"__c="true"', Q(**{u"a__b\"__c": "true"})),
],
)
def test_contains_query_generated_unicode(self, mock_get_host_model, filter_string, q_expected):
q = SmartFilter.query_from_string(filter_string)
assert str(q) == str(q_expected)
@pytest.mark.parametrize("filter_string,q_expected", [
('ansible_facts__a=null', Q(**{u"ansible_facts__contains": {u"a": None}})),
('ansible_facts__c="null"', Q(**{u"ansible_facts__contains": {u"c": u"\"null\""}})),
])
@pytest.mark.parametrize(
"filter_string,q_expected",
[
('ansible_facts__a=null', Q(**{u"ansible_facts__contains": {u"a": None}})),
('ansible_facts__c="null"', Q(**{u"ansible_facts__contains": {u"c": u"\"null\""}})),
],
)
def test_contains_query_generated_null(self, mock_get_host_model, filter_string, q_expected):
q = SmartFilter.query_from_string(filter_string)
assert str(q) == str(q_expected)
@pytest.mark.parametrize("filter_string,q_expected", [
('group__search=foo', Q(Q(**{u"group__name__icontains": u"foo"}) | Q(**{u"group__description__icontains": u"foo"}))),
('search=foo and group__search=foo', Q(
Q(**{u"name__icontains": u"foo"}) | Q(**{ u"description__icontains": u"foo"}),
Q(**{u"group__name__icontains": u"foo"}) | Q(**{u"group__description__icontains": u"foo"}))),
('search=foo or ansible_facts__a=null',
Q(Q(**{u"name__icontains": u"foo"}) | Q(**{u"description__icontains": u"foo"})) |
Q(**{u"ansible_facts__contains": {u"a": None}})),
])
@pytest.mark.parametrize(
"filter_string,q_expected",
[
('group__search=foo', Q(Q(**{u"group__name__icontains": u"foo"}) | Q(**{u"group__description__icontains": u"foo"}))),
(
'search=foo and group__search=foo',
Q(
Q(**{u"name__icontains": u"foo"}) | Q(**{u"description__icontains": u"foo"}),
Q(**{u"group__name__icontains": u"foo"}) | Q(**{u"group__description__icontains": u"foo"}),
),
),
(
'search=foo or ansible_facts__a=null',
Q(Q(**{u"name__icontains": u"foo"}) | Q(**{u"description__icontains": u"foo"})) | Q(**{u"ansible_facts__contains": {u"a": None}}),
),
],
)
def test_search_related_fields(self, mock_get_host_model, filter_string, q_expected):
q = SmartFilter.query_from_string(filter_string)
assert str(q) == str(q_expected)
class TestSmartFilterQueryFromStringNoDB():
@pytest.mark.parametrize("filter_string,q_expected", [
('ansible_facts__a="true" and ansible_facts__b="true" and ansible_facts__c="true"',
(Q(**{u"ansible_facts__contains": {u"a": u"true"}}) &
Q(**{u"ansible_facts__contains": {u"b": u"true"}}) &
Q(**{u"ansible_facts__contains": {u"c": u"true"}}))),
('ansible_facts__a="true" or ansible_facts__b="true" or ansible_facts__c="true"',
(Q(**{u"ansible_facts__contains": {u"a": u"true"}}) |
Q(**{u"ansible_facts__contains": {u"b": u"true"}}) |
Q(**{u"ansible_facts__contains": {u"c": u"true"}}))),
('search=foo',
Q(Q(**{ u"description__icontains": u"foo"}) | Q(**{u"name__icontains": u"foo"}))),
('search=foo and ansible_facts__a="null"',
Q(Q(**{u"description__icontains": u"foo"}) | Q(**{u"name__icontains": u"foo"})) &
Q(**{u"ansible_facts__contains": {u"a": u"\"null\""}})),
('name=foo or name=bar and name=foobar',
Q(name="foo") | Q(name="bar") & Q(name="foobar"))
])
class TestSmartFilterQueryFromStringNoDB:
@pytest.mark.parametrize(
"filter_string,q_expected",
[
(
'ansible_facts__a="true" and ansible_facts__b="true" and ansible_facts__c="true"',
(
Q(**{u"ansible_facts__contains": {u"a": u"true"}})
& Q(**{u"ansible_facts__contains": {u"b": u"true"}})
& Q(**{u"ansible_facts__contains": {u"c": u"true"}})
),
),
(
'ansible_facts__a="true" or ansible_facts__b="true" or ansible_facts__c="true"',
(
Q(**{u"ansible_facts__contains": {u"a": u"true"}})
| Q(**{u"ansible_facts__contains": {u"b": u"true"}})
| Q(**{u"ansible_facts__contains": {u"c": u"true"}})
),
),
('search=foo', Q(Q(**{u"description__icontains": u"foo"}) | Q(**{u"name__icontains": u"foo"}))),
(
'search=foo and ansible_facts__a="null"',
Q(Q(**{u"description__icontains": u"foo"}) | Q(**{u"name__icontains": u"foo"})) & Q(**{u"ansible_facts__contains": {u"a": u"\"null\""}}),
),
('name=foo or name=bar and name=foobar', Q(name="foo") | Q(name="bar") & Q(name="foobar")),
],
)
def test_does_not_invoke_db(self, filter_string, q_expected):
q = SmartFilter.query_from_string(filter_string)
assert str(q.query) == str(Host.objects.filter(q_expected).query)

View File

@@ -1,4 +1,3 @@
from awx.main.models import Job, JobEvent
from awx.main.utils.formatters import LogstashFormatter
@@ -9,8 +8,7 @@ def test_log_from_job_event_object():
event = JobEvent(job_id=job.id)
formatter = LogstashFormatter()
data_for_log = formatter.reformat_data_for_log(
dict(python_objects=dict(job_event=event)), kind='job_events')
data_for_log = formatter.reformat_data_for_log(dict(python_objects=dict(job_event=event)), kind='job_events')
# Check entire body of data for any exceptions from getattr on event object
for fd in data_for_log:

View File

@@ -7,8 +7,7 @@ from awx.main.tests.data.insights import TEST_INSIGHTS_HOSTS, TEST_INSIGHTS_PLAN
def test_filter_insights_api_response():
actual = filter_insights_api_response(
TEST_INSIGHTS_HOSTS['results'][0], TEST_INSIGHTS_PLANS, TEST_INSIGHTS_REMEDIATIONS)
actual = filter_insights_api_response(TEST_INSIGHTS_HOSTS['results'][0], TEST_INSIGHTS_PLANS, TEST_INSIGHTS_REMEDIATIONS)
assert actual['last_check_in'] == '2019-03-19T21:59:09.213151-04:00'
assert len(actual['reports']) == 5
@@ -17,11 +16,11 @@ def test_filter_insights_api_response():
rule = actual['reports'][0]['rule']
assert rule['severity'] == 'WARN'
assert rule['description'] == (
"Kernel vulnerable to side-channel attacks in modern microprocessors (CVE-2017-5715/Spectre)")
assert rule['description'] == ("Kernel vulnerable to side-channel attacks in modern microprocessors (CVE-2017-5715/Spectre)")
assert rule['category'] == 'Security'
assert rule['summary'] == (
"A vulnerability was discovered in modern microprocessors supported by the kernel,"
" whereby an unprivileged attacker can use this flaw to bypass restrictions to gain read"
" access to privileged memory.\nThe issue was reported as [CVE-2017-5715 / Spectre]"
"(https://access.redhat.com/security/cve/CVE-2017-5715).\n")
"(https://access.redhat.com/security/cve/CVE-2017-5715).\n"
)

View File

@@ -1,8 +1,5 @@
# AWX utils
from awx.main.utils.mem_inventory import (
MemInventory,
mem_data_to_dict, dict_to_mem_data
)
from awx.main.utils.mem_inventory import MemInventory, mem_data_to_dict, dict_to_mem_data
import pytest
import json
@@ -24,18 +21,10 @@ def memory_inventory():
def JSON_of_inv():
# Implemented as fixture becuase it may be change inside of tests
return {
"_meta": {
"hostvars": {
"group_host": {},
"my_host": {"foo": "bar"}
}
},
"all": {"children": ["my_group", "ungrouped"]},
"my_group": {
"hosts": ["group_host"],
"vars": {"foobar": "barfoo"}
},
"ungrouped": {"hosts": ["my_host"]}
"_meta": {"hostvars": {"group_host": {}, "my_host": {"foo": "bar"}}},
"all": {"children": ["my_group", "ungrouped"]},
"my_group": {"hosts": ["group_host"], "vars": {"foobar": "barfoo"}},
"ungrouped": {"hosts": ["my_host"]},
}
@@ -66,6 +55,7 @@ def JSON_with_lists():
# MemObject basic operations tests
@pytest.mark.inventory_import
def test_inventory_create_all_group():
inventory = MemInventory()
@@ -97,6 +87,7 @@ def test_ungrouped_mechanics():
# MemObject --> JSON tests
@pytest.mark.inventory_import
def test_convert_memory_to_JSON_with_vars(memory_inventory):
data = mem_data_to_dict(memory_inventory)
@@ -109,6 +100,7 @@ def test_convert_memory_to_JSON_with_vars(memory_inventory):
# JSON --> MemObject tests
@pytest.mark.inventory_import
def test_convert_JSON_to_memory_with_vars(JSON_of_inv):
inventory = dict_to_mem_data(JSON_of_inv)

View File

@@ -10,6 +10,12 @@ def test_produce_supervisor_command(mocker):
with mocker.patch.object(reload.subprocess, 'Popen', Popen_mock):
reload.supervisor_service_command("restart")
reload.subprocess.Popen.assert_called_once_with(
['supervisorctl', 'restart', 'tower-processes:*',],
stderr=-1, stdin=-1, stdout=-1)
[
'supervisorctl',
'restart',
'tower-processes:*',
],
stderr=-1,
stdin=-1,
stdout=-1,
)

View File

@@ -63,11 +63,7 @@ def test_kv_unsafe_deep_nesting():
def test_kv_unsafe_multiple():
assert safe_dump({'a': 'b', 'c': 'd'}) == '\n'.join([
"!unsafe 'a': !unsafe 'b'",
"!unsafe 'c': !unsafe 'd'",
""
])
assert safe_dump({'a': 'b', 'c': 'd'}) == '\n'.join(["!unsafe 'a': !unsafe 'b'", "!unsafe 'c': !unsafe 'd'", ""])
def test_safe_marking():
@@ -75,11 +71,7 @@ def test_safe_marking():
def test_safe_marking_mixed():
assert safe_dump({'a': 'b', 'c': 'd'}, safe_dict={'a': 'b'}) == '\n'.join([
"a: b",
"!unsafe 'c': !unsafe 'd'",
""
])
assert safe_dump({'a': 'b', 'c': 'd'}, safe_dict={'a': 'b'}) == '\n'.join(["a: b", "!unsafe 'c': !unsafe 'd'", ""])
def test_safe_marking_deep_nesting():