Skip to content

Commit

Permalink
[Unit Test]: Fix sonic config engine test not stable issue(sonic-net#…
Browse files Browse the repository at this point in the history
…10147)

Co-authored-by: azureuser <[email protected]>
  • Loading branch information
xumia and azureuser committed Mar 3, 2022
1 parent 82bf3b6 commit 76ed793
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 18 deletions.
13 changes: 13 additions & 0 deletions src/sonic-config-engine/tests/common_utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import json
import filecmp
import os
import re
import sys

Expand Down Expand Up @@ -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)

36 changes: 18 additions & 18 deletions src/sonic-config-engine/tests/test_j2files.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,30 +46,30 @@ def test_interfaces(self):
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')
Expand All @@ -82,19 +82,19 @@ def test_lldp(self):
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_bgpd_quagga(self):
conf_template = os.path.join(self.test_dir, '..', '..', '..', 'dockers', 'docker-fpm-quagga', 'bgpd.conf.j2')
Expand All @@ -117,7 +117,7 @@ def test_ipinip(self):
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
Expand Down Expand Up @@ -218,7 +218,7 @@ def test_qos_arista7050_render_template(self):
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_dell9332_render_template(self):
dell_dir_path = os.path.join(self.test_dir, '..', '..', '..', 'device', 'dell', 'x86_64-dellemc_z9332f_d1508-r0', 'DellEMC-Z9332f-O32')
Expand All @@ -237,7 +237,7 @@ def test_qos_dell9332_render_template(self):
os.remove(qos_config_file_new)

sample_output_file = os.path.join(self.test_dir, 'sample_output', utils.PYvX_DIR, 'qos-dell9332.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')
Expand All @@ -256,7 +256,7 @@ def test_qos_dell6100_render_template(self):
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')
Expand All @@ -275,7 +275,7 @@ def test_buffers_dell6100_render_template(self):
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
Expand Down Expand Up @@ -323,7 +323,7 @@ def test_ipinip_multi_asic(self):
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(
Expand Down Expand Up @@ -352,7 +352,7 @@ def test_swss_switch_render_template(self):
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
Expand Down Expand Up @@ -384,7 +384,7 @@ def test_swss_switch_render_template_multi_asic(self):
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):
Expand All @@ -394,7 +394,7 @@ def test_ndppd_conf(self):

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")
Expand All @@ -403,7 +403,7 @@ def test_ntp_conf(self):

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):
try:
Expand Down

0 comments on commit 76ed793

Please sign in to comment.