Commit 44c637b3 by Bodor Máté

image model unit test

parent 15d24c7f
Pipeline #800 passed with stage
in 1 minute 22 seconds
......@@ -12,7 +12,10 @@ class Disk(models.Model):
"""
name = models.CharField(
blank=True, max_length=100, verbose_name="name", help_text="Name of the disk"
blank=True,
max_length=100,
verbose_name="name",
help_text="Name of the disk"
)
remote_id = models.CharField(
max_length=40,
......@@ -62,7 +65,8 @@ class Image(models.Model):
@classmethod
def create(cls, name, description, remote_id, uploaded_by_user, created_by):
inst = cls(name=name, description=description, uploaded_by_user=uploaded_by_user,
inst = cls(name=name, description=description,
uploaded_by_user=uploaded_by_user,
remote_id=remote_id, created_by=created_by)
inst.full_clean()
inst.save()
......@@ -85,9 +89,11 @@ class Image(models.Model):
return new_image
@classmethod
def create_from_user(cls, description, file_format, image_file, name, user):
def create_from_user(cls, description,
file_format, image_file, name, user):
interface = OpenstackImageManager(settings.CONNECTION)
remote_image = interface.upload_file(name=name, path=image_file.temporary_file_path(),
remote_image = interface.upload_file(name=name,
path=image_file.temporary_file_path(),
format=file_format)
new_image = cls.create(
name=name,
......
from django.test import TestCase
class ImageTest(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 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,7 @@ class Instance(models.Model):
def create(cls, lease, owner, flavor, remote_id, params):
inst = cls(lease=lease, flavor=flavor, owner=owner,
remote_id=remote_id, **params)
inst.full_clean()
inst.save()
logger.info("New instance created")
......
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