448807a706
In PyYAML 5.1 they changed this default in the library. Because we weren't explicitly setting it, this broke our unit tests. Since default_flow_style=False is usually what you want (hence why PyYAML changed it), let's set it explicitly. Also note that this was already being set for dump, but not for dumps. This is also more consistent. Change-Id: I3f7dfce5ceb5d3b802878c410406994cbb0988b6
88 lines
2.5 KiB
Python
88 lines
2.5 KiB
Python
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
|
# not use this file except in compliance with the License. You may obtain
|
|
# a copy of the License at
|
|
#
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
#
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
# License for the specific language governing permissions and limitations
|
|
# under the License.
|
|
|
|
import os
|
|
import tempfile
|
|
import textwrap
|
|
import uuid
|
|
|
|
from oslotest import base
|
|
|
|
from oslo_serialization import yamlutils as yaml
|
|
|
|
|
|
class BehaviorTestCase(base.BaseTestCase):
|
|
|
|
def test_loading(self):
|
|
payload = textwrap.dedent('''
|
|
- foo: bar
|
|
- list:
|
|
- [one, two]
|
|
- {check: yaml, in: test}
|
|
''')
|
|
expected = [
|
|
{'foo': 'bar'},
|
|
{'list': None},
|
|
['one', 'two'],
|
|
{'check': 'yaml', 'in': 'test'}
|
|
]
|
|
loaded = yaml.load(payload)
|
|
self.assertEqual(loaded, expected)
|
|
|
|
def test_loading_with_unsafe(self):
|
|
payload = textwrap.dedent('''
|
|
!!python/object/apply:os.system ['echo "hello"']
|
|
''')
|
|
loaded = yaml.load(payload, is_safe=False)
|
|
expected = 0
|
|
self.assertEqual(loaded, expected)
|
|
|
|
def test_dumps(self):
|
|
payload = [
|
|
{'foo': 'bar'},
|
|
{'list': None},
|
|
['one', 'two'],
|
|
{'check': 'yaml', 'in': 'test'}
|
|
]
|
|
dumped = yaml.dumps(payload)
|
|
expected = textwrap.dedent('''\
|
|
- foo: bar
|
|
- list: null
|
|
- - one
|
|
- two
|
|
- check: yaml
|
|
in: test
|
|
''')
|
|
self.assertEqual(dumped, expected)
|
|
|
|
def test_dump(self):
|
|
payload = [
|
|
{'foo': 'bar'},
|
|
{'list': None},
|
|
['one', 'two'],
|
|
{'check': 'yaml', 'in': 'test'}
|
|
]
|
|
tmpfile = os.path.join(tempfile.gettempdir(), str(uuid.uuid4()))
|
|
with open(tmpfile, 'w+') as fp:
|
|
yaml.dump(payload, fp)
|
|
with open(tmpfile, 'r') as fp:
|
|
file_content = fp.read()
|
|
expected = textwrap.dedent('''\
|
|
- foo: bar
|
|
- list: null
|
|
- - one
|
|
- two
|
|
- check: yaml
|
|
in: test
|
|
''')
|
|
self.assertEqual(file_content, expected)
|