From 416943cc4e1ec7e4fc0e86640f24f0133a8b2f4e Mon Sep 17 00:00:00 2001 From: Dan Prince Date: Wed, 20 Aug 2014 21:46:46 -0400 Subject: Add support for parsing YAML Adds new YAML examples. Also adds some new CLI test cases which compare --noop stdout (from the CLI) for the json and yaml examples to verify they generate the same things. --- etc/os-net-config/samples/bond.yaml | 17 ++++++++ etc/os-net-config/samples/bridge_dhcp.yaml | 9 ++++ etc/os-net-config/samples/bridge_vlan.json | 2 +- etc/os-net-config/samples/bridge_vlan.yaml | 16 +++++++ os_net_config/cli.py | 6 +-- os_net_config/tests/test_cli.py | 70 ++++++++++++++++++++++++++++++ requirements.txt | 1 + 7 files changed, 117 insertions(+), 4 deletions(-) create mode 100644 etc/os-net-config/samples/bond.yaml create mode 100644 etc/os-net-config/samples/bridge_dhcp.yaml create mode 100644 etc/os-net-config/samples/bridge_vlan.yaml create mode 100644 os_net_config/tests/test_cli.py diff --git a/etc/os-net-config/samples/bond.yaml b/etc/os-net-config/samples/bond.yaml new file mode 100644 index 0000000..7b925a7 --- /dev/null +++ b/etc/os-net-config/samples/bond.yaml @@ -0,0 +1,17 @@ +network_config: + - + type: ovs_bridge + name: br-ctlplane + use_dhcp: true + members: + - + type: ovs_bond + name: bond1 + use_dhcp: true + members: + - + type: interface + name: em1 + - + type: interface + name: em2 diff --git a/etc/os-net-config/samples/bridge_dhcp.yaml b/etc/os-net-config/samples/bridge_dhcp.yaml new file mode 100644 index 0000000..aacd1e6 --- /dev/null +++ b/etc/os-net-config/samples/bridge_dhcp.yaml @@ -0,0 +1,9 @@ +network_config: + - + type: ovs_bridge + name: br-ctlplane + use_dhcp: true + members: + - + type: interface + name: em1 diff --git a/etc/os-net-config/samples/bridge_vlan.json b/etc/os-net-config/samples/bridge_vlan.json index 6a6480c..1e351e0 100644 --- a/etc/os-net-config/samples/bridge_vlan.json +++ b/etc/os-net-config/samples/bridge_vlan.json @@ -6,7 +6,7 @@ { "type": "vlan", "device": "em1", - "vlan_id": "16", + "vlan_id": 16, "addresses": [{ "ip_netmask": "192.0.2.1/24" }], diff --git a/etc/os-net-config/samples/bridge_vlan.yaml b/etc/os-net-config/samples/bridge_vlan.yaml new file mode 100644 index 0000000..9f25e09 --- /dev/null +++ b/etc/os-net-config/samples/bridge_vlan.yaml @@ -0,0 +1,16 @@ +network_config: + - + type: ovs_bridge + name: br-ctlplane + members: + - + type: vlan + device: em1 + vlan_id: 16 + addresses: + - + ip_netmask: 192.0.2.1/24 + routes: + - + next_hop: 192.0.2.1 + ip_netmask: 192.0.2.1/24 diff --git a/os_net_config/cli.py b/os_net_config/cli.py index 247a1b4..e919d02 100644 --- a/os_net_config/cli.py +++ b/os_net_config/cli.py @@ -16,10 +16,10 @@ import argparse -import json import logging import os import sys +import yaml import os_net_config from os_net_config import impl_eni @@ -37,7 +37,7 @@ def parse_opts(argv): ' config file format.') parser.add_argument('-c', '--config-file', metavar='CONFIG_FILE', help="""path to the configuration file.""", - default='/etc/os-net-config/config.json') + default='/etc/os-net-config/config.yaml') parser.add_argument('-p', '--provider', metavar='PROVIDER', help="""The provider to use.""" """One of: ifcfg, eni, iproute.""", @@ -118,7 +118,7 @@ def main(argv=sys.argv): if os.path.exists(opts.config_file): with open(opts.config_file) as cf: - iface_array = json.loads(cf.read()).get("network_config") + iface_array = yaml.load(cf.read()).get("network_config") logger.debug('network_config JSON: %s' % str(iface_array)) else: logger.error('No config file exists at: %s' % opts.config_file) diff --git a/os_net_config/tests/test_cli.py b/os_net_config/tests/test_cli.py new file mode 100644 index 0000000..505301c --- /dev/null +++ b/os_net_config/tests/test_cli.py @@ -0,0 +1,70 @@ +# -*- coding: utf-8 -*- + +# Copyright 2014 Red Hat, Inc. +# +# 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.path +import sys + +from os_net_config import cli +from os_net_config.tests import base +import six + + +SAMPLE_BASE = os.path.join('.', 'etc', 'os-net-config', 'samples') + + +class TestCli(base.TestCase): + + def run_cli(self, argstr, exitcodes=(0,)): + orig = sys.stdout + orig_stderr = sys.stderr + try: + sys.stdout = six.StringIO() + sys.stderr = six.StringIO() + cli.main(argstr.split()) + except SystemExit: + exc_type, exc_value, exc_traceback = sys.exc_info() + self.assertIn(exc_value.code, exitcodes) + finally: + stdout = sys.stdout.getvalue() + sys.stdout.close() + sys.stdout = orig + stderr = sys.stderr.getvalue() + sys.stderr.close() + sys.stderr = orig_stderr + return (stdout, stderr) + + def test_bond_noop_output(self): + bond_yaml = os.path.join(SAMPLE_BASE, 'bond.yaml') + bond_json = os.path.join(SAMPLE_BASE, 'bond.json') + stdout_yaml, stderr = self.run_cli('ARG0 -d --noop -c %s' % bond_yaml) + stdout_json, stderr = self.run_cli('ARG0 -d --noop -c %s' % bond_json) + self.assertEqual(stdout_yaml, stdout_json) + + def test_bridge_noop_output(self): + bridge_yaml = os.path.join(SAMPLE_BASE, 'bridge_dhcp.yaml') + bridge_json = os.path.join(SAMPLE_BASE, 'bridge_dhcp.json') + stdout_yaml, stderr = self.run_cli('ARG0 -d --noop -c %s' % + bridge_yaml) + stdout_json, stderr = self.run_cli('ARG0 -d --noop -c %s' % + bridge_json) + self.assertEqual(stdout_yaml, stdout_json) + + def test_vlan_noop_output(self): + vlan_yaml = os.path.join(SAMPLE_BASE, 'bridge_vlan.yaml') + vlan_json = os.path.join(SAMPLE_BASE, 'bridge_vlan.json') + stdout_yaml, stderr = self.run_cli('ARG0 -d --noop -c %s' % vlan_yaml) + stdout_json, stderr = self.run_cli('ARG0 -d --noop -c %s' % vlan_json) + self.assertEqual(stdout_yaml, stdout_json) diff --git a/requirements.txt b/requirements.txt index 3bf6ba4..b3cb7ed 100644 --- a/requirements.txt +++ b/requirements.txt @@ -6,3 +6,4 @@ eventlet>=0.13.0 iso8601>=0.1.9 netaddr>=0.7.6 oslo.config>=1.2.0 +PyYAML>=3.1.0 -- cgit 1.2.3-korg