Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
A
ansible
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
OpenEdx
ansible
Commits
b3452de8
Commit
b3452de8
authored
Oct 21, 2014
by
Toshio Kuratomi
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Hook up the general parsing unittests and get them passing
parent
0a8c91a8
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
37 additions
and
39 deletions
+37
-39
v2/ansible/parsing/__init__.py
+8
-8
v2/test/parsing/test_general.py
+29
-31
No files found.
v2/ansible/parsing/__init__.py
View file @
b3452de8
...
...
@@ -23,23 +23,23 @@ import json
from
yaml
import
YAMLError
from
ansible.errors
import
AnsibleError
,
AnsibleInternalError
from
ansible.errors
import
Ansible
Parser
Error
,
AnsibleInternalError
from
ansible.parsing.vault
import
VaultLib
from
ansible.parsing.yaml
import
safe_load
def
load
(
data
):
if
isinstance
(
data
,
file
):
fd
=
open
(
f
)
data
=
fd
.
read
()
fd
.
close
()
if
hasattr
(
data
,
'read'
)
and
hasattr
(
data
.
read
,
'__call__'
):
data
=
data
.
read
()
if
isinstance
(
data
,
basestring
):
try
:
return
json
.
loads
(
data
)
try
:
return
json
.
loads
(
data
)
except
:
return
safe_load
(
data
)
except
:
r
eturn
safe_load
(
data
)
r
aise
AnsibleParserError
(
"data was not valid yaml"
)
raise
AnsibleInternalError
(
"expected file or string, got
%
s"
%
type
(
data
))
v2/test/parsing/test_general.py
View file @
b3452de8
...
...
@@ -20,10 +20,11 @@ from __future__ import (absolute_import, division, print_function)
__metaclass__
=
type
from
ansible.compat.tests
import
unittest
from
ansible.errors
import
AnsibleParserError
from
ansible.errors
import
Ansible
InternalError
,
Ansible
ParserError
from
ansible.parsing
import
load
import
json
import
yaml
from
io
import
FileIO
...
...
@@ -34,13 +35,14 @@ class MockFile(FileIO):
self
.
method
=
method
def
read
(
self
):
if
method
==
'json'
:
return
json
.
dumps
(
ds
)
elif
method
==
'yaml'
:
return
yaml
.
dump
s
(
ds
)
elif
method
==
'fail'
:
if
self
.
method
==
'json'
:
return
json
.
dumps
(
self
.
ds
)
elif
self
.
method
==
'yaml'
:
return
yaml
.
dump
(
self
.
ds
)
elif
self
.
method
==
'fail'
:
return
"""
AAARGGGGH
AAARGGGGH:
*****
THIS WON'T PARSE !!!
NOOOOOOOOOOOOOOOOOO
"""
...
...
@@ -51,56 +53,52 @@ class MockFile(FileIO):
pass
class
TestGeneralParsing
(
unittest
.
TestCase
):
def
__init__
(
self
):
pass
def
setUp
(
self
):
pass
def
tearDown
(
self
):
pass
def
parse_json_from_string
(
self
):
input
=
"""
def
test_
parse_json_from_string
(
self
):
data
=
"""
{
"asdf" : "1234",
"jkl" : 5678
}
"""
output
=
load
_data
(
input
)
output
=
load
(
data
)
self
.
assertEqual
(
output
[
'asdf'
],
'1234'
)
self
.
assertEqual
(
output
[
'jkl'
],
5678
)
def
parse_json_from_file
(
self
):
output
=
load
_data
(
MockFile
(
dict
(
a
=
1
,
b
=
2
,
c
=
3
)),
'json'
)
self
.
assertEqual
(
ouput
,
dict
(
a
=
1
,
b
=
2
,
c
=
3
))
def
test_
parse_json_from_file
(
self
):
output
=
load
(
MockFile
(
dict
(
a
=
1
,
b
=
2
,
c
=
3
),
'json'
)
)
self
.
assertEqual
(
ou
t
put
,
dict
(
a
=
1
,
b
=
2
,
c
=
3
))
def
parse_yaml_from_dict
(
self
):
input
=
"""
def
test_
parse_yaml_from_dict
(
self
):
data
=
"""
asdf: '1234'
jkl: 5678
"""
output
=
load
_data
(
input
)
output
=
load
(
data
)
self
.
assertEqual
(
output
[
'asdf'
],
'1234'
)
self
.
assertEqual
(
output
[
'jkl'
],
5678
)
def
parse_yaml_from_file
(
self
):
output
=
load
_data
(
MockFile
(
dict
(
a
=
1
,
b
=
2
,
c
=
3
),
'yaml'
))
def
test_
parse_yaml_from_file
(
self
):
output
=
load
(
MockFile
(
dict
(
a
=
1
,
b
=
2
,
c
=
3
),
'yaml'
))
self
.
assertEqual
(
output
,
dict
(
a
=
1
,
b
=
2
,
c
=
3
))
def
parse_fail
(
self
):
input
=
"""
TEXT
def
test_
parse_fail
(
self
):
data
=
"""
TEXT
:
***
NOT VALID
"""
self
.
assertRaises
(
load_data
(
input
),
AnsibleParserError
)
self
.
assertRaises
(
AnsibleParserError
,
load
,
data
)
def
parse_fail_from_file
(
self
):
self
.
assertRaises
(
load_data
(
MockFile
(
None
,
'fail'
)),
AnsibleParserError
)
def
test_
parse_fail_from_file
(
self
):
self
.
assertRaises
(
AnsibleParserError
,
load
,
MockFile
(
None
,
'fail'
)
)
def
parse_fail_invalid_type
(
self
):
self
.
assertRaises
(
3000
,
AnsibleParsingError
)
self
.
assertRaises
(
dict
(
a
=
1
,
b
=
2
,
c
=
3
),
AnsibleParserError
)
def
test_
parse_fail_invalid_type
(
self
):
self
.
assertRaises
(
AnsibleInternalError
,
load
,
3000
)
self
.
assertRaises
(
AnsibleInternalError
,
load
,
dict
(
a
=
1
,
b
=
2
,
c
=
3
)
)
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment