2017-05-09 14:33:56 +00:00
|
|
|
from django.test import TestCase
|
2017-05-09 15:15:12 +00:00
|
|
|
from .models import VirtualMachine, VirtualMachineTemplate, OpenNebulaManager
|
2017-05-09 14:33:56 +00:00
|
|
|
|
|
|
|
class OpenNebulaManagerTestCases(TestCase):
|
|
|
|
"""This class defines the test suite for the opennebula manager model."""
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
"""Define the test client and other test variables."""
|
|
|
|
self.cores = 1
|
|
|
|
self.memory = 1
|
|
|
|
self.disk_size = 10.0
|
|
|
|
|
|
|
|
self.email = 'test@test.com'
|
|
|
|
self.password = 'testtest'
|
|
|
|
|
|
|
|
self.manager = OpenNebulaManager(email=None, password=None, create_user=False)
|
|
|
|
|
|
|
|
|
|
|
|
def test_model_can_connect_to_server(self):
|
|
|
|
"""Test the opennebula manager model can connect to a server."""
|
2017-05-09 23:11:49 +00:00
|
|
|
try:
|
2017-05-11 02:05:58 +00:00
|
|
|
user_pool = self.manager._get_user_pool()
|
2017-05-09 23:11:49 +00:00
|
|
|
except:
|
2017-05-11 02:05:58 +00:00
|
|
|
user_pool = None
|
|
|
|
self.assertFalse(user_pool is None)
|
2017-05-09 14:33:56 +00:00
|
|
|
|
|
|
|
def test_model_can_create_user(self):
|
|
|
|
"""Test the opennebula manager model can create a new user."""
|
2017-05-09 15:15:12 +00:00
|
|
|
old_count = len(self.manager._get_user_pool())
|
2017-05-09 14:33:56 +00:00
|
|
|
self.manager = OpenNebulaManager(email=self.email,
|
|
|
|
password=self.password,
|
|
|
|
create_user=True)
|
2017-05-11 02:05:58 +00:00
|
|
|
user_pool = self.manager._get_user_pool()
|
|
|
|
new_count = len(user_pool)
|
|
|
|
# Remove the user afterwards
|
|
|
|
user = user_pool.get_by_name(self.email)
|
|
|
|
user.delete()
|
|
|
|
|
2017-05-09 14:33:56 +00:00
|
|
|
self.assertNotEqual(old_count, new_count)
|
|
|
|
|
|
|
|
|
|
|
|
class VirtualMachineTemplateTestCase(TestCase):
|
|
|
|
"""This class defines the test suite for the virtualmachine template model."""
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
"""Define the test client and other test variables."""
|
|
|
|
self.template_name = "Standard"
|
|
|
|
self.base_price = 0.0
|
|
|
|
self.core_price = 5.0
|
|
|
|
self.memory_price = 2.0
|
|
|
|
self.disk_size_price = 0.6
|
|
|
|
|
|
|
|
self.cores = 1
|
|
|
|
self.memory = 1
|
|
|
|
self.disk_size = 10.0
|
|
|
|
|
|
|
|
self.manager = OpenNebulaManager(email=None, password=None, create_user=False)
|
2017-05-09 23:11:49 +00:00
|
|
|
self.opennebula_id = self.manager.create_template(name=self.template_name,
|
|
|
|
cores=self.cores,
|
|
|
|
memory=self.memory,
|
|
|
|
disk_size=self.disk_size)
|
2017-05-09 14:33:56 +00:00
|
|
|
|
|
|
|
self.template = VirtualMachineTemplate(opennebula_id=self.opennebula_id,
|
|
|
|
base_price=self.base_price,
|
|
|
|
memory_price=self.memory_price,
|
|
|
|
core_price=self.core_price,
|
|
|
|
disk_size_price=self.disk_size_price)
|
|
|
|
|
|
|
|
|
|
|
|
def test_model_can_create_a_virtualmachine_template(self):
|
|
|
|
"""Test the virtualmachine template model can create a template."""
|
|
|
|
old_count = VirtualMachineTemplate.objects.count()
|
|
|
|
self.template.save()
|
|
|
|
new_count = VirtualMachineTemplate.objects.count()
|
2017-05-11 02:05:58 +00:00
|
|
|
# Remove the template afterwards
|
|
|
|
template = self.manager._get_template(self.template.opennebula_id)
|
|
|
|
template.delete()
|
2017-05-09 14:33:56 +00:00
|
|
|
self.assertNotEqual(old_count, new_count)
|
|
|
|
|
2017-05-10 00:36:11 +00:00
|
|
|
def test_model_can_calculate_price(self):
|
|
|
|
price = self.cores * self.core_price
|
|
|
|
price += self.memory * self.memory_price
|
|
|
|
price += self.disk_size * self.disk_size_price
|
|
|
|
self.assertEqual(price, self.template.calculate_price())
|
|
|
|
|
|
|
|
|
2017-05-09 14:33:56 +00:00
|
|
|
|
|
|
|
class VirtualMachineTestCase(TestCase):
|
|
|
|
def setUp(self):
|
|
|
|
"""Define the test client and other test variables."""
|
2017-05-11 02:05:58 +00:00
|
|
|
self.template_name = "Standard"
|
|
|
|
self.base_price = 0.0
|
|
|
|
self.core_price = 5.0
|
|
|
|
self.memory_price = 2.0
|
|
|
|
self.disk_size_price = 0.6
|
|
|
|
|
|
|
|
self.cores = 1
|
|
|
|
self.memory = 1
|
|
|
|
self.disk_size = 10.0
|
|
|
|
self.manager = OpenNebulaManager(email=None, password=None, create_user=False)
|
|
|
|
self.opennebula_id = self.manager.create_template(name=self.template_name,
|
|
|
|
cores=self.cores,
|
|
|
|
memory=self.memory,
|
|
|
|
disk_size=self.disk_size)
|
|
|
|
|
|
|
|
self.template = VirtualMachineTemplate(opennebula_id=self.opennebula_id,
|
|
|
|
base_price=self.base_price,
|
|
|
|
memory_price=self.memory_price,
|
|
|
|
core_price=self.core_price,
|
|
|
|
disk_size_price=self.disk_size_price)
|
2017-05-09 15:15:12 +00:00
|
|
|
self.template_id = self.template.opennebula_id()
|
2017-05-09 14:33:56 +00:00
|
|
|
self.opennebula_id = self.manager.create_virtualmachine(template_id=self.template_id)
|
|
|
|
|
|
|
|
self.virtualmachine = VirtualMachine(opennebula_id=self.opennebula_id,
|
|
|
|
template=self.template)
|
|
|
|
|
|
|
|
def test_model_can_create_a_virtualmachine(self):
|
|
|
|
"""Test the virtualmachine model can create a virtualmachine."""
|
|
|
|
old_count = VirtualMachine.objects.count()
|
|
|
|
self.virtualmachine.save()
|
|
|
|
new_count = VirtualMachine.objects.count()
|
|
|
|
self.assertNotEqual(old_count, new_count)
|
|
|
|
|
2017-05-11 02:05:58 +00:00
|
|
|
def test_model_can_create_a_virtualmachine_for_user(self):
|
|
|
|
pass
|
|
|
|
|
2017-05-09 14:33:56 +00:00
|
|
|
def test_model_can_delete_a_virtualmachine(self):
|
|
|
|
"""Test the virtualmachine model can delete a virtualmachine."""
|
2017-05-09 23:31:27 +00:00
|
|
|
self.virtualmachine.save()
|
2017-05-09 14:33:56 +00:00
|
|
|
old_count = VirtualMachine.objects.count()
|
|
|
|
VirtualMachine.objects.first().delete()
|
|
|
|
new_count = VirtualMachine.objects.count()
|
|
|
|
self.assertNotEqual(old_count, new_count)
|