Overhauls to v2 code

* using inspect module instead of iteritems(self.__class__.__dict__, due
  to the fact that the later does not include attributes from parent
  classes
* added tags/when attributes to Base() class for use by all subclasses
* removed value/callable code from Attribute, as they are not used
* started moving some limited code from utils to new places in v2 tree
  (vault, yaml-parsing related defs)
* re-added ability of Block.load() to create implicit blocks from tasks
* started overhaul of Role class and role-related code
This commit is contained in:
James Cammarata 2014-10-19 00:14:30 -05:00
commit b0069a338e
9 changed files with 1130 additions and 91 deletions

View file

@ -20,7 +20,7 @@ from __future__ import (absolute_import, division, print_function)
__metaclass__ = type
from .. compat import unittest
from ansible.parsing import load
from ansible.parsing import load_data
from ansible.errors import AnsibleParserError
import json
@ -68,12 +68,12 @@ class TestGeneralParsing(unittest.TestCase):
"jkl" : 5678
}
"""
output = load(input)
output = load_data(input)
self.assertEqual(output['asdf'], '1234')
self.assertEqual(output['jkl'], 5678)
def parse_json_from_file(self):
output = load(MockFile(dict(a=1,b=2,c=3)),'json')
output = load_data(MockFile(dict(a=1,b=2,c=3)),'json')
self.assertEqual(ouput, dict(a=1,b=2,c=3))
def parse_yaml_from_dict(self):
@ -81,12 +81,12 @@ class TestGeneralParsing(unittest.TestCase):
asdf: '1234'
jkl: 5678
"""
output = load(input)
output = load_data(input)
self.assertEqual(output['asdf'], '1234')
self.assertEqual(output['jkl'], 5678)
def parse_yaml_from_file(self):
output = load(MockFile(dict(a=1,b=2,c=3),'yaml'))
output = load_data(MockFile(dict(a=1,b=2,c=3),'yaml'))
self.assertEqual(output, dict(a=1,b=2,c=3))
def parse_fail(self):
@ -95,10 +95,10 @@ class TestGeneralParsing(unittest.TestCase):
***
NOT VALID
"""
self.assertRaises(load(input), AnsibleParserError)
self.assertRaises(load_data(input), AnsibleParserError)
def parse_fail_from_file(self):
self.assertRaises(load(MockFile(None,'fail')), AnsibleParserError)
self.assertRaises(load_data(MockFile(None,'fail')), AnsibleParserError)
def parse_fail_invalid_type(self):
self.assertRaises(3000, AnsibleParsingError)

View file

@ -49,31 +49,39 @@ class TestBlock(unittest.TestCase):
def test_load_block_simple(self):
ds = dict(
begin = [],
block = [],
rescue = [],
end = [],
otherwise = [],
always = [],
#otherwise = [],
)
b = Block.load(ds)
self.assertEqual(b.begin, [])
self.assertEqual(b.block, [])
self.assertEqual(b.rescue, [])
self.assertEqual(b.end, [])
self.assertEqual(b.otherwise, [])
self.assertEqual(b.always, [])
# not currently used
#self.assertEqual(b.otherwise, [])
def test_load_block_with_tasks(self):
ds = dict(
begin = [dict(action='begin')],
block = [dict(action='block')],
rescue = [dict(action='rescue')],
end = [dict(action='end')],
otherwise = [dict(action='otherwise')],
always = [dict(action='always')],
#otherwise = [dict(action='otherwise')],
)
b = Block.load(ds)
self.assertEqual(len(b.begin), 1)
assert isinstance(b.begin[0], Task)
self.assertEqual(len(b.block), 1)
assert isinstance(b.block[0], Task)
self.assertEqual(len(b.rescue), 1)
assert isinstance(b.rescue[0], Task)
self.assertEqual(len(b.end), 1)
assert isinstance(b.end[0], Task)
self.assertEqual(len(b.otherwise), 1)
assert isinstance(b.otherwise[0], Task)
self.assertEqual(len(b.always), 1)
assert isinstance(b.always[0], Task)
# not currently used
#self.assertEqual(len(b.otherwise), 1)
#assert isinstance(b.otherwise[0], Task)
def test_load_implicit_block(self):
ds = [dict(action='foo')]
b = Block.load(ds)
self.assertEqual(len(b.block), 1)
assert isinstance(b.block[0], Task)

View file

@ -0,0 +1,52 @@
# (c) 2012-2014, Michael DeHaan <michael.dehaan@gmail.com>
#
# This file is part of Ansible
#
# Ansible is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Ansible is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
# Make coding more python3-ish
from __future__ import (absolute_import, division, print_function)
__metaclass__ = type
from ansible.playbook.block import Block
from ansible.playbook.role import Role
from ansible.playbook.task import Task
from .. compat import unittest
class TestRole(unittest.TestCase):
def setUp(self):
pass
def tearDown(self):
pass
def test_construct_empty_block(self):
r = Role()
def test_role__load_list_of_blocks(self):
task = dict(action='test')
r = Role()
self.assertEqual(r._load_list_of_blocks([]), [])
res = r._load_list_of_blocks([task])
self.assertEqual(len(res), 1)
assert isinstance(res[0], Block)
res = r._load_list_of_blocks([task,task,task])
self.assertEqual(len(res), 3)
def test_load_role_simple(self):
pass
def test_load_role_complex(self):
pass