Commit 1695e4f2 authored by Samuel GAIST's avatar Samuel GAIST Committed by Flavio TARSETTI
Browse files

[experiments][tests] Refactor test and add email sending checks

parent 81cc5e83
Pipeline #43017 failed with stage
in 14 minutes and 54 seconds
# vim: set fileencoding=utf-8 :
###############################################################################
# #
# Copyright (c) 2016 Idiap Research Institute, http://www.idiap.ch/ #
# Contact: beat.support@idiap.ch #
# #
# This file is part of the beat.web module of the BEAT platform. #
# #
# Commercial License Usage #
# Licensees holding valid commercial BEAT licenses may use this file in #
# accordance with the terms contained in a written agreement between you #
# and Idiap. For further information contact tto@idiap.ch #
# #
# Alternatively, this file may be used under the terms of the GNU Affero #
# Public License version 3 as published by the Free Software and appearing #
# in the file LICENSE.AGPL included in the packaging of this file. #
# The BEAT platform is distributed in the hope that it will be useful, but #
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY #
# or FITNESS FOR A PARTICULAR PURPOSE. #
# #
# You should have received a copy of the GNU Affero Public License along #
# with the BEAT platform. If not, see http://www.gnu.org/licenses/. #
# #
###############################################################################
import os
import shutil
from django.conf import settings
from django.contrib.auth.models import User
from ...algorithms.models import Algorithm
from ...backend.models import Environment
from ...backend.models import Queue
from ...common.testutils import BaseTestCase
from ...common.testutils import tearDownModule # noqa test runner will call it
from ...databases.models import Database
from ...dataformats.models import DataFormat
from ...toolchains.models import Toolchain
TEST_PWD = "1234" # nosec
HASHES = {
"addition1": "ff59a471cec5c17b45d1dfa5aff3ed897ee2d7ed87de205365b372be1c726c87",
"addition2": "41bd0ffd85bef70ea7b1499921797e269471b423c117e261349489d956df41a4",
"analysis": "ca9e22ea791bcf1cc63627c4429ae94bf644b1e7b2862e6ee70328f5661af0b6",
}
class ExperimentTestBase(BaseTestCase):
DECLARATION1 = {
"blocks": {
"addition1": {
"algorithm": "johndoe/sum/1",
"parameters": {},
"inputs": {"a": "a", "b": "b"},
"outputs": {"sum": "sum"},
},
"addition2": {
"algorithm": "johndoe/sum/1",
"parameters": {},
"inputs": {"a": "a", "b": "b"},
"outputs": {"sum": "sum"},
},
},
"datasets": {
"dataset1": {
"database": "integers/1",
"protocol": "triple",
"set": "default",
}
},
"analyzers": {
"analysis": {
"algorithm": "johndoe/analysis/1",
"parameters": {},
"inputs": {"in": "input"},
}
},
"globals": {
"environment": {"name": "env1", "version": "1.0"},
"queue": "queue1",
},
}
DECLARATION2 = {
"blocks": {
"addition1": {
"algorithm": "johndoe/sum/1",
"parameters": {},
"inputs": {"a": "a", "b": "b"},
"outputs": {"sum": "sum"},
}
},
"datasets": {
"dataset1": {
"database": "integers/1",
"protocol": "triple",
"set": "default",
}
},
"analyzers": {
"analysis": {
"algorithm": "johndoe/analysis/1",
"parameters": {},
"inputs": {"in": "input"},
}
},
"globals": {
"environment": {"name": "env1", "version": "1.0"},
"queue": "queue1",
},
}
DATABASE = {
"root_folder": "/path/to/root_folder",
"protocols": [
{
"name": "triple",
"template": "test",
"sets": [
{
"name": "default",
"template": "set",
"view": "dummy",
"outputs": {
"output1": "johndoe/single_integer/1",
"output2": "johndoe/single_integer/1",
"output3": "johndoe/single_integer/1",
},
}
],
}
],
}
def setUp(self):
for path in [
settings.TOOLCHAINS_ROOT,
settings.EXPERIMENTS_ROOT,
settings.DATAFORMATS_ROOT,
settings.ALGORITHMS_ROOT,
settings.CACHE_ROOT,
]:
if os.path.exists(path):
shutil.rmtree(path)
user = User.objects.create_user("johndoe", "johndoe@test.org", TEST_PWD)
# Create an environment and queue
environment = Environment(name="env1", version="1.0")
environment.save()
environment.share()
environment2 = Environment(name="private_env", version="1.0")
environment2.save()
queue = Queue(
name="queue1",
memory_limit=1024,
time_limit=60,
cores_per_slot=1,
max_slots_per_user=10,
)
queue.save()
queue.environments.add(environment)
queue.environments.add(environment2)
(integer, errors) = DataFormat.objects.create_dataformat(
author=user,
name="integer",
short_description="Default integer type need for algorithm prototype",
)
self.assertIsNotNone(integer, msg=errors)
DataFormat.objects.create_dataformat(
author=user,
name="single_integer",
short_description="description",
declaration={"value": "int32"},
)
database, errors = Database.objects.create_database(
"integers", declaration=self.DATABASE
)
self.assertIsNotNone(database, msg=errors)
database.sharing = Database.PUBLIC
database.save()
(self.toolchain1, errors) = Toolchain.objects.create_toolchain(
user,
"toolchain1",
"short description 1",
declaration={
"blocks": [
{
"name": "addition1",
"inputs": ["a", "b"],
"outputs": ["sum"],
"synchronized_channel": "dataset1",
},
{
"name": "addition2",
"inputs": ["a", "b"],
"outputs": ["sum"],
"synchronized_channel": "dataset1",
},
],
"datasets": [
{"name": "dataset1", "outputs": ["output1", "output2", "output3"]}
],
"connections": [
{
"from": "dataset1.output1",
"to": "addition1.a",
"channel": "dataset1",
},
{
"from": "dataset1.output2",
"to": "addition1.b",
"channel": "dataset1",
},
{
"from": "addition1.sum",
"to": "addition2.a",
"channel": "dataset1",
},
{
"from": "dataset1.output3",
"to": "addition2.b",
"channel": "dataset1",
},
{
"to": "analysis.input",
"from": "addition2.sum",
"channel": "dataset1",
},
],
"analyzers": [
{
"inputs": ["input"],
"synchronized_channel": "dataset1",
"name": "analysis",
}
],
"representation": {
"connections": {},
"blocks": {},
"channel_colors": {},
},
},
)
self.assertIsNone(errors, "Toolchain errors: %s" % errors)
(self.algorithm, errors) = Algorithm.objects.create_algorithm(
author=user,
name="sum",
short_description="description",
declaration="""{
"language": "python",
"splittable": false,
"groups": [
{
"inputs": {
"a": { "type": "johndoe/single_integer/1" },
"b": { "type": "johndoe/single_integer/1" }
},
"outputs": {
"sum": { "type": "johndoe/single_integer/1" }
}
}
],
"parameters": {
}
}""",
code="""class Algorithm:
def process(self, inputs, outputs):
data = outputs['sum'].createData()
data.value = inputs['a'].data.value + inputs['b'].data.value
outputs['sum'].write(data)
return True
""",
)
self.assertIsNotNone(self.algorithm, errors)
system_user = User.objects.create_user(
settings.SYSTEM_ACCOUNT, "system@test.org", TEST_PWD
)
(self.toolchain2, errors) = Toolchain.objects.create_toolchain(
user,
"toolchain2",
"short description 2",
declaration={
"blocks": [
{
"name": "addition1",
"inputs": ["a", "b"],
"outputs": ["sum"],
"synchronized_channel": "dataset1",
}
],
"datasets": [
{"name": "dataset1", "outputs": ["output1", "output2", "output3"]}
],
"connections": [
{
"from": "dataset1.output1",
"to": "addition1.a",
"channel": "dataset1",
},
{
"from": "dataset1.output2",
"to": "addition1.b",
"channel": "dataset1",
},
{
"to": "analysis.input",
"from": "addition1.sum",
"channel": "dataset1",
},
],
"analyzers": [
{
"inputs": ["input"],
"synchronized_channel": "dataset1",
"name": "analysis",
}
],
"representation": {
"connections": {},
"blocks": {},
"channel_colors": {},
},
},
)
self.assertIsNone(errors, "Toolchain errors: %s" % errors)
(dataformat, errors) = DataFormat.objects.create_dataformat(
author=system_user,
name="float",
short_description="description",
declaration={"value": "float64"},
)
self.assertIsNotNone(dataformat, errors)
(dataformat, errors) = DataFormat.objects.create_dataformat(
author=system_user,
name="text",
short_description="description",
declaration={"text": "string"},
)
self.assertIsNotNone(dataformat, errors)
(self.algorithm, errors) = Algorithm.objects.create_algorithm(
author=user,
name="analysis",
short_description="description",
declaration="""{
"language": "python",
"groups": [
{
"inputs": {
"in": { "type": "johndoe/single_integer/1" }
}
}
],
"results": {
"out_float": { "type": "float32" },
"out_text": { "type": "string" }
},
"parameters": {
}
}""",
code="""class Algorithm:
def process(self, inputs, output):
# We don't really care
return True
""",
)
self.assertIsNotNone(self.algorithm, errors)
def tearDown(self):
for path in [
settings.TOOLCHAINS_ROOT,
settings.EXPERIMENTS_ROOT,
settings.DATAFORMATS_ROOT,
settings.ALGORITHMS_ROOT,
settings.CACHE_ROOT,
]:
if os.path.exists(path):
shutil.rmtree(path)
def login(self, username):
self.client.login(username=username, password=TEST_PWD)
def login_johndoe(self):
self.login("johndoe")
def login_jackdoe(self):
self.login("jackdoe")
# vim: set fileencoding=utf-8 :
###############################################################################
# #
# Copyright (c) 2020 Idiap Research Institute, http://www.idiap.ch/ #
# Contact: beat.support@idiap.ch #
# #
# This file is part of the beat.web module of the BEAT platform. #
# #
# Commercial License Usage #
# Licensees holding valid commercial BEAT licenses may use this file in #
# accordance with the terms contained in a written agreement between you #
# and Idiap. For further information contact tto@idiap.ch #
# #
# Alternatively, this file may be used under the terms of the GNU Affero #
# Public License version 3 as published by the Free Software and appearing #
# in the file LICENSE.AGPL included in the packaging of this file. #
# The BEAT platform is distributed in the hope that it will be useful, but #
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY #
# or FITNESS FOR A PARTICULAR PURPOSE. #
# #
# You should have received a copy of the GNU Affero Public License along #
# with the BEAT platform. If not, see http://www.gnu.org/licenses/. #
# #
###############################################################################
from django.contrib.auth.models import User
from django.contrib.sites.models import Site
from django.core import mail
from django.test import override_settings
from ...algorithms.models import Algorithm
from ...team.models import Team
from ...toolchains.models import Toolchain
from ...utils.tests.helpers import reload_urlconf
from ..models import Experiment
from .core import TEST_PWD
from .core import ExperimentTestBase
# ----------------------------------------------------------
class TeamDeletionPropagationTestCase(ExperimentTestBase):
def setUp(self):
super().setUp()
user1 = User.objects.get(username="johndoe")
user2 = User.objects.create_user("jackdoe", "jackdoe@test.org", TEST_PWD)
user3 = User.objects.create_user("janedoe", "janedoe@test.org", TEST_PWD)
team1 = Team.objects.create(name="teamdoe", owner=user1)
team2 = Team.objects.create(name="teamdoe2", owner=user1)
Team.objects.create(name="teamdoe3", owner=user1)
Team.objects.create(name="teamdoe4", owner=user1)
team1.members.add(user2)
team2.members.add(user3)
def check_shared_objects(self, user, experiments, toolchains, algorithms):
self.assertEqual(Experiment.objects.for_user(user).count(), experiments)
self.assertEqual(Toolchain.objects.for_user(user).count(), toolchains)
self.assertEqual(Algorithm.objects.for_user(user).count(), algorithms)
def test_propagation(self):
jackdoe = User.objects.get(username="jackdoe")
johndoe = User.objects.get(username="johndoe")
janedoe = User.objects.get(username="janedoe")
team_to_delete = Team.objects.create(name="team_to_delete", owner=johndoe)
team_to_delete.members.add(janedoe, jackdoe)
self.check_shared_objects(janedoe, 0, 0, 0)
self.check_shared_objects(jackdoe, 0, 0, 0)
db_toolchain = Toolchain.objects.get(author=johndoe, name="toolchain1")
(experiment, toolchain, errors) = Experiment.objects.create_experiment(
author=johndoe,
toolchain=db_toolchain,
name="john_experiment1",
short_description="john_experiment1",
description="description",
declaration=ExperimentTestBase.DECLARATION1,
)
self.assertIsNotNone(experiment, errors)
algorithms_infos = {}
for algorithm in experiment.referenced_algorithms.filter(author=johndoe):
algorithms_infos[algorithm.fullname()] = {"opensource": True}
experiment.share(teams=[team_to_delete], algorithms_infos=algorithms_infos)
self.check_shared_objects(janedoe, 1, 1, 2)
self.check_shared_objects(jackdoe, 1, 1, 2)
experiment, toolchain, errors = Experiment.objects.create_experiment(
author=janedoe,
toolchain=db_toolchain,
name="jane_experiment1",
short_description="jane_experiment1",
description="description",
declaration=ExperimentTestBase.DECLARATION1,
)
self.assertIsNotNone(experiment, errors)
self.check_shared_objects(janedoe, 2, 1, 2)
self.check_shared_objects(jackdoe, 1, 1, 2)
team_to_delete.delete()
self.check_shared_objects(janedoe, 1, 1, 2)
self.check_shared_objects(jackdoe, 0, 0, 0)
class StatusEmailTestCase(ExperimentTestBase):
def test_emails(self):
johndoe = User.objects.get(username="johndoe")
db_toolchain = Toolchain.objects.get(author=johndoe, name="toolchain1")
(experiment, toolchain, errors) = Experiment.objects.create_experiment(
author=johndoe,
toolchain=db_toolchain,
name="john_experiment1",
short_description="john_experiment1",
description="description",
declaration=ExperimentTestBase.DECLARATION1,
)
current_site = Site.objects.get_current()
self.assertIsNotNone(experiment, errors)
for prefix in ["", "/platform"]:
with self.subTest(url_prefix=prefix):
with override_settings(URL_PREFIX=prefix):
reload_urlconf()
for status in [Experiment.DONE, Experiment.FAILED]:
mail.outbox = []
experiment.status = Experiment.DONE
experiment.save()
self.assertEqual(len(mail.outbox), 1)
self.assertTrue(
f"https://{current_site.domain}{prefix}" in mail.outbox[0].body
)
#!/usr/bin/env python
# vim: set fileencoding=utf-8 :
###############################################################################
......@@ -24,413 +23,31 @@
# with the BEAT platform. If not, see http://www.gnu.org/licenses/. #
# #
###############################################################################
import copy
import os
import shutil
from datetime import datetime
from datetime import timedelta
import simplejson as json
from django.conf import settings
from django.contrib.auth.models import User
from django.urls import reverse
from ...algorithms.models import Algorithm
from ...attestations.models import Attestation
from ...backend.models import Environment
from ...backend.models import Queue
from ...common.testutils import BaseTestCase
from ...common.testutils import tearDownModule # noqa test runner will call it
from ...databases.models import Database
from ...dataformats.models import DataFormat
from ...team.models import Team
from ...toolchains.models import Toolchain
from ..models import Block
from ..models import CachedFile
from ..models import Experiment
from ..models import Result
TEST_PWD = "1234" # nosec
HASHES = {
"addition1": "ff59a471cec5c17b45d1dfa5aff3ed897ee2d7ed87de205365b372be1c726c87",
"addition2": "41bd0ffd85bef70ea7b1499921797e269471b423c117e261349489d956df41a4",
"analysis": "ca9e22ea791bcf1cc63627c4429ae94bf644b1e7b2862e6ee70328f5661af0b6",
}
class ExperimentTestBase(BaseTestCase):
DECLARATION1 = {
"blocks": {