2017-05-31 16:01:54 +00:00
|
|
|
import random
|
|
|
|
import string
|
|
|
|
|
2017-11-25 23:55:14 +00:00
|
|
|
from django.conf import settings
|
2017-05-09 14:33:56 +00:00
|
|
|
from django.test import TestCase
|
2017-11-25 23:55:14 +00:00
|
|
|
from unittest import skipIf
|
2017-05-31 16:01:54 +00:00
|
|
|
|
|
|
|
from .models import OpenNebulaManager
|
2017-06-01 20:25:10 +00:00
|
|
|
from .serializers import VirtualMachineSerializer
|
2017-05-31 16:01:54 +00:00
|
|
|
from utils.models import CustomUser
|
2017-05-09 14:33:56 +00:00
|
|
|
|
2017-06-29 14:34:40 +00:00
|
|
|
|
2017-11-25 23:55:14 +00:00
|
|
|
@skipIf(
|
2017-11-26 00:49:54 +00:00
|
|
|
settings.OPENNEBULA_DOMAIN is None or
|
|
|
|
settings.OPENNEBULA_DOMAIN == "test_domain",
|
2017-11-26 00:12:16 +00:00
|
|
|
"""OpenNebula details unavailable, so skipping
|
|
|
|
OpenNebulaManagerTestCases"""
|
2017-11-25 23:55:14 +00:00
|
|
|
)
|
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."""
|
2017-05-31 16:01:54 +00:00
|
|
|
self.email = '{}@ungleich.ch'.format(''.join(random.choices(string.ascii_uppercase, k=10)))
|
2017-06-29 14:34:40 +00:00
|
|
|
self.password = ''.join(random.choices(string.ascii_uppercase + string.digits, k=20))
|
2017-05-09 14:33:56 +00:00
|
|
|
|
2017-05-31 16:01:54 +00:00
|
|
|
self.user = CustomUser.objects.create(name='test', email=self.email,
|
2017-06-29 14:34:40 +00:00
|
|
|
password=self.password)
|
2017-05-31 16:01:54 +00:00
|
|
|
|
|
|
|
self.vm_specs = {}
|
|
|
|
self.vm_specs['cpu'] = 1
|
|
|
|
self.vm_specs['memory'] = 2
|
|
|
|
self.vm_specs['disk_size'] = 10
|
|
|
|
|
2017-06-29 14:34:40 +00:00
|
|
|
self.manager = OpenNebulaManager()
|
2017-05-09 14:33:56 +00:00
|
|
|
|
2017-05-31 16:01:54 +00:00
|
|
|
def test_connect_to_server(self):
|
|
|
|
"""Test the opennebula manager can connect to a server."""
|
|
|
|
try:
|
|
|
|
ver = self.manager.oneadmin_client.version()
|
2017-06-29 14:34:40 +00:00
|
|
|
except:
|
2017-05-31 16:01:54 +00:00
|
|
|
ver = None
|
|
|
|
self.assertTrue(ver is not None)
|
|
|
|
|
|
|
|
def test_get_user(self):
|
|
|
|
"""Test the opennebula manager can get a existing user."""
|
|
|
|
self.manager.create_user(self.user)
|
|
|
|
user = self.manager._get_user(self.user)
|
|
|
|
name = user.name
|
|
|
|
self.assertNotEqual(name, None)
|
|
|
|
|
|
|
|
def test_create_and_delete_user(self):
|
|
|
|
"""Test the opennebula manager can create and delete 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,
|
2017-05-31 16:01:54 +00:00
|
|
|
password=self.password)
|
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-06-29 14:34:40 +00:00
|
|
|
|
2017-05-09 14:33:56 +00:00
|
|
|
self.assertNotEqual(old_count, new_count)
|
|
|
|
|
2017-05-31 16:01:54 +00:00
|
|
|
def test_user_can_login(self):
|
|
|
|
""" Test the manager can login to a new created user"""
|
|
|
|
self.manager.create_user(self.user)
|
|
|
|
user = self.manager._get_user(self.user)
|
|
|
|
client = self.manager._get_client(self.user)
|
|
|
|
version = client.version()
|
2017-05-09 14:33:56 +00:00
|
|
|
|
2017-06-29 14:34:40 +00:00
|
|
|
# Cleanup
|
2017-05-31 16:01:54 +00:00
|
|
|
user.delete()
|
|
|
|
self.assertNotEqual(version, None)
|
|
|
|
|
|
|
|
def test_add_public_key_to_user(self):
|
|
|
|
""" Test the manager can add a new public key to an user """
|
|
|
|
self.manager.create_user(self.user)
|
|
|
|
user = self.manager._get_user(self.user)
|
|
|
|
public_key = 'test'
|
|
|
|
self.manager.add_public_key(self.user, public_key)
|
|
|
|
# Fetch new user information from opennebula
|
|
|
|
user.info()
|
|
|
|
user_public_key = user.template.ssh_public_key
|
2017-06-29 14:34:40 +00:00
|
|
|
# Cleanup
|
2017-05-31 16:01:54 +00:00
|
|
|
user.delete()
|
2017-05-09 14:33:56 +00:00
|
|
|
|
2017-05-31 16:01:54 +00:00
|
|
|
self.assertEqual(user_public_key, public_key)
|
2017-06-29 14:34:40 +00:00
|
|
|
|
2017-06-01 17:08:38 +00:00
|
|
|
def test_append_public_key_to_user(self):
|
|
|
|
""" Test the manager can append a new public key to an user """
|
|
|
|
self.manager.create_user(self.user)
|
|
|
|
user = self.manager._get_user(self.user)
|
|
|
|
public_key = 'test'
|
|
|
|
self.manager.add_public_key(self.user, public_key)
|
|
|
|
# Fetch new user information from opennebula
|
|
|
|
user.info()
|
|
|
|
old_public_key = user.template.ssh_public_key
|
|
|
|
self.manager.add_public_key(self.user, public_key, merge=True)
|
|
|
|
user.info()
|
|
|
|
new_public_key = user.template.ssh_public_key
|
2017-06-29 14:34:40 +00:00
|
|
|
# Cleanup
|
2017-06-01 17:08:38 +00:00
|
|
|
user.delete()
|
|
|
|
|
|
|
|
self.assertEqual(new_public_key, '{}\n{}'.format(old_public_key,
|
2017-06-29 14:34:40 +00:00
|
|
|
public_key))
|
2017-05-10 00:36:11 +00:00
|
|
|
|
2017-06-01 18:47:11 +00:00
|
|
|
def test_remove_public_key_to_user(self):
|
|
|
|
""" Test the manager can remove a public key from an user """
|
|
|
|
self.manager.create_user(self.user)
|
|
|
|
user = self.manager._get_user(self.user)
|
|
|
|
public_key = 'test'
|
|
|
|
self.manager.add_public_key(self.user, public_key)
|
2017-06-01 20:25:10 +00:00
|
|
|
self.manager.add_public_key(self.user, public_key, merge=True)
|
|
|
|
user.info()
|
2017-06-01 18:47:11 +00:00
|
|
|
old_public_key = user.template.ssh_public_key
|
|
|
|
self.manager.remove_public_key(self.user, public_key)
|
|
|
|
user.info()
|
|
|
|
new_public_key = user.template.ssh_public_key
|
2017-06-29 14:34:40 +00:00
|
|
|
# Cleanup
|
2017-06-01 18:47:11 +00:00
|
|
|
user.delete()
|
2017-05-09 14:33:56 +00:00
|
|
|
|
2017-06-01 20:25:10 +00:00
|
|
|
self.assertEqual(new_public_key,
|
2017-06-29 14:34:40 +00:00
|
|
|
old_public_key.replace('{}\n'.format(public_key), '', 1))
|
2017-05-10 00:36:11 +00:00
|
|
|
|
2017-05-31 16:01:54 +00:00
|
|
|
def test_requires_ssh_key_for_new_vm(self):
|
|
|
|
"""Test the opennebula manager requires the user to have a ssh key when
|
|
|
|
creating a new vm"""
|
2017-05-10 00:36:11 +00:00
|
|
|
|
2017-11-26 00:12:16 +00:00
|
|
|
|
2017-11-25 23:55:14 +00:00
|
|
|
@skipIf(
|
2017-11-26 00:49:54 +00:00
|
|
|
settings.OPENNEBULA_DOMAIN is None or
|
|
|
|
settings.OPENNEBULA_DOMAIN == "test_domain",
|
2017-11-26 00:12:16 +00:00
|
|
|
"""OpenNebula details unavailable, so skipping
|
|
|
|
VirtualMachineSerializerTestCase"""
|
2017-11-25 23:55:14 +00:00
|
|
|
)
|
2017-06-01 20:25:10 +00:00
|
|
|
class VirtualMachineSerializerTestCase(TestCase):
|
2017-05-09 14:33:56 +00:00
|
|
|
def setUp(self):
|
|
|
|
"""Define the test client and other test variables."""
|
2017-06-01 20:25:10 +00:00
|
|
|
self.manager = OpenNebulaManager(email=None, password=None)
|
2017-05-26 21:23:39 +00:00
|
|
|
|
|
|
|
def test_serializer_strips_of_public(self):
|
2017-11-26 00:49:54 +00:00
|
|
|
""" Test the serialized virtual machine object contains no
|
|
|
|
'public-'."""
|
2017-05-26 21:23:39 +00:00
|
|
|
|
2017-06-01 20:25:10 +00:00
|
|
|
for vm in self.manager.get_vms():
|
|
|
|
serialized = VirtualMachineSerializer(vm)
|
|
|
|
self.assertEqual(serialized.data.get('name'), vm.name.strip('public-'))
|
|
|
|
break
|