From 349ecf6efe54e9144285d1f4170ef0d8ef241ff2 Mon Sep 17 00:00:00 2001 From: Toshio Kuratomi Date: Fri, 3 Apr 2015 11:35:01 -0700 Subject: [PATCH] Add a vault test to data_loader test and some additional yaml tests to parsing/yaml/test_loader --- v2/test/parsing/test_data_loader.py | 22 ++++++++++++++- v2/test/parsing/yaml/test_loader.py | 43 +++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+), 1 deletion(-) diff --git a/v2/test/parsing/test_data_loader.py b/v2/test/parsing/test_data_loader.py index 370046dbf34..75ceb662f73 100644 --- a/v2/test/parsing/test_data_loader.py +++ b/v2/test/parsing/test_data_loader.py @@ -22,7 +22,7 @@ __metaclass__ = type from yaml.scanner import ScannerError from ansible.compat.tests import unittest -from ansible.compat.tests.mock import patch +from ansible.compat.tests.mock import patch, mock_open from ansible.errors import AnsibleParserError from ansible.parsing import DataLoader @@ -62,3 +62,23 @@ class TestDataLoader(unittest.TestCase): """, True) self.assertRaises(AnsibleParserError, self._loader.load_from_file, 'dummy_yaml_bad.txt') +class TestDataLoaderWithVault(unittest.TestCase): + + def setUp(self): + self._loader = DataLoader(vault_password='ansible') + + def tearDown(self): + pass + + @patch.multiple(DataLoader, path_exists=lambda s, x: True, is_file=lambda s, x: True) + def test_parse_from_vault_1_1_file(self): + vaulted_data = """$ANSIBLE_VAULT;1.1;AES256 +33343734386261666161626433386662623039356366656637303939306563376130623138626165 +6436333766346533353463636566313332623130383662340a393835656134633665333861393331 +37666233346464636263636530626332623035633135363732623332313534306438393366323966 +3135306561356164310a343937653834643433343734653137383339323330626437313562306630 +3035 +""" + with patch('__builtin__.open', mock_open(read_data=vaulted_data)): + output = self._loader.load_from_file('dummy_vault.txt') + self.assertEqual(output, dict(foo='bar')) diff --git a/v2/test/parsing/yaml/test_loader.py b/v2/test/parsing/yaml/test_loader.py index 4c569626100..9a4746b99df 100644 --- a/v2/test/parsing/yaml/test_loader.py +++ b/v2/test/parsing/yaml/test_loader.py @@ -101,6 +101,49 @@ class TestAnsibleLoaderBasic(unittest.TestCase): self.assertEqual(data[0].ansible_pos, ('myfile.yml', 2, 19)) self.assertEqual(data[1].ansible_pos, ('myfile.yml', 3, 19)) + def test_parse_short_dict(self): + stream = StringIO("""{"foo": "bar"}""") + loader = AnsibleLoader(stream, 'myfile.yml') + data = loader.get_single_data() + self.assertEqual(data, dict(foo=u'bar')) + + self.assertEqual(data.ansible_pos, ('myfile.yml', 1, 1)) + self.assertEqual(data[u'foo'].ansible_pos, ('myfile.yml', 1, 9)) + + stream = StringIO("""foo: bar""") + loader = AnsibleLoader(stream, 'myfile.yml') + data = loader.get_single_data() + self.assertEqual(data, dict(foo=u'bar')) + + self.assertEqual(data.ansible_pos, ('myfile.yml', 1, 1)) + self.assertEqual(data[u'foo'].ansible_pos, ('myfile.yml', 1, 6)) + + def test_error_conditions(self): + stream = StringIO("""{""") + loader = AnsibleLoader(stream, 'myfile.yml') + self.assertRaises(loader.get_single_data) + + def test_front_matter(self): + stream = StringIO("""---\nfoo: bar""") + loader = AnsibleLoader(stream, 'myfile.yml') + data = loader.get_single_data() + self.assertEqual(data, dict(foo=u'bar')) + + self.assertEqual(data.ansible_pos, ('myfile.yml', 2, 1)) + self.assertEqual(data[u'foo'].ansible_pos, ('myfile.yml', 2, 6)) + + # Initial indent (See: #6348) + stream = StringIO(""" - foo: bar\n baz: qux""") + loader = AnsibleLoader(stream, 'myfile.yml') + data = loader.get_single_data() + self.assertEqual(data, [{u'foo': u'bar', u'baz': u'qux'}]) + + self.assertEqual(data.ansible_pos, ('myfile.yml', 1, 2)) + self.assertEqual(data[0].ansible_pos, ('myfile.yml', 1, 4)) + self.assertEqual(data[0][u'foo'].ansible_pos, ('myfile.yml', 1, 9)) + self.assertEqual(data[0][u'baz'].ansible_pos, ('myfile.yml', 2, 9)) + + class TestAnsibleLoaderPlay(unittest.TestCase): def setUp(self):