diff --git a/src/sonic-config-engine/tests/common_utils.py b/src/sonic-config-engine/tests/common_utils.py index 3d8a3029da..47fe9f37f2 100644 --- a/src/sonic-config-engine/tests/common_utils.py +++ b/src/sonic-config-engine/tests/common_utils.py @@ -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) + diff --git a/src/sonic-config-engine/tests/test_j2files.py b/src/sonic-config-engine/tests/test_j2files.py index 5e782ce2fa..b0bfbe9f37 100644 --- a/src/sonic-config-engine/tests/test_j2files.py +++ b/src/sonic-config-engine/tests/test_j2files.py @@ -46,37 +46,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') @@ -89,19 +89,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') @@ -109,7 +109,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 @@ -210,7 +210,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_dell9332_render_template(self): dell_dir_path = os.path.join(self.test_dir, '..', '..', '..', 'device', 'dell', 'x86_64-dellemc_z9332f_d1508-r0', 'DellEMC-Z9332f-O32') @@ -229,7 +229,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-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') @@ -248,7 +248,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) @@ -268,7 +268,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') @@ -288,7 +288,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( @@ -317,7 +317,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 @@ -349,7 +349,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): @@ -359,7 +359,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") @@ -368,7 +368,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"] = ""