Beispiel #1
0
 def setUp(self):
     self.client = Client()
     new_user = get_user_model().objects.create_superuser(**SUPER_USER)
     new_user.save()
     cluster = Cluster(**CLUSTER)
     cluster.save()
     self.cluster = cluster
     credential = Credential(
         user=new_user,
         cluster=cluster,
         **CREDENTIAL)
     credential.save()
     self.credential = credential
Beispiel #2
0
class UtilsServerTestCase(TestCase):

    def setUp(self):
        self.user = get_user_model().objects.create_user(**USER)
        test_path = os.path.join(settings.MEDIA_ROOT, "tests")
        with open(os.path.join(test_path, "id_rsa.pub"), 'r') as f:
            self.user.public_key = f.read()
        with open(os.path.join(test_path, "id_rsa"), 'r') as f:
            self.user.private_key = f.read()
        self.user.save()

        super_user = get_user_model().objects.create_superuser(**SUPER_USER)
        super_user.save()


        self.cluster = Cluster(**CLUSTER)
        self.cluster.save()
        self.credential = Credential(
            user=self.user, cluster=self.cluster, **CREDENTIAL)
        self.credential.save()
        self.credential2 = Credential(
            user=super_user, cluster=self.cluster, **CREDENTIAL)
        self.credential2.save()

    def test_run_standard_jobs_staff_error(self):
        results = utils.run_standard_jobs(self.credential, [''], {}, {})
        self.assertEqual(results["error"], SUBMIT_ERROR)

    def test_run_standard_jobs_invalid_credential(self):
        results = utils.run_standard_jobs(None, [''], {}, {})
        self.assertEqual(results["error"], CRED_ERROR)

    def test_run_standard_jobs(self):
        job = 'sleep 10'
        names = "TON,CON"
        results = utils.run_standard_jobs(
            self.credential2, names, {}, {'jobstring': job})
        self.assertEqual(results["error"], None)
        self.assertEqual(results["failed"], [])

    def test_run_standard_jobs_name_error(self):
        job = 'sleep 10'
        names = "E-N,C-N"
        results = utils.run_standard_jobs(
            self.credential2, names, {}, {'jobstring': job})
        for name, error in results['failed']:
            self.assertEqual(error, "Bad Substituent Name(s): ['N']")
Beispiel #3
0
    def setUp(self):
        self.client = Client()
        for user in self.users:
            new_user = get_user_model().objects.create_user(user["username"],
                                                user["email"],
                                                user["new_password1"])
            new_user.save()

        user = get_user_model().objects.all()[0]
        profile = user
        test_path = os.path.join(settings.MEDIA_ROOT, "tests")
        with open(os.path.join(test_path, "id_rsa.pub"), 'r') as f:
            profile.public_key = f.read()
        with open(os.path.join(test_path, "id_rsa"), 'r') as f:
            profile.private_key = f.read()
        profile.save()

        cluster = Cluster(
            name=self.cluster["name"],
            hostname=self.cluster["hostname"],
            port=self.cluster["port"])
        cluster.save()
        self.cluster = cluster
        credential = Credential(
            user=user,
            cluster=cluster,
            username=self.credential["username"],
            password='',
            use_password=False)
        credential.save()
        self.credential = credential
        credential2 = Credential(
            user=user,
            cluster=cluster,
            username=self.credential.username,
            password="******",
            use_password=True)
        credential2.save()
        self.credential2 = credential2

        s = "cp ~/.ssh/authorized_keys ~/.ssh/authorized_keys.chemtools.bak"
        with self.credential.get_ssh_connection() as ssh:
            ssh.exec_command(s)
Beispiel #4
0
    def setUp(self):
        self.mock_ssh, self.mock_sftp = build_mock_connections(self)
        self.client = Client()
        for user in self.users:
            new_user = get_user_model().objects.create_user(user["username"],
                                                user["email"],
                                                user["new_password1"])
            new_user.save()

        user = get_user_model().objects.all()[0]
        profile = user
        test_path = os.path.join(settings.MEDIA_ROOT, "tests")
        with open(os.path.join(test_path, "id_rsa.pub"), 'r') as f:
            profile.public_key = f.read()
        with open(os.path.join(test_path, "id_rsa"), 'r') as f:
            profile.private_key = f.read()
        profile.save()

        cluster = Cluster(
            name=self.cluster["name"],
            hostname=self.cluster["hostname"],
            port=self.cluster["port"])
        cluster.save()
        self.cluster = cluster
        credential = Credential(
            user=user,
            cluster=cluster,
            username=self.credential["username"],
            password='',
            use_password=False)
        credential.save()
        self.credential = credential
        credential2 = Credential(
            user=user,
            cluster=cluster,
            username=self.credential.username,
            password="******",
            use_password=True)
        credential2.save()
        self.credential2 = credential2
Beispiel #5
0
    def setUp(self):
        self.mock_ssh, self.mock_sftp = build_mock_connections(self)
        self.client = Client()
        for user in self.users:
            new_user = get_user_model().objects.create_user(
                user["username"], user["email"], user["new_password1"])
            new_user.save()

        user = get_user_model().objects.all()[0]
        profile = user
        test_path = os.path.join(settings.MEDIA_ROOT, "tests")
        with open(os.path.join(test_path, "id_rsa.pub"), 'r') as f:
            profile.public_key = f.read()
        with open(os.path.join(test_path, "id_rsa"), 'r') as f:
            profile.private_key = f.read()
        profile.save()

        cluster = Cluster(name=self.cluster["name"],
                          hostname=self.cluster["hostname"],
                          port=self.cluster["port"])
        cluster.save()
        self.cluster = cluster
        credential = Credential(user=user,
                                cluster=cluster,
                                username=self.credential["username"],
                                password='',
                                use_password=False)
        credential.save()
        self.credential = credential
        credential2 = Credential(user=user,
                                 cluster=cluster,
                                 username=self.credential.username,
                                 password="******",
                                 use_password=True)
        credential2.save()
        self.credential2 = credential2
Beispiel #6
0
    def setUp(self):
        self.user = get_user_model().objects.create_user(**USER)
        test_path = os.path.join(settings.MEDIA_ROOT, "tests")
        with open(os.path.join(test_path, "id_rsa.pub"), 'r') as f:
            self.user.public_key = f.read()
        with open(os.path.join(test_path, "id_rsa"), 'r') as f:
            self.user.private_key = f.read()
        self.user.save()

        super_user = get_user_model().objects.create_superuser(**SUPER_USER)
        super_user.save()


        self.cluster = Cluster(**CLUSTER)
        self.cluster.save()
        self.credential = Credential(
            user=self.user, cluster=self.cluster, **CREDENTIAL)
        self.credential.save()
        self.credential2 = Credential(
            user=super_user, cluster=self.cluster, **CREDENTIAL)
        self.credential2.save()
Beispiel #7
0
def editor_save_run(message):

    room = get_room_or_error(message["room"], message.user)
    code = message["code"]

    # run the code with test cases of the exercise
    result = compile_code(code, room.exercise)

    # result2 = compile_code2(code,room.exercise)
    #
    # print(str(result))

    if result["overall_success"] == True:

        # print("all pass")

        overall_success = True
    else:
        overall_success = False
        # print("failed")

    room.code = message["code"]
    room.save()

    result_json = json.dumps(result)

    version = Version()
    try:
        version_tree = analyser(code)
        version_tree_json = json.dumps(version_tree)
        version.version_tree = version_tree_json

    except Exception as e:
        # version tree cannot be generated
        version.version_tree = ""

    version.code = message["code"]
    version.room = room
    version.exercise = room.exercise
    version.result = result_json
    version.overall_success = overall_success
    version.save()

    # when it is a success submission

    if overall_success:

        student = Student.objects.get(user=message.user)

        # add complete student to exercise
        room.exercise.complete_student.add(student)
        room.exercise.save()

        data_matrix = []
        version_list = Version.objects.filter(exercise=room.exercise,
                                              overall_success=True)

        student = Student.objects.get(user=message.user)
        profile_tree = json.loads(student.profile_tree)

        # reduce to 0 and 1
        reduce_version_tree = reduce_tree(version_tree)

        # udpate profile tree
        new_profile_tree = add_tree(profile_tree, reduce_version_tree)

        student.profile_tree = json.dumps(new_profile_tree)

        student.save()

        # will only run when the number of success submission is >= limit
        if len(version_list) >= CONST_MIN_VERSION:

            # remove all old clusters

            Cluster.objects.filter(exercise=room.exercise).delete()

            cluster_result = clustering(version_list)

            print(cluster_result)

            cluster_list = cluster_result["cluster_list"]
            label = cluster_result["label"]
            common_skill = cluster_result["common_skill"]

            cluster_object_list = []

            # create new cluster
            for cluster in cluster_list:
                new_cluster = Cluster()
                new_cluster.exercise = room.exercise
                # print(cluster["necessary_skill"])
                new_cluster.necessary_skill = json.dumps(
                    cluster["necessary_skill"])
                new_cluster.redundant_skill = json.dumps(
                    cluster["redundant_skill"])
                new_cluster.center = ','.join(
                    map(str, cluster["center"].tolist()))
                new_cluster.data_count = cluster["data_count"]
                new_cluster.character_skill = json.dumps(
                    cluster["character_skill"])
                new_cluster.other_skill = json.dumps(cluster["other_skill"])
                new_cluster.save()
                cluster_object_list.append(new_cluster)

            for i in range(0, len(label)):
                cluster = cluster_object_list[label[i]]
                version_list[i].cluster = cluster
                version_list[i].save()

            room.exercise.common_skill = json.dumps(common_skill)
            room.exercise.save()

    #
    message.reply_channel.send({
        "text": json.dumps(result),
    })