Newer
Older
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
my_socket_error = socket_error()
my_socket_error.errno = errno.ECONNREFUSED
mock_ctn_request.side_effect = my_socket_error
# force timeout
with mock.patch("course.page.code.DOCKER_TIMEOUT", 0.0001):
res = request_python_run_with_retries(
run_req={}, run_timeout=0.1, retry_count=0)
self.assertEqual(res["result"], "uncaught_error")
self.assertEqual(res['message'],
"Timeout waiting for container.")
self.assertEqual(res["exec_host"], fake_host_ip)
self.assertIn(type(my_socket_error).__name__, res["traceback"])
with self.subTest(
case="Docker ping socket error with erron EAFNOSUPPORT"):
my_socket_error = socket_error()
# This errno should raise error
my_socket_error.errno = errno.EAFNOSUPPORT
mock_ctn_request.side_effect = my_socket_error
# force timeout
with mock.patch("course.page.code.DOCKER_TIMEOUT", 0.0001):
with self.assertRaises(socket_error) as e:
request_python_run_with_retries(
run_req={}, run_timeout=0.1, retry_count=0)
self.assertEqual(e.exception.errno, my_socket_error.errno)
with self.assertRaises(socket_error) as e:
request_python_run_with_retries(
run_req={}, run_timeout=0.1, retry_count=0)
self.assertEqual(e.exception.errno, my_socket_error.errno)
# This should be the last subTest, because this will the behavior of
# change mock_remove_ctn
with self.subTest(
case="Docker ping timeout with InvalidPingResponse and "
"remove container failed with APIError"):
invalid_ping_resp_msg = "my custom invalid ping response exception"
fake_host_ip = "0.0.0.0"
mock_inpect_ctn.return_value = {
"NetworkSettings": {
"Ports": {"%d/tcp" % RUNPY_PORT: (
{"HostIp": fake_host_ip, "HostPort": fake_host_port},
)}
}}
mock_ctn_request.side_effect = (
InvalidPingResponse(invalid_ping_resp_msg))
mock_remove_ctn.reset_mock()
from django.http import HttpResponse
fake_response_content = "this should not appear"
mock_remove_ctn.side_effect = DockerAPIError(
message="my custom docker api error",
response=HttpResponse(content=fake_response_content))
# force timeout
with mock.patch("course.page.code.DOCKER_TIMEOUT", 0.0001):
res = request_python_run_with_retries(
run_req={}, run_timeout=0.1, retry_count=0)
self.assertEqual(res["result"], "uncaught_error")
self.assertEqual(res['message'],
"Timeout waiting for container.")
self.assertEqual(res["exec_host"], "localhost")
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
self.assertIn(InvalidPingResponse.__name__, res["traceback"])
self.assertIn(invalid_ping_resp_msg, res["traceback"])
# No need to bother the students with this nonsense.
self.assertNotIn(DockerAPIError.__name__, res["traceback"])
self.assertNotIn(fake_response_content, res["traceback"])
@skipIf(six.PY2, "PY2 doesn't support subTest")
def test_docker_container_ping_return_not_ok(self):
with (
mock.patch("docker.client.Client.create_container")) as mock_create_ctn, ( # noqa
mock.patch("docker.client.Client.start")) as mock_ctn_start, (
mock.patch("docker.client.Client.logs")) as mock_ctn_logs, (
mock.patch("docker.client.Client.remove_container")) as mock_remove_ctn, ( # noqa
mock.patch("docker.client.Client.inspect_container")) as mock_inpect_ctn, ( # noqa
mock.patch("six.moves.http_client.HTTPConnection.request")) as mock_ctn_request, ( # noqa
mock.patch("six.moves.http_client.HTTPConnection.getresponse")) as mock_ctn_get_response: # noqa
mock_create_ctn.return_value = {"Id": "someid"}
mock_ctn_start.side_effect = lambda x: None
mock_ctn_logs.side_effect = lambda x: None
mock_remove_ctn.return_value = None
fake_host_ip = "192.168.1.100"
fake_host_port = "69999"
mock_inpect_ctn.return_value = {
"NetworkSettings": {
"Ports": {"%d/tcp" % RUNPY_PORT: (
{"HostIp": fake_host_ip, "HostPort": fake_host_port},
)}
}}
# force timeout
with mock.patch("course.page.code.DOCKER_TIMEOUT", 0.0001):
with self.subTest(
case="Docker ping response not OK"):
mock_ctn_request.side_effect = lambda x, y: None
mock_ctn_get_response.return_value = six.BytesIO(b"NOT OK")
res = request_python_run_with_retries(
run_req={}, run_timeout=0.1, retry_count=0)
self.assertEqual(res["result"], "uncaught_error")
self.assertEqual(res['message'],
"Timeout waiting for container.")
self.assertEqual(res["exec_host"], fake_host_ip)
self.assertIn(InvalidPingResponse.__name__, res["traceback"])
@skipIf(six.PY2, "PY2 doesn't support subTest")
def test_docker_container_runpy_timeout(self):
with (
mock.patch("docker.client.Client.create_container")) as mock_create_ctn, ( # noqa
mock.patch("docker.client.Client.start")) as mock_ctn_start, (
mock.patch("docker.client.Client.logs")) as mock_ctn_logs, (
mock.patch("docker.client.Client.remove_container")) as mock_remove_ctn, ( # noqa
mock.patch("docker.client.Client.inspect_container")) as mock_inpect_ctn, ( # noqa
mock.patch("six.moves.http_client.HTTPConnection.request")) as mock_ctn_request, ( # noqa
mock.patch("six.moves.http_client.HTTPConnection.getresponse")) as mock_ctn_get_response: # noqa
mock_create_ctn.return_value = {"Id": "someid"}
mock_ctn_start.side_effect = lambda x: None
mock_ctn_logs.side_effect = lambda x: None
mock_remove_ctn.return_value = None
fake_host_ip = "192.168.1.100"
fake_host_port = "69999"
mock_inpect_ctn.return_value = {
"NetworkSettings": {
"Ports": {"%d/tcp" % RUNPY_PORT: (
{"HostIp": fake_host_ip, "HostPort": fake_host_port},
)}
}}
with self.subTest(
case="Docker ping passed by runpy timed out"):
# first request is ping, second request raise socket.timeout
mock_ctn_request.side_effect = [None, sock_timeout]
mock_ctn_get_response.return_value = six.BytesIO(b"OK")
res = request_python_run_with_retries(
run_req={}, run_timeout=0.1, retry_count=0)
self.assertEqual(res["result"], "timeout")
self.assertEqual(res["exec_host"], fake_host_ip)
@skipIf(six.PY2, "PY2 doesn't support subTest")
def test_docker_container_runpy_retries_count(self):
with (
mock.patch("course.page.code.request_python_run")) as mock_req_run, ( # noqa
mock.patch("course.page.code.is_nuisance_failure")) as mock_is_nuisance_failure: # noqa
expected_result = "this is my custom result"
mock_req_run.return_value = {"result": expected_result}
with self.subTest(actual_retry_count=4):
mock_is_nuisance_failure.side_effect = [True, True, True, False]
res = request_python_run_with_retries(
run_req={}, run_timeout=0.1, retry_count=5)
self.assertEqual(res["result"], expected_result)
self.assertEqual(mock_req_run.call_count, 4)
self.assertEqual(mock_is_nuisance_failure.call_count, 4)
mock_req_run.reset_mock()
mock_is_nuisance_failure.reset_mock()
with self.subTest(actual_retry_count=2):
mock_is_nuisance_failure.side_effect = [True, True, True, False]
res = request_python_run_with_retries(
run_req={}, run_timeout=0.1, retry_count=1)
self.assertEqual(res["result"], expected_result)
self.assertEqual(mock_req_run.call_count, 2)
self.assertEqual(mock_is_nuisance_failure.call_count, 1)
class IsNuisanceFailureTest(unittest.TestCase):
# Testing is_nuisance_failure
def test_not_uncaught_error(self):
result = {"result": "not_uncaught_error"}
self.assertFalse(is_nuisance_failure(result))
def test_no_traceback(self):
result = {"result": "uncaught_error"}
self.assertFalse(is_nuisance_failure(result))
def test_traceback_unkown(self):
result = {"result": "uncaught_error",
"traceback": "unknow traceback"}
self.assertFalse(is_nuisance_failure(result))
def test_traceback_has_badstatusline(self):
result = {"result": "uncaught_error",
"traceback": "BadStatusLine: \nfoo"}
self.assertTrue(is_nuisance_failure(result))
def test_traceback_address_already_in_use(self):
result = {"result": "uncaught_error",
"traceback": "\nbind: address already in use \nfoo"}
self.assertTrue(is_nuisance_failure(result))
def test_traceback_new_connection_error(self):
result = {"result": "uncaught_error",
"traceback":
"\nrequests.packages.urllib3.exceptions."
"NewConnectionError: \nfoo"}
self.assertTrue(is_nuisance_failure(result))
def test_traceback_remote_disconnected(self):
result = {"result": "uncaught_error",
"traceback":
"\nhttp.client.RemoteDisconnected: \nfoo"}
self.assertTrue(is_nuisance_failure(result))
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
class CodeQuestionWithHumanTextFeedbackSpecialCase(
SingleCoursePageTestMixin, SubprocessRunpyContainerMixin, TestCase):
"""
https://github.com/inducer/relate/issues/269
https://github.com/inducer/relate/commit/2af0ad7aa053b735620b2cf0bae0b45822bfb87f # noqa
"""
flow_id = QUIZ_FLOW_ID
@classmethod
def setUpTestData(cls): # noqa
super(CodeQuestionWithHumanTextFeedbackSpecialCase, cls).setUpTestData()
cls.c.force_login(cls.student_participation.user)
cls.start_flow(cls.flow_id)
def setUp(self): # noqa
super(CodeQuestionWithHumanTextFeedbackSpecialCase, self).setUp()
self.c.force_login(self.student_participation.user)
self.rf = RequestFactory()
def get_grade_feedback(self, answer_data, page_value,
human_feedback_percentage, grade_data):
page_id = "py_simple_list"
course_identifier = self.course.identifier
flow_session_id = self.get_default_flow_session_id(course_identifier)
flow_session = FlowSession.objects.get(id=flow_session_id)
page_ordinal = self.get_page_ordinal_via_page_id(
page_id, course_identifier, flow_session_id)
post_data = answer_data.copy()
post_data.update({"submit": ""})
request = self.rf.post(
self.get_page_url_by_ordinal(
page_ordinal, course_identifier, flow_session_id),
post_data)
request.user = self.student_participation.user
pctx = CoursePageContext(request, course_identifier)
fpctx = FlowPageContext(
pctx.repo, pctx.course, self.flow_id, page_ordinal,
self.student_participation, flow_session, request)
page_desc = fpctx.page_desc
page_desc.value = page_value
page_desc.human_feedback_percentage = human_feedback_percentage
page = PythonCodeQuestionWithHumanTextFeedback(None, None, page_desc)
page_context = fpctx.page_context
grade_data.setdefault('grade_percent', None)
grade_data.setdefault('released', True)
grade_data.setdefault('feedback_text', "")
page_data = fpctx.page_data
feedback = page.grade(
page_context=page_context,
answer_data=answer_data,
page_data=page_data,
grade_data=grade_data)
return feedback
def test_code_with_human_feedback(self):
answer_data = {"answer": 'b = [a + 0] * 50'}
grade_data = {"grade_percent": 100}
page_value = 4
human_feedback_percentage = 60
feedback = self.get_grade_feedback(
answer_data, page_value, human_feedback_percentage, grade_data)
self.assertIn("The overall grade is 100%.", feedback.feedback)
self.assertIn(
"The autograder assigned 1.60/1.60 points.", feedback.feedback)
self.assertIn(
"The human grader assigned 2.40/2.40 points.", feedback.feedback)
def test_code_with_human_feedback_full_percentage(self):
answer_data = {"answer": 'b = [a + 0] * 50'}
grade_data = {"grade_percent": 100}
page_value = 0
human_feedback_percentage = 100
from course.page.base import AnswerFeedback
with mock.patch(
"course.page.code.PythonCodeQuestion.grade") as mock_py_grade:
# In this way, code_feedback.correctness is None
mock_py_grade.return_value = AnswerFeedback(correctness=None)
feedback = self.get_grade_feedback(
answer_data, page_value, human_feedback_percentage, grade_data)
self.assertIn("The overall grade is 100%.", feedback.feedback)
self.assertIn(
"No information on correctness of answer.", feedback.feedback)
self.assertIn(
"The human grader assigned 0/0 points.", feedback.feedback)
def test_code_with_human_feedback_zero_percentage(self):
answer_data = {"answer": 'b = [a + 0] * 50'}
grade_data = {}
page_value = 0
human_feedback_percentage = 0
feedback = self.get_grade_feedback(
answer_data, page_value, human_feedback_percentage, grade_data)
self.assertIn("The overall grade is 100%.", feedback.feedback)
self.assertIn(
"Your answer is correct.", feedback.feedback)
self.assertIn(
"The autograder assigned 0/0 points.", feedback.feedback)