Add support for removing machine placement in a bundle

This commit is contained in:
Chris MacNaughton
2018-10-09 17:34:12 +02:00
parent d60699ca6c
commit 89527526f5
4 changed files with 190 additions and 0 deletions
+1
View File
@@ -99,6 +99,7 @@ setup(
'functest-test = zaza.charm_lifecycle.test:main',
'current-apps = zaza.model:main',
'tempest-config = zaza.tempest_config:main',
'flatten-bundle = zaza.utilities.bundle:main',
]
},
license='Apache-2.0: http://www.apache.org/licenses/LICENSE-2.0',
+7
View File
@@ -51,3 +51,10 @@ passenv = USER
commands =
{envdir}/bin/python3 setup.py install
sudo su {env:USER} -c 'source {envdir}/bin/activate && functest-run-suite --keep-model'
[testenv:flatten]
basepython = python3
deps = -r{toxinidir}/requirements.txt
commands =
{envdir}/bin/python3 setup.py install
flatten-bundle {posargs}
@@ -0,0 +1,108 @@
# Copyright 2018 Canonical Ltd.
#
# 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 mock
import unit_tests.utils as ut_utils
import zaza.utilities.bundle as bundle
import yaml
TEST_BUNDLE_WITH_PLACEMENT = """
machines:
'0':
series: bionic
'1':
series: bionic
'2':
series: bionic
relations:
- - ceph-osd:mon
- ceph-mon:osd
series: bionic
services:
ceph-mon:
annotations:
gui-x: '750'
gui-y: '500'
charm: cs:ceph-mon-26
num_units: 3
options:
expected-osd-count: 3
source: cloud:bionic-rocky
to:
- lxd:0
- lxd:1
- lxd:2
ceph-osd:
annotations:
gui-x: '1000'
gui-y: '500'
charm: cs:ceph-osd-269
num_units: 3
options:
osd-devices: /dev/sdb
source: cloud:bionic-rocky
to:
- '0'
- '1'
- '2'
"""
TEST_BUNDLE_WITHOUT_PLACEMENT = """
relations:
- - ceph-osd:mon
- ceph-mon:osd
series: bionic
services:
ceph-mon:
annotations:
gui-x: '750'
gui-y: '500'
charm: cs:ceph-mon-26
num_units: 3
options:
expected-osd-count: 3
source: cloud:bionic-rocky
ceph-osd:
annotations:
gui-x: '1000'
gui-y: '500'
charm: cs:ceph-osd-269
num_units: 3
options:
osd-devices: /dev/sdb
source: cloud:bionic-rocky
"""
class TestUtilitiesBundle(ut_utils.BaseTestCase):
def test_flatten_bundle(self):
self.maxDiff = 1500
input_yaml = yaml.safe_load(TEST_BUNDLE_WITH_PLACEMENT)
flattened = bundle.remove_machine_specification(input_yaml)
expected = yaml.safe_load(TEST_BUNDLE_WITHOUT_PLACEMENT)
self.assertEqual(expected, flattened)
def test_add_series(self):
self.maxDiff = 1500
input_yaml = yaml.safe_load(TEST_BUNDLE_WITH_PLACEMENT)
input_yaml.pop('series', None)
flattened = bundle.remove_machine_specification(input_yaml)
expected = yaml.safe_load(TEST_BUNDLE_WITHOUT_PLACEMENT)
self.assertEqual(expected, flattened)
+74
View File
@@ -0,0 +1,74 @@
# Copyright 2018 Canonical Ltd.
#
# 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.
"""Module for disfiguring Juju bundles."""
import argparse
import yaml
import sys
def remove_machine_specification(input_yaml):
"""
Will remove the machien specifications from a supplied bundle.
:param input_yaml: Juju bundle to strip machinens from
:type input_yaml: dict
:returns: bundle without machine placements
:rtype: dict
"""
machines = input_yaml.pop("machines", None)
if not input_yaml.get('series'):
if machines:
for (name, details) in machines.items():
if details['series']:
input_yaml['series'] = details['series']
break
for (application_name, application) in input_yaml['services'].items():
application.pop("to", None)
return input_yaml
def parse_args(args):
"""Parse command line arguments.
:param args: List of configure functions functions
:type list: [str1, str2,...] List of command line arguments
:returns: Parsed arguments
:rtype: Namespace
"""
parser = argparse.ArgumentParser()
parser.add_argument('-i', '--input',
help='Bundle to flatten',
required=True)
parser.add_argument('-o', '--output',
help='Where to output',
default="/dev/stdout")
return parser.parse_args(args)
def main():
"""Run the configuration defined by the command line args.
Run the configuration defined by the command line args or if none were
provided read the configuration functions from the charms tests.yaml
config file
"""
args = parse_args(sys.argv[1:])
with open(args.input, 'r') as file:
input_yaml = yaml.safe_load(file)
stripped_yaml = remove_machine_specification(input_yaml)
with open(args.output, 'w') as output:
print(yaml.dump(stripped_yaml), file=output)