Commit 1a19e435 by Belákovics Ádám

Merge branch 'unit_tests' into 'DEV'

Unit tests

See merge request !11
parents f1525f93 db7eec0b
Pipeline #816 failed with stage
in 48 seconds
......@@ -2,4 +2,7 @@ unittest:
image: python:3.7.3
script:
- pip install -r requirements.txt
- mkdir interface_openstack
- shopt -s extglob
- mv !(interface_openstack) interface_openstack
- python -m unittest discover
......@@ -9,7 +9,6 @@ class OpenStackConnection(object):
The connection only connects when something called in it."""
def __init__(self, auth):
super(OpenStackConnection, self).__init__()
self.openstack = openstack.connect(auth_url=auth["auth_url"],
username=auth["username"],
password=auth["password"],
......
......@@ -52,7 +52,7 @@ class OSVirtualMachineManager(InstanceInterface, OpenStackConnection):
@openstackError
def create_vm_from_template(self, name, image, flavor, networks):
return self.create_multiple_vm_from_template(name, image, flavor,
networks, 1)
networks, 1)[0]
@openstackError
def create_multiple_vm_from_template(self, name, image, flavor, networks,
......@@ -64,13 +64,18 @@ class OSVirtualMachineManager(InstanceInterface, OpenStackConnection):
if not image:
raise ValueError("The template not found")
new_server = compute.create_server(name=name,
flavorRef=flav.id,
imageRef=image.id,
networks=networks,
min_count=number,
)
return self.convert_server_to_instance(new_server)
server_ids = []
# TODO: Maybe we need parallel execution of multiple creation
for i in range(0, number):
new_server = compute.create_server(name=name,
flavorRef=flav.id,
imageRef=image.id,
networks=networks,
)
server_ids.append(new_server.id)
return server_ids
@openstackError
def create_flavor(self, name, ram, vcpus, initial_disk):
......
import unittest
from unittest.mock import MagicMock, create_autospec
from implementation.vm.instance import OSVirtualMachineManager
import openstack
servers = [
MagicMock(
id="test1",
name="test1",
flavorRef="flav1",
imageRef="image1",
networks=[{"uuid": "network1"}],
terminated_at="20200320-15-31",
launched_at="20190510-13-22",
disks=["disk1"],
status="ACTIVE",
addresses=[{"mac": "12345678AB", "ipv4": "1.1.1.1"}]
)
]
class MockOpenStackCompute(MagicMock):
def setUp(self):
self.compute = MagicMock()
self.compute.create_server = MagicMock(return_value=servers[0])
self.compute.find_flavor = MagicMock(return_value=MagicMock(id="uuid1"))
self.compute.find_image = MagicMock(return_value=MagicMock(id="uuid2"))
self.compute.wait_for_server = MagicMock(return_value=True)
self.compute.list_servers = MagicMock(return_value=servers)
self.compute.get_server = create_autospec()
class InstanceCreateTestCase(unittest.TestCase):
def setUp(self):
self.conn = MockOpenStackCompute()
self.conn.setUp()
self.manager = OSVirtualMachineManager(self.conn)
def tearDown(self):
pass
def test_create_from_template(self):
instance = self.manager.create_vm_from_template('test1',
'imageid',
'flavorid',
['networkid1'])
self.conn.compute.create_server.assert_called()
self.assertEqual(instance.name, servers[0].name)
def test_create_from_template_params(self):
self.manager.create_vm_from_template('test', 'imageid', 'flavorid',
['networkid1'])
self.conn.compute.create_server.assert_called_with(
name='test',
flavorRef="uuid1",
imageRef="uuid2",
networks=['networkid1'],
min_count=1)
def test_suspend(self):
self.compute.suspend_server()
import unittest
from interface.image.Image import Image
class TestImage(unittest.TestCase):
def test_fields(self):
image = Image('id', 'name', 'format')
self.assertEqual(image.id, 'id')
self.assertEqual(image.name, 'name')
self.assertEqual(image.format, 'format')
if __name__ == '__main__':
unittest.main()
import unittest
from interface.storage.Snapshot import Snapshot
class TestSnapshot(unittest.TestCase):
def test_fields(self):
snapshot = Snapshot('id', 'volume_id', 1, 'status', 'created_at')
self.assertEqual(snapshot.id, 'id')
self.assertEqual(snapshot.volume_id, 'volume_id')
self.assertEqual(snapshot.size, 1)
self.assertEqual(snapshot.status, 'status')
self.assertEqual(snapshot.created_at, 'created_at')
if __name__ == '__main__':
unittest.main()
import unittest
from interface.storage.Volume import Volume
class TestVolume(unittest.TestCase):
def test_fields(self):
volume = Volume('id', 'image_id', 1, False, 'status', 'created_at')
self.assertEqual(volume.id, 'id')
self.assertEqual(volume.image_id, 'image_id')
self.assertEqual(volume.size, 1)
self.assertEqual(volume.bootable, False)
self.assertEqual(volume.status, 'status')
self.assertEqual(volume.created_at, 'created_at')
if __name__ == '__main__':
unittest.main()
import unittest
from interface.image.Image import Image
from interface_openstack.interface.image.image import Image
class TestImage(unittest.TestCase):
......@@ -11,7 +11,3 @@ class TestImage(unittest.TestCase):
self.assertEqual(image.id, 'id')
self.assertEqual(image.name, 'name')
self.assertEqual(image.format, 'format')
if __name__ == '__main__':
unittest.main()
import unittest
from interface.storage.Snapshot import Snapshot
from interface_openstack.interface.storage.snapshot import Snapshot
class TestSnapshot(unittest.TestCase):
......@@ -13,7 +13,3 @@ class TestSnapshot(unittest.TestCase):
self.assertEqual(snapshot.size, 1)
self.assertEqual(snapshot.status, 'status')
self.assertEqual(snapshot.created_at, 'created_at')
if __name__ == '__main__':
unittest.main()
import unittest
from unittest.mock import MagicMock, patch
from interface_openstack.implementation.vm.instance import OSVirtualMachineManager
servers = [
MagicMock(
id="test1",
name="test1",
flavorRef="flav1",
imageRef="image1",
networks=[{"uuid": "network1"}],
terminated_at="20200320-15-31",
launched_at="20190510-13-22",
disks=["disk1"],
status="ACTIVE",
addresses=[{"mac": "12345678AB", "ipv4": "1.1.1.1"}]
)
]
fake_auth = {
"auth_url": "",
"project_id": "",
"project_name": "",
"user_domain_name": "",
"user_domain_id": "",
"project_domain_id": "",
"username": "",
"password": "",
"region_name": "",
"interface": "",
"identity_api_version": "",
}
class InstanceCreateTestCase(unittest.TestCase):
'''
Test openstack methods called with the right values or exceptions raised.
Signature of tests:
def test_my_feature_with(self):
# Mock
--- mocking openstack compute API ---
# Execute
--- execute the tested method ---
# Assert
--- check values, exceptions, calls etc. ---
'''
@patch('openstack.connect')
def setUp(self, mock_connect):
self.compute = MagicMock()
self.compute.get_server = MagicMock(return_value=servers[0])
mock_connect().compute = self.compute
self.manager = OSVirtualMachineManager(auth=fake_auth)
def tearDown(self):
pass
def test_create_from_template(self):
# Mock
self.compute.create_server = MagicMock(return_value=servers[0])
self.compute.find_flavor = MagicMock(return_value=MagicMock(id="uuid1"))
self.compute.find_image = MagicMock(return_value=MagicMock(id="uuid2"))
# Execute
instance_id = self.manager.create_vm_from_template('test1',
'imageid',
'flavorid',
['networkid1'])
# Assert
self.manager.openstack.compute.create_server.assert_called()
self.assertEqual(instance_id, servers[0].id)
def test_create_from_template_params(self):
# Mock
self.compute.create_server = MagicMock(return_value=servers[0])
self.compute.find_flavor = MagicMock(return_value=MagicMock(id="uuid1"))
self.compute.find_image = MagicMock(return_value=MagicMock(id="uuid2"))
# Execute
self.manager.create_vm_from_template('test', 'imageid', 'flavorid',
['networkid1'])
# Assert
self.manager.openstack.compute.create_server.assert_called_with(
name='test',
flavorRef="uuid1",
imageRef="uuid2",
networks=['networkid1'],
)
def test_suspend(self):
# Mock
self.compute.suspend_server = MagicMock(return_value=True)
# Execute
self.manager.suspend_vm("test1")
# Assert
self.compute.get_server.assert_called()
self.compute.suspend_server.assert_called_once()
def test_start(self):
# Mock
self.compute.start_server = MagicMock(return_value=True)
# Execute
self.manager.start_vm("test1")
# Assert
self.compute.get_server.assert_called()
self.compute.start_server.assert_called_once()
def test_stop(self):
# Mock
self.compute.stop_server = MagicMock(return_value=True)
# Execute
self.manager.stop_vm("test1")
# Assert
self.compute.get_server.assert_called()
self.compute.stop_server.assert_called_once()
def test_wake_up(self):
# Mock
self.compute.resume_server = MagicMock(return_value=True)
# Execute
self.manager.wake_up_vm("test1")
# Assert
self.compute.get_server.assert_called()
self.compute.resume_server.assert_called_once()
def test_reboot(self):
# Mock
self.compute.reboot_server = MagicMock(return_value=True)
# Execute
self.manager.reboot_vm("test1")
# Assert
self.compute.get_server.assert_called()
self.compute.reboot_server.assert_called_once()
self.compute.reboot_server.assert_called_with(servers[0], reboot_type="SOFT")
def test_reset(self):
# Mock
self.compute.reboot_server = MagicMock(return_value=True)
# Execute
self.manager.reset_vm("test1")
# Assert
self.compute.get_server.assert_called()
self.compute.reboot_server.assert_called_once()
self.compute.reboot_server.assert_called_with(servers[0], reboot_type="HARD")
import unittest
from interface.storage.Volume import Volume
from interface_openstack.interface.storage.volume import Volume
class TestVolume(unittest.TestCase):
......@@ -14,7 +14,3 @@ class TestVolume(unittest.TestCase):
self.assertEqual(volume.bootable, False)
self.assertEqual(volume.status, 'status')
self.assertEqual(volume.created_at, 'created_at')
if __name__ == '__main__':
unittest.main()
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