uncloud-mravi/uncloud_django_based/uncloud/uncloud_vm/tests.py
2020-04-02 19:31:03 +02:00

112 lines
4.2 KiB
Python

import datetime
import parsedatetime
from django.test import TestCase
from django.contrib.auth import get_user_model
from django.utils import timezone
from django.core.exceptions import ValidationError
from uncloud_vm.models import VMDiskImageProduct, VMDiskProduct, VMProduct, VMHost
from uncloud_pay.models import Order
User = get_user_model()
cal = parsedatetime.Calendar()
# If you want to check the test database using some GUI/cli tool
# then use the following connecting parameters
# host: localhost
# database: test_uncloud
# user: root
# password:
# port: 5432
class VMTestCase(TestCase):
@classmethod
def setUpClass(cls):
# Setup vm host
cls.vm_host, created = VMHost.objects.get_or_create(
hostname='serverx.placey.ungleich.ch', physical_cores=32, usable_cores=320,
usable_ram_in_gb=512.0, status='active'
)
super().setUpClass()
def setUp(self) -> None:
# Setup two users as it is common to test with different user
self.user = User.objects.create_user(
username='testuser', email='test@test.com', first_name='Test', last_name='User'
)
self.user2 = User.objects.create_user(
username='Meow', email='meow123@test.com', first_name='Meow', last_name='Cat'
)
super().setUp()
def create_sample_vm(self, owner):
one_month_later, parse_status = cal.parse("1 month later")
return VMProduct.objects.create(
vmhost=self.vm_host, cores=2, ram_in_gb=4, owner=owner,
order=Order.objects.create(
owner=owner,
creation_date=datetime.datetime.now(tz=timezone.utc),
starting_date=datetime.datetime.now(tz=timezone.utc),
ending_date=datetime.datetime(*one_month_later[:6], tzinfo=timezone.utc),
recurring_price=4.0, one_time_price=5.0, recurring_period='per_month'
)
)
def test_disk_product(self):
"""Ensures that a VMDiskProduct can only be created from a VMDiskImageProduct
that is in status 'active'"""
vm = self.create_sample_vm(owner=self.user)
pending_disk_image = VMDiskImageProduct.objects.create(
owner=self.user, name='pending_disk_image', is_os_image=True, is_public=True, size_in_gb=10,
status='pending'
)
try:
vm_disk_product = VMDiskProduct.objects.create(
owner=self.user, vm=vm, image=pending_disk_image, size_in_gb=10
)
except ValidationError:
vm_disk_product = None
self.assertIsNone(
vm_disk_product,
msg='VMDiskProduct created with disk image whose status is not active.'
)
def test_vm_disk_product_creation(self):
"""Ensure that a user can only create a VMDiskProduct for an existing VM"""
disk_image = VMDiskImageProduct.objects.create(
owner=self.user, name='disk_image', is_os_image=True, is_public=True, size_in_gb=10,
status='active'
)
with self.assertRaises(ValidationError, msg='User created a VMDiskProduct for non-existing VM'):
# Create VMProduct object but don't save it in database
vm = VMProduct()
vm_disk_product = VMDiskProduct.objects.create(
owner=self.user, vm=vm, image=disk_image, size_in_gb=10
)
def test_vm_disk_product_creation_for_someone_else(self):
"""Ensure that a user can only create a VMDiskProduct for his/her own VM"""
# Create a VM which is ownership of self.user2
someone_else_vm = self.create_sample_vm(owner=self.user2)
# 'self.user' would try to create a VMDiskProduct for 'user2's VM
with self.assertRaises(ValidationError, msg='User created a VMDiskProduct for someone else VM.'):
vm_disk_product = VMDiskProduct.objects.create(
owner=self.user, vm=someone_else_vm,
size_in_gb=10,
image=VMDiskImageProduct.objects.create(
owner=self.user, name='disk_image', is_os_image=True, is_public=True, size_in_gb=10,
status='active'
)
)