def test_basic(): dcli = Docker_Client() # print(os.getcwd()) thread_list = list() for i in range(1): print("Spawn container: %d"%i) thread_list.append(dcli.spawn_process(name='myapp', num=i, params='%d 5000'%i, path=os.getcwd()+'/tests/SC', lang='PythonContainer', testcases = {'in':'in', 'out':'out'})) for i, t in enumerate(thread_list): assert('Hello container: %d\n'%i == t.result_queue.get().decode('utf-8'))
def test_server_simulation(): dcli = Docker_Client() thread_list = list() for turn in range(5): for i in range(NO_OF_CONTAINERS): container_num = ((NO_OF_CONTAINERS * turn) + i) print("Spawn container: %d" % container_num) thread_list.append( dcli.spawn_process(name='usn-%d' % container_num, num=container_num, params='%d 5000' % container_num, path=os.getcwd() + '/tests/SC', lang='CppContainer', testcases={ 'in': 'in', 'out': 'out' })) # thread_list.append(dcli.spawn_process(name='prototype%d'%container_num, num=container_num, sleep=2000, path=os.getcwd()+'/tests/SC')) time.sleep(5) for i, t in enumerate(thread_list): assert ('Hello container: %d' % i == t.result_queue.get().decode('utf-8'))
def uploadCode(inputJson): # Create a filename: # inpFileName = # USN_of_user + _ + questionHash + "." + extension_of_user_code # Eg: USN:usn-11, questionHash = 1, progLang = cpp gives : # inpFileName = usn-11_1.cpp # file_name = inputJson['USN']+"_"+inputJson['questionHash'] # inpFileName = os.path.join( # app.config["INPUT_FOLDER"], # file_name+"."+META_DATA[inputJson["progLang"]]["extension"] # ) # pdb.set_trace() file_name = inputJson["file_name"] codeFilePath = inputJson["codeFilePath"] # Open the file in 'w' mode to either overwrite previous # submission or create a new file for new submission. with open(codeFilePath, "w") as inputFp: inputFp.write( inputJson['code']) # Write incoming 'code' string into the file. inputFp.flush() os.fsync(inputFp) inputFp.close() # ------------------------------------------------------ # Add lines of code here to communicate with docker code # and then read the OP file. Not syncing here will # cause major discrepancies. # ------------------------------------------------------ # [TODO]: Ganesh # 1. Send lang to dcli # 2. For each testcase, run in loop # 3. Process output per testcase to give boolean pass or fail # [TODO]: RAHUL # Query to get TC input and output file name in two list variables(one question multiple TC). # path must be relative to INPUT_FOLDER. # For example, if INPUT_FOLDER is '/server/flaskr/codes/Input' and TC is like: # input: /server/flaskr/codes/Input/TC_IN/q1_1.in, q1_2.in and output: /server/flaskr/codes/Input/TC_OUT/q1_1.out, q1_2.out # Variables must be: tc_in = [TC_IN/q1_1.in, TC_IN/q1_2.in] and tc_out = [/TC_OUT/q1_1.out, /TC_OUT/q1_2.out] random.seed(inputJson['USN']) dcli = Docker_Client() thread_list = list() codeOutput = list() for t in inputJson['testcases']: thread_list.append( dcli.spawn_process( name='Input/' + file_name, num=random.randint(1, 99999999), params='%s 5000' % inputJson['USN'], path=app.config["CODE_FOLDER"], lang=META_DATA[inputJson["progLang"]]["container"], testcases=t, cpu=inputJson['cpu'], mem_limit=inputJson['mem_limit'])) # Create a filename: # opFileName = # "op" + _ + USN_of_user + _ + questionHash # opFileName = os.path.join( # app.config["OUTPUT_FOLDER"], # "op"+"_"+inputJson['USN']+"_"+inputJson['questionHash'] # ) compilePass = True for i, (thread, t) in enumerate(zip(thread_list, inputJson['testcases'])): outputFilePath = inputJson["outputFilePath"] + "_" + str(i) testcase_name = t['out'].split('/')[-1] with open(outputFilePath, 'w') as f: output = thread.result_queue.get().decode('utf-8') if (file_name in output): compilePass = False f.write(output) #codeOutput should be a dictionary. output_file = os.path.join(app.config['EXPECTED_OUTPUTS_FOLDER'], testcase_name) with open(output_file) as f: expected_out = f.read() print(output, expected_out) codeOutput.append({testcase_name: output == expected_out}) codeMessage = "" if compilePass else output codeOutput.append({'compilePass': compilePass}) codeOutput.append({'codeMessage': codeMessage}) return codeOutput
from titanium_silver.docker_client import Docker_Client dcli = Docker_Client() import os dcli.spawn_process(name='usn-%d' % 1, num=1, params='%d 5000' % 1, path=os.getcwd() + '/tests/SC', lang='CContainer')