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

Why I did it
Fix sonic config engine test not stable issue
This commit is contained in:
xumia 2022-03-29 10:41:11 +08:00 committed by GitHub
parent b10833425c
commit ca85c6be5e
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

@ -46,30 +46,30 @@ 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.assertTrue(utils.cmp(os.path.join(self.test_dir, 'sample_output', utils.PYvX_DIR, 'docker-dhcp-relay.supervisord.conf'), self.output_file))
def test_lldp(self):
lldpd_conf_template = os.path.join(self.test_dir, '..', '..', '..', 'dockers', 'docker-lldp', 'lldpd.conf.j2')
@ -82,19 +82,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')
@ -102,7 +102,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
@ -203,7 +203,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')
@ -222,7 +222,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_dell6100_render_template(self):
dell_dir_path = os.path.join(self.test_dir, '..', '..', '..', 'device', 'dell', 'x86_64-dell_s6100_c2538-r0', 'Force10-S6100')
@ -241,7 +241,7 @@ class TestJ2Files(TestCase):
os.remove(buffers_config_file_new)
sample_output_file = os.path.join(self.test_dir, 'sample_output', utils.PYvX_DIR, 'buffers-dell6100.json')
assert filecmp.cmp(sample_output_file, self.output_file)
assert utils.cmp(sample_output_file, self.output_file)
def test_buffers_mellanox2700_render_template(self):
# Mellanox buffer template rendering for single ingress pool mode
@ -261,7 +261,7 @@ class TestJ2Files(TestCase):
os.remove(buffers_config_file_new)
sample_output_file = os.path.join(self.test_dir, 'sample_output', utils.PYvX_DIR, 'buffers-mellanox2700.json')
assert filecmp.cmp(sample_output_file, self.output_file)
assert utils.cmp(sample_output_file, self.output_file)
def test_buffers_mellanox2410_render_template(self):
# Mellanox buffer template rendering for double ingress pools mode
@ -289,7 +289,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(
@ -318,7 +318,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
@ -350,7 +350,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):
@ -360,7 +360,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")
@ -369,7 +369,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"] = ""