1#!/usr/bin/env python3 2# SPDX-License-Identifier: BSD-3-Clause 3# Copyright (C) 2018 Intel Corporation 4# All rights reserved. 5# 6 7import pexpect 8import os 9import sys 10import re 11 12 13def execute_command(cmd, element=None, element_exists=False): 14 child.sendline(cmd) 15 child.expect("/>") 16 if "error response" in child.before.decode(): 17 print("Error in cmd: %s" % cmd) 18 exit(1) 19 ls_tree = cmd.split(" ")[0] 20 if ls_tree and element: 21 child.sendline("ls %s" % ls_tree) 22 child.expect("/>") 23 if element_exists: 24 if element not in child.before.decode(): 25 print("Element %s not in list:\n%s" % (element, child.before.decode())) 26 exit(1) 27 else: 28 if element in child.before.decode(): 29 print("Element %s is in list:\n%s" % (element, child.before.decode())) 30 exit(1) 31 32 33if __name__ == "__main__": 34 socket = "/var/tmp/spdk.sock" 35 port = None 36 if len(sys.argv) == 3: 37 socket = sys.argv[2] 38 elif len(sys.argv) == 4: 39 port = sys.argv[3] 40 testdir = os.path.dirname(os.path.realpath(sys.argv[0])) 41 42 if port is None: 43 child = pexpect.spawn(os.path.join(testdir, "../../scripts/spdkcli.py") + " -s %s" % socket) 44 else: 45 child = pexpect.spawn(os.path.join(testdir, "../../scripts/spdkcli.py") + " -s %s -p %s" % (socket, port)) 46 child.expect(">") 47 child.sendline("cd /") 48 child.expect("/>") 49 50 cmd_lines = sys.argv[1].strip().split("\n") 51 for line in cmd_lines: 52 data = line.strip() 53 p = re.compile('\'(.*?)\'') 54 cmd = p.findall(data) 55 if data[-1] != "\'": 56 cmd.append(data.rsplit(" ", 1)[1].strip()) 57 if cmd[-1] == "False": 58 cmd[-1] = False 59 else: 60 cmd[-1] = True 61 else: 62 cmd.append(False) 63 print("Executing command: %s" % cmd) 64 execute_command(*cmd[0:3]) 65