diff --git a/tests/fixtures/centos-7.7/nmcli-connection-all.json b/tests/fixtures/centos-7.7/nmcli-connection-all.json new file mode 100644 index 00000000..df986a93 --- /dev/null +++ b/tests/fixtures/centos-7.7/nmcli-connection-all.json @@ -0,0 +1 @@ +[{"name":"ens33","uuid":"d92ece08-9e02-47d5-b2d2-92c80e155744","type":"ethernet","timestamp":1645643581,"timestamp_real":"Wed 23 Feb 2022 11:13:01 AM PST","autoconnect":"yes","autoconnect_priority":0,"readonly":"no","dbus_path":"/org/freedesktop/NetworkManager/Settings/1","active":"yes","device":"ens33","state":"activated","active_path":"/org/freedesktop/NetworkManager/ActiveConnection/1","slave":null,"filename":"/etc/sysconfig/network-scripts/ifcfg-ens33"}] diff --git a/tests/fixtures/centos-7.7/nmcli-connection-show-ens33.json b/tests/fixtures/centos-7.7/nmcli-connection-show-ens33.json new file mode 100644 index 00000000..cb2058f7 --- /dev/null +++ b/tests/fixtures/centos-7.7/nmcli-connection-show-ens33.json @@ -0,0 +1 @@ +[{"connection_id":"ens33","connection_uuid":"d92ece08-9e02-47d5-b2d2-92c80e155744","connection_stable_id":null,"connection_type":"802-3-ethernet","connection_interface_name":"ens33","connection_autoconnect":"yes","connection_autoconnect_priority":0,"connection_autoconnect_retries":-1,"connection_autoconnect_retries_text":"default","connection_multi_connect":0,"connection_multi_connect_text":"default","connection_auth_retries":-1,"connection_timestamp":1645570618,"connection_read_only":"no","connection_permissions":null,"connection_zone":null,"connection_master":null,"connection_slave_type":null,"connection_autoconnect_slaves":-1,"connection_autoconnect_slaves_text":"default","connection_secondaries":null,"connection_gateway_ping_timeout":0,"connection_metered":"unknown","connection_lldp":"default","connection_mdns":-1,"connection_mdns_text":"default","connection_llmnr":-1,"connection_llmnr_text":"default","802_3_ethernet_port":null,"802_3_ethernet_speed":0,"802_3_ethernet_duplex":null,"802_3_ethernet_auto_negotiate":"no","802_3_ethernet_mac_address":null,"802_3_ethernet_cloned_mac_address":null,"802_3_ethernet_generate_mac_address_mask":null,"802_3_ethernet_mac_address_blacklist":null,"802_3_ethernet_mtu":"auto","802_3_ethernet_s390_subchannels":null,"802_3_ethernet_s390_nettype":null,"802_3_ethernet_s390_options":null,"802_3_ethernet_wake_on_lan":"default","802_3_ethernet_wake_on_lan_password":null,"ipv4_method":"auto","ipv4_dns":null,"ipv4_dns_search":null,"ipv4_dns_options":"","ipv4_dns_priority":0,"ipv4_addresses":null,"ipv4_gateway":null,"ipv4_routes":null,"ipv4_route_metric":-1,"ipv4_route_table":0,"ipv4_route_table_text":"unspec","ipv4_routing_rules":null,"ipv4_ignore_auto_routes":"no","ipv4_ignore_auto_dns":"no","ipv4_dhcp_client_id":null,"ipv4_dhcp_timeout":0,"ipv4_dhcp_timeout_text":"default","ipv4_dhcp_send_hostname":"yes","ipv4_dhcp_hostname":null,"ipv4_dhcp_fqdn":null,"ipv4_never_default":"no","ipv4_may_fail":"yes","ipv4_dad_timeout":-1,"ipv4_dad_timeout_text":"default","ipv6_method":"auto","ipv6_dns":null,"ipv6_dns_search":null,"ipv6_dns_options":"","ipv6_dns_priority":0,"ipv6_addresses":null,"ipv6_gateway":null,"ipv6_routes":null,"ipv6_route_metric":-1,"ipv6_route_table":0,"ipv6_route_table_text":"unspec","ipv6_routing_rules":null,"ipv6_ignore_auto_routes":"no","ipv6_ignore_auto_dns":"no","ipv6_never_default":"no","ipv6_may_fail":"yes","ipv6_ip6_privacy":-1,"ipv6_ip6_privacy_text":"unknown","ipv6_addr_gen_mode":"stable-privacy","ipv6_dhcp_duid":null,"ipv6_dhcp_send_hostname":"yes","ipv6_dhcp_hostname":null,"ipv6_token":null,"proxy_method":"none","proxy_browser_only":"no","proxy_pac_url":null,"proxy_pac_script":null,"name":"ens33","uuid":"d92ece08-9e02-47d5-b2d2-92c80e155744","devices":"ens33","state":"activated","default":"yes","default6":"no","spec_object":null,"vpn":"no","dbus_path":"/org/freedesktop/NetworkManager/ActiveConnection/1","con_path":"/org/freedesktop/NetworkManager/Settings/1","zone":null,"master_path":null,"ip4_address_1":"192.168.71.180/24","ip4_gateway":"192.168.71.2","ip4_route_1":{"dst":"0.0.0.0/0","nh":"192.168.71.2","mt":100},"ip4_route_2":{"dst":"192.168.71.0/24","nh":"0.0.0.0","mt":100},"ip4_dns_1":"192.168.71.2","ip4_domain_1":"localdomain","dhcp4_option_1":{"name":"broadcast_address","value":"192.168.71.255"},"dhcp4_option_2":{"name":"dhcp_lease_time","value":1800},"dhcp4_option_3":{"name":"dhcp_message_type","value":5},"dhcp4_option_4":{"name":"dhcp_server_identifier","value":"192.168.71.254"},"dhcp4_option_5":{"name":"domain_name","value":"localdomain"},"dhcp4_option_6":{"name":"domain_name_servers","value":"192.168.71.2"},"dhcp4_option_7":{"name":"expiry","value":1645572241},"dhcp4_option_8":{"name":"ip_address","value":"192.168.71.180"},"dhcp4_option_9":{"name":"network_number","value":"192.168.71.0"},"dhcp4_option_10":{"name":"next_server","value":"192.168.71.254"},"dhcp4_option_11":{"name":"requested_broadcast_address","value":1},"dhcp4_option_12":{"name":"requested_classless_static_routes","value":1},"dhcp4_option_13":{"name":"requested_domain_name","value":1},"dhcp4_option_14":{"name":"requested_domain_name_servers","value":1},"dhcp4_option_15":{"name":"requested_domain_search","value":1},"dhcp4_option_16":{"name":"requested_host_name","value":1},"dhcp4_option_17":{"name":"requested_interface_mtu","value":1},"dhcp4_option_18":{"name":"requested_ms_classless_static_routes","value":1},"dhcp4_option_19":{"name":"requested_nis_domain","value":1},"dhcp4_option_20":{"name":"requested_nis_servers","value":1},"dhcp4_option_21":{"name":"requested_ntp_servers","value":1},"dhcp4_option_22":{"name":"requested_rfc3442_classless_static_routes","value":1},"dhcp4_option_23":{"name":"requested_root_path","value":1},"dhcp4_option_24":{"name":"requested_routers","value":1},"dhcp4_option_25":{"name":"requested_static_routes","value":1},"dhcp4_option_26":{"name":"requested_subnet_mask","value":1},"dhcp4_option_27":{"name":"requested_time_offset","value":1},"dhcp4_option_28":{"name":"requested_wpad","value":1},"dhcp4_option_29":{"name":"routers","value":"192.168.71.2"},"dhcp4_option_30":{"name":"subnet_mask","value":"255.255.255.0"},"ip6_address_1":"fe80::c1cb:715d:bc3e:b8a0/64","ip6_gateway":null,"ip6_route_1":{"dst":"fe80::/64","nh":"::","mt":100},"ip6_route_2":{"dst":"ff00::/8","nh":"::","mt":256,"table":255}}] diff --git a/tests/fixtures/centos-7.7/nmcli-connection.json b/tests/fixtures/centos-7.7/nmcli-connection.json new file mode 100644 index 00000000..823ebaa3 --- /dev/null +++ b/tests/fixtures/centos-7.7/nmcli-connection.json @@ -0,0 +1 @@ +[{"name":"ens33","uuid":"d92ece08-9e02-47d5-b2d2-92c80e155744","type":"ethernet","device":"ens33"}] diff --git a/tests/fixtures/centos-7.7/nmcli-device-all.json b/tests/fixtures/centos-7.7/nmcli-device-all.json new file mode 100644 index 00000000..482d2116 --- /dev/null +++ b/tests/fixtures/centos-7.7/nmcli-device-all.json @@ -0,0 +1 @@ +[{"device":"ens33","type":"ethernet","state":"connected","ip4_connectivity":"full","ip6_connectivity":"full","dbus_path":"/org/freedesktop/NetworkManager/Devices/2","connection":"ens33","con_uuid":"d92ece08-9e02-47d5-b2d2-92c80e155744","con_path":"/org/freedesktop/NetworkManager/ActiveConnection/1"},{"device":"docker0","type":"bridge","state":"unmanaged","ip4_connectivity":"unknown","ip6_connectivity":"unknown","dbus_path":"/org/freedesktop/NetworkManager/Devices/3","connection":null,"con_uuid":null,"con_path":null},{"device":"lo","type":"loopback","state":"unmanaged","ip4_connectivity":"unknown","ip6_connectivity":"unknown","dbus_path":"/org/freedesktop/NetworkManager/Devices/1","connection":null,"con_uuid":null,"con_path":null}] diff --git a/tests/fixtures/centos-7.7/nmcli-device-show-ens33.json b/tests/fixtures/centos-7.7/nmcli-device-show-ens33.json new file mode 100644 index 00000000..9d3e6629 --- /dev/null +++ b/tests/fixtures/centos-7.7/nmcli-device-show-ens33.json @@ -0,0 +1 @@ +[{"device":"ens33","type":"ethernet","hwaddr":"00:0C:29:3B:58:0E","mtu":1500,"state":100,"state_text":"connected","connection":"ens33","con_path":"/org/freedesktop/NetworkManager/ActiveConnection/1","wired_properties_carrier":"on","ip4_address_1":"192.168.71.180/24","ip4_gateway":"192.168.71.2","ip4_route_1":{"dst":"0.0.0.0/0","nh":"192.168.71.2","mt":100},"ip4_route_2":{"dst":"192.168.71.0/24","nh":"0.0.0.0","mt":100},"ip4_dns_1":"192.168.71.2","ip4_domain_1":"localdomain","ip6_address_1":"fe80::c1cb:715d:bc3e:b8a0/64","ip6_gateway":null,"ip6_route_1":{"dst":"fe80::/64","nh":"::","mt":100},"ip6_route_2":{"dst":"ff00::/8","nh":"::","mt":256,"table":255}}] diff --git a/tests/fixtures/centos-7.7/nmcli-device-show-lo.json b/tests/fixtures/centos-7.7/nmcli-device-show-lo.json new file mode 100644 index 00000000..b18607db --- /dev/null +++ b/tests/fixtures/centos-7.7/nmcli-device-show-lo.json @@ -0,0 +1 @@ +[{"device":"lo","type":"loopback","hwaddr":"00:00:00:00:00:00","mtu":65536,"state":10,"state_text":"unmanaged","connection":null,"con_path":null,"ip4_address_1":"127.0.0.1/8","ip4_gateway":null,"ip6_address_1":"::1/128","ip6_gateway":null}] diff --git a/tests/fixtures/centos-7.7/nmcli-device-show.json b/tests/fixtures/centos-7.7/nmcli-device-show.json new file mode 100644 index 00000000..78b87947 --- /dev/null +++ b/tests/fixtures/centos-7.7/nmcli-device-show.json @@ -0,0 +1 @@ +[{"device":"ens33","type":"ethernet","hwaddr":"00:0C:29:3B:58:0E","mtu":1500,"state":100,"state_text":"connected","connection":"ens33","con_path":"/org/freedesktop/NetworkManager/ActiveConnection/1","wired_properties_carrier":"on","ip4_address_1":"192.168.71.180/24","ip4_gateway":"192.168.71.2","ip4_route_1":{"dst":"0.0.0.0/0","nh":"192.168.71.2","mt":100},"ip4_route_2":{"dst":"192.168.71.0/24","nh":"0.0.0.0","mt":100},"ip4_dns_1":"192.168.71.2","ip4_domain_1":"localdomain","ip6_address_1":"fe80::c1cb:715d:bc3e:b8a0/64","ip6_gateway":null,"ip6_route_1":{"dst":"fe80::/64","nh":"::","mt":100},"ip6_route_2":{"dst":"ff00::/8","nh":"::","mt":256,"table":255}},{"device":"docker0","type":"bridge","hwaddr":"02:42:99:67:E8:21","mtu":1500,"state":10,"state_text":"unmanaged","connection":null,"con_path":null,"ip4_address_1":"172.17.0.1/16","ip4_gateway":null,"ip4_route_1":{"dst":"172.17.0.0/16","nh":"0.0.0.0","mt":0},"ip6_gateway":null},{"device":"lo","type":"loopback","hwaddr":"00:00:00:00:00:00","mtu":65536,"state":10,"state_text":"unmanaged","connection":null,"con_path":null,"ip4_address_1":"127.0.0.1/8","ip4_gateway":null,"ip6_address_1":"::1/128","ip6_gateway":null}] diff --git a/tests/fixtures/centos-7.7/nmcli-device.json b/tests/fixtures/centos-7.7/nmcli-device.json new file mode 100644 index 00000000..24eb3c58 --- /dev/null +++ b/tests/fixtures/centos-7.7/nmcli-device.json @@ -0,0 +1 @@ +[{"device":"ens33","type":"ethernet","state":"connected","connection":"ens33"},{"device":"docker0","type":"bridge","state":"unmanaged","connection":null},{"device":"lo","type":"loopback","state":"unmanaged","connection":null}] diff --git a/tests/fixtures/centos-7.7/nmcli-general-all.json b/tests/fixtures/centos-7.7/nmcli-general-all.json new file mode 100644 index 00000000..fd1a97dd --- /dev/null +++ b/tests/fixtures/centos-7.7/nmcli-general-all.json @@ -0,0 +1 @@ +[{"running":"running","version":"1.18.0","state":"connected","startup":"started","connectivity":"full","networking":"enabled","wifi_hw":"enabled","wifi":"enabled","wwan_hw":"enabled","wwan":"enabled"}] diff --git a/tests/fixtures/centos-7.7/nmcli-general-permissions.json b/tests/fixtures/centos-7.7/nmcli-general-permissions.json new file mode 100644 index 00000000..1e63dbf0 --- /dev/null +++ b/tests/fixtures/centos-7.7/nmcli-general-permissions.json @@ -0,0 +1 @@ +[{"org_freedesktop_networkmanager_enable_disable_network":"yes","org_freedesktop_networkmanager_enable_disable_wifi":"yes","org_freedesktop_networkmanager_enable_disable_wwan":"yes","org_freedesktop_networkmanager_enable_disable_wimax":"yes","org_freedesktop_networkmanager_sleep_wake":"yes","org_freedesktop_networkmanager_network_control":"yes","org_freedesktop_networkmanager_wifi_share_protected":"yes","org_freedesktop_networkmanager_wifi_share_open":"yes","org_freedesktop_networkmanager_settings_modify_system":"yes","org_freedesktop_networkmanager_settings_modify_own":"yes","org_freedesktop_networkmanager_settings_modify_hostname":"yes","org_freedesktop_networkmanager_settings_modify_global_dns":"yes","org_freedesktop_networkmanager_reload":"yes","org_freedesktop_networkmanager_checkpoint_rollback":"yes","org_freedesktop_networkmanager_enable_disable_statistics":"yes","org_freedesktop_networkmanager_enable_disable_connectivity_check":"yes","org_freedesktop_networkmanager_wifi_scan":"unknown"}] diff --git a/tests/test_nmcli.py b/tests/test_nmcli.py new file mode 100644 index 00000000..b0f75718 --- /dev/null +++ b/tests/test_nmcli.py @@ -0,0 +1,155 @@ +import os +import unittest +import json +import jc.parsers.nmcli +from jc.exceptions import ParseError + +THIS_DIR = os.path.dirname(os.path.abspath(__file__)) + + +class MyTests(unittest.TestCase): + + def setUp(self): + # input + with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/centos-7.7/nmcli.out'), 'r', encoding='utf-8') as f: + self.centos_7_7_nmcli = f.read() + + with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/centos-7.7/nmcli-connection-all.out'), 'r', encoding='utf-8') as f: + self.centos_7_7_nmcli_connection_all = f.read() + + with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/centos-7.7/nmcli-connection-show-ens33.out'), 'r', encoding='utf-8') as f: + self.centos_7_7_nmcli_connection_show_ens33 = f.read() + + with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/centos-7.7/nmcli-connection.out'), 'r', encoding='utf-8') as f: + self.centos_7_7_nmcli_connection = f.read() + + with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/centos-7.7/nmcli-device-all.out'), 'r', encoding='utf-8') as f: + self.centos_7_7_nmcli_device_all = f.read() + + with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/centos-7.7/nmcli-device-show-ens33.out'), 'r', encoding='utf-8') as f: + self.centos_7_7_nmcli_device_show_ens33 = f.read() + + with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/centos-7.7/nmcli-device-show-lo.out'), 'r', encoding='utf-8') as f: + self.centos_7_7_nmcli_device_show_lo = f.read() + + with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/centos-7.7/nmcli-device-show.out'), 'r', encoding='utf-8') as f: + self.centos_7_7_nmcli_device_show = f.read() + + with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/centos-7.7/nmcli-device.out'), 'r', encoding='utf-8') as f: + self.centos_7_7_nmcli_device = f.read() + + with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/centos-7.7/nmcli-general-all.out'), 'r', encoding='utf-8') as f: + self.centos_7_7_nmcli_general_all = f.read() + + with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/centos-7.7/nmcli-general-permissions.out'), 'r', encoding='utf-8') as f: + self.centos_7_7_nmcli_general_permissions = f.read() + + + # output + with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/centos-7.7/nmcli-connection-all.json'), 'r', encoding='utf-8') as f: + self.centos_7_7_nmcli_connection_all_json = json.loads(f.read()) + + with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/centos-7.7/nmcli-connection-show-ens33.json'), 'r', encoding='utf-8') as f: + self.centos_7_7_nmcli_connection_show_ens33_json = json.loads(f.read()) + + with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/centos-7.7/nmcli-connection.json'), 'r', encoding='utf-8') as f: + self.centos_7_7_nmcli_connection_json = json.loads(f.read()) + + with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/centos-7.7/nmcli-device-all.json'), 'r', encoding='utf-8') as f: + self.centos_7_7_nmcli_device_all_json = json.loads(f.read()) + + with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/centos-7.7/nmcli-device-show-ens33.json'), 'r', encoding='utf-8') as f: + self.centos_7_7_nmcli_device_show_ens33_json = json.loads(f.read()) + + with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/centos-7.7/nmcli-device-show-lo.json'), 'r', encoding='utf-8') as f: + self.centos_7_7_nmcli_device_show_lo_json = json.loads(f.read()) + + with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/centos-7.7/nmcli-device-show.json'), 'r', encoding='utf-8') as f: + self.centos_7_7_nmcli_device_show_json = json.loads(f.read()) + + with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/centos-7.7/nmcli-device.json'), 'r', encoding='utf-8') as f: + self.centos_7_7_nmcli_device_json = json.loads(f.read()) + + with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/centos-7.7/nmcli-general-all.json'), 'r', encoding='utf-8') as f: + self.centos_7_7_nmcli_general_all_json = json.loads(f.read()) + + with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/centos-7.7/nmcli-general-permissions.json'), 'r', encoding='utf-8') as f: + self.centos_7_7_nmcli_general_permissions_json = json.loads(f.read()) + + + + def test_nmcli_nodata(self): + """ + Test 'nmcli' with no data + """ + self.assertEqual(jc.parsers.nmcli.parse('', quiet=True), []) + + def test_nmcli_centos_7_7(self): + """ + Test 'nmcli' on Centos 7.7 - this should raise a ParseError exception + """ + self.assertRaises(ParseError, jc.parsers.nmcli.parse, self.centos_7_7_nmcli, quiet=True) + + def test_nmcli_connection_all_centos_7_7(self): + """ + Test 'nmcli -f all connection' on Centos 7.7 + """ + self.assertEqual(jc.parsers.nmcli.parse(self.centos_7_7_nmcli_connection_all, quiet=True), self.centos_7_7_nmcli_connection_all_json) + + def test_nmcli_connection_show_ens33_centos_7_7(self): + """ + Test 'nmcli connection show ens33' on Centos 7.7 + """ + self.assertEqual(jc.parsers.nmcli.parse(self.centos_7_7_nmcli_connection_show_ens33, quiet=True), self.centos_7_7_nmcli_connection_show_ens33_json) + + def test_nmcli_connection_centos_7_7(self): + """ + Test 'nmcli connection' on Centos 7.7 + """ + self.assertEqual(jc.parsers.nmcli.parse(self.centos_7_7_nmcli_connection, quiet=True), self.centos_7_7_nmcli_connection_json) + + def test_nmcli_device_all_centos_7_7(self): + """ + Test 'nmcli -f all device' on Centos 7.7 + """ + self.assertEqual(jc.parsers.nmcli.parse(self.centos_7_7_nmcli_device_all, quiet=True), self.centos_7_7_nmcli_device_all_json) + + def test_nmcli_device_show_ens33_centos_7_7(self): + """ + Test 'nmcli device show ens33' on Centos 7.7 + """ + self.assertEqual(jc.parsers.nmcli.parse(self.centos_7_7_nmcli_device_show_ens33, quiet=True), self.centos_7_7_nmcli_device_show_ens33_json) + + def test_nmcli_device_show_lo_centos_7_7(self): + """ + Test 'nmcli device show lo' on Centos 7.7 + """ + self.assertEqual(jc.parsers.nmcli.parse(self.centos_7_7_nmcli_device_show_lo, quiet=True), self.centos_7_7_nmcli_device_show_lo_json) + + def test_nmcli_device_show_centos_7_7(self): + """ + Test 'nmcli device show' on Centos 7.7 + """ + self.assertEqual(jc.parsers.nmcli.parse(self.centos_7_7_nmcli_device_show, quiet=True), self.centos_7_7_nmcli_device_show_json) + + def test_nmcli_device_centos_7_7(self): + """ + Test 'nmcli device' on Centos 7.7 + """ + self.assertEqual(jc.parsers.nmcli.parse(self.centos_7_7_nmcli_device, quiet=True), self.centos_7_7_nmcli_device_json) + + def test_nmcli_general_all_centos_7_7(self): + """ + Test 'nmcli -f all general' on Centos 7.7 + """ + self.assertEqual(jc.parsers.nmcli.parse(self.centos_7_7_nmcli_general_all, quiet=True), self.centos_7_7_nmcli_general_all_json) + + def test_nmcli_general_permissions_centos_7_7(self): + """ + Test 'nmcli general permissions' on Centos 7.7 + """ + self.assertEqual(jc.parsers.nmcli.parse(self.centos_7_7_nmcli_general_permissions, quiet=True), self.centos_7_7_nmcli_general_permissions_json) + + +if __name__ == '__main__': + unittest.main()