Merge pull request #142 from ChrisMacNaughton/bundle-flattener

Add support for removing machine placement in a bundle
This commit is contained in:
Liam Young
2018-10-12 12:34:19 +01:00
committed by GitHub
4 changed files with 204 additions and 0 deletions

View File

@@ -100,6 +100,7 @@ setup(
'functest-test = zaza.charm_lifecycle.test:main',
'current-apps = zaza.model:main',
'tempest-config = zaza.tempest_config:main',
'remove-placement = zaza.utilities.bundle:main',
]
},
license='Apache-2.0: http://www.apache.org/licenses/LICENSE-2.0',

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:remove-placement]
basepython = python3
deps = -r{toxinidir}/requirements.txt
commands =
{envdir}/bin/python3 setup.py install
remove-placement {posargs}

View File

@@ -0,0 +1,121 @@
# 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)
def test_parser(self):
args = bundle.parse_args([
'-i', 'bundle.yaml'])
self.assertEqual(args.input, 'bundle.yaml')
self.assertEqual(args.output, '/dev/stdout')
def test_parser_output(self):
args = bundle.parse_args([
'-i', 'bundle.yaml',
'-o', 'bundle_out.yaml'])
self.assertEqual(args.input, 'bundle.yaml')
self.assertEqual(args.output, 'bundle_out.yaml')

75
zaza/utilities/bundle.py Normal file
View File

@@ -0,0 +1,75 @@
# 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 machine 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)
input_series = input_yaml.get('series', None)
if machines:
for (name, details) in machines.items():
if details['series']:
if input_series and details['series'] != input_series:
raise Exception("Mixing series is not supported")
input_yaml['series'] = details['series']
input_series = input_yaml['series']
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 remove placement from',
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.
Remove machine placement as specified in the command line args
"""
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)