From 08dca58358c95a41615150513a006379c4ee7825 Mon Sep 17 00:00:00 2001 From: Justin Berger Date: Sun, 3 Sep 2017 20:09:24 -0600 Subject: Tests: reworked server tests to allow other operation modes --- Tests/Server/CMakeLists.txt | 12 +++--- Tests/Server/cmakelib.py | 91 +++++++++++++++++++++++++++++++++++++++++---- Tests/Server/server-test.py | 26 ++++--------- 3 files changed, 97 insertions(+), 32 deletions(-) diff --git a/Tests/Server/CMakeLists.txt b/Tests/Server/CMakeLists.txt index 08bef0c..41d1131 100644 --- a/Tests/Server/CMakeLists.txt +++ b/Tests/Server/CMakeLists.txt @@ -3,10 +3,10 @@ project(Server CXX) find_package(PythonInterp REQUIRED) -macro(do_test bsname file) +macro(do_test bsname file type) execute_process(COMMAND ${PYTHON_EXECUTABLE} -B # no .pyc files - "${CMAKE_SOURCE_DIR}/server-test.py" + "${CMAKE_SOURCE_DIR}/${type}-test.py" "${CMAKE_COMMAND}" "${CMAKE_SOURCE_DIR}/${file}" "${CMAKE_SOURCE_DIR}" @@ -20,9 +20,9 @@ macro(do_test bsname file) endif() endmacro() -do_test("test_cache" "tc_cache.json") -do_test("test_handshake" "tc_handshake.json") -do_test("test_globalSettings" "tc_globalSettings.json") -do_test("test_buildsystem1" "tc_buildsystem1.json") +do_test("test_cache" "tc_cache.json" "server") +do_test("test_handshake" "tc_handshake.json" "server") +do_test("test_globalSettings" "tc_globalSettings.json" "server") +do_test("test_buildsystem1" "tc_buildsystem1.json" "server") add_executable(Server empty.cpp) diff --git a/Tests/Server/cmakelib.py b/Tests/Server/cmakelib.py index 2218e02..aebc84f 100644 --- a/Tests/Server/cmakelib.py +++ b/Tests/Server/cmakelib.py @@ -1,5 +1,5 @@ from __future__ import print_function -import sys, subprocess, json +import sys, subprocess, json, os, select termwidth = 150 @@ -38,6 +38,35 @@ def col_print(title, array): for index in range(numRows): print(indent + pad.join(item.ljust(maxitemwidth) for item in array[index::numRows])) +filterPacket = lambda x: x + +def defaultExitWithError(proc): + data = "" + try: + while select.select([proc.stdout], [], [], 3.)[0]: + data = data + proc.stdout.read(1) + if len(data): + print("Rest of raw buffer from server:") + printServer(data) + except: + pass + proc.stdout.close() + proc.stdin.close() + proc.kill() + sys.exit(1) + +exitWithError = lambda proc: defaultExitWithError(proc) + +def printServer(*args): + print("SERVER>", *args) + print() + sys.stdout.flush() + +def printClient(*args): + print("CLIENT>", *args) + print() + sys.stdout.flush() + def waitForRawMessage(cmakeCommand): stdoutdata = "" payload = "" @@ -50,12 +79,16 @@ def waitForRawMessage(cmakeCommand): begin = stdoutdata.find('[== "CMake Server" ==[\n') end = stdoutdata.find(']== "CMake Server" ==]') - if (begin != -1 and end != -1): + if begin != -1 and end != -1: begin += len('[== "CMake Server" ==[\n') payload = stdoutdata[begin:end] - if print_communication: - print("\nSERVER>", json.loads(payload), "\n") - return json.loads(payload) + jsonPayload = json.loads(payload) + filteredPayload = filterPacket(jsonPayload) + if print_communication and filteredPayload: + printServer(filteredPayload) + if filteredPayload is not None or jsonPayload is None: + return jsonPayload + stdoutdata = stdoutdata[(end+len(']== "CMake Server" ==]')):] def writeRawData(cmakeCommand, content): writeRawData.counter += 1 @@ -71,7 +104,8 @@ def writeRawData(cmakeCommand, content): payload = payload.replace('\n', '\r\n') if print_communication: - print("\nCLIENT>", content, "(Use \\r\\n:", rn, ")\n") + printClient(content, "(Use \\r\\n:", rn, ")") + cmakeCommand.stdin.write(payload.encode('utf-8')) cmakeCommand.stdin.flush() writeRawData.counter = 0 @@ -79,7 +113,7 @@ writeRawData.counter = 0 def writePayload(cmakeCommand, obj): writeRawData(cmakeCommand, json.dumps(obj)) -def initProc(cmakeCommand): +def initServerProc(cmakeCommand): cmakeCommand = subprocess.Popen([cmakeCommand, "-E", "server", "--experimental", "--debug"], stdin=subprocess.PIPE, stdout=subprocess.PIPE) @@ -115,7 +149,8 @@ def waitForMessage(cmakeCommand, expected): packet = ordered(waitForRawMessage(cmakeCommand)) if packet != data: - sys.exit(-1) + print ("Received unexpected message; test failed") + exitWithError(cmakeCommand) return packet def waitForReply(cmakeCommand, originalType, cookie, skipProgress): @@ -236,3 +271,43 @@ def validateCache(cmakeCommand, data): if (not hadHomeDir): print('No CMAKE_HOME_DIRECTORY found in cache.') sys.exit(1) + +def handleBasicMessage(proc, obj, debug): + if 'sendRaw' in obj: + data = obj['sendRaw'] + if debug: print("Sending raw:", data) + writeRawData(proc, data) + return True + elif 'send' in obj: + data = obj['send'] + if debug: print("Sending:", json.dumps(data)) + writePayload(proc, data) + return True + elif 'recv' in obj: + data = obj['recv'] + if debug: print("Waiting for:", json.dumps(data)) + waitForMessage(proc, data) + return True + elif 'message' in obj: + print("MESSAGE:", obj["message"]) + sys.stdout.flush() + return True + return False + +def shutdownProc(proc): + # Tell the server to exit. + proc.stdin.close() + proc.stdout.close() + + # Wait for the server to exit. + # If this version of python supports it, terminate the server after a timeout. + try: + proc.wait(timeout=5) + except TypeError: + proc.wait() + except: + proc.terminate() + raise + + print('cmake-server exited: %d' % proc.returncode) + sys.exit(proc.returncode) diff --git a/Tests/Server/server-test.py b/Tests/Server/server-test.py index 9380910..f3c3fc2 100644 --- a/Tests/Server/server-test.py +++ b/Tests/Server/server-test.py @@ -9,7 +9,7 @@ sourceDir = sys.argv[3] buildDir = sys.argv[4] + "/" + os.path.splitext(os.path.basename(testFile))[0] cmakeGenerator = sys.argv[5] -print("Test:", testFile, +print("Server Test:", testFile, "\n-- SourceDir:", sourceDir, "\n-- BuildDir:", buildDir, "\n-- Generator:", cmakeGenerator) @@ -17,24 +17,16 @@ print("Test:", testFile, if os.path.exists(buildDir): shutil.rmtree(buildDir) -proc = cmakelib.initProc(cmakeCommand) +cmakelib.filterBase = sourceDir + +proc = cmakelib.initServerProc(cmakeCommand) with open(testFile) as f: testData = json.loads(f.read()) for obj in testData: - if 'sendRaw' in obj: - data = obj['sendRaw'] - if debug: print("Sending raw:", data) - cmakelib.writeRawData(proc, data) - elif 'send' in obj: - data = obj['send'] - if debug: print("Sending:", json.dumps(data)) - cmakelib.writePayload(proc, data) - elif 'recv' in obj: - data = obj['recv'] - if debug: print("Waiting for:", json.dumps(data)) - cmakelib.waitForMessage(proc, data) + if cmakelib.handleBasicMessage(proc, obj, debug): + pass elif 'reply' in obj: data = obj['reply'] if debug: print("Waiting for reply:", json.dumps(data)) @@ -103,13 +95,11 @@ for obj in testData: print("MESSAGE:", obj["message"]) elif 'reconnect' in obj: cmakelib.exitProc(proc) - proc = cmakelib.initProc(cmakeCommand) + proc = cmakelib.initServerProc(cmakeCommand) else: print("Unknown command:", json.dumps(obj)) sys.exit(2) print("Completed") -cmakelib.exitProc(proc) -print('cmake-server exited: %d' % proc.returncode) -sys.exit(proc.returncode) +cmakelib.shutdownProc(proc) -- cgit v0.12