예제 #1
0
    def get_queryset(self):
        service = CalipsoUserQuotaServices()

        username = self.kwargs.get('username')
        if username == self.request.user.username:
            return service.get_default_quota(username=username)
        else:
            raise PermissionDenied
    def setUp(self):
        self.logger.debug(
            '#### setUp CalipsoContainerServiceTestCase : START ####')

        self.containers_service = CalipsoContainersServices()
        self.containers_quota = CalipsoUserQuotaServices()

        self.logger.debug(
            '#### setUp CalipsoContainerServiceTestCase : END ####')
예제 #3
0
 def get(self, request, username):
     service = CalipsoUserQuotaServices()
     username = self.kwargs.get('username')
     if username == self.request.user.username:
         quote = service.get_default_quota(username=username)
         serializer_class = CalipsoUserQuotaSerializer(quote, many=True)
         return Response(serializer_class.data)
     else:
         raise PermissionDenied
예제 #4
0
 def post(self, request, username):
     data = request.data
     service = CalipsoUserQuotaServices()
     username = self.kwargs.get('username')
     # Todo add check to make sure current logged in user is an admin
     service.change_user_quota(username=username,
                               max_simulations=data['max_simultaneous'],
                               memory=data['memory'],
                               cpu=data['cpu'],
                               hdd=data['hdd'])
     return Response(status=status.HTTP_200_OK)
import re
import uuid
import docker

from apprest.models import CalipsoSession
from apprest.models.container import CalipsoContainer
from apprest.services.image import CalipsoAvailableImagesServices

from django.conf import settings

from apprest.services.quota import CalipsoUserQuotaServices
from apprest.services.session import CalipsoSessionsServices
from apprest.utils.exceptions import QuotaMaxSimultaneousExceeded, QuotaHddExceeded, QuotaMemoryExceeded, \
    QuotaCpuExceeded, DockerExceptionNotFound

quota_service = CalipsoUserQuotaServices()
image_service = CalipsoAvailableImagesServices()
session_service = CalipsoSessionsServices()


class CalipsoContainersServices:
    def __init__(self):
        self.logger = logging.getLogger(__name__)
        try:
            self.client = docker.DockerClient(
                tls=False, base_url=settings.DOCKER_URL_DAEMON)
            self.logger.debug('Docker deamon has been initialized')
        except Exception as e:
            self.logger.critical("Docker deamon not found.")
            self.logger.critical(e)
 def setUp(self):
     self.logger.debug('#### setUp START ####')
     self.scientist_1 = CalipsoUser.objects.get(pk=1)
     self.quota_service = CalipsoUserQuotaServices()
     self.logger.debug('#### setUp END ####')
class ContainerViewsTestCase(CalipsoTestCase):
    logger = logging.getLogger(__name__)
    fixtures = ['users.json', 'images.json', 'quotas.json']

    def setUp(self):
        self.logger.debug('#### setUp START ####')
        self.scientist_1 = CalipsoUser.objects.get(pk=1)
        self.quota_service = CalipsoUserQuotaServices()
        self.logger.debug('#### setUp END ####')

    def test_view_run_stop_rm_container(self):
        self.logger.debug(
            '#### TEST test_view_run_stop_rm_container START ####')

        base_url = '/container/list/%s/'

        # Not authenticated -> 403
        url = base_url % self.scientist_1.user.username

        # Login and check methods
        self.login_and_check_http_methods(self.scientist_1.user.username, url,
                                          ['GET', 'HEAD', 'OPTIONS'])

        # run
        response_run = self.client.get(
            reverse('run_container',
                    kwargs={
                        'username': '******',
                        'experiment': 'EXPERIMENT_SN',
                        'public_name': 'base_jupyter'
                    }))
        self.assertEqual(response_run.status_code, status.HTTP_201_CREATED)

        json_content = json.loads(response_run.content.decode("utf-8"))

        container_name = json_content['container_name']

        # stop
        response_stop = self.client.get(
            reverse('stop_container',
                    kwargs={
                        'username': '******',
                        'container_name': container_name
                    }))
        self.assertEqual(response_stop.status_code, status.HTTP_200_OK)

        # remove
        response_rm = self.client.get(
            reverse('rm_container',
                    kwargs={
                        'username': '******',
                        'container_name': container_name
                    }))
        self.assertEqual(response_rm.status_code, status.HTTP_200_OK)

        self.logger.debug('#### TEST test_view_run_stop_rm_container END ####')

    def test_user_HTTP_403_FORBIDDEN(self):
        self.logger.debug('#### TEST test_user_HTTP_403_FORBIDDEN START ####')

        response_run = self.client.get(
            reverse('run_container',
                    kwargs={
                        'username': '******',
                        'experiment': 'EXPERIMENT_SN',
                        'public_name': 'base_jupyter'
                    }))
        self.assertEqual(response_run.status_code, status.HTTP_403_FORBIDDEN)

        self.logger.debug('#### TEST test_user_HTTP_403_FORBIDDEN END ####')

    def test_max_MAX_CONTAINER_PER_USER_containers_active_per_user(self):
        self.logger.debug(
            '#### TEST test_max_MAX_CONTAINER_PER_USER_containers_active_per_user START ####'
        )

        base_url = '/container/list/%s/'

        url = base_url % self.scientist_1.user.username

        # Login and check methods
        self.login_and_check_http_methods(self.scientist_1.user.username, url,
                                          ['GET', 'HEAD', 'OPTIONS'])

        all_container_responses = []

        quota = self.quota_service.get_default_quota(
            username=self.scientist_1.user.username)[0]
        logger.info("MAX SIM:" + str(quota.max_simultaneous))

        for x in range(0, quota.max_simultaneous):
            all_container_responses.append(
                self.client.get(
                    reverse('run_container',
                            kwargs={
                                'username': '******',
                                'experiment': 'EXPERIMENTS',
                                'public_name': 'base_jupyter'
                            })))
            self.assertEqual(all_container_responses[x].status_code,
                             status.HTTP_201_CREATED)

        last_fail_container = self.client.get(
            reverse('run_container',
                    kwargs={
                        'username': '******',
                        'experiment': 'EXPERIMENT_LAST',
                        'public_name': 'base_jupyter'
                    }))

        self.assertEqual(last_fail_container.status_code,
                         status.HTTP_204_NO_CONTENT)

        # stop and remove all
        for x in range(0, quota.max_simultaneous):
            self.stop_remove_container(
                container_response=all_container_responses.pop(),
                username='******')

        self.logger.debug(
            '#### TEST max_3_containers_active_per_user END ####')

    def stop_remove_container(self, container_response, username):
        # stop
        json_content = json.loads(container_response.content.decode("utf-8"))
        container_name = json_content['container_name']

        response_stop = self.client.get(
            reverse('stop_container',
                    kwargs={
                        'username': username,
                        'container_name': container_name
                    }))
        self.assertEqual(response_stop.status_code, status.HTTP_200_OK)

        # remove
        response_rm = self.client.get(
            reverse('rm_container',
                    kwargs={
                        'username': username,
                        'container_name': container_name
                    }))
        self.assertEqual(response_rm.status_code, status.HTTP_200_OK)

    def test_read_logs_from_container(self):

        self.logger.debug('#### TEST test_read_logs_from_container START ####')
        base_url = '/container/list/%s/'

        # Not authenticated -> 403
        url = base_url % self.scientist_1.user.username

        # Login and check methods
        self.login_and_check_http_methods(self.scientist_1.user.username, url,
                                          ['GET', 'HEAD', 'OPTIONS'])

        # run
        response_run = self.client.get(
            reverse('run_container',
                    kwargs={
                        'username': '******',
                        'experiment': 'EXPERIMENT_SN',
                        'public_name': 'base_jupyter'
                    }))

        self.assertEqual(response_run.status_code, status.HTTP_201_CREATED)

        self.stop_remove_container(container_response=response_run,
                                   username='******')

        self.logger.debug('#### TEST test_read_logs_from_container END ####')
class CalipsoContainerServiceTestCase(APITestCase):
    logger = logging.getLogger(__name__)
    fixtures = ['users.json', 'images.json', 'quotas.json']

    def setUp(self):
        self.logger.debug(
            '#### setUp CalipsoContainerServiceTestCase : START ####')

        self.containers_service = CalipsoContainersServices()
        self.containers_quota = CalipsoUserQuotaServices()

        self.logger.debug(
            '#### setUp CalipsoContainerServiceTestCase : END ####')

    def test_service_run_stop_rm_container_with_default_quota(self):
        self.logger.debug(
            '#### TEST test_service_run_stop_rm_container_with_default_quota START ####'
        )

        container = self.containers_service.run_container(
            username='******',
            experiment='55555',
            container_public_name='base_jupyter')

        self.assertEqual(len(container.container_id), 64)
        self.assertEqual(container.container_status, 'created')

        self.containers_service.stop_container(container.container_name)
        self.containers_service.rm_container(container.container_name)

        self.logger.debug(
            '#### TEST test_service_run_stop_rm_container_with_default_quota END ####'
        )

    def test_service_run_container_default_quota(self):
        self.logger.debug(
            '#### TEST test_service_run_container_default_quota START ####')

        all_container_responses = []

        quota = self.containers_quota.get_default_quota(username='******')
        self.logger.debug('quota found .. max:%d' % quota.max_simultaneous)

        for x in range(0, quota.max_simultaneous):
            self.logger.debug('append container %d' % x)
            all_container_responses.append(
                self.containers_service.run_container(
                    username='******',
                    experiment=str(x),
                    container_public_name='base_jupyter'))
            self.assertNotEquals(all_container_responses[x], None)

        # stop all
        for x in range(0, quota.max_simultaneous):
            container = all_container_responses.pop()
            self.containers_service.stop_container(
                container_name=container.container_name)
            self.containers_service.rm_container(
                container_name=container.container_name)

        self.logger.debug(
            '#### TEST test_service_run_container_default_quota END ####')