Skip to content

Commit

Permalink
[cfggen] Add basic functional test (#375)
Browse files Browse the repository at this point in the history
[cfggen] Add basic functional test
  • Loading branch information
taoyl-ms authored Mar 17, 2017
1 parent 799f072 commit e0fd12a
Show file tree
Hide file tree
Showing 8 changed files with 207 additions and 0 deletions.
3 changes: 3 additions & 0 deletions sonic-slave/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ RUN apt-get update && apt-get install -y libmysqlclient-dev libmysqld-dev libper
# For mpdecimal
RUN apt-get update && apt-get install -y docutils-common libjs-sphinxdoc libjs-underscore python-docutils python-jinja2 python-markupsafe python-pygments python-roman python-sphinx sphinx-common python3-sphinx

# For sonic config engine testing
RUN apt-get install -y python-lxml python-jinja2 python-netaddr python-ipaddr python-yaml

# For templating
RUN pip install j2cli

Expand Down
1 change: 1 addition & 0 deletions src/sonic-config-engine/MANIFEST.in
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
recursive-include platform *.json *.ini *.conf
recursive-include tests *.j2 *.yml *.xml
7 changes: 7 additions & 0 deletions src/sonic-config-engine/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from setuptools import setup
import os.path
import unittest

def get_platform_file_list():
data_files = []
Expand All @@ -15,6 +16,11 @@ def get_platform_file_list():
data_files.append( (os.path.join('/usr/share/sonic', platform), files) )
return data_files

def get_test_suite():
test_loader = unittest.TestLoader()
test_suite = test_loader.discover('tests', pattern='*.py')
return test_suite

setup(name='sonic-config-engine',
version='1.0',
description='Utilities for generating SONiC configuration files',
Expand All @@ -25,4 +31,5 @@ def get_platform_file_list():
scripts=['sonic-cfggen'],
data_files=get_platform_file_list(),
install_requires=['lxml', 'jinja2', 'netaddr', 'ipaddr', 'pyyaml'],
test_suite='setup.get_test_suite',
)
Empty file.
129 changes: 129 additions & 0 deletions src/sonic-config-engine/tests/sample_graph.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
<DeviceMiniGraph xmlns="Microsoft.Search.Autopilot.Evolution" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<CpgDec>
<IsisRouters xmlns:a="http://schemas.datacontract.org/2004/07/Microsoft.Search.Autopilot.Evolution"/>
<PeeringSessions>
<BGPSession>
<StartRouter>OCPSCH0104001MS</StartRouter>
<StartPeer>10.10.1.14</StartPeer>
<EndRouter>OCPSCH01040DDLF</EndRouter>
<EndPeer>10.10.1.13</EndPeer>
<Multihop>1</Multihop>
<HoldTime>10</HoldTime>
<KeepAliveTime>3</KeepAliveTime>
</BGPSession>
<BGPSession>
<StartRouter>OCPSCH0104002MS</StartRouter>
<StartPeer>10.10.2.14</StartPeer>
<EndRouter>OCPSCH01040DDLF</EndRouter>
<EndPeer>10.10.2.13</EndPeer>
<Multihop>1</Multihop>
<HoldTime>10</HoldTime>
<KeepAliveTime>3</KeepAliveTime>
</BGPSession>
</PeeringSessions>
<Routers xmlns:a="http://schemas.datacontract.org/2004/07/Microsoft.Search.Autopilot.Evolution">
<a:BGPRouterDeclaration>
<a:ASN>64536</a:ASN>
<a:Hostname>OCPSCH01040DDLF</a:Hostname>
<a:Peers>
<BGPPeer>
<Address>10.10.1.14</Address>
<RouteMapIn i:nil="true"/>
<RouteMapOut i:nil="true"/>
</BGPPeer>
<BGPPeer>
<Address>10.10.2.14</Address>
<RouteMapIn i:nil="true"/>
<RouteMapOut i:nil="true"/>
</BGPPeer>
</a:Peers>
<a:RouteMaps/>
</a:BGPRouterDeclaration>
<a:BGPRouterDeclaration>
<a:ASN>64542</a:ASN>
<a:Hostname>OCPSCH0104001MS</a:Hostname>
<a:RouteMaps/>
</a:BGPRouterDeclaration>
<a:BGPRouterDeclaration>
<a:ASN>64543</a:ASN>
<a:Hostname>OCPSCH0104002MS</a:Hostname>
<a:RouteMaps/>
</a:BGPRouterDeclaration>
</Routers>
</CpgDec>
<DpgDec>
<DeviceDataPlaneInfo>
<IPSecTunnels/>
<LoopbackIPInterfaces xmlns:a="http://schemas.datacontract.org/2004/07/Microsoft.Search.Autopilot.Evolution">
<a:LoopbackIPInterface>
<Name>HostIP</Name>
<AttachTo>Loopback0</AttachTo>
<a:Prefix xmlns:b="Microsoft.Search.Autopilot.NetMux">
<b:IPPrefix>100.0.0.6/32</b:IPPrefix>
</a:Prefix>
<a:PrefixStr>100.0.0.6/32</a:PrefixStr>
</a:LoopbackIPInterface>
</LoopbackIPInterfaces>
<ManagementIPInterfaces xmlns:a="http://schemas.datacontract.org/2004/07/Microsoft.Search.Autopilot.Evolution">
<a:ManagementIPInterface>
<Name>ManagementIP1</Name>
<AttachTo>Management0</AttachTo>
<a:Prefix xmlns:b="Microsoft.Search.Autopilot.NetMux">
<b:IPPrefix>192.168.200.15/24</b:IPPrefix>
</a:Prefix>
<a:PrefixStr>192.168.200.15/24</a:PrefixStr>
</a:ManagementIPInterface>
</ManagementIPInterfaces>
<MplsInterfaces/>
<MplsTeInterfaces/>
<RsvpInterfaces/>
<Hostname>OCPSCH01040DDLF</Hostname>
<PortChannelInterfaces/>
<VlanInterfaces/>
<IPInterfaces>
<IPInterface>
<Name i:nil="true"/>
<AttachTo>Ethernet0</AttachTo>
<Prefix>10.10.1.13/30</Prefix>
</IPInterface>
<IPInterface>
<Name i:nil="true"/>
<AttachTo>Ethernet1</AttachTo>
<Prefix>10.10.2.13/30</Prefix>
</IPInterface>
</IPInterfaces>
<DataAcls/>
<AclInterfaces/>
<DownstreamSummaries/>
<DownstreamSummarySet xmlns:a="http://schemas.datacontract.org/2004/07/Microsoft.Search.Autopilot.Evolution"/>
</DeviceDataPlaneInfo>
</DpgDec>
<PngDec>
<DeviceInterfaceLinks>
<DeviceLinkBase i:type="DeviceInterfaceLink">
<Bandwidth>40000</Bandwidth>
<ElementType>DeviceInterfaceLink</ElementType>
<EndDevice>OCPSCH01040DDLF</EndDevice>
<EndPort>Ethernet0</EndPort>
<StartDevice>OCPSCH0104012MS</StartDevice>
<StartPort>Ethernet12</StartPort>
</DeviceLinkBase>
<DeviceLinkBase i:type="DeviceInterfaceLink">
<Bandwidth>40000</Bandwidth>
<ElementType>DeviceInterfaceLink</ElementType>
<EndDevice>OCPCH01040DDLF</EndDevice>
<EndPort>Ethernet1</EndPort>
<StartDevice>OCPSCH0104002MS</StartDevice>
<StartPort>Ethernet12</StartPort>
</DeviceLinkBase>
</DeviceInterfaceLinks>
<Devices>
<Device i:type="LeafRouter">
<Hostname>OCPSCH01040DDLF</Hostname>
<HwSku>Force10-Z9100</HwSku>
</Device>
</Devices>
</PngDec>
<Hostname>OCPSCH01040DDLF</Hostname>
<HwSku>Force10-Z9100</HwSku>
</DeviceMiniGraph>
3 changes: 3 additions & 0 deletions src/sonic-config-engine/tests/test.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{% for item in yml_item -%}
{{ item }}
{% endfor %}
3 changes: 3 additions & 0 deletions src/sonic-config-engine/tests/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
yml_item:
- value1
- value2
61 changes: 61 additions & 0 deletions src/sonic-config-engine/tests/test_cfggen.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
from unittest import TestCase
import subprocess
import os

class TestCfgGen(TestCase):

def setUp(self):
self.test_dir = os.path.dirname(os.path.realpath(__file__))
self.script_file = os.path.join(self.test_dir, '..', 'sonic-cfggen')
self.sample_graph = os.path.join(self.test_dir, 'sample_graph.xml')

def run_script(self, argument):
print '\n Running sonic-cfggen ' + argument
output = subprocess.check_output(self.script_file + ' ' + argument, shell=True)
linecount = output.strip().count('\n')
if linecount <= 0:
print ' Output: ' + output.strip()
else:
print ' Output: ({0} lines, {1} bytes)'.format(linecount + 1, len(output))
return output

def test_dummy_run(self):
argument = ''
output = self.run_script(argument)
self.assertEqual(output, '')

def test_minigraph_sku(self):
argument = '-v minigraph_hwsku -m "' + self.sample_graph + '"'
output = self.run_script(argument)
self.assertEqual(output.strip(), 'Force10-Z9100')

def test_print_data(self):
argument = '-m "' + self.sample_graph + '" --print-data'
output = self.run_script(argument)
self.assertTrue(len(output.strip()) > 0)

def test_jinja_expression(self):
argument = '-m "' + self.sample_graph + '" -v "minigraph_devices[minigraph_hostname][\'type\']"'
output = self.run_script(argument)
self.assertEqual(output.strip(), 'LeafRouter')

def test_print_alias_mapping(self):
argument = '-s'
output = self.run_script(argument)
self.assertTrue(len(output.strip()) > 0)

def test_additional_json_data(self):
argument = '-a \'{"key1":"value1"}\' -v key1'
output = self.run_script(argument)
self.assertEqual(output.strip(), 'value1')

def test_read_yaml(self):
argument = '-v yml_item -y ' + os.path.join(self.test_dir, 'test.yml')
output = self.run_script(argument)
self.assertEqual(output.strip(), '[\'value1\', \'value2\']')

def test_render_template(self):
argument = '-y ' + os.path.join(self.test_dir, 'test.yml') + ' -t' + os.path.join(self.test_dir, 'test.j2')
output = self.run_script(argument)
self.assertEqual(output.strip(), 'value1\nvalue2')

0 comments on commit e0fd12a

Please sign in to comment.