forked from uncloud/uncloud
108 lines
4 KiB
Python
108 lines
4 KiB
Python
|
import datetime
|
||
|
|
||
|
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()
|
||
|
|
||
|
|
||
|
# If you want to check the test database 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='server1.place11.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):
|
||
|
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(2020, 4, 2, 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='alpine3.11', 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='alpine3.11', 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='alpine3.11', is_os_image=True, is_public=True, size_in_gb=10,
|
||
|
status='active'
|
||
|
)
|
||
|
)
|