tester in docker works
This commit is contained in:
@@ -1,17 +0,0 @@
|
||||
import asyncio
|
||||
import sys
|
||||
|
||||
|
||||
class BaseDaemon:
|
||||
def command(self):
|
||||
raise NotImplementedError()
|
||||
|
||||
async def execute(self):
|
||||
cmd = self.command()
|
||||
proc = await asyncio.create_subprocess_shell(cmd, stdin=sys.stdin, stdout=sys.stdout, stderr=sys.stderr)
|
||||
stdout, stderr = await proc.communicate()
|
||||
print(f"[{cmd!r} exited with {proc.returncode}]")
|
||||
if stdout:
|
||||
print(f"[stdout]\n{stdout.decode()}")
|
||||
if stderr:
|
||||
print(f"[stderr]\n{stderr.decode()}")
|
12
SprintLib/queue.py
Normal file
12
SprintLib/queue.py
Normal file
@@ -0,0 +1,12 @@
|
||||
import pika
|
||||
|
||||
from Sprint import settings
|
||||
|
||||
|
||||
def send_testing(solution_id):
|
||||
with pika.BlockingConnection(
|
||||
pika.ConnectionParameters(host=settings.RABBIT_HOST, port=settings.RABBIT_PORT)
|
||||
) as connection:
|
||||
channel = connection.channel()
|
||||
channel.queue_declare(queue="test")
|
||||
channel.basic_publish(exchange="", routing_key="test", body=bytes(str(solution_id), encoding='utf-8'))
|
@@ -1,5 +1,5 @@
|
||||
from os import listdir
|
||||
from os.path import join
|
||||
from os import listdir, mkdir
|
||||
from os.path import join, exists
|
||||
from shutil import copyfile, rmtree
|
||||
from subprocess import call, TimeoutExpired
|
||||
|
||||
@@ -18,10 +18,17 @@ class BaseTester:
|
||||
working_directory = "app"
|
||||
|
||||
def before_test(self):
|
||||
files = [file for file in listdir(self.solution.testing_directory) if file.endswith('.' + self.solution.language.file_type)]
|
||||
code = self.solution.exec_command(f'{self.build_command} {" ".join(files)}', working_directory=self.working_directory)
|
||||
files = [
|
||||
file
|
||||
for file in listdir(self.solution.testing_directory)
|
||||
if file.endswith("." + self.solution.language.file_type)
|
||||
]
|
||||
code = self.solution.exec_command(
|
||||
f'{self.build_command} {" ".join(files)}',
|
||||
working_directory=self.working_directory,
|
||||
)
|
||||
if code != 0:
|
||||
raise TestException('CE')
|
||||
raise TestException("CE")
|
||||
|
||||
def test(self, filename):
|
||||
code = self.solution.exec_command(
|
||||
@@ -30,9 +37,7 @@ class BaseTester:
|
||||
)
|
||||
if code != 0:
|
||||
raise TestException("RE")
|
||||
result = open(
|
||||
join(self.solution.testing_directory, "output.txt"), "r"
|
||||
).read()
|
||||
result = open(join(self.solution.testing_directory, "output.txt"), "r").read()
|
||||
if result.strip() != self.predicted.strip():
|
||||
raise TestException("WA")
|
||||
|
||||
@@ -51,17 +56,23 @@ class BaseTester:
|
||||
self.solution = solution
|
||||
|
||||
def execute(self):
|
||||
copy_content(self.solution.directory, self.solution.testing_directory, ('test_dir',))
|
||||
if not exists(self.solution.testing_directory):
|
||||
mkdir(self.solution.testing_directory)
|
||||
copy_content(
|
||||
self.solution.directory, self.solution.testing_directory, ("test_dir",)
|
||||
)
|
||||
self.solution.result = CONSTS["testing_status"]
|
||||
self.solution.save()
|
||||
call(
|
||||
f"docker run --name solution_{self.solution.id} --volume={self.solution.testing_directory}:/{self.working_directory} -t -d {self.solution.language.image}",
|
||||
shell=True,
|
||||
)
|
||||
docker_command = f"docker run --name solution_{self.solution.id} --volume={self.solution.volume_directory}:/{self.working_directory} -t -d {self.solution.language.image}"
|
||||
print(docker_command)
|
||||
call(docker_command, shell=True)
|
||||
print("Container created")
|
||||
for file in ExtraFile.objects.filter(task=self.solution.task):
|
||||
copyfile(file.path, join(self.solution.testing_directory, file.filename))
|
||||
print("Files copied")
|
||||
try:
|
||||
self.before_test()
|
||||
print("before test finished")
|
||||
for test in self.solution.task.tests:
|
||||
if not test.filename.endswith(".a"):
|
||||
self.predicted = ExtraFile.objects.get(
|
||||
@@ -70,7 +81,9 @@ class BaseTester:
|
||||
self.test(test.filename)
|
||||
self.after_test()
|
||||
self.solution.result = CONSTS["ok_status"]
|
||||
progress = Progress.objects.get(user=self.solution.user, task=self.solution.task)
|
||||
progress = Progress.objects.get(
|
||||
user=self.solution.user, task=self.solution.task
|
||||
)
|
||||
if progress.finished_time is None:
|
||||
progress.finished_time = self.solution.time_sent
|
||||
progress.finished = True
|
||||
@@ -88,9 +101,11 @@ class BaseTester:
|
||||
rmtree(self.solution.testing_directory)
|
||||
self.solution.user.userinfo.refresh_from_db()
|
||||
if self.solution.user.userinfo.notification_solution_result:
|
||||
bot.send_message(self.solution.user.userinfo.telegram_chat_id,
|
||||
f'Задача: {self.solution.task.name}\n'
|
||||
f'Результат: {self.solution.result}\n'
|
||||
f'Очки решения: {Progress.by_solution(self.solution).score}\n'
|
||||
f'Текущий рейтинг: {self.solution.user.userinfo.rating}',
|
||||
parse_mode='html')
|
||||
bot.send_message(
|
||||
self.solution.user.userinfo.telegram_chat_id,
|
||||
f"Задача: {self.solution.task.name}\n"
|
||||
f"Результат: {self.solution.result}\n"
|
||||
f"Очки решения: {Progress.by_solution(self.solution).score}\n"
|
||||
f"Текущий рейтинг: {self.solution.user.userinfo.rating}",
|
||||
parse_mode="html",
|
||||
)
|
||||
|
@@ -16,4 +16,4 @@ class Python3Tester(BaseTester):
|
||||
|
||||
@property
|
||||
def command(self):
|
||||
return f"python {self.file}"
|
||||
return f"python3 {self.file}"
|
||||
|
Reference in New Issue
Block a user