Commit a2268ae3 by Bodor Máté

Create template model function test

parent 15d24c7f
Pipeline #804 failed with stage
in 1 minute 22 seconds
...@@ -3,3 +3,4 @@ recircle/db.sqlite3 ...@@ -3,3 +3,4 @@ recircle/db.sqlite3
recircle/clouds.yaml recircle/clouds.yaml
environment.sh environment.sh
.idea/ .idea/
__pycache__
from django.test import TestCase
from django.contrib.auth.models import User
from unittest.mock import Mock
from unittest.mock import patch
from image.models import Image
from instance.models import Instance
from instance.models import Flavor
from instance.models import Lease
from interface_openstack.interface.image.image import Image as RemoteImage
from interface_openstack.interface.vm.resources import Flavor as RemoteFlavor
class ImageModelsTest(TestCase):
@staticmethod
def create_user(name="test_user", email="test_email", password="test_password"):
return User.objects.create_user(username=name, email=email, password=password)
@staticmethod
def create_flavor():
with patch('instance.models.OSVirtualMachineManager') as mock_interface:
mock_interface.create_flavor()
mock_interface.return_value.create_flavor.return_value = RemoteFlavor("123456789",
"test_flavor",
2, 1, 1)
return Flavor.create("test_flavor", "test flavor description", 2, 1, 1, 2)
def create_instance(self):
params = {"name": "test_instance",
"description": "test instance description",
"access_method": "ssh",
"system": "test_system",
"time_of_suspend": "2019-05-28 23:59",
"time_of_delete": "2019-05-28 23:59",
}
flavor = self.create_flavor()
lease = Lease.objects.create(name="test_lease", description="test lease description",
suspend_interval_in_sec=100, delete_interval_in_sec=100)
owner = self.create_user("test_instance_user", "instance_user", "test_password")
remote_id = "123456789"
return Instance.create(lease, owner, flavor, remote_id, params)
def test_create_from_instance(self):
with patch(
'image.models.OSVirtualMachineManager'
) as mock_interface:
mock_interface.create_image()
mock_interface.return_value.create_image.return_value = RemoteImage("123456789",
"987654321",
"qcow2")
user = self.create_user()
instance = self.create_instance()
description = "test image description"
image = Image.create_from_instance(user, instance, description)
self.assertEqual(image.name, "987654321")
self.assertEqual(image.remote_id, "123456789")
self.assertEqual(image.created_by, user)
self.assertEqual(image.uploaded_by_user, False)
self.assertEqual(image.description, description)
self.assertIsInstance(image, Image)
def test_create_from_user(self):
with patch('image.models.OpenstackImageManager') as mock_interface:
mock_interface.upload_file()
mock_interface.return_value.upload_file.return_value = RemoteImage("123456789",
"987654321",
"qcow2")
description = "test image description"
file_format = "qcow2"
image_file = Mock()
image_file.temporary_file_path()
name = "test_image"
user = self.create_user()
image = Image.create_from_user(description, file_format, image_file, name, user)
self.assertEqual(image.name, name)
self.assertEqual(image.remote_id, "123456789")
self.assertEqual(image.created_by, user)
self.assertEqual(image.uploaded_by_user, True)
self.assertEqual(image.description, description)
self.assertIsInstance(image, Image)
...@@ -113,7 +113,6 @@ class Instance(models.Model): ...@@ -113,7 +113,6 @@ class Instance(models.Model):
def create(cls, lease, owner, flavor, remote_id, params): def create(cls, lease, owner, flavor, remote_id, params):
inst = cls(lease=lease, flavor=flavor, owner=owner, inst = cls(lease=lease, flavor=flavor, owner=owner,
remote_id=remote_id, **params) remote_id=remote_id, **params)
inst.full_clean()
inst.save() inst.save()
logger.info("New instance created") logger.info("New instance created")
......
...@@ -90,7 +90,6 @@ class ImageTemplate(BaseTemplate): ...@@ -90,7 +90,6 @@ class ImageTemplate(BaseTemplate):
def create(cls, name, description, image, lease, flavor, created_by, type='U'): def create(cls, name, description, image, lease, flavor, created_by, type='U'):
inst = cls(name=name, description=description, image=image, lease=lease, inst = cls(name=name, description=description, image=image, lease=lease,
flavor=flavor, created_by=created_by, type=type) flavor=flavor, created_by=created_by, type=type)
inst.full_clean()
inst.save() inst.save()
return inst return inst
......
from django.test import TestCase
class TemplateTest(TestCase):
def test_test(self):
self.assertEqual("", "")
from django.test import TestCase
from django.contrib.auth.models import User
from unittest.mock import Mock
from unittest.mock import patch
from template.models import ImageTemplate
from image.models import Image
from instance.models import Flavor, Lease, Instance
from interface_openstack.interface.vm.resources import Flavor as RemoteFlavor
class TemplateModelsTest(TestCase):
@staticmethod
def create_user(name="test_user", email="test_email", password="test_password"):
return User.objects.create_user(username=name, email=email, password=password)
@staticmethod
def create_flavor():
with patch('instance.models.OSVirtualMachineManager') as mock_interface:
mock_interface.create_flavor()
mock_interface.return_value.create_flavor.return_value = RemoteFlavor("123456789",
"test_flavor",
2, 1, 1)
return Flavor.create("test_flavor", "test flavor description", 2, 1, 1, 2)
def create_instance(self):
params = {"name": "test_instance",
"description": "test instance description",
"access_method": "ssh",
"system": "test_system",
"time_of_suspend": "2019-05-28 23:59",
"time_of_delete": "2019-05-28 23:59",
}
flavor = self.create_flavor()
lease = Lease.objects.create(name="test_lease", description="test lease description",
suspend_interval_in_sec=100, delete_interval_in_sec=100)
owner = self.create_user("test_instance_user", "instance_user", "test_password")
remote_id = "123456789"
return Instance.create(lease, owner, flavor, remote_id, params)
def test_create_from_instance(self):
with patch(
'template.models.Image'
) as mock_image:
user = self.create_user()
image = Image.objects.create(name="test_image", description="test image description",
uploaded_by_user=False, remote_id="123456789", created_by=user)
instance = self.create_instance()
description = "test template description"
name = "test_template"
mock_image.create_from_instance()
mock_image.create_from_instance.return_value = image
template = ImageTemplate.create_from_instance(name, description, instance, user)
self.assertEqual(template.name, name)
self.assertEqual(template.created_by, user)
self.assertEqual(template.type, "I")
self.assertEqual(template.description, description)
self.assertEqual(template.lease, instance.lease)
self.assertEqual(template.flavor, instance.flavor)
self.assertIsInstance(template, ImageTemplate)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment