Use safe loader/dumper to load/dump YAMLs things

This also prevent Octavia job from voting

Change-Id: Ia2eb26d3b304c6a3858975365f5b738f43ec1c5c
This commit is contained in:
Federico Ressi 2021-10-25 09:31:41 +02:00
parent 7b852c0f0a
commit ff351b69d2
16 changed files with 52 additions and 34 deletions

View File

@ -34,6 +34,7 @@ from tobiko.common import _skip
from tobiko.common import _testcase
from tobiko.common import _time
from tobiko.common import _utils
from tobiko.common import _yaml
TOBIKO_PACKAGE_DIR = os.path.dirname(os.path.realpath(__file__))
@ -147,5 +148,8 @@ true_seconds = _time.true_seconds
get_short_hostname = _utils.get_short_hostname
dump_yaml = _yaml.dump_yaml
load_yaml = _yaml.load_yaml
from tobiko import config # noqa
config.init_config()

View File

@ -21,7 +21,8 @@ from oslo_log import log
import six
import testtools
from testtools import content
import yaml
from tobiko.common import _yaml
LOG = log.getLogger(__name__)
@ -120,7 +121,7 @@ def get_yaml_to_get_bytes(get_yaml):
def get_text():
obj = get_yaml()
yield yaml.dump(obj).encode(errors='ignore')
yield _yaml.dump_yaml(obj).encode(errors='ignore')
return get_text

24
tobiko/common/_yaml.py Normal file
View File

@ -0,0 +1,24 @@
# Copyright 2021 Red Hat
#
# 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.
from __future__ import absolute_import
import yaml
def load_yaml(stream):
return yaml.safe_load(stream)
def dump_yaml(data, stream=None, **kwargs):
return yaml.safe_dump(data, stream=stream, **kwargs)

View File

@ -19,7 +19,6 @@ import sys
import typing
from heatclient.common import template_utils
import yaml
import tobiko
@ -52,7 +51,7 @@ class HeatTemplateFixture(tobiko.SharedFixture):
tobiko.check_valid_type(self.outputs, collections.Mapping)
tobiko.check_valid_type(self.parameters, collections.Mapping)
tobiko.check_valid_type(self.resources, collections.Mapping)
self.template_yaml = yaml.safe_dump(self.template)
self.template_yaml = tobiko.dump_yaml(self.template)
@property
def outputs(self) -> typing.Dict[str, typing.Any]:

View File

@ -17,7 +17,6 @@ import json
import os
from oslo_log import log
import yaml
import tobiko
from tobiko.openstack.keystone import _credentials
@ -181,7 +180,7 @@ class CloudsFileKeystoneCredentialsFixture(
clouds_content = json.load(f)
else:
LOG.debug('Load YAML clouds file: %r', clouds_file)
clouds_content = yaml.safe_load(f)
clouds_content = tobiko.load_yaml(f)
LOG.debug('Clouds file content loaded from %r:\n%s',
clouds_file, json.dumps(clouds_content,
indent=4,

View File

@ -20,7 +20,6 @@ import typing # noqa
from oslo_log import log
import testtools
import yaml
import tobiko
@ -345,7 +344,7 @@ def api_version_from_url(auth_url) -> typing.Optional[int]:
def print_credentials():
credentials = default_keystone_credentials()
yaml.dump(dict(credentials.to_dict()),
sys.stdout,
indent=4,
sort_keys=True)
tobiko.dump_yaml(dict(credentials.to_dict()),
sys.stdout,
indent=4,
sort_keys=True)

View File

@ -18,7 +18,6 @@ import contextlib
import typing
from oslo_log import log
import yaml
import tobiko
from tobiko.shell import sh
@ -81,7 +80,7 @@ class CloudConfig(dict):
@property
def user_data(self):
return '#cloud-config\n' + yaml.dump(dict(self))
return '#cloud-config\n' + tobiko.dump_yaml(dict(self))
def __add__(self, other):
return combine_cloud_configs([self, other])
@ -164,7 +163,7 @@ def get_cloud_init_status(
else:
raise
status = yaml.load(output)
status = tobiko.load_yaml(output)
tobiko.check_valid_type(status, dict)
tobiko.check_valid_type(status['status'], str)
return status['status']

View File

@ -15,8 +15,6 @@ from __future__ import absolute_import
import typing
import yaml
import tobiko
from tobiko import config
from tobiko.openstack import glance
@ -148,7 +146,7 @@ class UbuntuImageFixture(UbuntuMinimalImageFixture,
@property
def write_files(self) -> typing.Dict[str, str]:
write_files = super().write_files
write_files['/etc/netplan/75-tobiko-vlan.yaml'] = yaml.dump(
write_files['/etc/netplan/75-tobiko-vlan.yaml'] = tobiko.dump_yaml(
self.vlan_config)
return write_files

View File

@ -19,7 +19,6 @@ from keystoneclient.v2_0 import client as v2_client
from keystoneclient.v3 import client as v3_client
from oslo_log import log
import testtools
import yaml
import tobiko
from tobiko.openstack import keystone
@ -36,7 +35,7 @@ class TobikoKeystoneCredentialsCommandTest(testtools.TestCase):
def test_execute(self):
with sh.local_process('tobiko-keystone-credentials') as process:
actual = yaml.full_load(process.stdout)
actual = tobiko.load_yaml(process.stdout)
process.check_exit_status()
expected = keystone.default_keystone_credentials().to_dict()
self.assertEqual(expected, actual)

View File

@ -16,8 +16,8 @@ from __future__ import absolute_import
import os
import testtools
import yaml
import tobiko
from tobiko import config
from tobiko import tripleo
@ -50,7 +50,7 @@ class InventoryFileTest(testtools.TestCase):
inventory_yaml = tripleo.read_tripleo_ansible_inventory()
self.assertIsInstance(inventory_yaml, str)
self.assertTrue(inventory_yaml)
inventory = yaml.safe_load(inventory_yaml)
inventory = tobiko.load_yaml(inventory_yaml)
self.assertIn('Undercloud', inventory)
self.assertIn('Controller', inventory)
self.assertIn('Compute', inventory)

View File

@ -19,7 +19,6 @@ import time
from heatclient.v1 import client as heatclient
from heatclient import exc
import mock
import yaml
import tobiko
from tobiko.openstack import heat
@ -156,7 +155,7 @@ class HeatStackFixtureTest(openstack.OpenstackTest):
if call_create:
client.stacks.create.assert_called_once_with(
parameters=parameters, stack_name=stack.stack_name,
template=yaml.safe_dump(stack.template.template))
template=tobiko.dump_yaml(stack.template.template))
else:
client.stacks.create.assert_not_called()

View File

@ -16,7 +16,6 @@ from __future__ import absolute_import
import os
from heatclient.common import template_utils
import yaml
import tobiko
from tobiko.openstack import heat
@ -44,5 +43,5 @@ class HeatTemplateFileTest(openstack.OpenstackTest):
template_file))
self.assertEqual(template_dict, template.template)
self.assertEqual(template_files, template.template_files)
template_yaml = yaml.safe_dump(template_dict)
template_yaml = tobiko.dump_yaml(template_dict)
self.assertEqual(template_yaml, template.template_yaml)

View File

@ -19,8 +19,6 @@ import os
import tempfile
import typing # noqa
import yaml
import tobiko
from tobiko.openstack import keystone
from tobiko.openstack.keystone import _clouds_file
@ -87,7 +85,7 @@ class CloudsFileFixture(tobiko.SharedFixture):
if self.suffix in _clouds_file.JSON_SUFFIXES:
json.dump(clouds_content, clouds_stream)
elif self.suffix in _clouds_file.YAML_SUFFIXES:
yaml.safe_dump(clouds_content, clouds_stream)
tobiko.dump_yaml(clouds_content, clouds_stream)
finally:
clouds_stream.close()

View File

@ -14,8 +14,8 @@
from __future__ import absolute_import
import testtools
import yaml
import tobiko
from tobiko.openstack import nova
@ -38,7 +38,7 @@ class TestUserData(testtools.TestCase):
def assert_equal_cloud_config(self, expected, actual):
self.assertTrue(actual.startswith('#cloud-config'))
self.assertEqual(expected, yaml.load(actual))
self.assertEqual(expected, tobiko.load_yaml(actual))
class TestCloudConfig(testtools.TestCase):

View File

@ -16,8 +16,6 @@ from __future__ import absolute_import
import io
import os
import yaml
import tobiko
from tobiko.shell import sh
from tobiko.tripleo import _undercloud
@ -27,7 +25,7 @@ from tobiko.tripleo import _config
def get_tripleo_ansible_inventory():
inventory_file = get_tripleo_ansible_inventory_file()
with io.open(inventory_file, 'rb') as fd:
return yaml.safe_load(fd)
return tobiko.load_yaml(fd)
def has_tripleo_ansible_inventory():

View File

@ -8,7 +8,7 @@
- devstack-tobiko-ceph
- devstack-tobiko-heat
- devstack-tobiko-minimal
- devstack-tobiko-octavia
# - devstack-tobiko-octavia
- devstack-tobiko-neutron
- devstack-tobiko-nova
- devstack-tobiko-ovs
@ -27,6 +27,8 @@
voting: false
periodic:
jobs:
- devstack-tobiko-octavia:
voting: false
- tobiko-infrared
- tobiko-infrared-centos-7
- tobiko-infrared-fedora