server: Updated server tests to try various communication channels

This commit is contained in:
Justin Berger 2017-09-03 19:56:01 -06:00
parent 08dca58358
commit d46b4ba835
2 changed files with 142 additions and 105 deletions

View File

@ -1,5 +1,5 @@
from __future__ import print_function
import sys, subprocess, json, os, select
import sys, subprocess, json, os, select, shutil, time, socket
termwidth = 150
@ -40,25 +40,35 @@ def col_print(title, array):
filterPacket = lambda x: x
STDIN = 0
PIPE = 1
communicationMethods = [STDIN]
if hasattr(socket, 'AF_UNIX'):
communicationMethods.append(PIPE)
def defaultExitWithError(proc):
data = ""
try:
while select.select([proc.stdout], [], [], 3.)[0]:
data = data + proc.stdout.read(1)
while select.select([proc.outPipe], [], [], 3.)[0]:
data = data + proc.outPipe.read(1)
if len(data):
print("Rest of raw buffer from server:")
printServer(data)
except:
pass
proc.stdout.close()
proc.stdin.close()
proc.outPipe.close()
proc.inPipe.close()
proc.kill()
sys.exit(1)
exitWithError = lambda proc: defaultExitWithError(proc)
serverTag = "SERVER"
def printServer(*args):
print("SERVER>", *args)
print(serverTag + ">", *args)
print()
sys.stdout.flush()
@ -71,7 +81,7 @@ def waitForRawMessage(cmakeCommand):
stdoutdata = ""
payload = ""
while not cmakeCommand.poll():
stdoutdataLine = cmakeCommand.stdout.readline()
stdoutdataLine = cmakeCommand.outPipe.readline()
if stdoutdataLine:
stdoutdata += stdoutdataLine.decode('utf-8')
else:
@ -106,26 +116,51 @@ def writeRawData(cmakeCommand, content):
if print_communication:
printClient(content, "(Use \\r\\n:", rn, ")")
cmakeCommand.stdin.write(payload.encode('utf-8'))
cmakeCommand.stdin.flush()
cmakeCommand.write(payload.encode('utf-8'))
writeRawData.counter = 0
def writePayload(cmakeCommand, obj):
writeRawData(cmakeCommand, json.dumps(obj))
def initServerProc(cmakeCommand):
cmakeCommand = subprocess.Popen([cmakeCommand, "-E", "server", "--experimental", "--debug"],
stdin=subprocess.PIPE,
stdout=subprocess.PIPE)
def getPipeName():
return "/tmp/server-test-socket"
def attachPipe(cmakeCommand, pipeName):
time.sleep(1)
sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
sock.connect(pipeName)
global serverTag
serverTag = "SERVER(PIPE)"
cmakeCommand.outPipe = sock.makefile()
cmakeCommand.inPipe = sock
cmakeCommand.write = cmakeCommand.inPipe.sendall
def writeAndFlush(pipe, val):
pipe.write(val)
pipe.flush()
def initServerProc(cmakeCommand, comm):
if comm == PIPE:
pipeName = getPipeName()
cmakeCommand = subprocess.Popen([cmakeCommand, "-E", "server", "--experimental", "--pipe=" + pipeName])
attachPipe(cmakeCommand, pipeName)
else:
cmakeCommand = subprocess.Popen([cmakeCommand, "-E", "server", "--experimental", "--debug"],
stdin=subprocess.PIPE,
stdout=subprocess.PIPE)
cmakeCommand.outPipe = cmakeCommand.stdout
cmakeCommand.inPipe = cmakeCommand.stdin
cmakeCommand.write = lambda val: writeAndFlush(cmakeCommand.inPipe, val)
packet = waitForRawMessage(cmakeCommand)
if packet == None:
print("Not in server mode")
sys.exit(1)
sys.exit(2)
if packet['type'] != 'hello':
print("No hello message")
sys.exit(1)
sys.exit(3)
return cmakeCommand
@ -159,25 +194,27 @@ def waitForReply(cmakeCommand, originalType, cookie, skipProgress):
packet = waitForRawMessage(cmakeCommand)
t = packet['type']
if packet['cookie'] != cookie or packet['inReplyTo'] != originalType:
sys.exit(1)
print("cookie or inReplyTo mismatch")
sys.exit(4)
if t == 'message' or t == 'progress':
if skipProgress:
continue
if t == 'reply':
break
sys.exit(1)
print("Unrecognized message", packet)
sys.exit(5)
return packet
def waitForError(cmakeCommand, originalType, cookie, message):
packet = waitForRawMessage(cmakeCommand)
if packet['cookie'] != cookie or packet['type'] != 'error' or packet['inReplyTo'] != originalType or packet['errorMessage'] != message:
sys.exit(1)
sys.exit(6)
def waitForProgress(cmakeCommand, originalType, cookie, current, message):
packet = waitForRawMessage(cmakeCommand)
if packet['cookie'] != cookie or packet['type'] != 'progress' or packet['inReplyTo'] != originalType or packet['progressCurrent'] != current or packet['progressMessage'] != message:
sys.exit(1)
sys.exit(7)
def handshake(cmakeCommand, major, minor, source, build, generator, extraGenerator):
version = { 'major': major }
@ -202,9 +239,9 @@ def validateGlobalSettings(cmakeCommand, cmakeCommandPath, data):
versionString = version['string']
vs = str(version['major']) + '.' + str(version['minor']) + '.' + str(version['patch'])
if (versionString != vs and not versionString.startswith(vs + '-')):
sys.exit(1)
sys.exit(8)
if (versionString != cmakeVersion):
sys.exit(1)
sys.exit(9)
# validate generators:
generatorObjects = capabilities['generators']
@ -237,16 +274,16 @@ def validateGlobalSettings(cmakeCommand, cmakeCommandPath, data):
for gen in cmakeGenerators:
if (not gen in generators):
sys.exit(1)
sys.exit(10)
gen = packet['generator']
if (gen != '' and not (gen in generators)):
sys.exit(1)
sys.exit(11)
for i in data:
print("Validating", i)
if (packet[i] != data[i]):
sys.exit(1)
sys.exit(12)
def validateCache(cmakeCommand, data):
packet = waitForReply(cmakeCommand, 'cache', '', False)
@ -296,8 +333,8 @@ def handleBasicMessage(proc, obj, debug):
def shutdownProc(proc):
# Tell the server to exit.
proc.stdin.close()
proc.stdout.close()
proc.inPipe.close()
proc.outPipe.close()
# Wait for the server to exit.
# If this version of python supports it, terminate the server after a timeout.

View File

@ -19,87 +19,87 @@ if os.path.exists(buildDir):
cmakelib.filterBase = sourceDir
proc = cmakelib.initServerProc(cmakeCommand)
with open(testFile) as f:
testData = json.loads(f.read())
for obj in testData:
if cmakelib.handleBasicMessage(proc, obj, debug):
pass
elif 'reply' in obj:
data = obj['reply']
if debug: print("Waiting for reply:", json.dumps(data))
originalType = ""
cookie = ""
skipProgress = False;
if 'cookie' in data: cookie = data['cookie']
if 'type' in data: originalType = data['type']
if 'skipProgress' in data: skipProgress = data['skipProgress']
cmakelib.waitForReply(proc, originalType, cookie, skipProgress)
elif 'error' in obj:
data = obj['error']
if debug: print("Waiting for error:", json.dumps(data))
originalType = ""
cookie = ""
message = ""
if 'cookie' in data: cookie = data['cookie']
if 'type' in data: originalType = data['type']
if 'message' in data: message = data['message']
cmakelib.waitForError(proc, originalType, cookie, message)
elif 'progress' in obj:
data = obj['progress']
if debug: print("Waiting for progress:", json.dumps(data))
originalType = ''
cookie = ""
current = 0
message = ""
if 'cookie' in data: cookie = data['cookie']
if 'type' in data: originalType = data['type']
if 'current' in data: current = data['current']
if 'message' in data: message = data['message']
cmakelib.waitForProgress(proc, originalType, cookie, current, message)
elif 'handshake' in obj:
data = obj['handshake']
if debug: print("Doing handshake:", json.dumps(data))
major = -1
minor = -1
generator = cmakeGenerator
extraGenerator = ''
sourceDirectory = sourceDir
buildDirectory = buildDir
if 'major' in data: major = data['major']
if 'minor' in data: minor = data['minor']
if 'buildDirectory' in data: buildDirectory = data['buildDirectory']
if 'sourceDirectory' in data: sourceDirectory = data['sourceDirectory']
if 'generator' in data: generator = data['generator']
if 'extraGenerator' in data: extraGenerator = data['extraGenerator']
if not os.path.isabs(buildDirectory):
buildDirectory = buildDir + "/" + buildDirectory
if sourceDirectory != '' and not os.path.isabs(sourceDirectory):
sourceDirectory = sourceDir + "/" + sourceDirectory
cmakelib.handshake(proc, major, minor, sourceDirectory, buildDirectory,
generator, extraGenerator)
elif 'validateGlobalSettings' in obj:
data = obj['validateGlobalSettings']
if not 'buildDirectory' in data: data['buildDirectory'] = buildDir
if not 'sourceDirectory' in data: data['sourceDirectory'] = sourceDir
if not 'generator' in data: data['generator'] = cmakeGenerator
if not 'extraGenerator' in data: data['extraGenerator'] = ''
cmakelib.validateGlobalSettings(proc, cmakeCommand, data)
elif 'validateCache' in obj:
data = obj['validateCache']
if not 'isEmpty' in data: data['isEmpty'] = false
cmakelib.validateCache(proc, data)
elif 'message' in obj:
print("MESSAGE:", obj["message"])
elif 'reconnect' in obj:
cmakelib.exitProc(proc)
proc = cmakelib.initServerProc(cmakeCommand)
else:
print("Unknown command:", json.dumps(obj))
sys.exit(2)
for communicationMethod in cmakelib.communicationMethods:
proc = cmakelib.initServerProc(cmakeCommand, communicationMethod)
if proc is None:
continue
for obj in testData:
if cmakelib.handleBasicMessage(proc, obj, debug):
pass
elif 'reply' in obj:
data = obj['reply']
if debug: print("Waiting for reply:", json.dumps(data))
originalType = ""
cookie = ""
skipProgress = False;
if 'cookie' in data: cookie = data['cookie']
if 'type' in data: originalType = data['type']
if 'skipProgress' in data: skipProgress = data['skipProgress']
cmakelib.waitForReply(proc, originalType, cookie, skipProgress)
elif 'error' in obj:
data = obj['error']
if debug: print("Waiting for error:", json.dumps(data))
originalType = ""
cookie = ""
message = ""
if 'cookie' in data: cookie = data['cookie']
if 'type' in data: originalType = data['type']
if 'message' in data: message = data['message']
cmakelib.waitForError(proc, originalType, cookie, message)
elif 'progress' in obj:
data = obj['progress']
if debug: print("Waiting for progress:", json.dumps(data))
originalType = ''
cookie = ""
current = 0
message = ""
if 'cookie' in data: cookie = data['cookie']
if 'type' in data: originalType = data['type']
if 'current' in data: current = data['current']
if 'message' in data: message = data['message']
cmakelib.waitForProgress(proc, originalType, cookie, current, message)
elif 'handshake' in obj:
data = obj['handshake']
if debug: print("Doing handshake:", json.dumps(data))
major = -1
minor = -1
generator = cmakeGenerator
extraGenerator = ''
sourceDirectory = sourceDir
buildDirectory = buildDir
if 'major' in data: major = data['major']
if 'minor' in data: minor = data['minor']
if 'buildDirectory' in data: buildDirectory = data['buildDirectory']
if 'sourceDirectory' in data: sourceDirectory = data['sourceDirectory']
if 'generator' in data: generator = data['generator']
if 'extraGenerator' in data: extraGenerator = data['extraGenerator']
if not os.path.isabs(buildDirectory):
buildDirectory = buildDir + "/" + buildDirectory
if sourceDirectory != '' and not os.path.isabs(sourceDirectory):
sourceDirectory = sourceDir + "/" + sourceDirectory
cmakelib.handshake(proc, major, minor, sourceDirectory, buildDirectory,
generator, extraGenerator)
elif 'validateGlobalSettings' in obj:
data = obj['validateGlobalSettings']
if not 'buildDirectory' in data: data['buildDirectory'] = buildDir
if not 'sourceDirectory' in data: data['sourceDirectory'] = sourceDir
if not 'generator' in data: data['generator'] = cmakeGenerator
if not 'extraGenerator' in data: data['extraGenerator'] = ''
cmakelib.validateGlobalSettings(proc, cmakeCommand, data)
elif 'validateCache' in obj:
data = obj['validateCache']
if not 'isEmpty' in data: data['isEmpty'] = false
cmakelib.validateCache(proc, data)
elif 'reconnect' in obj:
cmakelib.exitProc(proc)
proc = cmakelib.initServerProc(cmakeCommand, communicationMethod)
else:
print("Unknown command:", json.dumps(obj))
sys.exit(2)
cmakelib.shutdownProc(proc)
print("Completed")
cmakelib.shutdownProc(proc)