diff --git a/test/utils.py b/test/utils.py index ffb9fd4d..a92c8597 100644 --- a/test/utils.py +++ b/test/utils.py @@ -2,18 +2,22 @@ import re import subprocess + def run_cmd(cmd): - res = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) + res = subprocess.Popen( + cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE + ) output, err = res.communicate() msg = output.decode() + err.decode() return res.returncode, msg + def gnmi_set(delete_list, update_list, replace_list): path = os.getcwd() - cmd = path + '/build/bin/gnmi_set ' - cmd += '-insecure -username admin -password sonicadmin ' - cmd += '-target_addr 127.0.0.1:8080 ' - cmd += '-alsologtostderr ' + cmd = path + "/build/bin/gnmi_set " + cmd += "-insecure -username admin -password sonicadmin " + cmd += "-target_addr 127.0.0.1:8080 " + cmd += "-alsologtostderr " for delete in delete_list: cmd += " -delete " + delete for update in update_list: @@ -22,15 +26,16 @@ def gnmi_set(delete_list, update_list, replace_list): cmd += " -replace " + replace ret, msg = run_cmd(cmd) if ret == 0: - return ret, '' + return ret, "" return ret, msg + def gnmi_set_with_password(delete_list, update_list, replace_list, user, password): path = os.getcwd() - cmd = path + '/build/bin/gnmi_set ' - cmd += '-insecure -username %s -password %s '%(user, password) - cmd += '-target_addr 127.0.0.1:8080 ' - cmd += '-alsologtostderr ' + cmd = path + "/build/bin/gnmi_set " + cmd += "-insecure -username %s -password %s " % (user, password) + cmd += "-target_addr 127.0.0.1:8080 " + cmd += "-alsologtostderr " for delete in delete_list: cmd += " -delete " + delete for update in update_list: @@ -39,15 +44,16 @@ def gnmi_set_with_password(delete_list, update_list, replace_list, user, passwor cmd += " -replace " + replace ret, msg = run_cmd(cmd) if ret == 0: - return ret, '' + return ret, "" return ret, msg + def gnmi_set_with_jwt(delete_list, update_list, replace_list, token): path = os.getcwd() - cmd = path + '/build/bin/gnmi_set ' - cmd += '-insecure -jwt_token ' + token + ' ' - cmd += '-target_addr 127.0.0.1:8080 ' - cmd += '-alsologtostderr ' + cmd = path + "/build/bin/gnmi_set " + cmd += "-insecure -jwt_token " + token + " " + cmd += "-target_addr 127.0.0.1:8080 " + cmd += "-alsologtostderr " for delete in delete_list: cmd += " -delete " + delete for update in update_list: @@ -56,53 +62,56 @@ def gnmi_set_with_jwt(delete_list, update_list, replace_list, token): cmd += " -replace " + replace ret, msg = run_cmd(cmd) if ret == 0: - return ret, '' + return ret, "" return ret, msg + def gnmi_get(path_list): path = os.getcwd() - cmd = path + '/build/bin/gnmi_get ' - cmd += '-insecure -username admin -password sonicadmin ' - cmd += '-target_addr 127.0.0.1:8080 ' - cmd += '-alsologtostderr ' + cmd = path + "/build/bin/gnmi_get " + cmd += "-insecure -username admin -password sonicadmin " + cmd += "-target_addr 127.0.0.1:8080 " + cmd += "-alsologtostderr " for path in path_list: cmd += " -xpath " + path ret, msg = run_cmd(cmd) if ret == 0: - msg = msg.replace('\\', '') - find_list = re.findall( r'json_ietf_val:\s*"(.*?)"\s*>', msg) + msg = msg.replace("\\", "") + find_list = re.findall(r'json_ietf_val:\s*"(.*?)"\s*>', msg) if find_list: return ret, find_list else: return -1, [msg] return ret, [msg] + def gnmi_get_with_encoding(path_list, encoding): path = os.getcwd() - cmd = path + '/build/bin/gnmi_get ' - cmd += '-insecure -username admin -password sonicadmin ' - cmd += '-target_addr 127.0.0.1:8080 ' - cmd += '-alsologtostderr ' - cmd += '-encoding %s '%(encoding) + cmd = path + "/build/bin/gnmi_get " + cmd += "-insecure -username admin -password sonicadmin " + cmd += "-target_addr 127.0.0.1:8080 " + cmd += "-alsologtostderr " + cmd += "-encoding %s " % (encoding) for path in path_list: cmd += " -xpath " + path ret, msg = run_cmd(cmd) if ret == 0: - msg = msg.replace('\\', '') - find_list = re.findall( r'json_ietf_val:\s*"(.*?)"\s*>', msg) + msg = msg.replace("\\", "") + find_list = re.findall(r'json_ietf_val:\s*"(.*?)"\s*>', msg) if find_list: return ret, find_list else: return -1, [msg] return ret, [msg] + def gnmi_get_proto(path_list, file_list): path = os.getcwd() - cmd = path + '/build/bin/gnmi_get ' - cmd += '-insecure -username admin -password sonicadmin ' - cmd += '-target_addr 127.0.0.1:8080 ' - cmd += '-alsologtostderr ' - cmd += '-encoding PROTO ' + cmd = path + "/build/bin/gnmi_get " + cmd += "-insecure -username admin -password sonicadmin " + cmd += "-target_addr 127.0.0.1:8080 " + cmd += "-alsologtostderr " + cmd += "-encoding PROTO " for path in path_list: cmd += " -xpath " + path for file in file_list: @@ -110,209 +119,220 @@ def gnmi_get_proto(path_list, file_list): ret, msg = run_cmd(cmd) return ret, msg + def gnmi_get_with_password(path_list, user, password): path = os.getcwd() - cmd = path + '/build/bin/gnmi_get ' - cmd += '-insecure -username %s -password %s '%(user, password) - cmd += '-target_addr 127.0.0.1:8080 ' - cmd += '-alsologtostderr ' + cmd = path + "/build/bin/gnmi_get " + cmd += "-insecure -username %s -password %s " % (user, password) + cmd += "-target_addr 127.0.0.1:8080 " + cmd += "-alsologtostderr " for path in path_list: cmd += " -xpath " + path ret, msg = run_cmd(cmd) if ret == 0: - msg = msg.replace('\\', '') - find_list = re.findall( r'json_ietf_val:\s*"(.*?)"\s*>', msg) + msg = msg.replace("\\", "") + find_list = re.findall(r'json_ietf_val:\s*"(.*?)"\s*>', msg) if find_list: return ret, find_list else: return -1, [msg] return ret, [msg] + def gnmi_get_with_jwt(path_list, token): path = os.getcwd() - cmd = path + '/build/bin/gnmi_get ' - cmd += '-insecure -jwt_token ' + token + ' ' - cmd += '-target_addr 127.0.0.1:8080 ' - cmd += '-alsologtostderr ' + cmd = path + "/build/bin/gnmi_get " + cmd += "-insecure -jwt_token " + token + " " + cmd += "-target_addr 127.0.0.1:8080 " + cmd += "-alsologtostderr " for path in path_list: cmd += " -xpath " + path ret, msg = run_cmd(cmd) if ret == 0: - msg = msg.replace('\\', '') - find_list = re.findall( r'json_ietf_val:\s*"(.*?)"\s*>', msg) + msg = msg.replace("\\", "") + find_list = re.findall(r'json_ietf_val:\s*"(.*?)"\s*>', msg) if find_list: return ret, find_list else: return -1, [msg] return ret, [msg] + def gnmi_capabilities(): path = os.getcwd() - cmd = path + '/build/bin/gnmi_cli ' - cmd += '-client_types=gnmi -a 127.0.0.1:8080 -logtostderr -insecure ' - cmd += '-capabilities ' + cmd = path + "/build/bin/gnmi_cli " + cmd += "-client_types=gnmi -a 127.0.0.1:8080 -logtostderr -insecure " + cmd += "-capabilities " ret, msg = run_cmd(cmd) return ret, msg + def gnmi_subscribe_poll(gnmi_path, interval, count, timeout): path = os.getcwd() - cmd = path + '/build/bin/gnmi_cli ' - cmd += '-client_types=gnmi -a 127.0.0.1:8080 -logtostderr -insecure ' + cmd = path + "/build/bin/gnmi_cli " + cmd += "-client_types=gnmi -a 127.0.0.1:8080 -logtostderr -insecure " # Use sonic-db as default origin - cmd += '-origin=sonic-db ' + cmd += "-origin=sonic-db " if timeout: - cmd += '-streaming_timeout=%u ' % timeout - cmd += '-query_type=polling ' - cmd += '-polling_interval %us -count %u ' % (interval, count) - cmd += '-q %s' % (gnmi_path) + cmd += "-streaming_timeout=%u " % timeout + cmd += "-query_type=polling " + cmd += "-polling_interval %us -count %u " % (interval, count) + cmd += "-q %s" % (gnmi_path) ret, msg = run_cmd(cmd) return ret, msg + def gnmi_subscribe_stream_sample(gnmi_path, interval, count, timeout): path = os.getcwd() - cmd = path + '/build/bin/gnmi_cli ' - cmd += '-client_types=gnmi -a 127.0.0.1:8080 -logtostderr -insecure ' + cmd = path + "/build/bin/gnmi_cli " + cmd += "-client_types=gnmi -a 127.0.0.1:8080 -logtostderr -insecure " # Use sonic-db as default origin - cmd += '-origin=sonic-db ' + cmd += "-origin=sonic-db " if timeout: - cmd += '-streaming_timeout=%u ' % timeout - cmd += '-query_type=streaming ' - cmd += '-streaming_type=SAMPLE ' - cmd += '-streaming_sample_interval %u -expected_count %u ' % (interval, count) - cmd += '-q %s' % (gnmi_path) + cmd += "-streaming_timeout=%u " % timeout + cmd += "-query_type=streaming " + cmd += "-streaming_type=SAMPLE " + cmd += "-streaming_sample_interval %u -expected_count %u " % (interval, count) + cmd += "-q %s" % (gnmi_path) ret, msg = run_cmd(cmd) return ret, msg + def gnmi_subscribe_stream_onchange(gnmi_path, count, timeout): path = os.getcwd() - cmd = path + '/build/bin/gnmi_cli ' - cmd += '-client_types=gnmi -a 127.0.0.1:8080 -logtostderr -insecure -latency ' + cmd = path + "/build/bin/gnmi_cli " + cmd += "-client_types=gnmi -a 127.0.0.1:8080 -logtostderr -insecure -latency " # Use sonic-db as default origin - cmd += '-origin=sonic-db ' + cmd += "-origin=sonic-db " if timeout: - cmd += '-streaming_timeout=%u ' % timeout - cmd += '-query_type=streaming ' - cmd += '-streaming_type=ON_CHANGE -updates_only ' - cmd += '-expected_count %u ' % count - cmd += '-q %s' % (gnmi_path) + cmd += "-streaming_timeout=%u " % timeout + cmd += "-query_type=streaming " + cmd += "-streaming_type=ON_CHANGE -updates_only " + cmd += "-expected_count %u " % count + cmd += "-q %s" % (gnmi_path) ret, msg = run_cmd(cmd) return ret, msg + def gnmi_dump(name): path = os.getcwd() - cmd = 'sudo ' + path + '/build/bin/gnmi_dump' + cmd = "sudo " + path + "/build/bin/gnmi_dump" ret, msg = run_cmd(cmd) if ret == 0: - msg_list = msg.split('\n') + msg_list = msg.split("\n") for line in msg_list: - if '---' in line: - current = line.split('---') + if "---" in line: + current = line.split("---") if current[0] == name: return 0, int(current[1]) return -1, 0 return ret, 0 -def gnoi_time(): + +def gnoi_client_cmd_common(): + """ + Returns the gnoi_client command with default target + """ path = os.getcwd() - cmd = path + '/build/bin/gnoi_client ' - cmd += '-insecure -target 127.0.0.1:8080 ' - cmd += '-rpc Time ' + cmd = path + "/build/bin/gnoi_client " + cmd += "-insecure -target 127.0.0.1:8080 " + return cmd + + +def gnoi_time(): + cmd = gnoi_client_cmd_common() + cmd += "-rpc Time " ret, msg = run_cmd(cmd) return ret, msg + def gnoi_reboot(method, delay, message): - path = os.getcwd() - cmd = path + '/build/bin/gnoi_client ' - cmd += '-insecure -target 127.0.0.1:8080 ' - cmd += '-rpc Reboot ' - cmd += '-jsonin "{\\\"method\\\":%d, \\\"delay\\\":%d, \\\"message\\\":\\\"%s\\\"}"'%(method, delay, message) + cmd = gnoi_client_cmd_common() + cmd += "-rpc Reboot " + cmd += '-jsonin "{\\"method\\":%d, \\"delay\\":%d, \\"message\\":\\"%s\\"}"' % ( + method, + delay, + message, + ) ret, msg = run_cmd(cmd) return ret, msg + def gnoi_kill_process(json_data): - path = os.getcwd() - cmd = path + '/build/bin/gnoi_client ' - cmd += '-insecure -target 127.0.0.1:8080 ' - cmd += '-rpc KillProcess ' - cmd += f'-jsonin \'{json_data}\'' + cmd = gnoi_client_cmd_common() + cmd += "-rpc KillProcess " + cmd += f"-jsonin '{json_data}'" ret, msg = run_cmd(cmd) return ret, msg + def gnoi_restart_process(json_data): - path = os.getcwd() - cmd = path + '/build/bin/gnoi_client ' - cmd += '-insecure -target 127.0.0.1:8080 ' - cmd += '-rpc KillProcess ' - cmd += f'-jsonin \'{json_data}\'' + cmd = gnoi_client_cmd_common() + cmd += "-rpc KillProcess " + cmd += f"-jsonin '{json_data}'" ret, msg = run_cmd(cmd) return ret, msg + def gnoi_rebootstatus(): - path = os.getcwd() - cmd = path + '/build/bin/gnoi_client ' - cmd += '-insecure -target 127.0.0.1:8080 ' - cmd += '-rpc RebootStatus ' + cmd = gnoi_client_cmd_common() + cmd += "-rpc RebootStatus " ret, msg = run_cmd(cmd) return ret, msg + def gnoi_cancelreboot(message): - path = os.getcwd() - cmd = path + '/build/bin/gnoi_client ' - cmd += '-insecure -target 127.0.0.1:8080 ' - cmd += '-rpc CancelReboot ' - cmd += '-jsonin "{\\\"message\\\":\\\"%s\\\"}"'%(message) + cmd = gnoi_client_cmd_common() + cmd += "-rpc CancelReboot " + cmd += '-jsonin "{\\"message\\":\\"%s\\"}"' % (message) ret, msg = run_cmd(cmd) return ret, msg + def gnoi_ping(dst): - path = os.getcwd() - cmd = path + '/build/bin/gnoi_client ' - cmd += '-insecure -target 127.0.0.1:8080 ' - cmd += '-rpc Ping ' - cmd += '-jsonin "{\\\"destination\\\":\\\"%s\\\"}"'%(dst) + cmd = gnoi_client_cmd_common() + cmd += "-rpc Ping " + cmd += '-jsonin "{\\"destination\\":\\"%s\\"}"' % (dst) ret, msg = run_cmd(cmd) return ret, msg def gnoi_traceroute(dst): - path = os.getcwd() - cmd = path + '/build/bin/gnoi_client ' - cmd += '-insecure -target 127.0.0.1:8080 ' - cmd += '-rpc Traceroute ' - cmd += '-jsonin "{\\\"destination\\\":\\\"%s\\\"}"'%(dst) + cmd = gnoi_client_cmd_common() + cmd += "-rpc Traceroute " + cmd += '-jsonin "{\\"destination\\":\\"%s\\"}"' % (dst) ret, msg = run_cmd(cmd) return ret, msg + def gnoi_setpackage(): - path = os.getcwd() - cmd = path + '/build/bin/gnoi_client ' - cmd += '-insecure -target 127.0.0.1:8080 ' - cmd += '-rpc SetPackage ' + cmd = gnoi_client_cmd_common() + cmd += "-rpc SetPackage " ret, msg = run_cmd(cmd) return ret, msg + def gnoi_switchcontrolprocessor(): - path = os.getcwd() - cmd = path + '/build/bin/gnoi_client ' - cmd += '-insecure -target 127.0.0.1:8080 ' - cmd += '-rpc SwitchControlProcessor ' + cmd = gnoi_client_cmd_common() + cmd += "-rpc SwitchControlProcessor " ret, msg = run_cmd(cmd) return ret, msg + def gnoi_authenticate(username, password): - path = os.getcwd() - cmd = path + '/build/bin/gnoi_client ' - cmd += '-insecure -target 127.0.0.1:8080 ' - cmd += '-module Sonic -rpc authenticate ' - cmd += '-jsonin "{\\\"Username\\\":\\\"%s\\\", \\\"Password\\\":\\\"%s\\\"}"'%(username, password) + cmd = gnoi_client_cmd_common() + cmd += "-module Sonic -rpc authenticate " + cmd += '-jsonin "{\\"Username\\":\\"%s\\", \\"Password\\":\\"%s\\"}"' % ( + username, + password, + ) ret, msg = run_cmd(cmd) return ret, msg + def gnoi_refresh_with_jwt(token): - path = os.getcwd() - cmd = path + '/build/bin/gnoi_client ' - cmd += '-insecure -target 127.0.0.1:8080 ' - cmd += '-jwt_token ' + token + ' ' - cmd += '-module Sonic -rpc refresh ' + cmd = gnoi_client_cmd_common() + cmd += "-jwt_token " + token + " " + cmd += "-module Sonic -rpc refresh " ret, msg = run_cmd(cmd) - return ret, msg \ No newline at end of file + return ret, msg