2020-03-02 11:54:36 +00:00
|
|
|
import datetime
|
|
|
|
|
2020-03-02 14:20:12 +00:00
|
|
|
import parsedatetime
|
|
|
|
|
2020-03-02 11:54:36 +00:00
|
|
|
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
|
2020-03-09 11:02:17 +00:00
|
|
|
from uncloud_pay.models import Order, RecurringPeriod
|
2020-03-02 11:54:36 +00:00
|
|
|
|
|
|
|
User = get_user_model()
|
2020-03-02 14:20:12 +00:00
|
|
|
cal = parsedatetime.Calendar()
|
2020-03-02 11:54:36 +00:00
|
|
|
|
|
|
|
|
2020-03-02 12:42:54 +00:00
|
|
|
# If you want to check the test database using some GUI/cli tool
|
|
|
|
# then use the following connecting parameters
|
2020-03-02 11:54:36 +00:00
|
|
|
|
|
|
|
# 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(
|
2020-03-02 12:42:54 +00:00
|
|
|
hostname='serverx.placey.ungleich.ch', physical_cores=32, usable_cores=320,
|
2020-03-02 11:54:36 +00:00
|
|
|
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):
|
2020-03-02 14:20:12 +00:00
|
|
|
one_month_later, parse_status = cal.parse("1 month later")
|
2020-03-02 11:54:36 +00:00
|
|
|
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),
|
2020-03-02 14:20:12 +00:00
|
|
|
ending_date=datetime.datetime(*one_month_later[:6], tzinfo=timezone.utc),
|
2020-03-09 11:02:17 +00:00
|
|
|
recurring_period=RecurringPeriod.PER_MONTH
|
2020-03-02 11:54:36 +00:00
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2020-03-09 11:02:17 +00:00
|
|
|
# TODO: the logic tested by this test is not implemented yet.
|
|
|
|
# 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.'
|
|
|
|
# )
|
2020-03-02 11:54:36 +00:00
|
|
|
|
2020-03-09 11:02:17 +00:00
|
|
|
# TODO: the logic tested by this test is not implemented yet.
|
|
|
|
# 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'
|
|
|
|
# )
|
|
|
|
# )
|