From b5b35faea7f4205f353f57178ddc795b7dce5043 Mon Sep 17 00:00:00 2001 From: Timotej Lazar Date: Sun, 7 Oct 2018 19:02:26 +0200 Subject: Refactor SSH testing code out of individual tasks Catch SSH errors and report failures as hints. Also some cleanups and 2to3 fixes. --- .../tasks/edit_find_grep_compile_convert/task.py | 57 +++++++--------------- 1 file changed, 18 insertions(+), 39 deletions(-) (limited to 'kpov_judge/tasks/edit_find_grep_compile_convert') diff --git a/kpov_judge/tasks/edit_find_grep_compile_convert/task.py b/kpov_judge/tasks/edit_find_grep_compile_convert/task.py index a2f1ed0..23825c5 100644 --- a/kpov_judge/tasks/edit_find_grep_compile_convert/task.py +++ b/kpov_judge/tasks/edit_find_grep_compile_convert/task.py @@ -191,35 +191,24 @@ params_meta = { } def task(testip, testuser, passwd, magicstr, dstfile, progname, pubseed): - from pexpect import pxssh import random - N_TITA = 40 - target = pxssh.pxssh() - target.login(testip, testuser, passwd) - target.sendline('ls -l ~/{}'.format(dstfile)) - target.prompt() - dst_ls = target.before - target.sendline('cat ~/{}'.format(dstfile)) - target.prompt() - dst_file_contents = target.before - target.sendline('ls ~/'.format(dstfile)) - target.prompt() - home_ls = target.before - results = { - 'dst_ls': dst_ls, - 'dst_file_contents': dst_file_contents, - 'home_ls': home_ls, - } - tita_return = "" + r = random.Random(pubseed) + tests = [ + ('dst_ls', 'ls -l ~/{}'.format(dstfile)), + ('dst_file_contents', 'cat ~/{}'.format(dstfile)), + ('home_ls', 'ls ~/'.format(dstfile)), + ] + + N_TITA = 40 for i in range(N_TITA): b = r.randint(0, 7) x = oct(r.randint(37, 127)).replace('o', '') - target.sendline('echo -e "\\{}" | ~/{} {}'.format(x, progname, b)) - target.prompt() - tita_return += target.before - results['tita_return'] = tita_return - target.logout() + tests += [('tita-{:02}'.format(i), 'echo -e "\\{}" | ~/{} {}'.format(x, progname, b))] + + results = kpov_util.ssh_test(testip, testuser, passwd, tests) + results['tita_return'] = ''.join(val for key, val in results.items() if key.startswith('tita-')) + return results def gen_params(user_id, params_meta): @@ -258,11 +247,6 @@ def task_check(results, params): x = start + mid + end hidden_contents += x + "\r\n" expected_contents = hidden_contents - #expected_contents = re.sub(r"^po.*lz\r$", - # r"pokakalz\r", - # hidden_contents, - # re.MULTILINE) - expected_contents = "cat ~/{}\r\n".format(params['dstfile']) + expected_contents if results["dst_file_contents"] == expected_contents: score += 3 else: @@ -275,7 +259,7 @@ def task_check(results, params): break hints += ["wrong file contents\n" + str(diff_pos[1])] #hints += ["wrong file contents"] - expected_ls = "ls -l ~/{dstfile}\r\n-rw--w---- 1 {testuser} bilbo .*{dstfile}.*\r\n".format(**params) + expected_ls = "-rw--w---- 1 {testuser} bilbo .*{dstfile}.*\r\n".format(**params) if re.match(expected_ls, results["dst_ls"]): score += 3 else: @@ -290,7 +274,6 @@ def task_check(results, params): b = r.randint(0, 7) x_i = r.randint(37, 127) x = oct(x_i).replace('o', '') - expected_tita += 'echo -e "\\{}" | ~/{} {}\r\n'.format(x, params['progname'], b) if x_i & (1 << b): expected_tita += "ta\r\n" else: @@ -298,7 +281,6 @@ def task_check(results, params): if results["tita_return"] == expected_tita: score += 2 else: - #hints += [u"program output incorrect\n" + results["tita_return"] + expected_tita] hints += ["program output incorrect\n" + results["tita_return"]] return score, hints @@ -306,10 +288,7 @@ def task_check(results, params): def prepare_disks(templates, task_params, global_params): import random import os - #print "Haha!" - #print params - #print templates - d = templates['student-entrance2'] + # first create the file contents to make it easyer to check. hidden_contents = task_params['magicstr'] r = random.Random(task_params['rndseed']) @@ -318,12 +297,15 @@ def prepare_disks(templates, task_params, global_params): x += "".join([r.choice("uiasdfghjkyxcvbnm1234567890ASDFGHJKYZXCVBNM") for i in range(60)]) x += r.choice(["lz", "1z", "Iz", "iz", "l2", "I2", "12"]) hidden_contents += x + "\n" + # create hidden file dir_list = ['Qlipper', 'Thunar', 'blender', 'autostart', 'kazam', 'mc', 'netsurf', 'pulse', 'qupzilla', 'radare2', 'teamviewer', 'texstudio', 'vlc'] ending_list = ['rc', '.conf', ''] start_list = ['net', 'dev', 'doc', 'lib', 'time', 'conf'] r.shuffle(dir_list) file_letters = ["mod", "co"] + + d = templates['student-entrance2'] for potential_dir in dir_list: try: potential_dir = os.path.join('/home/bilbo/.config', potential_dir) @@ -344,6 +326,3 @@ def prepare_disks(templates, task_params, global_params): # TODO create some additional files write_default_config(templates['simpleArbiterDhcpGW'], global_params) - # finish here - # rename - -- cgit v1.2.1