[Unit Test]: Fix sonic config engine test not stable issue(#10147) (#10356)

Why I did it
Fix sonic config engine test not stable issue
This commit is contained in:
xumia 2022-04-11 14:05:16 +08:00 committed by GitHub
parent 7de0ea4e04
commit 7c20568d43
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 31 additions and 18 deletions

View File

@ -1,4 +1,6 @@
import json
import filecmp
import os
import re
import sys
@ -31,3 +33,14 @@ def liststr_to_dict(liststr):
return list_obj
def cmp(file1, file2):
""" compare files """
try:
with open(file1, 'r') as f:
obj1 = json.load(f)
with open(file2, 'r') as f:
obj2 = json.load(f)
return obj1 == obj2
except:
return filecmp.cmp(file1, file2)

View File

@ -45,37 +45,37 @@ class TestJ2Files(TestCase):
interfaces_template = os.path.join(self.test_dir, '..', '..', '..', 'files', 'image_config', 'interfaces', 'interfaces.j2')
argument = '-m ' + self.t0_minigraph + ' -a \'{\"hwaddr\":\"e4:1d:2d:a5:f3:ad\"}\' -t ' + interfaces_template + ' > ' + self.output_file
self.run_script(argument)
self.assertTrue(filecmp.cmp(os.path.join(self.test_dir, 'sample_output', utils.PYvX_DIR, 'interfaces'), self.output_file))
self.assertTrue(utils.cmp(os.path.join(self.test_dir, 'sample_output', utils.PYvX_DIR, 'interfaces'), self.output_file))
argument = '-m ' + self.t0_mvrf_minigraph + ' -a \'{\"hwaddr\":\"e4:1d:2d:a5:f3:ad\"}\' -t ' + interfaces_template + ' > ' + self.output_file
self.run_script(argument)
self.assertTrue(filecmp.cmp(os.path.join(self.test_dir, 'sample_output', utils.PYvX_DIR, 'mvrf_interfaces'), self.output_file))
self.assertTrue(utils.cmp(os.path.join(self.test_dir, 'sample_output', utils.PYvX_DIR, 'mvrf_interfaces'), self.output_file))
def test_ports_json(self):
ports_template = os.path.join(self.test_dir, '..', '..', '..', 'dockers', 'docker-orchagent', 'ports.json.j2')
argument = '-m ' + self.simple_minigraph + ' -p ' + self.t0_port_config + ' -t ' + ports_template + ' > ' + self.output_file
self.run_script(argument)
self.assertTrue(filecmp.cmp(os.path.join(self.test_dir, 'sample_output', utils.PYvX_DIR, 'ports.json'), self.output_file))
self.assertTrue(utils.cmp(os.path.join(self.test_dir, 'sample_output', utils.PYvX_DIR, 'ports.json'), self.output_file))
def test_dhcp_relay(self):
# Test generation of wait_for_intf.sh
template_path = os.path.join(self.test_dir, '..', '..', '..', 'dockers', 'docker-dhcp-relay', 'wait_for_intf.sh.j2')
argument = '-m ' + self.t0_minigraph + ' -p ' + self.t0_port_config + ' -t ' + template_path + ' > ' + self.output_file
self.run_script(argument)
self.assertTrue(filecmp.cmp(os.path.join(self.test_dir, 'sample_output', utils.PYvX_DIR, 'wait_for_intf.sh'), self.output_file))
self.assertTrue(utils.cmp(os.path.join(self.test_dir, 'sample_output', utils.PYvX_DIR, 'wait_for_intf.sh'), self.output_file))
# Test generation of docker-dhcp-relay.supervisord.conf
template_path = os.path.join(self.test_dir, '..', '..', '..', 'dockers', 'docker-dhcp-relay', 'docker-dhcp-relay.supervisord.conf.j2')
argument = '-m ' + self.t0_minigraph + ' -p ' + self.t0_port_config + ' -t ' + template_path + ' > ' + self.output_file
self.run_script(argument)
self.assertTrue(filecmp.cmp(os.path.join(self.test_dir, 'sample_output', utils.PYvX_DIR, 'docker-dhcp-relay.supervisord.conf'), self.output_file), self.output_file)
self.assertTrue(utils.cmp(os.path.join(self.test_dir, 'sample_output', utils.PYvX_DIR, 'docker-dhcp-relay.supervisord.conf'), self.output_file), self.output_file)
def test_radv(self):
# Test generation of radvd.conf with multiple ipv6 prefixes
template_path = os.path.join(self.test_dir, '..', '..', '..', 'dockers', 'docker-router-advertiser', 'radvd.conf.j2')
argument = '-m ' + self.radv_test_minigraph + ' -p ' + self.t0_port_config + ' -t ' + template_path + ' > ' + self.output_file
self.run_script(argument)
self.assertTrue(filecmp.cmp(os.path.join(self.test_dir, 'sample_output', utils.PYvX_DIR, 'radvd.conf'), self.output_file))
self.assertTrue(utils.cmp(os.path.join(self.test_dir, 'sample_output', utils.PYvX_DIR, 'radvd.conf'), self.output_file))
def test_lldp(self):
lldpd_conf_template = os.path.join(self.test_dir, '..', '..', '..', 'dockers', 'docker-lldp', 'lldpd.conf.j2')
@ -88,19 +88,19 @@ class TestJ2Files(TestCase):
mgmt_iface_ipv4_and_ipv6_json = os.path.join(self.test_dir, "data", "lldp", "mgmt_iface_ipv4_and_ipv6.json")
argument = '-j {} -t {} > {}'.format(mgmt_iface_ipv4_and_ipv6_json, lldpd_conf_template, self.output_file)
self.run_script(argument)
self.assertTrue(filecmp.cmp(expected_mgmt_ipv4_and_ipv6, self.output_file))
self.assertTrue(utils.cmp(expected_mgmt_ipv4_and_ipv6, self.output_file))
# Test generation of lldpd.conf if management interface IPv4 only exist
mgmt_iface_ipv4_json = os.path.join(self.test_dir, "data", "lldp", "mgmt_iface_ipv4.json")
argument = '-j {} -t {} > {}'.format(mgmt_iface_ipv4_json, lldpd_conf_template, self.output_file)
self.run_script(argument)
self.assertTrue(filecmp.cmp(expected_mgmt_ipv4, self.output_file))
self.assertTrue(utils.cmp(expected_mgmt_ipv4, self.output_file))
# Test generation of lldpd.conf if Management interface IPv6 only exist
mgmt_iface_ipv6_json = os.path.join(self.test_dir, "data", "lldp", "mgmt_iface_ipv6.json")
argument = '-j {} -t {} > {}'.format(mgmt_iface_ipv6_json, lldpd_conf_template, self.output_file)
self.run_script(argument)
self.assertTrue(filecmp.cmp(expected_mgmt_ipv6, self.output_file))
self.assertTrue(utils.cmp(expected_mgmt_ipv6, self.output_file))
def test_ipinip(self):
ipinip_file = os.path.join(self.test_dir, '..', '..', '..', 'dockers', 'docker-orchagent', 'ipinip.json.j2')
@ -108,7 +108,7 @@ class TestJ2Files(TestCase):
self.run_script(argument)
sample_output_file = os.path.join(self.test_dir, 'sample_output', utils.PYvX_DIR, 'ipinip.json')
assert filecmp.cmp(sample_output_file, self.output_file)
assert utils.cmp(sample_output_file, self.output_file)
def test_l2switch_template(self):
argument = '-k Mellanox-SN2700 --preset l2 -p ' + self.t0_port_config
@ -209,7 +209,7 @@ class TestJ2Files(TestCase):
os.remove(qos_config_file_new)
sample_output_file = os.path.join(self.test_dir, 'sample_output', utils.PYvX_DIR, 'qos-arista7050.json')
assert filecmp.cmp(sample_output_file, self.output_file)
assert utils.cmp(sample_output_file, self.output_file)
def test_qos_dell6100_render_template(self):
dell_dir_path = os.path.join(self.test_dir, '..', '..', '..', 'device', 'dell', 'x86_64-dell_s6100_c2538-r0', 'Force10-S6100')
@ -228,7 +228,7 @@ class TestJ2Files(TestCase):
os.remove(qos_config_file_new)
sample_output_file = os.path.join(self.test_dir, 'sample_output', utils.PYvX_DIR, 'qos-dell6100.json')
assert filecmp.cmp(sample_output_file, self.output_file)
assert utils.cmp(sample_output_file, self.output_file)
def _test_buffers_render_template(self, vendor, platform, sku, minigraph, buffer_template, expected):
dir_path = os.path.join(self.test_dir, '..', '..', '..', 'device', vendor, platform, sku)
@ -248,7 +248,7 @@ class TestJ2Files(TestCase):
os.remove(buffers_config_file_new)
sample_output_file = os.path.join(self.test_dir, 'sample_output', utils.PYvX_DIR, expected)
assert filecmp.cmp(sample_output_file, self.output_file)
assert utils.cmp(sample_output_file, self.output_file)
def test_buffers_dell6100_render_template(self):
self._test_buffers_render_template('dell', 'x86_64-dell_s6100_c2538-r0', 'Force10-S6100', 'sample-dell-6100-t0-minigraph.xml', 'buffers.json.j2', 'buffers-dell6100.json')
@ -268,7 +268,7 @@ class TestJ2Files(TestCase):
print(argument)
self.run_script(argument)
sample_output_file = os.path.join(self.test_dir, 'multi_npu_data', utils.PYvX_DIR, 'ipinip.json')
assert filecmp.cmp(sample_output_file, self.output_file)
assert utils.cmp(sample_output_file, self.output_file)
def test_swss_switch_render_template(self):
switch_template = os.path.join(
@ -297,7 +297,7 @@ class TestJ2Files(TestCase):
self.test_dir, 'sample_output', v["output"]
)
self.run_script(argument)
assert filecmp.cmp(sample_output_file, self.output_file)
assert utils.cmp(sample_output_file, self.output_file)
def test_swss_switch_render_template_multi_asic(self):
# verify the ECMP hash seed changes per namespace
@ -329,7 +329,7 @@ class TestJ2Files(TestCase):
self.test_dir, 'sample_output', v["output"]
)
self.run_script(argument)
assert filecmp.cmp(sample_output_file, self.output_file)
assert utils.cmp(sample_output_file, self.output_file)
os.environ["NAMESPACE_ID"] = ""
def test_ndppd_conf(self):
@ -339,7 +339,7 @@ class TestJ2Files(TestCase):
argument = '-j {} -t {} > {}'.format(vlan_interfaces_json, conf_template, self.output_file)
self.run_script(argument)
assert filecmp.cmp(expected, self.output_file), self.run_diff(expected, self.output_file)
assert utils.cmp(expected, self.output_file), self.run_diff(expected, self.output_file)
def test_ntp_conf(self):
conf_template = os.path.join(self.test_dir, "ntp.conf.j2")
@ -348,7 +348,7 @@ class TestJ2Files(TestCase):
argument = '-j {} -t {} > {}'.format(ntp_interfaces_json, conf_template, self.output_file)
self.run_script(argument)
assert filecmp.cmp(expected, self.output_file), self.run_diff(expected, self.output_file)
assert utils.cmp(expected, self.output_file), self.run_diff(expected, self.output_file)
def tearDown(self):
os.environ["CFGGEN_UNIT_TESTING"] = ""